secondary_service.go 49KB

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