secondary_service.go 47KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "math/rand"
  8. "strconv"
  9. "time"
  10. )
  11. //生成编号,规则为:"SH-"+4位随机数
  12. func CreateCode() string {
  13. s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000))
  14. code := "SH-" + s
  15. return code
  16. }
  17. //查询仓库名称是否重复(用于修改)
  18. func IsStorehouseNameUp(orgid, id int64, storehouse_name string) (bool, error) {
  19. var total int
  20. var tmp bool
  21. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_name = ? and user_org_id = ? and status = 1 and id != ?", storehouse_name, orgid, id).Count(&total).Error
  22. if total > 0 {
  23. tmp = true //有重复的
  24. } else {
  25. tmp = false
  26. }
  27. return tmp, err
  28. }
  29. //查询仓库地址是否重复(用于修改)
  30. func IsStorehouseAddressUp(orgid, id int64, storehouse_address string) (bool, error) {
  31. var total int
  32. var tmp bool
  33. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_address = ? and user_org_id = ? and status = 1 and id != ?", storehouse_address, orgid, id).Count(&total).Error
  34. if total > 0 {
  35. tmp = true //有重复的
  36. } else {
  37. tmp = false
  38. }
  39. return tmp, err
  40. }
  41. //查询编号是否重复
  42. func FindStorehouseCode(orgid int64, code string) bool {
  43. var total int
  44. XTReadDB().Model(&models.Storehouse{}).Where(" storehouse_code = ? and user_org_id = ? and status = 1", code, orgid).Count(&total)
  45. if total > 0 {
  46. return true
  47. } else {
  48. return false
  49. }
  50. }
  51. //判断仓库的库存是否为零 true为零,false不为零
  52. func IsStorehouseNil(id, orgid int64) bool { //根据 id gro_id 查为零的库存
  53. var h, y int
  54. //判断耗材是否为零
  55. XTReadDB().Model(&models.WarehousingInfo{}).Where("stock_count > 0 and status = 1 and org_id = ? and storehouse_id = ?", orgid, id).Count(&h)
  56. if h > 0 {
  57. return false
  58. }
  59. //判断药品是否为零
  60. XTReadDB().Model(&models.XtDrugWarehouseInfo{}).Where("stock_max_number > 0 or stock_min_number > 0 ").Where(" org_id = ? and storehouse_id = ?", orgid, id).Count(&y)
  61. if y > 0 {
  62. return false
  63. }
  64. return true
  65. }
  66. //修改仓库状态
  67. func UpdateStorehouseStatus(id int64) error {
  68. err := XTWriteDB().Exec("UPDATE xt_storehouse,(SELECT CASE storehouse_status WHEN 1 THEN 0 WHEN 0 THEN 1 END AS tt FROM xt_storehouse WHERE id=?)tmp SET storehouse_status = tmp.tt where id=?", id, id).Error
  69. return err
  70. }
  71. //删除仓库
  72. func DeleteStorehouse(id int64) error {
  73. err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ?", id).Update("status", 0).Error
  74. return err
  75. }
  76. //查询仓库名称是否重复
  77. func IsStorehouseName(orgid int64, storehouse_name string) (bool, error) {
  78. var total int
  79. var tmp bool
  80. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_name = ? and user_org_id = ? and status = 1", storehouse_name, orgid).Count(&total).Error
  81. if total > 0 {
  82. tmp = true //有重复的
  83. } else {
  84. tmp = false
  85. }
  86. return tmp, err
  87. }
  88. //查询仓库地址是否重复
  89. func IsStorehouseAddress(orgid int64, storehouse_address string) (bool, error) {
  90. var total int
  91. var tmp bool
  92. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_address = ? and user_org_id = ? and status = 1", storehouse_address, orgid).Count(&total).Error
  93. if total > 0 {
  94. tmp = true //有重复的
  95. } else {
  96. tmp = false
  97. }
  98. return tmp, err
  99. }
  100. //新增仓库
  101. func AddStroehouse(storehouse models.Storehouse) error {
  102. tx := XTWriteDB().Begin()
  103. defer func() {
  104. if err != nil {
  105. tx.Rollback()
  106. } else {
  107. tx.Commit()
  108. }
  109. }()
  110. //创建仓库
  111. err := tx.Create(&storehouse).Error
  112. if err != nil {
  113. return err
  114. }
  115. var id models.Storehouse
  116. //获取创建仓库的id
  117. err = tx.Model(&models.Storehouse{}).Select("id").Where("status = 1 and user_org_id = ?", storehouse.UserOrgId).Order("id desc").First(&id).Error
  118. if err != nil {
  119. return err
  120. }
  121. //判断是否存在仓库配置
  122. boolean := IsStorehouseconfigXT(storehouse.UserOrgId, tx)
  123. if boolean == false {
  124. //创建仓库配置
  125. err = CreateStorehouseConfigXT(storehouse.UserOrgId, id.ID, tx)
  126. } else {
  127. utils.ErrorLog("仓库配置已存在")
  128. }
  129. return err
  130. }
  131. //修改仓库
  132. func UpdateStroehouse(storehouse models.Storehouse) error {
  133. err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ? and status = 1", storehouse.ID).Updates(
  134. map[string]interface{}{
  135. "storehouse_name": storehouse.StorehouseName,
  136. "storehouse_address": storehouse.StorehouseAddress,
  137. "storehouse_status": storehouse.StorehouseStatus,
  138. "storehouse_admin_id": storehouse.StorehouseAdminId,
  139. "mtime": storehouse.Mtime,
  140. }).Error
  141. return err
  142. }
  143. //查询一条仓库的信息
  144. func GetOneStorehouse(id, orgid int64) (storehouse models.Storehouse, err error) {
  145. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ? and user_org_id = ?", id, orgid).Find(&storehouse).Error
  146. return
  147. }
  148. //分页
  149. func StorehouseList(page, limit, orgid int64, keyword string, slicekey []int64) (storehouse []models.Storehouse, total int64, err error) {
  150. db := XTReadDB().Model(&storehouse).Where("status = 1 and user_org_id = ?", orgid)
  151. offset := (page - 1) * limit
  152. if len(keyword) > 0 {
  153. var adminid string = "storehouse_code like ? or storehouse_name like ? or storehouse_address like ? "
  154. if len(slicekey) > 0 {
  155. for i := 0; i < len(slicekey); i++ {
  156. adminid = adminid + " or storehouse_admin_id =" + strconv.FormatInt(slicekey[i], 10)
  157. }
  158. }
  159. keyword = "%" + keyword + "%"
  160. db = db.Where(adminid, keyword, keyword, keyword)
  161. }
  162. err = db.Count(&total).Offset(offset).Order("id").Limit(limit).Find(&storehouse).Error
  163. return storehouse, total, err
  164. }
  165. //获取当前机构所有可用的仓库名字
  166. func GetAllStorehouseName(orgid int64) (storehouse []models.Storehouse, err error) {
  167. err = XTReadDB().Model(&models.Storehouse{}).Where("storehouse_status = 1 and status = 1 and user_org_id = ?", orgid).Find(&storehouse).Error
  168. return
  169. }
  170. //根据机构id,生成一个默认仓库
  171. func GetDefaultStorehouse(orgid int64) error {
  172. var code string
  173. for a := true; a == true; {
  174. code = CreateCode()
  175. tmp := FindStorehouseCode(orgid, code)
  176. //如果没有重复的编码结束循环
  177. if tmp == false {
  178. a = false
  179. }
  180. }
  181. createid, err := Getcreateid(orgid)
  182. if err != nil {
  183. return err
  184. }
  185. storehouse := models.Storehouse{
  186. StorehouseCode: code,
  187. StorehouseName: "默认仓库",
  188. StorehouseAddress: "",
  189. StorehouseStatus: 1,
  190. UserOrgId: orgid,
  191. StorehouseAdminId: createid.Creator,
  192. Status: 1,
  193. Ctime: time.Now().Unix(),
  194. }
  195. err = AddStroehouse(storehouse)
  196. return err
  197. }
  198. //查找该机构id是否有仓库存在,ture存在,false不存在
  199. func IsStorehouse(orgid int64) bool {
  200. var total int
  201. XTReadDB().Model(&models.Storehouse{}).Where("user_org_id = ?", orgid).Count(&total)
  202. if total > 0 {
  203. return true
  204. } else {
  205. return false
  206. }
  207. }
  208. //查找该机构id是否有仓库配置存在,ture存在,false不存在
  209. func IsStorehouseconfigXT(orgid int64, tx *gorm.DB) bool {
  210. var total int
  211. tx.Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Count(&total)
  212. if total > 0 {
  213. return true
  214. } else {
  215. return false
  216. }
  217. }
  218. // 根据机构id查询仓库配置
  219. //storehouse_info:耗材 自动入库 的仓库id
  220. //storehouse_out_info:耗材 自动出库 的仓库id
  221. //drug_storehouse_info:药品 自动入库 的仓库id
  222. //drug_storehouse_out:药品 自动出库 的仓库id
  223. func FindStorehouseConfig(orgid int64) (storehouse_config models.StorehouseConfig, err error) {
  224. err = XTReadDB().Model(&models.StorehouseConfig{}).Where("status = 1 and user_org_id = ?", orgid).Find(&storehouse_config).Error
  225. return
  226. }
  227. //根据机构id,仓库id新建一个仓库配置
  228. func CreateStorehouseConfig(orgid, id int64) (err error) {
  229. storeconfig := models.StorehouseConfig{
  230. UserOrgId: orgid,
  231. StorehouseInfo: id,
  232. StorehouseOutInfo: id,
  233. DrugStorehouseInfo: id,
  234. DrugStorehouseOut: id,
  235. Status: 1,
  236. Ctime: time.Now().Unix(),
  237. }
  238. err = XTWriteDB().Create(&storeconfig).Error
  239. return
  240. }
  241. //根据机构id,仓库id新建一个仓库配置,带事务
  242. func CreateStorehouseConfigXT(orgid, id int64, tx *gorm.DB) (err error) {
  243. storeconfig := models.StorehouseConfig{
  244. UserOrgId: orgid,
  245. StorehouseInfo: id,
  246. StorehouseOutInfo: id,
  247. DrugStorehouseInfo: id,
  248. DrugStorehouseOut: id,
  249. Status: 1,
  250. Ctime: time.Now().Unix(),
  251. }
  252. err = tx.Create(&storeconfig).Error
  253. return
  254. }
  255. //更改耗材自动入库仓库
  256. func UpdateInfo(orgid, id int64) (err error) {
  257. mtime := time.Now().Unix()
  258. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_info": id, "mtime": mtime}).Error
  259. return
  260. }
  261. //更改耗材自动出库仓库
  262. func UpdateOutInfo(orgid, id int64) (err error) {
  263. mtime := time.Now().Unix()
  264. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_out_info": id, "mtime": mtime}).Error
  265. return
  266. }
  267. //更改药品自动入库仓库
  268. func UpdateDrugInfo2(orgid, id int64) (err error) {
  269. mtime := time.Now().Unix()
  270. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_info": id, "mtime": mtime}).Error
  271. return
  272. }
  273. //更改药品自动出库仓库
  274. func UpdateDrugOut(orgid, id int64) (err error) {
  275. mtime := time.Now().Unix()
  276. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_out": id, "mtime": mtime}).Error
  277. return
  278. }
  279. //根据id查询仓库名称
  280. func FindStorehouseName(id int64) (storehouse models.Storehouse, err error) {
  281. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ?", id).Find(&storehouse).Error
  282. return
  283. }
  284. //判断该仓库是否在仓库配置表中
  285. func IsInConfig(orgid, id int64) bool {
  286. var total int
  287. XTReadDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Where(
  288. "storehouse_info = ? or storehouse_out_info = ? or drug_storehouse_info = ? or drug_storehouse_out = ?", id, id, id, id).Count(&total)
  289. if total > 0 {
  290. return true //存在
  291. } else {
  292. return false
  293. }
  294. }
  295. //兼容旧数据
  296. func Byliinit() (err error) {
  297. tx := XTWriteDB().Begin()
  298. defer func() {
  299. if err != nil && err.Error() != "record not found" {
  300. tx.Rollback()
  301. } else {
  302. tx.Commit()
  303. }
  304. }()
  305. err = initsgj_user_admin(tx)
  306. if err != nil && err.Error() != "record not found" {
  307. return
  308. }
  309. err = StoreReduceOrg(tx)
  310. if err != nil && err.Error() != "record not found" {
  311. return
  312. }
  313. err = StoreReduceConfig(tx)
  314. if err != nil && err.Error() != "record not found" {
  315. return
  316. }
  317. err = initxt_storehouse(tx)
  318. if err != nil && err.Error() != "record not found" {
  319. return
  320. }
  321. err = initxt_stock_flow(tx)
  322. if err != nil && err.Error() != "record not found" {
  323. return
  324. }
  325. err = initxt_drug_flow(tx)
  326. if err != nil && err.Error() != "record not found" {
  327. return
  328. }
  329. err = initdialysis_before_prepare(tx)
  330. if err != nil && err.Error() != "record not found" {
  331. return
  332. }
  333. err = initxt_automatic_reduce_detail(tx)
  334. if err != nil && err.Error() != "record not found" {
  335. return
  336. }
  337. err = initxt_drug_automatic_reduce_detail(tx)
  338. if err != nil && err.Error() != "record not found" {
  339. return
  340. }
  341. err = initxt_warehouse(tx)
  342. if err != nil && err.Error() != "record not found" {
  343. return
  344. }
  345. err = initxt_warehouse_info(tx)
  346. if err != nil && err.Error() != "record not found" {
  347. return
  348. }
  349. err = initxt_warehouse_out(tx)
  350. if err != nil && err.Error() != "record not found" {
  351. return
  352. }
  353. err = initxt_warehouse_out_info(tx)
  354. if err != nil && err.Error() != "record not found" {
  355. return
  356. }
  357. err = initxt_drug_warehouse(tx)
  358. if err != nil && err.Error() != "record not found" {
  359. return
  360. }
  361. err = initxt_drug_warehouse_info(tx)
  362. if err != nil && err.Error() != "record not found" {
  363. return
  364. }
  365. err = initxt_drug_warehouse_out(tx)
  366. if err != nil && err.Error() != "record not found" {
  367. return
  368. }
  369. err = initxt_drug_warehouse_out_info(tx)
  370. if err != nil && err.Error() != "record not found" {
  371. return
  372. }
  373. err = initxt_cancel_stock(tx)
  374. if err != nil && err.Error() != "record not found" {
  375. return
  376. }
  377. err = initxt_cancel_stock_info(tx)
  378. if err != nil && err.Error() != "record not found" {
  379. return
  380. }
  381. err = initxt_drug_cancel_stock(tx)
  382. if err != nil && err.Error() != "record not found" {
  383. return
  384. }
  385. err = initxt_drug_cancel_stock_info(tx)
  386. if err != nil && err.Error() != "record not found" {
  387. return
  388. }
  389. err = initxt_supplier_warehouse_info(tx)
  390. if err != nil && err.Error() != "record not found" {
  391. return
  392. }
  393. err = initxt_supplier_warehousing_info_order(tx)
  394. if err != nil && err.Error() != "record not found" {
  395. return
  396. }
  397. err = initxt_supplier_warehouse_out(tx)
  398. if err != nil && err.Error() != "record not found" {
  399. return
  400. }
  401. err = initxt_supplier_warehousing_out_order(tx)
  402. if err != nil && err.Error() != "record not found" {
  403. return
  404. }
  405. err = initxt_supplier_warehouse_cancel(tx)
  406. if err != nil && err.Error() != "record not found" {
  407. return
  408. }
  409. err = initxt_supplier_warehousing_cancel_order(tx)
  410. return
  411. }
  412. //初始化管理员名字
  413. func initsgj_user_admin(tx *gorm.DB) (err error) {
  414. err = tx.Exec("update sgj_users.sgj_user_admin set name = \"超级管理员\" where name is null or name = \"\"").Error
  415. return
  416. }
  417. //根据xt_gobal_template 获取的机构id减去仓库表存在的机构id,把结果插入到仓库表
  418. func StoreReduceOrg(tx *gorm.DB) (err error) {
  419. err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  420. "SELECT CONCAT(\"SH-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  421. "FROM" +
  422. "(select t1.org_id as id FROM sgj_xt.xt_gobal_template as t1 LEFT JOIN " +
  423. "(SELECT distinct user_org_id as i FROM sgj_xt.xt_storehouse) as t2 on t1.org_id = t2.i WHERE t2.i is null and t1.status = 1)tmp").Error
  424. //err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  425. // "SELECT CONCAT(\"sh-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  426. // "FROM" +
  427. // "(select t1.id FROM sgj_users.sgj_user_org as t1 LEFT JOIN " +
  428. // "(SELECT distinct user_org_id as i FROM sgj_xt.xt_storehouse) as t2 on t1.id = t2.i WHERE t2.i is null and t1.status != 0 and t1.import = 0)tmp").Error
  429. return
  430. }
  431. //查找仓库表的orgid 减去仓库配置表的orgid,把结果插入到仓库配置表
  432. func StoreReduceConfig(tx *gorm.DB) (err error) {
  433. err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse_config(user_org_id,storehouse_info,storehouse_out_info,drug_storehouse_info,drug_storehouse_out,ctime)" +
  434. "SELECT tmp.user_org_id,tmp.id,tmp.id,tmp.id,tmp.id,UNIX_TIMESTAMP()" +
  435. "FROM(select t1.id,t1.user_org_id FROM sgj_xt.xt_storehouse as t1 LEFT JOIN " +
  436. "(SELECT user_org_id as i FROM sgj_xt.xt_storehouse_config) as t2 on t1.user_org_id = t2.i WHERE t2.i is null and t1.status = 1)tmp").Error
  437. return
  438. }
  439. //初始化 xt_storehouse 管理员的名字
  440. func initxt_storehouse(tx *gorm.DB) (err error) {
  441. err = tx.Exec("UPDATE sgj_xt.xt_storehouse as t," +
  442. "(SELECT o.id,o.creator from sgj_users.sgj_user_org as o," +
  443. "(SELECT DISTINCT org_id from sgj_xt.xt_gobal_template)as t " +
  444. "WHERE t.org_id = o.id )tmp " +
  445. "SET t.storehouse_admin_id = tmp.creator,t.mtime = UNIX_TIMESTAMP()" +
  446. "WHERE t.status = 1 and t.user_org_id = tmp.id").Error
  447. return
  448. }
  449. //初始化 xt_stock_flow(完成)
  450. func initxt_stock_flow(tx *gorm.DB) (err error) {
  451. err = tx.Exec("UPDATE sgj_xt.xt_stock_flow as t," +
  452. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  453. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_stock_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  454. "WHERE t1.user_org_id = t2.orgid)tmp " +
  455. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  456. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  457. return
  458. }
  459. //初始化 xt_drug_flow(完成)
  460. func initxt_drug_flow(tx *gorm.DB) (err error) {
  461. err = tx.Exec("UPDATE sgj_xt.xt_drug_flow as t," +
  462. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  463. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_drug_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  464. "WHERE t1.user_org_id = t2.orgid)tmp " +
  465. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  466. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  467. return
  468. }
  469. //初始化 dialysis_before_prepare(完成)
  470. func initdialysis_before_prepare(tx *gorm.DB) (err error) {
  471. err = tx.Exec("UPDATE sgj_xt.dialysis_before_prepare as t," +
  472. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  473. "(SELECT distinct user_org_id as orgid FROM sgj_xt.dialysis_before_prepare WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  474. "WHERE t1.user_org_id = t2.orgid)tmp " +
  475. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  476. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  477. return
  478. }
  479. //初始化 xt_automatic_reduce_detail(完成)
  480. func initxt_automatic_reduce_detail(tx *gorm.DB) (err error) {
  481. err = tx.Exec("UPDATE sgj_xt.xt_automatic_reduce_detail as t," +
  482. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  483. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  484. "WHERE t1.user_org_id = t2.orgid)tmp " +
  485. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  486. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  487. return
  488. }
  489. //初始化 xt_drug_automatic_reduce_detail(完成)
  490. func initxt_drug_automatic_reduce_detail(tx *gorm.DB) (err error) {
  491. err = tx.Exec("UPDATE sgj_xt.xt_drug_automatic_reduce_detail as t," +
  492. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  493. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  494. "WHERE t1.user_org_id = t2.orgid)tmp " +
  495. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  496. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  497. return
  498. }
  499. //初始化 xt_warehouse(完成)
  500. func initxt_warehouse(tx *gorm.DB) (err error) {
  501. err = tx.Exec("UPDATE sgj_xt.xt_warehouse as t," +
  502. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  503. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  504. "WHERE t1.user_org_id = t2.orgid)tmp " +
  505. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  506. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  507. return
  508. }
  509. //初始化 xt_warehouse_info(完成)
  510. func initxt_warehouse_info(tx *gorm.DB) (err error) {
  511. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_info as t," +
  512. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  513. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  514. "WHERE t1.user_org_id = t2.orgid)tmp " +
  515. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  516. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  517. return
  518. }
  519. //初始化 xt_warehouse_out(完成)
  520. func initxt_warehouse_out(tx *gorm.DB) (err error) {
  521. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out as t," +
  522. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  523. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  524. "WHERE t1.user_org_id = t2.orgid)tmp " +
  525. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  526. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  527. return
  528. }
  529. //初始化 xt_warehouse_out_info(完成)
  530. func initxt_warehouse_out_info(tx *gorm.DB) (err error) {
  531. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out_info as t," +
  532. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  533. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  534. "WHERE t1.user_org_id = t2.orgid)tmp " +
  535. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  536. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  537. return
  538. }
  539. //初始化 xt_drug_warehouse(完成)
  540. func initxt_drug_warehouse(tx *gorm.DB) (err error) {
  541. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse as t," +
  542. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  543. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  544. "WHERE t1.user_org_id = t2.orgid)tmp " +
  545. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  546. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  547. return
  548. }
  549. //初始化 xt_drug_warehouse_info(完成)
  550. func initxt_drug_warehouse_info(tx *gorm.DB) (err error) {
  551. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_info as t," +
  552. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  553. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  554. "WHERE t1.user_org_id = t2.orgid)tmp " +
  555. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  556. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  557. return
  558. }
  559. //初始化 xt_drug_warehouse_out(完成)
  560. func initxt_drug_warehouse_out(tx *gorm.DB) (err error) {
  561. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out as t," +
  562. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  563. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  564. "WHERE t1.user_org_id = t2.orgid)tmp " +
  565. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  566. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  567. return
  568. }
  569. //初始化 xt_drug_warehouse_out_info(完成)
  570. func initxt_drug_warehouse_out_info(tx *gorm.DB) (err error) {
  571. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out_info as t," +
  572. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  573. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  574. "WHERE t1.user_org_id = t2.orgid)tmp " +
  575. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  576. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  577. return
  578. }
  579. //初始化 xt_cancel_stock(完成)
  580. func initxt_cancel_stock(tx *gorm.DB) (err error) {
  581. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock as t," +
  582. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  583. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  584. "WHERE t1.user_org_id = t2.orgid)tmp " +
  585. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  586. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  587. return
  588. }
  589. //初始化 xt_cancel_stock_info(完成)
  590. func initxt_cancel_stock_info(tx *gorm.DB) (err error) {
  591. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock_info as t," +
  592. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  593. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  594. "WHERE t1.user_org_id = t2.orgid)tmp " +
  595. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  596. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  597. return
  598. }
  599. //初始化 xt_drug_cancel_stock(完成)
  600. func initxt_drug_cancel_stock(tx *gorm.DB) (err error) {
  601. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  602. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  603. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  604. "WHERE t1.user_org_id = t2.orgid)tmp " +
  605. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  606. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  607. return
  608. }
  609. //初始化 xt_drug_cancel_stock_info(完成)
  610. func initxt_drug_cancel_stock_info(tx *gorm.DB) (err error) {
  611. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock_info as t," +
  612. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  613. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  614. "WHERE t1.user_org_id = t2.orgid)tmp " +
  615. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  616. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  617. return
  618. }
  619. //初始化 xt_supplier_warehouse_info(完成)
  620. func initxt_supplier_warehouse_info(tx *gorm.DB) (err error) {
  621. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_info as t," +
  622. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  623. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  624. "WHERE t1.user_org_id = t2.orgid)tmp " +
  625. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  626. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  627. return
  628. }
  629. //初始化 xt_supplier_warehousing_info_order(完成)
  630. func initxt_supplier_warehousing_info_order(tx *gorm.DB) (err error) {
  631. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_info_order as t," +
  632. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  633. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehousing_info_order WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  634. "WHERE t1.user_org_id = t2.orgid)tmp " +
  635. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  636. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  637. return
  638. }
  639. //初始化 xt_supplier_warehouse_out(完成)
  640. func initxt_supplier_warehouse_out(tx *gorm.DB) (err error) {
  641. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_out as t," +
  642. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  643. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  644. "WHERE t1.user_org_id = t2.orgid)tmp " +
  645. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  646. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  647. return
  648. }
  649. //初始化 xt_supplier_warehousing_out_order(完成)
  650. func initxt_supplier_warehousing_out_order(tx *gorm.DB) (err error) {
  651. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_out_order as t," +
  652. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  653. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehousing_out_order WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  654. "WHERE t1.user_org_id = t2.orgid)tmp " +
  655. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  656. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  657. return
  658. }
  659. //初始化 xt_supplier_warehouse_cancel(完成)
  660. func initxt_supplier_warehouse_cancel(tx *gorm.DB) (err error) {
  661. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_cancel as t," +
  662. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  663. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_cancel WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  664. "WHERE t1.user_org_id = t2.orgid)tmp " +
  665. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  666. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  667. return
  668. }
  669. //初始化 xt_supplier_warehousing_cancel_order(完成)
  670. func initxt_supplier_warehousing_cancel_order(tx *gorm.DB) (err error) {
  671. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_cancel_order as t," +
  672. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  673. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehousing_cancel_order WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  674. "WHERE t1.user_org_id = t2.orgid)tmp " +
  675. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  676. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  677. return
  678. }
  679. //查询机构创建者的id
  680. func Getcreateid(orgid int64) (createid models.UserOrg, err error) {
  681. err = XTReadDB().Select("id,creator").Where("id = ? and status = 1", orgid).Find(&createid).Error
  682. return
  683. }
  684. //根据机构id查询拥有仓库管理权限的角色
  685. func FindRoles(orgid int64) map[string]int {
  686. role := []models.RolePurviews{}
  687. roles := make(map[string]int)
  688. XTReadDB().Select("role_id").Where("purview_ids like \"%299%\" and org_id = ? and status = 1", orgid).Find(&role)
  689. for i := 0; i < len(role); i++ {
  690. roles[strconv.FormatInt(role[i].RoleId, 10)] = i
  691. }
  692. return roles
  693. }
  694. //判断两个切片中的元素是否有重复的,true有重复的元素,false无相同元素
  695. func IsIdentical(str1 []string, str2 []string) (t bool) {
  696. t = false
  697. if len(str1) == 0 || len(str2) == 0 {
  698. return
  699. }
  700. map1, map2 := make(map[string]int), make(map[string]int)
  701. for i := 0; i < len(str1); i++ {
  702. map1[str1[i]] = i
  703. }
  704. for i := 0; i < len(str2); i++ {
  705. map2[str2[i]] = i
  706. }
  707. for k, _ := range map1 {
  708. if _, ok := map2[k]; ok {
  709. t = true
  710. }
  711. }
  712. return
  713. }
  714. //
  715. func GetAllStoreHouseList(orgid int64) (house []*models.XtStorehouse, err error) {
  716. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&house).Error
  717. return house, err
  718. }
  719. func GetAllStoreHouseListThree(orgid int64) (house []*models.XtStorehouse, err error) {
  720. err = XTReadDB().Where("user_org_id = ? and status = 1 and storehouse_status = 1", orgid).Find(&house).Error
  721. return house, err
  722. }
  723. func GetAllStoreHouseListOne(orgid int64) (list []*models.VmStorehouseName, err error) {
  724. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.storehouse_id > 0")
  725. table := XTReadDB().Table("xt_storehouse as s").Where("s.status= 1")
  726. fmt.Println(table)
  727. if orgid > 0 {
  728. db = db.Where("(x.stock_max_number >0 or x.stock_min_number >0 ) and x.org_id = ?", orgid)
  729. }
  730. err = db.Select("x.storehouse_id,s.storehouse_name,s.id").Joins("left join xt_storehouse as s on s.id =x.storehouse_id").Group("x.storehouse_id").Find(&list).Error
  731. return list, err
  732. }
  733. func GetAllStoreHouseListTwo(orgid int64) (list []*models.VmStorehouseNameOne, err error) {
  734. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  735. table := XTReadDB().Table("xt_storehouse as s").Where("s.status= 1")
  736. fmt.Println(table)
  737. if orgid > 0 {
  738. db = db.Where("stock_count >0 and x.org_id = ?", orgid)
  739. }
  740. err = db.Select("x.storehouse_id,s.storehouse_name,s.id").Joins("left join xt_storehouse as s on s.id =x.storehouse_id").Group("x.storehouse_id").Find(&list).Error
  741. return list, err
  742. }
  743. func GetAllStoreHouseConfig(orgid int64) (models.XtStorehouseConfig, error) {
  744. config := models.XtStorehouseConfig{}
  745. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  746. return config, err
  747. }
  748. func FindStoreHouseByStorehouseId(id int64, user_org_id int64) (models.XtStorehouse, error) {
  749. storehouse := models.XtStorehouse{}
  750. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, user_org_id).Find(&storehouse).Error
  751. return storehouse, err
  752. }
  753. func GetSecondWarehouseByNumber(user_org_id int64, warehousing_order string) (*models.XtSecondWarehouse, error) {
  754. warehouse := models.XtSecondWarehouse{}
  755. var err error
  756. err = XTReadDB().Where("user_org_id = ? and second_order_number = ? and status =1", user_org_id, warehousing_order).Find(&warehouse).Error
  757. if err == gorm.ErrRecordNotFound {
  758. return nil, err
  759. }
  760. if err != nil {
  761. return nil, err
  762. }
  763. return &warehouse, nil
  764. }
  765. func CreateSecondeWarehouse(warehouse models.XtSecondWarehouse) error {
  766. err := XTWriteDB().Create(&warehouse).Error
  767. return err
  768. }
  769. func CreateSencondWarehousingInfo(info *models.XtSecondWarehouseInfo) error {
  770. err := XTWriteDB().Create(&info).Error
  771. return err
  772. }
  773. func GetAllSecondeOrderList(is_check int64, startime int64, endtime int64, keyword string, page int64, limit int64, orgid int64) (list []*models.VmSecondWarehouse, total int64, err error) {
  774. db := XTReadDB().Table("sgj_xt.xt_second_warehouse").Where("sgj_xt.xt_second_warehouse.status = 1")
  775. likeKey := "%" + keyword + "%"
  776. offset := (page - 1) * limit
  777. if is_check > 0 {
  778. db = db.Where("sgj_xt.xt_second_warehouse.is_check = ?", is_check)
  779. }
  780. if len(keyword) > 0 {
  781. db = db.Joins("join sgj_xt.xt_storehouse on sgj_xt.xt_storehouse.id = sgj_xt.xt_second_warehouse.storehouse_in_id or sgj_xt.xt_storehouse.id = sgj_xt.xt_second_warehouse.storehouse_out_id").Group("sgj_xt.xt_second_warehouse.id")
  782. db = db.Where("sgj_xt.xt_second_warehouse.second_order_number like ? or sgj_xt.xt_storehouse.storehouse_name like ? ", likeKey, likeKey)
  783. }
  784. if startime > 0 {
  785. db = db.Where("sgj_xt.xt_second_warehouse.record_date >= ?", startime)
  786. }
  787. if endtime > 0 {
  788. db = db.Where("sgj_xt.xt_second_warehouse.record_date <= ?", endtime)
  789. }
  790. if orgid > 0 {
  791. db = db.Where("sgj_xt.xt_second_warehouse.user_org_id = ?", orgid)
  792. }
  793. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  794. return list, total, err
  795. }
  796. func GetSecondWarehouseOrderById(id int64) (models.XtSecondWarehouse, error) {
  797. warehouse := models.XtSecondWarehouse{}
  798. err := XTReadDB().Where("id = ? and status = 1", id).Find(&warehouse).Error
  799. return warehouse, err
  800. }
  801. func GetLastSecondWarehouse(orgid int64) (models.XtSecondWarehouse, error) {
  802. warehouse := models.XtSecondWarehouse{}
  803. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&warehouse).Error
  804. return warehouse, err
  805. }
  806. func GetSencondOrderDetail(id int64, orgid int64) (info []*models.XtSecondWarehouseInfo, err error) {
  807. err = XTReadDB().Where("warehouse_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&info).Error
  808. return info, err
  809. }
  810. func UpdateModifySecondWarehouse(id int64, warehouse models.XtSecondWarehouse) error {
  811. err := XTWriteDB().Model(&warehouse).Where("id=? and status = 1", id).Update(map[string]interface{}{"storehouse_in_id": warehouse.StorehouseInId, "storehouse_out_id": warehouse.StorehouseOutId, "record_date": warehouse.RecordDate}).Error
  812. return err
  813. }
  814. func UpdateStoreWarehousing(info *models.XtSecondWarehouseInfo) error {
  815. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", info.ID).Update(map[string]interface{}{"project_name": info.ProjectName, "second_specification_name": info.SecondSpecificationName, "project_type": info.ProjectType, "sencond_unit": info.SencondUnit, "count": info.Count, "second_total": info.SecondTotal, "storehouse_in_id": info.StorehouseInId, "storehouse_out_id": info.StorehouseOutId, "project_id": info.ProjectId, "remake": info.Remake, "min_price": info.MinPrice, "is_source": info.IsSource}).Error
  816. return err
  817. }
  818. func GetSecondOrderList(ids []string, orgid int64) (warehouse []*models.XtSecondWarehouseInfo, err error) {
  819. err = XTReadDB().Where("warehouse_id in(?) and user_org_id = ? and status = 1", ids, orgid).Find(&warehouse).Error
  820. return warehouse, err
  821. }
  822. func UpdateStoreOrderByArray(ids []string, orgid int64, checker int64) error {
  823. err := XTWriteDB().Model(&models.XtSecondWarehouse{}).Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker, "check_time": time.Now().Unix()}).Error
  824. return err
  825. }
  826. func GetWarehouseInfoByStoreHouseId(good_id int64, storehouse_id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  827. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1", good_id, storehouse_id, orgid).Find(&info).Error
  828. return info, err
  829. }
  830. func GetWarehouseInfoByStoreHouseIdOne(good_id int64, storehouse_id int64, orgid int64, second_warehouse_info_id int64) (info []*models.WarehousingInfo, err error) {
  831. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and second_warehouse_info_id = ?", good_id, storehouse_id, orgid, second_warehouse_info_id).Find(&info).Error
  832. return info, err
  833. }
  834. func GetLastWarehouseOutBySys(is_sys int64, orgid int64, warehouse_out_time int64, second_warehouse_id int64) (models.WarehouseOut, error) {
  835. out := models.WarehouseOut{}
  836. err := XTReadDB().Where("is_sys = ? and org_id = ? and status = 1 and warehouse_out_time = ? and second_warehouse_id = ?", is_sys, orgid, warehouse_out_time, second_warehouse_id).Last(&out).Error
  837. return out, err
  838. }
  839. func GetDrugWarehouseInfoByStoreHouseId(drug_id int64, storehouse_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  840. err = XTReadDB().Where("drug_id = ? and storehouse_id = ? and org_id = ? and status = 1", drug_id, storehouse_id, orgid).Find(&info).Error
  841. return info, err
  842. }
  843. func GetDrugWarehouseInfoByStoreHouseIdOne(drug_id int64, storehouse_id int64, orgid int64, second_warehouse_info_id int64) (info []*models.DrugWarehouseInfo, err error) {
  844. err = XTReadDB().Where("drug_id = ? and storehouse_id = ? and org_id = ? and status = 1 and second_warehouse_info_id = ?", drug_id, storehouse_id, orgid, second_warehouse_info_id).Find(&info).Error
  845. return info, err
  846. }
  847. func DeleteStorehouseList(id int64) error {
  848. err = XTWriteDB().Model(&models.XtSecondWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  849. err = XTWriteDB().Model(&models.XtSecondWarehouseInfo{}).Where("warehouse_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  850. return err
  851. }
  852. func GetStoreWarehouseOutList(good_id int64, second_warehouse_info_id int64, orgid int64) (info []*models.WarehouseOutInfo, err error) {
  853. err = XTReadDB().Where("good_id = ? and status = 1 and second_warehouse_info_id = ? and org_id = ?", good_id, second_warehouse_info_id, orgid).Find(&info).Error
  854. return info, err
  855. }
  856. func ModifyStoreWarehouseById(id int64, stock_count int64) error {
  857. info := models.WarehousingInfo{}
  858. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  859. return err
  860. }
  861. func GetStoreWarehouseById(storehouse_id int64, orgid int64) (models.Warehousing, error) {
  862. warehousing := models.Warehousing{}
  863. err := XTReadDB().Where("second_warehouse_id = ? and org_id = ?", storehouse_id, orgid).Find(&warehousing).Error
  864. return warehousing, err
  865. }
  866. func ModifyStoreWarehouse(id int64, orgid int64, good_id int64) error {
  867. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  868. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) and org_id = ? and status =1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  869. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_id in(?) and user_org_id = ? and status = 1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  870. return err
  871. }
  872. func GetStoreWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  873. out := models.WarehouseOut{}
  874. err := XTReadDB().Where("id = ? and org_id = ?", id, orgid).Find(&out).Error
  875. return out, err
  876. }
  877. func ModifyWarehouseOut(id int64, orgid int64, good_id int64) error {
  878. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  879. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and org_id = ? and status =1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  880. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and user_org_id = ? and status = 1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  881. return err
  882. }
  883. func GetStoreDrugWarehouseOutList(drug_id int64, second_warehouse_info_id int64, orgid int64) (drugInfo []*models.DrugWarehouseOutInfo, err error) {
  884. err = XTReadDB().Where("drug_id = ? and status = 1 and second_warehouse_info_id = ? and org_id = ?", drug_id, second_warehouse_info_id, orgid).Find(&drugInfo).Error
  885. return drugInfo, err
  886. }
  887. func ModifyDrugStoreWarehouseInfo(id int64, stock_max_number int64) error {
  888. info := models.DrugWarehouseInfo{}
  889. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  890. return err
  891. }
  892. func ModifyDrugStoreWarehouseInfoOne(id int64, stock_min_number int64) error {
  893. info := models.DrugWarehouseInfo{}
  894. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  895. return err
  896. }
  897. func GetStoreDrugWarehouseById(second_warehouse_id int64, orgid int64) (models.DrugWarehouse, error) {
  898. warehouse := models.DrugWarehouse{}
  899. err := XTReadDB().Where("second_warehouse_id = ? and org_id = ?", second_warehouse_id, orgid).Find(&warehouse).Error
  900. return warehouse, err
  901. }
  902. func DeleteStoreWarehousingId(id int64, orgid int64, drug_id int64) error {
  903. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  904. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id in(?) and org_id = ? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  905. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_id in(?) and user_org_id =? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  906. return err
  907. }
  908. func GetStoreDrugWarehouseOutById(id int64, orgid int64) (models.DrugWarehouseOut, error) {
  909. warehouse := models.DrugWarehouseOut{}
  910. err = XTReadDB().Where("id = ? and org_id = ?", id, orgid).Find(&warehouse).Error
  911. return warehouse, err
  912. }
  913. func DeleteStoreWarehouseOut(id int64, orgid int64, drug_id int64) error {
  914. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  915. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id in(?) and org_id = ? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  916. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehouse_out_id in(?) and user_org_id =? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  917. return err
  918. }
  919. func ModifyStoreHouseById(id []string, orgid int64) error {
  920. err := XTWriteDB().Model(&models.XtSecondWarehouse{}).Where("id in(?) and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 2, "checker": 0, "check_time": 0}).Error
  921. return err
  922. }
  923. func GetStoreWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  924. info := models.WarehousingInfo{}
  925. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  926. return info, err
  927. }
  928. func GetStoreHouseDrugWarehouseingInfo(id int64) (models.DrugWarehouseInfo, error) {
  929. info := models.DrugWarehouseInfo{}
  930. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  931. return info, err
  932. }
  933. func GetDrugSumCountByStorehouseId(storehouse_id int64, orgid int64, drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  934. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and (stock_max_number >0 or stock_min_number>0) and drug_id =?", storehouse_id, orgid, drug_id).Find(&info).Error
  935. return info, err
  936. }
  937. func GetDrugSumCountByStorehouseIdSix(storehouse_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  938. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and (stock_max_number >0 or stock_min_number>0)", storehouse_id, orgid).Find(&info).Error
  939. return info, err
  940. }
  941. func GetGoodSumCountByStoreId(storehouse_id int64, goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  942. err = XTReadDB().Where("storehouse_id = ? and good_id = ? and org_id =? and status =1 and stock_count > 0", storehouse_id, goodid, orgid).Find(&info).Error
  943. return info, err
  944. }
  945. func GetGoodSumCountByStoreIdSix(storehouse_id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  946. err = XTReadDB().Where("storehouse_id = ? and org_id =? and status =1 and stock_count > 0", storehouse_id, orgid).Find(&info).Error
  947. return info, err
  948. }
  949. func GetSupplyDrugListOne(orgid int64, storehouse_id int64) (drug []*models.SpBaseDrug, err error) {
  950. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',drug_status) = 0")
  951. if orgid > 0 {
  952. db = db.Where("x.org_id = ?", orgid)
  953. }
  954. err = db.Preload("DrugWarehouseInfo", "status = 1 and org_id = ? and (stock_max_number >0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Find(&drug).Error
  955. return drug, err
  956. }
  957. func GetSupplyGoodListOne(orgid int64, storehouse_id int64) (good []*models.SpGoodInformation, err error) {
  958. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 AND find_in_set('停用',good_status) = 0")
  959. if orgid > 0 {
  960. db = db.Where("x.org_id = ?", orgid)
  961. }
  962. err = db.Preload("GoodWarehouseInfo", "status = 1 and org_id = ? and storehouse_id = ?", orgid, storehouse_id).Find(&good).Error
  963. return good, err
  964. }
  965. func GetDrugSumSecondCount(drug_id int64, storehouse_id int64, orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  966. err = XTReadDB().Where("drug_id = ? and storehouse_id = ? and org_id = ? and status = 1 and (stock_max_number >0 or stock_min_number>0)", drug_id, storehouse_id, orgid).Find(&info).Error
  967. return info, err
  968. }
  969. func GetGoodSumSecondCount(good_id int64, storehouse_id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  970. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and stock_count > 0", good_id, storehouse_id, orgid).Find(&info).Error
  971. return info, err
  972. }
  973. func DeleteSecondOrderInfo(id int64) error {
  974. err := XTWriteDB().Model(&models.XtSecondWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  975. return err
  976. }