secondary_service.go 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "math/rand"
  8. "time"
  9. )
  10. //生成编号,规则为:"SH-"+4位随机数
  11. func CreateCode() string {
  12. s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000))
  13. code := "SH-" + s
  14. return code
  15. }
  16. //查询编号是否重复
  17. func FindStorehouseCode(orgid int64, code string) bool {
  18. var total int
  19. XTReadDB().Model(&models.Storehouse{}).Where(" storehouse_code = ? and user_org_id = ? and status = 1", code, orgid).Count(&total)
  20. if total > 0 {
  21. return true
  22. } else {
  23. return false
  24. }
  25. }
  26. //判断仓库的库存是否为零 true为零,false不为零
  27. func IsStorehouseNil(id, orgid int64) bool { //根据 id gro_id 查为零的库存
  28. var h, y int
  29. //判断耗材是否为零
  30. XTReadDB().Model(&models.WarehousingInfo{}).Where("stock_count > 0 and status = 1 and org_id = ? and storehouse_id = ?", orgid, id).Count(&h)
  31. if h > 0 {
  32. return false
  33. }
  34. //判断药品是否为零
  35. XTReadDB().Model(&models.XtDrugWarehouseInfo{}).Where("stock_max_number > 0 or stock_min_number > 0 ").Where(" org_id = ? and storehouse_id = ?", orgid, id).Count(&y)
  36. if y > 0 {
  37. return false
  38. }
  39. return true
  40. }
  41. //修改仓库状态
  42. func UpdateStorehouseStatus(id int64) error {
  43. 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
  44. return err
  45. }
  46. //删除仓库
  47. func DeleteStorehouse(id int64) error {
  48. err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ?", id).Update("status", 0).Error
  49. return err
  50. }
  51. //查询仓库名称是否重复
  52. func IsStorehouseName(orgid int64, storehouse_name string) (bool, error) {
  53. var total int
  54. var tmp bool
  55. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_name = ? and user_org_id = ? and status = 1", storehouse_name, orgid).Count(&total).Error
  56. if total > 0 {
  57. tmp = true //有重复的
  58. } else {
  59. tmp = false
  60. }
  61. return tmp, err
  62. }
  63. //查询仓库地址是否重复
  64. func IsStorehouseAddress(orgid int64, storehouse_address string) (bool, error) {
  65. var total int
  66. var tmp bool
  67. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_address = ? and user_org_id = ? and status = 1", storehouse_address, orgid).Count(&total).Error
  68. if total > 0 {
  69. tmp = true //有重复的
  70. } else {
  71. tmp = false
  72. }
  73. return tmp, err
  74. }
  75. //新增仓库
  76. func AddStroehouse(storehouse models.Storehouse) error {
  77. tx := XTWriteDB().Begin()
  78. defer func() {
  79. if err != nil {
  80. tx.Rollback()
  81. } else {
  82. tx.Commit()
  83. }
  84. }()
  85. //创建仓库
  86. err := tx.Create(&storehouse).Error
  87. if err != nil {
  88. return err
  89. }
  90. var id models.Storehouse
  91. //获取创建仓库的id
  92. err = tx.Model(&models.Storehouse{}).Select("id").Where("status = 1 and user_org_id = ?", storehouse.UserOrgId).Order("id desc").First(&id).Error
  93. if err != nil {
  94. return err
  95. }
  96. //判断是否存在仓库配置
  97. boolean := IsStorehouseconfigXT(storehouse.UserOrgId, tx)
  98. if boolean == false {
  99. //创建仓库配置
  100. err = CreateStorehouseConfigXT(storehouse.UserOrgId, id.ID, tx)
  101. } else {
  102. utils.ErrorLog("仓库配置已存在")
  103. }
  104. return err
  105. }
  106. //修改仓库
  107. func UpdateStroehouse(storehouse models.Storehouse) error {
  108. err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ? and status = 1", storehouse.ID).Updates(
  109. map[string]interface{}{
  110. "storehouse_name": storehouse.StorehouseName,
  111. "storehouse_address": storehouse.StorehouseAddress,
  112. "storehouse_status": storehouse.StorehouseStatus,
  113. "mtime": storehouse.Mtime,
  114. }).Error
  115. return err
  116. }
  117. //查询一条仓库的信息
  118. func GetOneStorehouse(id, orgid int64) (storehouse models.Storehouse, err error) {
  119. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ? and user_org_id = ?", id, orgid).Find(&storehouse).Error
  120. return
  121. }
  122. //分页
  123. func StorehouseList(page, limit, orgid int64, keyword string) (storehouse []models.Storehouse, total int64, err error) {
  124. db := XTReadDB().Model(&storehouse).Where("status = 1 and user_org_id = ?", orgid)
  125. offset := (page - 1) * limit
  126. if len(keyword) > 0 {
  127. keyword = "%" + keyword + "%"
  128. db = db.Where("storehouse_code like ? or storehouse_name like ? or storehouse_address like ? or storehouse_admin_name like ?", keyword, keyword, keyword, keyword)
  129. }
  130. err = db.Count(&total).Offset(offset).Order("id").Limit(limit).Find(&storehouse).Error
  131. return storehouse, total, err
  132. }
  133. //获取当前机构所有可用的仓库名字
  134. func GetAllStorehouseName(orgid int64) (storehouse []models.Storehouse, err error) {
  135. err = XTReadDB().Model(&models.Storehouse{}).Where("storehouse_status = 1 and status = 1 and user_org_id = ?", orgid).Find(&storehouse).Error
  136. return
  137. }
  138. //根据机构id,生成一个默认仓库
  139. func GetDefaultStorehouse(orgid int64) error {
  140. var code string
  141. for a := true; a == true; {
  142. code = CreateCode()
  143. tmp := FindStorehouseCode(orgid, code)
  144. //如果没有重复的编码结束循环
  145. if tmp == false {
  146. a = false
  147. }
  148. }
  149. storehouse := models.Storehouse{
  150. StorehouseCode: code,
  151. StorehouseName: "默认仓库",
  152. StorehouseAddress: "",
  153. StorehouseStatus: 1,
  154. UserOrgId: orgid,
  155. StorehouseAdminName: "admin",
  156. Status: 1,
  157. Ctime: time.Now().Unix(),
  158. }
  159. err := AddStroehouse(storehouse)
  160. return err
  161. }
  162. //查找该机构id是否有仓库存在,ture存在,false不存在
  163. func IsStorehouse(orgid int64) bool {
  164. var total int
  165. XTReadDB().Model(&models.Storehouse{}).Where("user_org_id = ?", orgid).Count(&total)
  166. if total > 0 {
  167. return true
  168. } else {
  169. return false
  170. }
  171. }
  172. //查找该机构id是否有仓库配置存在,ture存在,false不存在
  173. func IsStorehouseconfigXT(orgid int64, tx *gorm.DB) bool {
  174. var total int
  175. tx.Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Count(&total)
  176. if total > 0 {
  177. return true
  178. } else {
  179. return false
  180. }
  181. }
  182. // 根据机构id查询仓库配置
  183. //storehouse_info:耗材 自动入库 的仓库id
  184. //storehouse_out_info:耗材 自动出库 的仓库id
  185. //drug_storehouse_info:药品 自动入库 的仓库id
  186. //drug_storehouse_out:药品 自动出库 的仓库id
  187. func FindStorehouseConfig(orgid int64) (storehouse_config models.StorehouseConfig, err error) {
  188. err = XTReadDB().Model(&models.StorehouseConfig{}).Where("status = 1 and user_org_id = ?", orgid).Find(&storehouse_config).Error
  189. return
  190. }
  191. //根据机构id,仓库id新建一个仓库配置
  192. func CreateStorehouseConfig(orgid, id int64) (err error) {
  193. storeconfig := models.StorehouseConfig{
  194. UserOrgId: orgid,
  195. StorehouseInfo: id,
  196. StorehouseOutInfo: id,
  197. DrugStorehouseInfo: id,
  198. DrugStorehouseOut: id,
  199. Status: 1,
  200. Ctime: time.Now().Unix(),
  201. }
  202. err = XTWriteDB().Create(&storeconfig).Error
  203. return
  204. }
  205. //根据机构id,仓库id新建一个仓库配置,带事务
  206. func CreateStorehouseConfigXT(orgid, id int64, tx *gorm.DB) (err error) {
  207. storeconfig := models.StorehouseConfig{
  208. UserOrgId: orgid,
  209. StorehouseInfo: id,
  210. StorehouseOutInfo: id,
  211. DrugStorehouseInfo: id,
  212. DrugStorehouseOut: id,
  213. Status: 1,
  214. Ctime: time.Now().Unix(),
  215. }
  216. err = tx.Create(&storeconfig).Error
  217. return
  218. }
  219. //更改耗材自动入库仓库
  220. func UpdateInfo(orgid, id int64) (err error) {
  221. mtime := time.Now().Unix()
  222. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_info": id, "mtime": mtime}).Error
  223. return
  224. }
  225. //更改耗材自动出库仓库
  226. func UpdateOutInfo(orgid, id int64) (err error) {
  227. mtime := time.Now().Unix()
  228. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_out_info": id, "mtime": mtime}).Error
  229. return
  230. }
  231. //更改药品自动入库仓库
  232. func UpdateDrugInfo2(orgid, id int64) (err error) {
  233. mtime := time.Now().Unix()
  234. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_info": id, "mtime": mtime}).Error
  235. return
  236. }
  237. //更改药品自动出库仓库
  238. func UpdateDrugOut(orgid, id int64) (err error) {
  239. mtime := time.Now().Unix()
  240. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_out": id, "mtime": mtime}).Error
  241. return
  242. }
  243. //根据id查询仓库名称
  244. func FindStorehouseName(id int64) (storehouse models.Storehouse, err error) {
  245. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ?", id).Find(&storehouse).Error
  246. return
  247. }
  248. //判断该仓库是否在仓库配置表中
  249. func IsInConfig(orgid, id int64) bool {
  250. var total int
  251. XTReadDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Where(
  252. "storehouse_info = ? or storehouse_out_info = ? or drug_storehouse_info = ? or drug_storehouse_out = ?", id, id, id, id).Count(&total)
  253. if total > 0 {
  254. return true //存在
  255. } else {
  256. return false
  257. }
  258. }
  259. //兼容旧数据
  260. func Byliinit() (err error) {
  261. tx := XTWriteDB().Begin()
  262. defer func() {
  263. if err != nil && err.Error() != "record not found" {
  264. tx.Rollback()
  265. } else {
  266. tx.Commit()
  267. }
  268. }()
  269. err = StoreReduceOrg(tx)
  270. if err != nil && err.Error() != "record not found" {
  271. return
  272. }
  273. err = StoreReduceConfig(tx)
  274. if err != nil && err.Error() != "record not found" {
  275. return
  276. }
  277. err = initxt_stock_flow(tx)
  278. if err != nil && err.Error() != "record not found" {
  279. return
  280. }
  281. err = initxt_drug_flow(tx)
  282. if err != nil && err.Error() != "record not found" {
  283. return
  284. }
  285. err = initdialysis_before_prepare(tx)
  286. if err != nil && err.Error() != "record not found" {
  287. return
  288. }
  289. err = initxt_automatic_reduce_detail(tx)
  290. if err != nil && err.Error() != "record not found" {
  291. return
  292. }
  293. err = initxt_drug_automatic_reduce_detail(tx)
  294. if err != nil && err.Error() != "record not found" {
  295. return
  296. }
  297. err = initxt_warehouse(tx)
  298. if err != nil && err.Error() != "record not found" {
  299. return
  300. }
  301. err = initxt_warehouse_info(tx)
  302. if err != nil && err.Error() != "record not found" {
  303. return
  304. }
  305. err = initxt_warehouse_out(tx)
  306. if err != nil && err.Error() != "record not found" {
  307. return
  308. }
  309. err = initxt_warehouse_out_info(tx)
  310. if err != nil && err.Error() != "record not found" {
  311. return
  312. }
  313. err = initxt_drug_warehouse(tx)
  314. if err != nil && err.Error() != "record not found" {
  315. return
  316. }
  317. err = initxt_drug_warehouse_info(tx)
  318. if err != nil && err.Error() != "record not found" {
  319. return
  320. }
  321. err = initxt_drug_warehouse_out(tx)
  322. if err != nil && err.Error() != "record not found" {
  323. return
  324. }
  325. err = initxt_drug_warehouse_out_info(tx)
  326. if err != nil && err.Error() != "record not found" {
  327. return
  328. }
  329. err = initxt_cancel_stock(tx)
  330. if err != nil && err.Error() != "record not found" {
  331. return
  332. }
  333. err = initxt_cancel_stock_info(tx)
  334. if err != nil && err.Error() != "record not found" {
  335. return
  336. }
  337. err = initxt_drug_cancel_stock(tx)
  338. if err != nil && err.Error() != "record not found" {
  339. return
  340. }
  341. err = initxt_drug_cancel_stock_info(tx)
  342. if err != nil && err.Error() != "record not found" {
  343. return
  344. }
  345. err = initxt_supplier_warehouse_info(tx)
  346. if err != nil && err.Error() != "record not found" {
  347. return
  348. }
  349. err = initxt_supplier_warehousing_info_order(tx)
  350. if err != nil && err.Error() != "record not found" {
  351. return
  352. }
  353. err = initxt_supplier_warehouse_out(tx)
  354. if err != nil && err.Error() != "record not found" {
  355. return
  356. }
  357. err = initxt_supplier_warehousing_out_order(tx)
  358. if err != nil && err.Error() != "record not found" {
  359. return
  360. }
  361. err = initxt_supplier_warehouse_cancel(tx)
  362. if err != nil && err.Error() != "record not found" {
  363. return
  364. }
  365. err = initxt_supplier_warehousing_cancel_order(tx)
  366. return
  367. }
  368. //根据xt_gobal_template 获取的机构id减去仓库表存在的机构id,把结果插入到仓库表
  369. func StoreReduceOrg(tx *gorm.DB) (err error) {
  370. err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  371. "SELECT CONCAT(\"SH-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  372. "FROM" +
  373. "(select t1.org_id as id FROM sgj_xt.xt_gobal_template as t1 LEFT JOIN " +
  374. "(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
  375. //err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  376. // "SELECT CONCAT(\"sh-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  377. // "FROM" +
  378. // "(select t1.id FROM sgj_users.sgj_user_org as t1 LEFT JOIN " +
  379. // "(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
  380. return
  381. }
  382. //查找仓库表的orgid 减去仓库配置表的orgid,把结果插入到仓库配置表
  383. func StoreReduceConfig(tx *gorm.DB) (err error) {
  384. 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)" +
  385. "SELECT tmp.user_org_id,tmp.id,tmp.id,tmp.id,tmp.id,UNIX_TIMESTAMP()" +
  386. "FROM(select t1.id,t1.user_org_id FROM sgj_xt.xt_storehouse as t1 LEFT JOIN " +
  387. "(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
  388. return
  389. }
  390. //初始化 xt_stock_flow(完成)
  391. func initxt_stock_flow(tx *gorm.DB) (err error) {
  392. err = tx.Exec("UPDATE sgj_xt.xt_stock_flow as t," +
  393. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  394. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_stock_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  395. "WHERE t1.user_org_id = t2.orgid)tmp " +
  396. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  397. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  398. return
  399. }
  400. //初始化 xt_drug_flow(完成)
  401. func initxt_drug_flow(tx *gorm.DB) (err error) {
  402. err = tx.Exec("UPDATE sgj_xt.xt_drug_flow as t," +
  403. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  404. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_drug_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  405. "WHERE t1.user_org_id = t2.orgid)tmp " +
  406. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  407. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  408. return
  409. }
  410. //初始化 dialysis_before_prepare(完成)
  411. func initdialysis_before_prepare(tx *gorm.DB) (err error) {
  412. err = tx.Exec("UPDATE sgj_xt.dialysis_before_prepare as t," +
  413. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  414. "(SELECT distinct user_org_id as orgid FROM sgj_xt.dialysis_before_prepare WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  415. "WHERE t1.user_org_id = t2.orgid)tmp " +
  416. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  417. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  418. return
  419. }
  420. //初始化 xt_automatic_reduce_detail(完成)
  421. func initxt_automatic_reduce_detail(tx *gorm.DB) (err error) {
  422. err = tx.Exec("UPDATE sgj_xt.xt_automatic_reduce_detail as t," +
  423. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  424. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  425. "WHERE t1.user_org_id = t2.orgid)tmp " +
  426. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  427. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  428. return
  429. }
  430. //初始化 xt_drug_automatic_reduce_detail(完成)
  431. func initxt_drug_automatic_reduce_detail(tx *gorm.DB) (err error) {
  432. err = tx.Exec("UPDATE sgj_xt.xt_drug_automatic_reduce_detail as t," +
  433. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  434. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  435. "WHERE t1.user_org_id = t2.orgid)tmp " +
  436. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  437. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  438. return
  439. }
  440. //初始化 xt_warehouse(完成)
  441. func initxt_warehouse(tx *gorm.DB) (err error) {
  442. err = tx.Exec("UPDATE sgj_xt.xt_warehouse as t," +
  443. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  444. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  445. "WHERE t1.user_org_id = t2.orgid)tmp " +
  446. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  447. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  448. return
  449. }
  450. //初始化 xt_warehouse_info(完成)
  451. func initxt_warehouse_info(tx *gorm.DB) (err error) {
  452. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_info as t," +
  453. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  454. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  455. "WHERE t1.user_org_id = t2.orgid)tmp " +
  456. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  457. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  458. return
  459. }
  460. //初始化 xt_warehouse_out(完成)
  461. func initxt_warehouse_out(tx *gorm.DB) (err error) {
  462. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out as t," +
  463. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  464. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  465. "WHERE t1.user_org_id = t2.orgid)tmp " +
  466. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  467. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  468. return
  469. }
  470. //初始化 xt_warehouse_out_info(完成)
  471. func initxt_warehouse_out_info(tx *gorm.DB) (err error) {
  472. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out_info as t," +
  473. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  474. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  475. "WHERE t1.user_org_id = t2.orgid)tmp " +
  476. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  477. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  478. return
  479. }
  480. //初始化 xt_drug_warehouse(完成)
  481. func initxt_drug_warehouse(tx *gorm.DB) (err error) {
  482. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse as t," +
  483. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  484. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  485. "WHERE t1.user_org_id = t2.orgid)tmp " +
  486. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  487. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  488. return
  489. }
  490. //初始化 xt_drug_warehouse_info(完成)
  491. func initxt_drug_warehouse_info(tx *gorm.DB) (err error) {
  492. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_info as t," +
  493. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  494. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  495. "WHERE t1.user_org_id = t2.orgid)tmp " +
  496. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  497. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  498. return
  499. }
  500. //初始化 xt_drug_warehouse_out(完成)
  501. func initxt_drug_warehouse_out(tx *gorm.DB) (err error) {
  502. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out as t," +
  503. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  504. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  505. "WHERE t1.user_org_id = t2.orgid)tmp " +
  506. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  507. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  508. return
  509. }
  510. //初始化 xt_drug_warehouse_out_info(完成)
  511. func initxt_drug_warehouse_out_info(tx *gorm.DB) (err error) {
  512. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out_info as t," +
  513. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  514. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  515. "WHERE t1.user_org_id = t2.orgid)tmp " +
  516. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  517. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  518. return
  519. }
  520. //初始化 xt_cancel_stock(完成)
  521. func initxt_cancel_stock(tx *gorm.DB) (err error) {
  522. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock as t," +
  523. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  524. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  525. "WHERE t1.user_org_id = t2.orgid)tmp " +
  526. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  527. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  528. return
  529. }
  530. //初始化 xt_cancel_stock_info(完成)
  531. func initxt_cancel_stock_info(tx *gorm.DB) (err error) {
  532. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock_info as t," +
  533. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  534. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  535. "WHERE t1.user_org_id = t2.orgid)tmp " +
  536. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  537. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  538. return
  539. }
  540. //初始化 xt_drug_cancel_stock(完成)
  541. func initxt_drug_cancel_stock(tx *gorm.DB) (err error) {
  542. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  543. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  544. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  545. "WHERE t1.user_org_id = t2.orgid)tmp " +
  546. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  547. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  548. return
  549. }
  550. //初始化 xt_drug_cancel_stock_info(完成)
  551. func initxt_drug_cancel_stock_info(tx *gorm.DB) (err error) {
  552. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock_info as t," +
  553. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  554. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  555. "WHERE t1.user_org_id = t2.orgid)tmp " +
  556. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  557. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  558. return
  559. }
  560. //初始化 xt_supplier_warehouse_info(完成)
  561. func initxt_supplier_warehouse_info(tx *gorm.DB) (err error) {
  562. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_info as t," +
  563. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  564. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  565. "WHERE t1.user_org_id = t2.orgid)tmp " +
  566. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  567. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  568. return
  569. }
  570. //初始化 xt_supplier_warehousing_info_order(完成)
  571. func initxt_supplier_warehousing_info_order(tx *gorm.DB) (err error) {
  572. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_info_order as t," +
  573. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  574. "(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 " +
  575. "WHERE t1.user_org_id = t2.orgid)tmp " +
  576. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  577. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  578. return
  579. }
  580. //初始化 xt_supplier_warehouse_out(完成)
  581. func initxt_supplier_warehouse_out(tx *gorm.DB) (err error) {
  582. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_out as t," +
  583. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  584. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  585. "WHERE t1.user_org_id = t2.orgid)tmp " +
  586. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  587. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  588. return
  589. }
  590. //初始化 xt_supplier_warehousing_out_order(完成)
  591. func initxt_supplier_warehousing_out_order(tx *gorm.DB) (err error) {
  592. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_out_order as t," +
  593. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  594. "(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 " +
  595. "WHERE t1.user_org_id = t2.orgid)tmp " +
  596. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  597. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  598. return
  599. }
  600. //初始化 xt_supplier_warehouse_cancel(完成)
  601. func initxt_supplier_warehouse_cancel(tx *gorm.DB) (err error) {
  602. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_cancel as t," +
  603. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  604. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_cancel WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  605. "WHERE t1.user_org_id = t2.orgid)tmp " +
  606. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  607. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  608. return
  609. }
  610. //初始化 xt_supplier_warehousing_cancel_order(完成)
  611. func initxt_supplier_warehousing_cancel_order(tx *gorm.DB) (err error) {
  612. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_cancel_order as t," +
  613. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  614. "(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 " +
  615. "WHERE t1.user_org_id = t2.orgid)tmp " +
  616. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  617. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  618. return
  619. }