secondary_service.go 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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_storehouse(tx)
  278. if err != nil && err.Error() != "record not found" {
  279. return
  280. }
  281. err = initxt_stock_flow(tx)
  282. if err != nil && err.Error() != "record not found" {
  283. return
  284. }
  285. err = initxt_drug_flow(tx)
  286. if err != nil && err.Error() != "record not found" {
  287. return
  288. }
  289. err = initdialysis_before_prepare(tx)
  290. if err != nil && err.Error() != "record not found" {
  291. return
  292. }
  293. err = initxt_automatic_reduce_detail(tx)
  294. if err != nil && err.Error() != "record not found" {
  295. return
  296. }
  297. err = initxt_drug_automatic_reduce_detail(tx)
  298. if err != nil && err.Error() != "record not found" {
  299. return
  300. }
  301. err = initxt_warehouse(tx)
  302. if err != nil && err.Error() != "record not found" {
  303. return
  304. }
  305. err = initxt_warehouse_info(tx)
  306. if err != nil && err.Error() != "record not found" {
  307. return
  308. }
  309. err = initxt_warehouse_out(tx)
  310. if err != nil && err.Error() != "record not found" {
  311. return
  312. }
  313. err = initxt_warehouse_out_info(tx)
  314. if err != nil && err.Error() != "record not found" {
  315. return
  316. }
  317. err = initxt_drug_warehouse(tx)
  318. if err != nil && err.Error() != "record not found" {
  319. return
  320. }
  321. err = initxt_drug_warehouse_info(tx)
  322. if err != nil && err.Error() != "record not found" {
  323. return
  324. }
  325. err = initxt_drug_warehouse_out(tx)
  326. if err != nil && err.Error() != "record not found" {
  327. return
  328. }
  329. err = initxt_drug_warehouse_out_info(tx)
  330. if err != nil && err.Error() != "record not found" {
  331. return
  332. }
  333. err = initxt_cancel_stock(tx)
  334. if err != nil && err.Error() != "record not found" {
  335. return
  336. }
  337. err = initxt_cancel_stock_info(tx)
  338. if err != nil && err.Error() != "record not found" {
  339. return
  340. }
  341. err = initxt_drug_cancel_stock(tx)
  342. if err != nil && err.Error() != "record not found" {
  343. return
  344. }
  345. err = initxt_drug_cancel_stock_info(tx)
  346. if err != nil && err.Error() != "record not found" {
  347. return
  348. }
  349. err = initxt_supplier_warehouse_info(tx)
  350. if err != nil && err.Error() != "record not found" {
  351. return
  352. }
  353. err = initxt_supplier_warehousing_info_order(tx)
  354. if err != nil && err.Error() != "record not found" {
  355. return
  356. }
  357. err = initxt_supplier_warehouse_out(tx)
  358. if err != nil && err.Error() != "record not found" {
  359. return
  360. }
  361. err = initxt_supplier_warehousing_out_order(tx)
  362. if err != nil && err.Error() != "record not found" {
  363. return
  364. }
  365. err = initxt_supplier_warehouse_cancel(tx)
  366. if err != nil && err.Error() != "record not found" {
  367. return
  368. }
  369. err = initxt_supplier_warehousing_cancel_order(tx)
  370. return
  371. }
  372. //根据xt_gobal_template 获取的机构id减去仓库表存在的机构id,把结果插入到仓库表
  373. func StoreReduceOrg(tx *gorm.DB) (err error) {
  374. err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  375. "SELECT CONCAT(\"SH-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  376. "FROM" +
  377. "(select t1.org_id as id FROM sgj_xt.xt_gobal_template as t1 LEFT JOIN " +
  378. "(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
  379. //err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  380. // "SELECT CONCAT(\"sh-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  381. // "FROM" +
  382. // "(select t1.id FROM sgj_users.sgj_user_org as t1 LEFT JOIN " +
  383. // "(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
  384. return
  385. }
  386. //查找仓库表的orgid 减去仓库配置表的orgid,把结果插入到仓库配置表
  387. func StoreReduceConfig(tx *gorm.DB) (err error) {
  388. 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)" +
  389. "SELECT tmp.user_org_id,tmp.id,tmp.id,tmp.id,tmp.id,UNIX_TIMESTAMP()" +
  390. "FROM(select t1.id,t1.user_org_id FROM sgj_xt.xt_storehouse as t1 LEFT JOIN " +
  391. "(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
  392. return
  393. }
  394. //初始化 xt_storehouse()把插入管理员的名字
  395. func initxt_storehouse(tx *gorm.DB) (err error) {
  396. err = tx.Exec("UPDATE sgj_xt.xt_storehouse as t," +
  397. "(SELECT o.id,o.creator,u.name from sgj_users.sgj_user_org as o,sgj_users.sgj_user_admin as u," +
  398. "(SELECT DISTINCT org_id from sgj_xt.xt_gobal_template)as t " +
  399. "WHERE o.creator = u.id AND t.org_id = o.id )tmp " +
  400. "SET t.storehouse_admin_id = tmp.creator,t.storehouse_admin_name = tmp.name,t.mtime = UNIX_TIMESTAMP()" +
  401. "WHERE t.status = 1 and t.user_org_id = tmp.id").Error
  402. return
  403. }
  404. //初始化 xt_stock_flow(完成)
  405. func initxt_stock_flow(tx *gorm.DB) (err error) {
  406. err = tx.Exec("UPDATE sgj_xt.xt_stock_flow as t," +
  407. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  408. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_stock_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  409. "WHERE t1.user_org_id = t2.orgid)tmp " +
  410. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  411. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  412. return
  413. }
  414. //初始化 xt_drug_flow(完成)
  415. func initxt_drug_flow(tx *gorm.DB) (err error) {
  416. err = tx.Exec("UPDATE sgj_xt.xt_drug_flow as t," +
  417. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  418. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_drug_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  419. "WHERE t1.user_org_id = t2.orgid)tmp " +
  420. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  421. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  422. return
  423. }
  424. //初始化 dialysis_before_prepare(完成)
  425. func initdialysis_before_prepare(tx *gorm.DB) (err error) {
  426. err = tx.Exec("UPDATE sgj_xt.dialysis_before_prepare as t," +
  427. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  428. "(SELECT distinct user_org_id as orgid FROM sgj_xt.dialysis_before_prepare WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  429. "WHERE t1.user_org_id = t2.orgid)tmp " +
  430. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  431. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  432. return
  433. }
  434. //初始化 xt_automatic_reduce_detail(完成)
  435. func initxt_automatic_reduce_detail(tx *gorm.DB) (err error) {
  436. err = tx.Exec("UPDATE sgj_xt.xt_automatic_reduce_detail as t," +
  437. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  438. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  439. "WHERE t1.user_org_id = t2.orgid)tmp " +
  440. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  441. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  442. return
  443. }
  444. //初始化 xt_drug_automatic_reduce_detail(完成)
  445. func initxt_drug_automatic_reduce_detail(tx *gorm.DB) (err error) {
  446. err = tx.Exec("UPDATE sgj_xt.xt_drug_automatic_reduce_detail as t," +
  447. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  448. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  449. "WHERE t1.user_org_id = t2.orgid)tmp " +
  450. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  451. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  452. return
  453. }
  454. //初始化 xt_warehouse(完成)
  455. func initxt_warehouse(tx *gorm.DB) (err error) {
  456. err = tx.Exec("UPDATE sgj_xt.xt_warehouse as t," +
  457. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  458. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  459. "WHERE t1.user_org_id = t2.orgid)tmp " +
  460. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  461. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  462. return
  463. }
  464. //初始化 xt_warehouse_info(完成)
  465. func initxt_warehouse_info(tx *gorm.DB) (err error) {
  466. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_info as t," +
  467. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  468. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  469. "WHERE t1.user_org_id = t2.orgid)tmp " +
  470. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  471. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  472. return
  473. }
  474. //初始化 xt_warehouse_out(完成)
  475. func initxt_warehouse_out(tx *gorm.DB) (err error) {
  476. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out as t," +
  477. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  478. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  479. "WHERE t1.user_org_id = t2.orgid)tmp " +
  480. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  481. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  482. return
  483. }
  484. //初始化 xt_warehouse_out_info(完成)
  485. func initxt_warehouse_out_info(tx *gorm.DB) (err error) {
  486. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out_info as t," +
  487. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  488. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  489. "WHERE t1.user_org_id = t2.orgid)tmp " +
  490. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  491. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  492. return
  493. }
  494. //初始化 xt_drug_warehouse(完成)
  495. func initxt_drug_warehouse(tx *gorm.DB) (err error) {
  496. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse as t," +
  497. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  498. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  499. "WHERE t1.user_org_id = t2.orgid)tmp " +
  500. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  501. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  502. return
  503. }
  504. //初始化 xt_drug_warehouse_info(完成)
  505. func initxt_drug_warehouse_info(tx *gorm.DB) (err error) {
  506. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_info as t," +
  507. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  508. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  509. "WHERE t1.user_org_id = t2.orgid)tmp " +
  510. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  511. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  512. return
  513. }
  514. //初始化 xt_drug_warehouse_out(完成)
  515. func initxt_drug_warehouse_out(tx *gorm.DB) (err error) {
  516. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out as t," +
  517. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  518. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  519. "WHERE t1.user_org_id = t2.orgid)tmp " +
  520. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  521. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  522. return
  523. }
  524. //初始化 xt_drug_warehouse_out_info(完成)
  525. func initxt_drug_warehouse_out_info(tx *gorm.DB) (err error) {
  526. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out_info as t," +
  527. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  528. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  529. "WHERE t1.user_org_id = t2.orgid)tmp " +
  530. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  531. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  532. return
  533. }
  534. //初始化 xt_cancel_stock(完成)
  535. func initxt_cancel_stock(tx *gorm.DB) (err error) {
  536. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock as t," +
  537. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  538. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  539. "WHERE t1.user_org_id = t2.orgid)tmp " +
  540. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  541. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  542. return
  543. }
  544. //初始化 xt_cancel_stock_info(完成)
  545. func initxt_cancel_stock_info(tx *gorm.DB) (err error) {
  546. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock_info as t," +
  547. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  548. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  549. "WHERE t1.user_org_id = t2.orgid)tmp " +
  550. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  551. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  552. return
  553. }
  554. //初始化 xt_drug_cancel_stock(完成)
  555. func initxt_drug_cancel_stock(tx *gorm.DB) (err error) {
  556. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  557. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  558. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  559. "WHERE t1.user_org_id = t2.orgid)tmp " +
  560. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  561. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  562. return
  563. }
  564. //初始化 xt_drug_cancel_stock_info(完成)
  565. func initxt_drug_cancel_stock_info(tx *gorm.DB) (err error) {
  566. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock_info as t," +
  567. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  568. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  569. "WHERE t1.user_org_id = t2.orgid)tmp " +
  570. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  571. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  572. return
  573. }
  574. //初始化 xt_supplier_warehouse_info(完成)
  575. func initxt_supplier_warehouse_info(tx *gorm.DB) (err error) {
  576. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_info as t," +
  577. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  578. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  579. "WHERE t1.user_org_id = t2.orgid)tmp " +
  580. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  581. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  582. return
  583. }
  584. //初始化 xt_supplier_warehousing_info_order(完成)
  585. func initxt_supplier_warehousing_info_order(tx *gorm.DB) (err error) {
  586. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_info_order as t," +
  587. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  588. "(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 " +
  589. "WHERE t1.user_org_id = t2.orgid)tmp " +
  590. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  591. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  592. return
  593. }
  594. //初始化 xt_supplier_warehouse_out(完成)
  595. func initxt_supplier_warehouse_out(tx *gorm.DB) (err error) {
  596. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_out as t," +
  597. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  598. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  599. "WHERE t1.user_org_id = t2.orgid)tmp " +
  600. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  601. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  602. return
  603. }
  604. //初始化 xt_supplier_warehousing_out_order(完成)
  605. func initxt_supplier_warehousing_out_order(tx *gorm.DB) (err error) {
  606. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_out_order as t," +
  607. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  608. "(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 " +
  609. "WHERE t1.user_org_id = t2.orgid)tmp " +
  610. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  611. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  612. return
  613. }
  614. //初始化 xt_supplier_warehouse_cancel(完成)
  615. func initxt_supplier_warehouse_cancel(tx *gorm.DB) (err error) {
  616. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_cancel as t," +
  617. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  618. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_cancel WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  619. "WHERE t1.user_org_id = t2.orgid)tmp " +
  620. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  621. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  622. return
  623. }
  624. //初始化 xt_supplier_warehousing_cancel_order(完成)
  625. func initxt_supplier_warehousing_cancel_order(tx *gorm.DB) (err error) {
  626. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_cancel_order as t," +
  627. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  628. "(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 " +
  629. "WHERE t1.user_org_id = t2.orgid)tmp " +
  630. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  631. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  632. return
  633. }
  634. //查询机构下的所有用户名
  635. func GetuserName(orgid int64) (username []models.App_Role_byli, err error) {
  636. err = XTReadDB().Select("id,org_id,user_name").Where("org_id = ?", orgid).Find(&username).Error
  637. return
  638. }