secondary_service.go 29KB

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