secondary_service.go 47KB

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