secondary_service.go 49KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. package service
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "strconv"
  6. "time"
  7. "XT_New/models"
  8. "XT_New/utils"
  9. "github.com/jinzhu/gorm"
  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. //
  229. // storehouse_info:耗材 自动入库 的仓库id
  230. // storehouse_out_info:耗材 自动出库 的仓库id
  231. // drug_storehouse_info:药品 自动入库 的仓库id
  232. // drug_storehouse_out:药品 自动出库 的仓库id
  233. func FindStorehouseConfig(orgid int64) (storehouse_config models.StorehouseConfig, err error) {
  234. err = XTReadDB().Model(&models.StorehouseConfig{}).Where("status = 1 and user_org_id = ?", orgid).Find(&storehouse_config).Error
  235. return
  236. }
  237. // 根据机构id,仓库id新建一个仓库配置
  238. func CreateStorehouseConfig(orgid, id int64) (err error) {
  239. storeconfig := models.StorehouseConfig{
  240. UserOrgId: orgid,
  241. StorehouseInfo: id,
  242. StorehouseOutInfo: id,
  243. DrugStorehouseInfo: id,
  244. DrugStorehouseOut: id,
  245. Status: 1,
  246. Ctime: time.Now().Unix(),
  247. }
  248. err = XTWriteDB().Create(&storeconfig).Error
  249. return
  250. }
  251. // 根据机构id,仓库id新建一个仓库配置,带事务
  252. func CreateStorehouseConfigXT(orgid, id int64, tx *gorm.DB) (err error) {
  253. storeconfig := models.StorehouseConfig{
  254. UserOrgId: orgid,
  255. StorehouseInfo: id,
  256. StorehouseOutInfo: id,
  257. DrugStorehouseInfo: id,
  258. DrugStorehouseOut: id,
  259. Status: 1,
  260. Ctime: time.Now().Unix(),
  261. }
  262. err = tx.Create(&storeconfig).Error
  263. return
  264. }
  265. // 更改耗材自动入库仓库
  266. func UpdateInfo(orgid, id int64) (err error) {
  267. mtime := time.Now().Unix()
  268. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_info": id, "mtime": mtime}).Error
  269. return
  270. }
  271. // 更改耗材自动出库仓库
  272. func UpdateOutInfo(orgid, id int64) (err error) {
  273. mtime := time.Now().Unix()
  274. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_out_info": id, "mtime": mtime}).Error
  275. return
  276. }
  277. // 更改药品自动入库仓库
  278. func UpdateDrugInfo2(orgid, id int64) (err error) {
  279. mtime := time.Now().Unix()
  280. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_info": id, "mtime": mtime}).Error
  281. return
  282. }
  283. // 更改药品自动出库仓库
  284. func UpdateDrugOut(orgid, id int64) (err error) {
  285. mtime := time.Now().Unix()
  286. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_out": id, "mtime": mtime}).Error
  287. return
  288. }
  289. // 根据id查询仓库名称
  290. func FindStorehouseName(id int64) (storehouse models.Storehouse, err error) {
  291. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ?", id).Find(&storehouse).Error
  292. return
  293. }
  294. // 判断该仓库是否在仓库配置表中
  295. func IsInConfig(orgid, id int64) bool {
  296. var total int
  297. XTReadDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Where(
  298. "storehouse_info = ? or storehouse_out_info = ? or drug_storehouse_info = ? or drug_storehouse_out = ?", id, id, id, id).Count(&total)
  299. if total > 0 {
  300. return true //存在
  301. } else {
  302. return false
  303. }
  304. }
  305. // 兼容旧数据
  306. func Byliinit() (err error) {
  307. tx := XTWriteDB().Begin()
  308. defer func() {
  309. if err != nil && err.Error() != "record not found" {
  310. tx.Rollback()
  311. } else {
  312. tx.Commit()
  313. }
  314. }()
  315. err = initsgj_user_admin(tx)
  316. if err != nil && err.Error() != "record not found" {
  317. return
  318. }
  319. err = StoreReduceOrg(tx)
  320. if err != nil && err.Error() != "record not found" {
  321. return
  322. }
  323. err = StoreReduceConfig(tx)
  324. if err != nil && err.Error() != "record not found" {
  325. return
  326. }
  327. err = initxt_storehouse(tx)
  328. if err != nil && err.Error() != "record not found" {
  329. return
  330. }
  331. err = initxt_stock_flow(tx)
  332. if err != nil && err.Error() != "record not found" {
  333. return
  334. }
  335. err = initxt_drug_flow(tx)
  336. if err != nil && err.Error() != "record not found" {
  337. return
  338. }
  339. err = initdialysis_before_prepare(tx)
  340. if err != nil && err.Error() != "record not found" {
  341. return
  342. }
  343. err = initxt_automatic_reduce_detail(tx)
  344. if err != nil && err.Error() != "record not found" {
  345. return
  346. }
  347. err = initxt_drug_automatic_reduce_detail(tx)
  348. if err != nil && err.Error() != "record not found" {
  349. return
  350. }
  351. err = initxt_warehouse(tx)
  352. if err != nil && err.Error() != "record not found" {
  353. return
  354. }
  355. err = initxt_warehouse_info(tx)
  356. if err != nil && err.Error() != "record not found" {
  357. return
  358. }
  359. err = initxt_warehouse_out(tx)
  360. if err != nil && err.Error() != "record not found" {
  361. return
  362. }
  363. err = initxt_warehouse_out_info(tx)
  364. if err != nil && err.Error() != "record not found" {
  365. return
  366. }
  367. err = initxt_drug_warehouse(tx)
  368. if err != nil && err.Error() != "record not found" {
  369. return
  370. }
  371. err = initxt_drug_warehouse_info(tx)
  372. if err != nil && err.Error() != "record not found" {
  373. return
  374. }
  375. err = initxt_drug_warehouse_out(tx)
  376. if err != nil && err.Error() != "record not found" {
  377. return
  378. }
  379. err = initxt_drug_warehouse_out_info(tx)
  380. if err != nil && err.Error() != "record not found" {
  381. return
  382. }
  383. err = initxt_cancel_stock(tx)
  384. if err != nil && err.Error() != "record not found" {
  385. return
  386. }
  387. err = initxt_cancel_stock_info(tx)
  388. if err != nil && err.Error() != "record not found" {
  389. return
  390. }
  391. err = initxt_drug_cancel_stock(tx)
  392. if err != nil && err.Error() != "record not found" {
  393. return
  394. }
  395. err = initxt_drug_cancel_stock_info(tx)
  396. if err != nil && err.Error() != "record not found" {
  397. return
  398. }
  399. err = initxt_supplier_warehouse_info(tx)
  400. if err != nil && err.Error() != "record not found" {
  401. return
  402. }
  403. err = initxt_supplier_warehousing_info_order(tx)
  404. if err != nil && err.Error() != "record not found" {
  405. return
  406. }
  407. err = initxt_supplier_warehouse_out(tx)
  408. if err != nil && err.Error() != "record not found" {
  409. return
  410. }
  411. err = initxt_supplier_warehousing_out_order(tx)
  412. if err != nil && err.Error() != "record not found" {
  413. return
  414. }
  415. err = initxt_supplier_warehouse_cancel(tx)
  416. if err != nil && err.Error() != "record not found" {
  417. return
  418. }
  419. err = initxt_supplier_warehousing_cancel_order(tx)
  420. return
  421. }
  422. // 初始化管理员名字
  423. func initsgj_user_admin(tx *gorm.DB) (err error) {
  424. err = tx.Exec("update sgj_users.sgj_user_admin set name = \"超级管理员\" where name is null or name = \"\"").Error
  425. return
  426. }
  427. // 根据xt_gobal_template 获取的机构id减去仓库表存在的机构id,把结果插入到仓库表
  428. func StoreReduceOrg(tx *gorm.DB) (err error) {
  429. err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  430. "SELECT CONCAT(\"SH-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  431. "FROM" +
  432. "(select t1.org_id as id FROM sgj_xt.xt_gobal_template as t1 LEFT JOIN " +
  433. "(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
  434. //err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  435. // "SELECT CONCAT(\"sh-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  436. // "FROM" +
  437. // "(select t1.id FROM sgj_users.sgj_user_org as t1 LEFT JOIN " +
  438. // "(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
  439. return
  440. }
  441. // 查找仓库表的orgid 减去仓库配置表的orgid,把结果插入到仓库配置表
  442. func StoreReduceConfig(tx *gorm.DB) (err error) {
  443. 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)" +
  444. "SELECT tmp.user_org_id,tmp.id,tmp.id,tmp.id,tmp.id,UNIX_TIMESTAMP()" +
  445. "FROM(select t1.id,t1.user_org_id FROM sgj_xt.xt_storehouse as t1 LEFT JOIN " +
  446. "(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
  447. return
  448. }
  449. // 初始化 xt_storehouse 管理员的名字
  450. func initxt_storehouse(tx *gorm.DB) (err error) {
  451. err = tx.Exec("UPDATE sgj_xt.xt_storehouse as t," +
  452. "(SELECT o.id,o.creator from sgj_users.sgj_user_org as o," +
  453. "(SELECT DISTINCT org_id from sgj_xt.xt_gobal_template)as t " +
  454. "WHERE t.org_id = o.id )tmp " +
  455. "SET t.storehouse_admin_id = tmp.creator,t.mtime = UNIX_TIMESTAMP()" +
  456. "WHERE t.status = 1 and t.user_org_id = tmp.id").Error
  457. return
  458. }
  459. // 初始化 xt_stock_flow(完成)
  460. func initxt_stock_flow(tx *gorm.DB) (err error) {
  461. err = tx.Exec("UPDATE sgj_xt.xt_stock_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_stock_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. // 初始化 xt_drug_flow(完成)
  470. func initxt_drug_flow(tx *gorm.DB) (err error) {
  471. err = tx.Exec("UPDATE sgj_xt.xt_drug_flow 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.xt_drug_flow 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. // 初始化 dialysis_before_prepare(完成)
  480. func initdialysis_before_prepare(tx *gorm.DB) (err error) {
  481. err = tx.Exec("UPDATE sgj_xt.dialysis_before_prepare as t," +
  482. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  483. "(SELECT distinct user_org_id as orgid FROM sgj_xt.dialysis_before_prepare 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.user_org_id = tmp.orgid").Error
  487. return
  488. }
  489. // 初始化 xt_automatic_reduce_detail(完成)
  490. func initxt_automatic_reduce_detail(tx *gorm.DB) (err error) {
  491. err = tx.Exec("UPDATE sgj_xt.xt_automatic_reduce_detail as t," +
  492. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  493. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_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_drug_automatic_reduce_detail(完成)
  500. func initxt_drug_automatic_reduce_detail(tx *gorm.DB) (err error) {
  501. err = tx.Exec("UPDATE sgj_xt.xt_drug_automatic_reduce_detail as t," +
  502. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  503. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_automatic_reduce_detail 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(完成)
  510. func initxt_warehouse(tx *gorm.DB) (err error) {
  511. err = tx.Exec("UPDATE sgj_xt.xt_warehouse 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 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_info(完成)
  520. func initxt_warehouse_info(tx *gorm.DB) (err error) {
  521. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_info as t," +
  522. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  523. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_info 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(完成)
  530. func initxt_warehouse_out(tx *gorm.DB) (err error) {
  531. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out 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 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_warehouse_out_info(完成)
  540. func initxt_warehouse_out_info(tx *gorm.DB) (err error) {
  541. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out_info as t," +
  542. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  543. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out_info 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(完成)
  550. func initxt_drug_warehouse(tx *gorm.DB) (err error) {
  551. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse 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 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_info(完成)
  560. func initxt_drug_warehouse_info(tx *gorm.DB) (err error) {
  561. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_info as t," +
  562. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  563. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_info 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(完成)
  570. func initxt_drug_warehouse_out(tx *gorm.DB) (err error) {
  571. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out 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 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_drug_warehouse_out_info(完成)
  580. func initxt_drug_warehouse_out_info(tx *gorm.DB) (err error) {
  581. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out_info as t," +
  582. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  583. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out_info 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(完成)
  590. func initxt_cancel_stock(tx *gorm.DB) (err error) {
  591. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock 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 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_cancel_stock_info(完成)
  600. func initxt_cancel_stock_info(tx *gorm.DB) (err error) {
  601. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock_info as t," +
  602. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  603. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock_info 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(完成)
  610. func initxt_drug_cancel_stock(tx *gorm.DB) (err error) {
  611. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock 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 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_drug_cancel_stock_info(完成)
  620. func initxt_drug_cancel_stock_info(tx *gorm.DB) (err error) {
  621. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock_info as t," +
  622. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  623. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock_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.org_id = tmp.orgid").Error
  627. return
  628. }
  629. // 初始化 xt_supplier_warehouse_info(完成)
  630. func initxt_supplier_warehouse_info(tx *gorm.DB) (err error) {
  631. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_info 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_warehouse_info 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_warehousing_info_order(完成)
  640. func initxt_supplier_warehousing_info_order(tx *gorm.DB) (err error) {
  641. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_info_order as t," +
  642. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  643. "(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 " +
  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_warehouse_out(完成)
  650. func initxt_supplier_warehouse_out(tx *gorm.DB) (err error) {
  651. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_out as t," +
  652. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  653. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_out 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_warehousing_out_order(完成)
  660. func initxt_supplier_warehousing_out_order(tx *gorm.DB) (err error) {
  661. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_out_order 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_warehousing_out_order 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_warehouse_cancel(完成)
  670. func initxt_supplier_warehouse_cancel(tx *gorm.DB) (err error) {
  671. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_cancel 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_warehouse_cancel 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. // 初始化 xt_supplier_warehousing_cancel_order(完成)
  680. func initxt_supplier_warehousing_cancel_order(tx *gorm.DB) (err error) {
  681. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_cancel_order as t," +
  682. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  683. "(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 " +
  684. "WHERE t1.user_org_id = t2.orgid)tmp " +
  685. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  686. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  687. return
  688. }
  689. // 查询机构创建者的id
  690. func Getcreateid(orgid int64) (createid models.UserOrg, err error) {
  691. err = XTReadDB().Select("id,creator").Where("id = ? and status = 1", orgid).Find(&createid).Error
  692. return
  693. }
  694. // 根据机构id查询拥有仓库管理权限的角色
  695. func FindRoles(orgid int64) map[string]int {
  696. role := []models.RolePurviews{}
  697. tmp := models.PurviewTmp{}
  698. roles := make(map[string]int)
  699. //查询sgj_user_purview表条件是urlfor="/stock/warehousequery" and name="仓库管理"
  700. XTReadDB().Select("id").Where("urlfor=\"/stock/warehousequery\" and name=\"仓库管理\"").Find(&tmp)
  701. tmpstring := "%" + fmt.Sprint(tmp.Id) + "%"
  702. XTReadDB().Select("role_id").Where("purview_ids like ? and org_id = ? and status = 1", tmpstring, orgid).Find(&role)
  703. for i := 0; i < len(role); i++ {
  704. roles[strconv.FormatInt(role[i].RoleId, 10)] = i
  705. }
  706. return roles
  707. }
  708. // 判断两个切片中的元素是否有重复的,true有重复的元素,false无相同元素
  709. func IsIdentical(str1 []string, str2 []string) (t bool) {
  710. t = false
  711. if len(str1) == 0 || len(str2) == 0 {
  712. return
  713. }
  714. map1, map2 := make(map[string]int), make(map[string]int)
  715. for i := 0; i < len(str1); i++ {
  716. map1[str1[i]] = i
  717. }
  718. for i := 0; i < len(str2); i++ {
  719. map2[str2[i]] = i
  720. }
  721. for k, _ := range map1 {
  722. if _, ok := map2[k]; ok {
  723. t = true
  724. }
  725. }
  726. return
  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).First(&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 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. }
  990. func GetStockFlowOrderList(good_id int64, user_org_id int64) (list []*models.VmStockFlow, err error) {
  991. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1", good_id, user_org_id).Order("id asc").Find(&list).Error
  992. return list, err
  993. }
  994. func UpdateOverCount(id int64, count int64) (err error) {
  995. err = XTWriteDB().Model(models.VmStockFlow{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"flush_over_count": count}).Error
  996. return err
  997. }
  998. func UpdateDrugOverCount(id int64, count int64) (err error) {
  999. err = XTWriteDB().Model(models.DrugFlow{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"flush_over_count": count}).Error
  1000. return err
  1001. }
  1002. func GetLastOverCount(id int64) (models.VmStockFlow, error) {
  1003. stockFlow := models.VmStockFlow{}
  1004. err := XTReadDB().Where("id=? and status=1", id).Find(&stockFlow).Error
  1005. return stockFlow, err
  1006. }
  1007. func GetLastDrugOverCount(id int64) (models.DrugFlow, error) {
  1008. stockFlow := models.DrugFlow{}
  1009. err := XTReadDB().Where("id=? and status=1", id).Find(&stockFlow).Error
  1010. return stockFlow, err
  1011. }
  1012. func GetNewAllStoreHouseConfig(orgid int64, tx *gorm.DB) (models.XtStorehouseConfig, error) {
  1013. config := models.XtStorehouseConfig{}
  1014. err := tx.Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  1015. if err != gorm.ErrRecordNotFound {
  1016. if err != nil {
  1017. tx.Rollback()
  1018. return config, err
  1019. }
  1020. }
  1021. return config, err
  1022. }