secondary_service.go 76KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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. }
  690. //
  691. func GetAllStoreHouseList(orgid int64) (house []*models.XtStorehouse, err error) {
  692. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&house).Error
  693. return house, err
  694. }
  695. func GetAllStoreHouseListThree(orgid int64) (house []*models.XtStorehouse, err error) {
  696. err = XTReadDB().Where("user_org_id = ? and status = 1 and storehouse_status = 1", orgid).Find(&house).Error
  697. return house, err
  698. }
  699. func GetAllStoreHouseListOne(orgid int64) (list []*models.VmStorehouseName, err error) {
  700. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.storehouse_id > 0")
  701. table := XTReadDB().Table("xt_storehouse as s").Where("s.status= 1")
  702. fmt.Println(table)
  703. if orgid > 0 {
  704. db = db.Where("(x.stock_max_number >0 or x.stock_min_number >0 ) and x.org_id = ?", orgid)
  705. }
  706. 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
  707. return list, err
  708. }
  709. func GetAllStoreHouseListTwo(orgid int64) (list []*models.VmStorehouseNameOne, err error) {
  710. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  711. table := XTReadDB().Table("xt_storehouse as s").Where("s.status= 1")
  712. fmt.Println(table)
  713. if orgid > 0 {
  714. db = db.Where("stock_count >0 and x.org_id = ?", orgid)
  715. }
  716. 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
  717. return list, err
  718. }
  719. func GetAllStoreHouseConfig(orgid int64) (models.XtStorehouseConfig, error) {
  720. config := models.XtStorehouseConfig{}
  721. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  722. return config, err
  723. }
  724. func FindStoreHouseByStorehouseId(id int64, user_org_id int64) (models.XtStorehouse, error) {
  725. storehouse := models.XtStorehouse{}
  726. err := XTReadDB().Where("id = ? and user_org_id = ? and status = 1", id, user_org_id).Find(&storehouse).Error
  727. return storehouse, err
  728. }
  729. func GetSecondWarehouseByNumber(user_org_id int64, warehousing_order string) (*models.XtSecondWarehouse, error) {
  730. warehouse := models.XtSecondWarehouse{}
  731. var err error
  732. err = XTReadDB().Where("user_org_id = ? and second_order_number = ? and status =1", user_org_id, warehousing_order).Find(&warehouse).Error
  733. if err == gorm.ErrRecordNotFound {
  734. return nil, err
  735. }
  736. if err != nil {
  737. return nil, err
  738. }
  739. return &warehouse, nil
  740. }
  741. func CreateSecondeWarehouse(warehouse models.XtSecondWarehouse) error {
  742. err := XTWriteDB().Create(&warehouse).Error
  743. return err
  744. }
  745. func CreateSencondWarehousingInfo(info *models.XtSecondWarehouseInfo) error {
  746. err := XTWriteDB().Create(&info).Error
  747. return err
  748. }
  749. func GetAllSecondeOrderList(is_check int64, startime int64, endtime int64, keyword string, page int64, limit int64, orgid int64) (list []*models.VmSecondWarehouse, total int64, err error) {
  750. db := XTReadDB().Table("sgj_xt.xt_second_warehouse").Where("sgj_xt.xt_second_warehouse.status = 1")
  751. likeKey := "%" + keyword + "%"
  752. offset := (page - 1) * limit
  753. if is_check > 0 {
  754. db = db.Where("sgj_xt.xt_second_warehouse.is_check = ?", is_check)
  755. }
  756. if len(keyword) > 0 {
  757. 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")
  758. db = db.Where("sgj_xt.xt_second_warehouse.second_order_number like ? or sgj_xt.xt_storehouse.storehouse_name like ? ", likeKey, likeKey)
  759. }
  760. if startime > 0 {
  761. db = db.Where("sgj_xt.xt_second_warehouse.record_date >= ?", startime)
  762. }
  763. if endtime > 0 {
  764. db = db.Where("sgj_xt.xt_second_warehouse.record_date <= ?", endtime)
  765. }
  766. if orgid > 0 {
  767. db = db.Where("sgj_xt.xt_second_warehouse.user_org_id = ?", orgid)
  768. }
  769. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  770. return list, total, err
  771. }
  772. func GetSecondWarehouseOrderById(id int64) (models.XtSecondWarehouse, error) {
  773. warehouse := models.XtSecondWarehouse{}
  774. err := XTReadDB().Where("id = ? and status = 1", id).Find(&warehouse).Error
  775. return warehouse, err
  776. }
  777. func GetLastSecondWarehouse(orgid int64) (models.XtSecondWarehouse, error) {
  778. warehouse := models.XtSecondWarehouse{}
  779. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Last(&warehouse).Error
  780. return warehouse, err
  781. }
  782. func GetSencondOrderDetail(id int64, orgid int64) (info []*models.XtSecondWarehouseInfo, err error) {
  783. err = XTReadDB().Where("warehouse_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&info).Error
  784. return info, err
  785. }
  786. func UpdateModifySecondWarehouse(id int64, warehouse models.XtSecondWarehouse) error {
  787. 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
  788. return err
  789. }
  790. func UpdateStoreWarehousing(info *models.XtSecondWarehouseInfo) error {
  791. 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
  792. return err
  793. }
  794. func GetSecondOrderList(ids []string, orgid int64) (warehouse []*models.XtSecondWarehouseInfo, err error) {
  795. err = XTReadDB().Where("warehouse_id in(?) and user_org_id = ? and status = 1", ids, orgid).Find(&warehouse).Error
  796. return warehouse, err
  797. }
  798. func UpdateStoreOrderByArray(ids []string, orgid int64, checker int64) error {
  799. 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
  800. return err
  801. }
  802. func GetWarehouseInfoByStoreHouseId(good_id int64, storehouse_id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  803. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1", good_id, storehouse_id, orgid).Find(&info).Error
  804. return info, err
  805. }
  806. func GetWarehouseInfoByStoreHouseIdOne(good_id int64, storehouse_id int64, orgid int64, second_warehouse_info_id int64) (info []*models.WarehousingInfo, err error) {
  807. 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
  808. return info, err
  809. }
  810. func GetLastWarehouseOutBySys(is_sys int64, orgid int64, warehouse_out_time int64, second_warehouse_id int64) (models.WarehouseOut, error) {
  811. out := models.WarehouseOut{}
  812. 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
  813. return out, err
  814. }
  815. func GetDrugWarehouseInfoByStoreHouseId(drug_id int64, storehouse_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  816. err = XTReadDB().Where("drug_id = ? and storehouse_id = ? and org_id = ? and status = 1", drug_id, storehouse_id, orgid).Find(&info).Error
  817. return info, err
  818. }
  819. func GetDrugWarehouseInfoByStoreHouseIdOne(drug_id int64, storehouse_id int64, orgid int64, second_warehouse_info_id int64) (info []*models.DrugWarehouseInfo, err error) {
  820. 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
  821. return info, err
  822. }
  823. func DeleteStorehouseList(id int64) error {
  824. err = XTWriteDB().Model(&models.XtSecondWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  825. err = XTWriteDB().Model(&models.XtSecondWarehouseInfo{}).Where("warehouse_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  826. return err
  827. }
  828. func GetStoreWarehouseOutList(good_id int64, second_warehouse_info_id int64, orgid int64) (info []*models.WarehouseOutInfo, err error) {
  829. 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
  830. return info, err
  831. }
  832. func ModifyStoreWarehouseById(id int64, stock_count int64) error {
  833. info := models.WarehousingInfo{}
  834. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  835. return err
  836. }
  837. func GetStoreWarehouseById(storehouse_id int64, orgid int64) (models.Warehousing, error) {
  838. warehousing := models.Warehousing{}
  839. err := XTReadDB().Where("second_warehouse_id = ? and org_id = ?", storehouse_id, orgid).Find(&warehousing).Error
  840. return warehousing, err
  841. }
  842. func ModifyStoreWarehouse(id int64, orgid int64, good_id int64) error {
  843. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  844. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) and org_id = ? and status =1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  845. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_id in(?) and user_org_id = ? and status = 1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  846. return err
  847. }
  848. func GetStoreWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  849. out := models.WarehouseOut{}
  850. err := XTReadDB().Where("id = ? and org_id = ?", id, orgid).Find(&out).Error
  851. return out, err
  852. }
  853. import (
  854. "XT_New/models"
  855. "XT_New/utils"
  856. "fmt"
  857. "github.com/jinzhu/gorm"
  858. "math/rand"
  859. "strconv"
  860. "time"
  861. )
  862. //生成编号,规则为:"SH-"+4位随机数
  863. func CreateCode() string {
  864. s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000))
  865. code := "SH-" + s
  866. return code
  867. }
  868. //查询编号是否重复
  869. func FindStorehouseCode(orgid int64, code string) bool {
  870. var total int
  871. XTReadDB().Model(&models.Storehouse{}).Where(" storehouse_code = ? and user_org_id = ? and status = 1", code, orgid).Count(&total)
  872. if total > 0 {
  873. return true
  874. } else {
  875. return false
  876. }
  877. }
  878. //判断仓库的库存是否为零 true为零,false不为零
  879. func IsStorehouseNil(id, orgid int64) bool { //根据 id gro_id 查为零的库存
  880. var h, y int
  881. //判断耗材是否为零
  882. XTReadDB().Model(&models.WarehousingInfo{}).Where("stock_count > 0 and status = 1 and org_id = ? and storehouse_id = ?", orgid, id).Count(&h)
  883. if h > 0 {
  884. return false
  885. }
  886. //判断药品是否为零
  887. XTReadDB().Model(&models.XtDrugWarehouseInfo{}).Where("stock_max_number > 0 or stock_min_number > 0 ").Where(" org_id = ? and storehouse_id = ?", orgid, id).Count(&y)
  888. if y > 0 {
  889. return false
  890. }
  891. return true
  892. }
  893. //修改仓库状态
  894. func UpdateStorehouseStatus(id int64) error {
  895. 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
  896. return err
  897. }
  898. //删除仓库
  899. func DeleteStorehouse(id int64) error {
  900. err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ?", id).Update("status", 0).Error
  901. return err
  902. }
  903. //查询仓库名称是否重复
  904. func IsStorehouseName(orgid int64, storehouse_name string) (bool, error) {
  905. var total int
  906. var tmp bool
  907. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_name = ? and user_org_id = ? and status = 1", storehouse_name, orgid).Count(&total).Error
  908. if total > 0 {
  909. tmp = true //有重复的
  910. } else {
  911. tmp = false
  912. }
  913. return tmp, err
  914. }
  915. //查询仓库地址是否重复
  916. func IsStorehouseAddress(orgid int64, storehouse_address string) (bool, error) {
  917. var total int
  918. var tmp bool
  919. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_address = ? and user_org_id = ? and status = 1", storehouse_address, orgid).Count(&total).Error
  920. if total > 0 {
  921. tmp = true //有重复的
  922. } else {
  923. tmp = false
  924. }
  925. return tmp, err
  926. }
  927. //查询仓库名称是否重复(用于修改)
  928. func IsStorehouseNameUp(orgid, id int64, storehouse_name string) (bool, error) {
  929. var total int
  930. var tmp bool
  931. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_name = ? and user_org_id = ? and status = 1 and id != ?", storehouse_name, orgid, id).Count(&total).Error
  932. if total > 0 {
  933. tmp = true //有重复的
  934. } else {
  935. tmp = false
  936. }
  937. return tmp, err
  938. }
  939. //查询仓库地址是否重复(用于修改)
  940. func IsStorehouseAddressUp(orgid, id int64, storehouse_address string) (bool, error) {
  941. var total int
  942. var tmp bool
  943. err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_address = ? and user_org_id = ? and status = 1 and id != ?", storehouse_address, orgid, id).Count(&total).Error
  944. if total > 0 {
  945. tmp = true //有重复的
  946. } else {
  947. tmp = false
  948. }
  949. return tmp, err
  950. }
  951. //新增仓库
  952. func AddStroehouse(storehouse models.Storehouse) error {
  953. tx := XTWriteDB().Begin()
  954. defer func() {
  955. if err != nil {
  956. utils.ErrorLog("新增仓库_事务失败,原因为", err)
  957. tx.Rollback()
  958. } else {
  959. tx.Commit()
  960. }
  961. }()
  962. //创建仓库
  963. err := tx.Create(&storehouse).Error
  964. if err != nil {
  965. return err
  966. }
  967. var id models.Storehouse
  968. //获取创建仓库的id
  969. err = tx.Model(&models.Storehouse{}).Select("id").Where("status = 1 and user_org_id = ?", storehouse.UserOrgId).Order("id desc").First(&id).Error
  970. if err != nil {
  971. return err
  972. }
  973. //判断是否存在仓库配置
  974. boolean := IsStorehouseconfigXT(storehouse.UserOrgId, tx)
  975. if boolean == false {
  976. //创建仓库配置
  977. err = CreateStorehouseConfigXT(storehouse.UserOrgId, id.ID, tx)
  978. } else {
  979. utils.ErrorLog("仓库配置已存在")
  980. }
  981. return err
  982. }
  983. //修改仓库
  984. func UpdateStroehouse(storehouse models.Storehouse) error {
  985. err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ? and status = 1", storehouse.ID).Updates(
  986. map[string]interface{}{
  987. "storehouse_name": storehouse.StorehouseName,
  988. "storehouse_address": storehouse.StorehouseAddress,
  989. "storehouse_status": storehouse.StorehouseStatus,
  990. "storehouse_admin_id": storehouse.StorehouseAdminId,
  991. "mtime": storehouse.Mtime,
  992. }).Error
  993. return err
  994. }
  995. //查询一条仓库的信息
  996. func GetOneStorehouse(id, orgid int64) (storehouse models.Storehouse, err error) {
  997. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ? and user_org_id = ?", id, orgid).Find(&storehouse).Error
  998. return
  999. }
  1000. //分页
  1001. func StorehouseList(page, limit, orgid int64, keyword string, slicekey []int64) (storehouse []models.Storehouse, total int64, err error) {
  1002. db := XTReadDB().Model(&storehouse).Where("status = 1 and user_org_id = ?", orgid)
  1003. offset := (page - 1) * limit
  1004. if len(keyword) > 0 {
  1005. var adminid string = "storehouse_code like ? or storehouse_name like ? or storehouse_address like ? "
  1006. if len(slicekey) > 0 {
  1007. for i := 0; i < len(slicekey); i++ {
  1008. adminid = adminid + " or storehouse_admin_id =" + strconv.FormatInt(slicekey[i], 10)
  1009. }
  1010. }
  1011. keyword = "%" + keyword + "%"
  1012. db = db.Where(adminid, keyword, keyword, keyword)
  1013. }
  1014. err = db.Count(&total).Offset(offset).Order("id").Limit(limit).Find(&storehouse).Error
  1015. return storehouse, total, err
  1016. }
  1017. //获取当前机构所有可用的仓库名字
  1018. func GetAllStorehouseName(orgid int64) (storehouse []models.Storehouse, err error) {
  1019. err = XTReadDB().Model(&models.Storehouse{}).Where("storehouse_status = 1 and status = 1 and user_org_id = ?", orgid).Find(&storehouse).Error
  1020. return
  1021. }
  1022. //根据机构id,生成一个默认仓库
  1023. func GetDefaultStorehouse(orgid int64) error {
  1024. var code string
  1025. for a := true; a == true; {
  1026. code = CreateCode()
  1027. tmp := FindStorehouseCode(orgid, code)
  1028. //如果没有重复的编码结束循环
  1029. if tmp == false {
  1030. a = false
  1031. }
  1032. }
  1033. createid, err := Getcreateid(orgid)
  1034. if err != nil {
  1035. return err
  1036. }
  1037. storehouse := models.Storehouse{
  1038. StorehouseCode: code,
  1039. StorehouseName: "默认仓库",
  1040. StorehouseAddress: "",
  1041. StorehouseStatus: 1,
  1042. UserOrgId: orgid,
  1043. StorehouseAdminId: createid.Creator,
  1044. Status: 1,
  1045. Ctime: time.Now().Unix(),
  1046. }
  1047. err = AddStroehouse(storehouse)
  1048. return err
  1049. }
  1050. //查找该机构id是否有仓库存在,ture存在,false不存在
  1051. func IsStorehouse(orgid int64) bool {
  1052. var total int
  1053. XTReadDB().Model(&models.Storehouse{}).Where("user_org_id = ?", orgid).Count(&total)
  1054. if total > 0 {
  1055. return true
  1056. } else {
  1057. return false
  1058. }
  1059. }
  1060. //查找该机构id是否有仓库配置存在,ture存在,false不存在
  1061. func IsStorehouseconfigXT(orgid int64, tx *gorm.DB) bool {
  1062. var total int
  1063. tx.Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Count(&total)
  1064. if total > 0 {
  1065. return true
  1066. } else {
  1067. return false
  1068. }
  1069. }
  1070. // 根据机构id查询仓库配置
  1071. //storehouse_info:耗材 自动入库 的仓库id
  1072. //storehouse_out_info:耗材 自动出库 的仓库id
  1073. //drug_storehouse_info:药品 自动入库 的仓库id
  1074. //drug_storehouse_out:药品 自动出库 的仓库id
  1075. func FindStorehouseConfig(orgid int64) (storehouse_config models.StorehouseConfig, err error) {
  1076. err = XTReadDB().Model(&models.StorehouseConfig{}).Where("status = 1 and user_org_id = ?", orgid).Find(&storehouse_config).Error
  1077. return
  1078. }
  1079. //根据机构id,仓库id新建一个仓库配置
  1080. func CreateStorehouseConfig(orgid, id int64) (err error) {
  1081. storeconfig := models.StorehouseConfig{
  1082. UserOrgId: orgid,
  1083. StorehouseInfo: id,
  1084. StorehouseOutInfo: id,
  1085. DrugStorehouseInfo: id,
  1086. DrugStorehouseOut: id,
  1087. Status: 1,
  1088. Ctime: time.Now().Unix(),
  1089. }
  1090. err = XTWriteDB().Create(&storeconfig).Error
  1091. return
  1092. }
  1093. //根据机构id,仓库id新建一个仓库配置,带事务
  1094. func CreateStorehouseConfigXT(orgid, id int64, tx *gorm.DB) (err error) {
  1095. storeconfig := models.StorehouseConfig{
  1096. UserOrgId: orgid,
  1097. StorehouseInfo: id,
  1098. StorehouseOutInfo: id,
  1099. DrugStorehouseInfo: id,
  1100. DrugStorehouseOut: id,
  1101. Status: 1,
  1102. Ctime: time.Now().Unix(),
  1103. }
  1104. err = tx.Create(&storeconfig).Error
  1105. return
  1106. }
  1107. //更改耗材自动入库仓库
  1108. func UpdateInfo(orgid, id int64) (err error) {
  1109. mtime := time.Now().Unix()
  1110. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_info": id, "mtime": mtime}).Error
  1111. return
  1112. }
  1113. //更改耗材自动出库仓库
  1114. func UpdateOutInfo(orgid, id int64) (err error) {
  1115. mtime := time.Now().Unix()
  1116. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_out_info": id, "mtime": mtime}).Error
  1117. return
  1118. }
  1119. //更改药品自动入库仓库
  1120. func UpdateDrugInfo2(orgid, id int64) (err error) {
  1121. mtime := time.Now().Unix()
  1122. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_info": id, "mtime": mtime}).Error
  1123. return
  1124. }
  1125. //更改药品自动出库仓库
  1126. func UpdateDrugOut(orgid, id int64) (err error) {
  1127. mtime := time.Now().Unix()
  1128. err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_out": id, "mtime": mtime}).Error
  1129. return
  1130. }
  1131. //根据id查询仓库名称
  1132. func FindStorehouseName(id int64) (storehouse models.Storehouse, err error) {
  1133. err = XTReadDB().Model(&models.Storehouse{}).Where("id = ?", id).Find(&storehouse).Error
  1134. return
  1135. }
  1136. //判断该仓库是否在仓库配置表中
  1137. func IsInConfig(orgid, id int64) bool {
  1138. var total int
  1139. XTReadDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Where(
  1140. "storehouse_info = ? or storehouse_out_info = ? or drug_storehouse_info = ? or drug_storehouse_out = ?", id, id, id, id).Count(&total)
  1141. if total > 0 {
  1142. return true //存在
  1143. } else {
  1144. return false
  1145. }
  1146. }
  1147. //兼容旧数据
  1148. func Byliinit() (err error) {
  1149. tx := XTWriteDB().Begin()
  1150. defer func() {
  1151. if err != nil && err.Error() != "record not found" {
  1152. tx.Rollback()
  1153. } else {
  1154. tx.Commit()
  1155. }
  1156. }()
  1157. err = initsgj_user_admin(tx)
  1158. if err != nil && err.Error() != "record not found" {
  1159. return
  1160. }
  1161. err = StoreReduceOrg(tx)
  1162. if err != nil && err.Error() != "record not found" {
  1163. return
  1164. }
  1165. err = StoreReduceConfig(tx)
  1166. if err != nil && err.Error() != "record not found" {
  1167. return
  1168. }
  1169. err = initxt_storehouse(tx)
  1170. if err != nil && err.Error() != "record not found" {
  1171. return
  1172. }
  1173. err = initxt_stock_flow(tx)
  1174. if err != nil && err.Error() != "record not found" {
  1175. return
  1176. }
  1177. err = initxt_drug_flow(tx)
  1178. if err != nil && err.Error() != "record not found" {
  1179. return
  1180. }
  1181. err = initdialysis_before_prepare(tx)
  1182. if err != nil && err.Error() != "record not found" {
  1183. return
  1184. }
  1185. err = initxt_automatic_reduce_detail(tx)
  1186. if err != nil && err.Error() != "record not found" {
  1187. return
  1188. }
  1189. err = initxt_drug_automatic_reduce_detail(tx)
  1190. if err != nil && err.Error() != "record not found" {
  1191. return
  1192. }
  1193. err = initxt_warehouse(tx)
  1194. if err != nil && err.Error() != "record not found" {
  1195. return
  1196. }
  1197. err = initxt_warehouse_info(tx)
  1198. if err != nil && err.Error() != "record not found" {
  1199. return
  1200. }
  1201. err = initxt_warehouse_out(tx)
  1202. if err != nil && err.Error() != "record not found" {
  1203. return
  1204. }
  1205. err = initxt_warehouse_out_info(tx)
  1206. if err != nil && err.Error() != "record not found" {
  1207. return
  1208. }
  1209. err = initxt_drug_warehouse(tx)
  1210. if err != nil && err.Error() != "record not found" {
  1211. return
  1212. }
  1213. err = initxt_drug_warehouse_info(tx)
  1214. if err != nil && err.Error() != "record not found" {
  1215. return
  1216. }
  1217. err = initxt_drug_warehouse_out(tx)
  1218. if err != nil && err.Error() != "record not found" {
  1219. return
  1220. }
  1221. err = initxt_drug_warehouse_out_info(tx)
  1222. if err != nil && err.Error() != "record not found" {
  1223. return
  1224. }
  1225. err = initxt_cancel_stock(tx)
  1226. if err != nil && err.Error() != "record not found" {
  1227. return
  1228. }
  1229. err = initxt_cancel_stock_info(tx)
  1230. if err != nil && err.Error() != "record not found" {
  1231. return
  1232. }
  1233. err = initxt_drug_cancel_stock(tx)
  1234. if err != nil && err.Error() != "record not found" {
  1235. return
  1236. }
  1237. err = initxt_drug_cancel_stock_info(tx)
  1238. if err != nil && err.Error() != "record not found" {
  1239. return
  1240. }
  1241. err = initxt_supplier_warehouse_info(tx)
  1242. if err != nil && err.Error() != "record not found" {
  1243. return
  1244. }
  1245. err = initxt_supplier_warehousing_info_order(tx)
  1246. if err != nil && err.Error() != "record not found" {
  1247. return
  1248. }
  1249. err = initxt_supplier_warehouse_out(tx)
  1250. if err != nil && err.Error() != "record not found" {
  1251. return
  1252. }
  1253. err = initxt_supplier_warehousing_out_order(tx)
  1254. if err != nil && err.Error() != "record not found" {
  1255. return
  1256. }
  1257. err = initxt_supplier_warehouse_cancel(tx)
  1258. if err != nil && err.Error() != "record not found" {
  1259. return
  1260. }
  1261. err = initxt_supplier_warehousing_cancel_order(tx)
  1262. return
  1263. }
  1264. //初始化管理员名字
  1265. func initsgj_user_admin(tx *gorm.DB) (err error) {
  1266. err = tx.Exec("update sgj_users.sgj_user_admin set name = \"超级管理员\" where name is null or name = \"\"").Error
  1267. return
  1268. }
  1269. //根据xt_gobal_template 获取的机构id减去仓库表存在的机构id,把结果插入到仓库表
  1270. func StoreReduceOrg(tx *gorm.DB) (err error) {
  1271. err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  1272. "SELECT CONCAT(\"SH-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  1273. "FROM" +
  1274. "(select t1.org_id as id FROM sgj_xt.xt_gobal_template as t1 LEFT JOIN " +
  1275. "(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
  1276. //err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" +
  1277. // "SELECT CONCAT(\"sh-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" +
  1278. // "FROM" +
  1279. // "(select t1.id FROM sgj_users.sgj_user_org as t1 LEFT JOIN " +
  1280. // "(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
  1281. return
  1282. }
  1283. //查找仓库表的orgid 减去仓库配置表的orgid,把结果插入到仓库配置表
  1284. func StoreReduceConfig(tx *gorm.DB) (err error) {
  1285. 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)" +
  1286. "SELECT tmp.user_org_id,tmp.id,tmp.id,tmp.id,tmp.id,UNIX_TIMESTAMP()" +
  1287. "FROM(select t1.id,t1.user_org_id FROM sgj_xt.xt_storehouse as t1 LEFT JOIN " +
  1288. "(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
  1289. return
  1290. }
  1291. //初始化 xt_storehouse 管理员的名字
  1292. func initxt_storehouse(tx *gorm.DB) (err error) {
  1293. err = tx.Exec("UPDATE sgj_xt.xt_storehouse as t," +
  1294. "(SELECT o.id,o.creator from sgj_users.sgj_user_org as o," +
  1295. "(SELECT DISTINCT org_id from sgj_xt.xt_gobal_template)as t " +
  1296. "WHERE t.org_id = o.id )tmp " +
  1297. "SET t.storehouse_admin_id = tmp.creator,t.mtime = UNIX_TIMESTAMP()" +
  1298. "WHERE t.status = 1 and t.user_org_id = tmp.id").Error
  1299. return
  1300. }
  1301. //初始化 xt_stock_flow(完成)
  1302. func initxt_stock_flow(tx *gorm.DB) (err error) {
  1303. err = tx.Exec("UPDATE sgj_xt.xt_stock_flow as t," +
  1304. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1305. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_stock_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1306. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1307. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1308. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1309. return
  1310. }
  1311. //初始化 xt_drug_flow(完成)
  1312. func initxt_drug_flow(tx *gorm.DB) (err error) {
  1313. err = tx.Exec("UPDATE sgj_xt.xt_drug_flow as t," +
  1314. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1315. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_drug_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1316. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1317. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1318. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1319. return
  1320. }
  1321. //初始化 dialysis_before_prepare(完成)
  1322. func initdialysis_before_prepare(tx *gorm.DB) (err error) {
  1323. err = tx.Exec("UPDATE sgj_xt.dialysis_before_prepare as t," +
  1324. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1325. "(SELECT distinct user_org_id as orgid FROM sgj_xt.dialysis_before_prepare WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1326. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1327. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1328. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1329. return
  1330. }
  1331. //初始化 xt_automatic_reduce_detail(完成)
  1332. func initxt_automatic_reduce_detail(tx *gorm.DB) (err error) {
  1333. err = tx.Exec("UPDATE sgj_xt.xt_automatic_reduce_detail as t," +
  1334. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  1335. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1336. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1337. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1338. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1339. return
  1340. }
  1341. //初始化 xt_drug_automatic_reduce_detail(完成)
  1342. func initxt_drug_automatic_reduce_detail(tx *gorm.DB) (err error) {
  1343. err = tx.Exec("UPDATE sgj_xt.xt_drug_automatic_reduce_detail as t," +
  1344. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  1345. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1346. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1347. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1348. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1349. return
  1350. }
  1351. //初始化 xt_warehouse(完成)
  1352. func initxt_warehouse(tx *gorm.DB) (err error) {
  1353. err = tx.Exec("UPDATE sgj_xt.xt_warehouse as t," +
  1354. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1355. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1356. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1357. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1358. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1359. return
  1360. }
  1361. //初始化 xt_warehouse_info(完成)
  1362. func initxt_warehouse_info(tx *gorm.DB) (err error) {
  1363. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_info as t," +
  1364. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1365. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1366. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1367. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1368. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1369. return
  1370. }
  1371. //初始化 xt_warehouse_out(完成)
  1372. func initxt_warehouse_out(tx *gorm.DB) (err error) {
  1373. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out as t," +
  1374. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  1375. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1376. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1377. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1378. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1379. return
  1380. }
  1381. //初始化 xt_warehouse_out_info(完成)
  1382. func initxt_warehouse_out_info(tx *gorm.DB) (err error) {
  1383. err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out_info as t," +
  1384. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  1385. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1386. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1387. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1388. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1389. return
  1390. }
  1391. //初始化 xt_drug_warehouse(完成)
  1392. func initxt_drug_warehouse(tx *gorm.DB) (err error) {
  1393. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse as t," +
  1394. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1395. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1396. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1397. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1398. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1399. return
  1400. }
  1401. //初始化 xt_drug_warehouse_info(完成)
  1402. func initxt_drug_warehouse_info(tx *gorm.DB) (err error) {
  1403. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_info as t," +
  1404. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1405. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1406. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1407. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1408. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1409. return
  1410. }
  1411. //初始化 xt_drug_warehouse_out(完成)
  1412. func initxt_drug_warehouse_out(tx *gorm.DB) (err error) {
  1413. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out as t," +
  1414. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  1415. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1416. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1417. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1418. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1419. return
  1420. }
  1421. //初始化 xt_drug_warehouse_out_info(完成)
  1422. func initxt_drug_warehouse_out_info(tx *gorm.DB) (err error) {
  1423. err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out_info as t," +
  1424. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  1425. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1426. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1427. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1428. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1429. return
  1430. }
  1431. //初始化 xt_cancel_stock(完成)
  1432. func initxt_cancel_stock(tx *gorm.DB) (err error) {
  1433. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock as t," +
  1434. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  1435. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1436. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1437. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1438. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1439. return
  1440. }
  1441. //初始化 xt_cancel_stock_info(完成)
  1442. func initxt_cancel_stock_info(tx *gorm.DB) (err error) {
  1443. err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock_info as t," +
  1444. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  1445. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1446. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1447. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1448. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1449. return
  1450. }
  1451. //初始化 xt_drug_cancel_stock(完成)
  1452. func initxt_drug_cancel_stock(tx *gorm.DB) (err error) {
  1453. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  1454. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  1455. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1456. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1457. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1458. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1459. return
  1460. }
  1461. //初始化 xt_drug_cancel_stock_info(完成)
  1462. func initxt_drug_cancel_stock_info(tx *gorm.DB) (err error) {
  1463. err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock_info as t," +
  1464. "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  1465. "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1466. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1467. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1468. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  1469. return
  1470. }
  1471. //初始化 xt_supplier_warehouse_info(完成)
  1472. func initxt_supplier_warehouse_info(tx *gorm.DB) (err error) {
  1473. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_info as t," +
  1474. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1475. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1476. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1477. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1478. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1479. return
  1480. }
  1481. //初始化 xt_supplier_warehousing_info_order(完成)
  1482. func initxt_supplier_warehousing_info_order(tx *gorm.DB) (err error) {
  1483. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_info_order as t," +
  1484. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1485. "(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 " +
  1486. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1487. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1488. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1489. return
  1490. }
  1491. //初始化 xt_supplier_warehouse_out(完成)
  1492. func initxt_supplier_warehouse_out(tx *gorm.DB) (err error) {
  1493. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_out as t," +
  1494. "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," +
  1495. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1496. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1497. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1498. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1499. return
  1500. }
  1501. //初始化 xt_supplier_warehousing_out_order(完成)
  1502. func initxt_supplier_warehousing_out_order(tx *gorm.DB) (err error) {
  1503. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_out_order as t," +
  1504. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1505. "(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 " +
  1506. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1507. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1508. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1509. return
  1510. }
  1511. //初始化 xt_supplier_warehouse_cancel(完成)
  1512. func initxt_supplier_warehouse_cancel(tx *gorm.DB) (err error) {
  1513. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_cancel as t," +
  1514. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1515. "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_cancel WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  1516. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1517. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1518. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1519. return
  1520. }
  1521. //初始化 xt_supplier_warehousing_cancel_order(完成)
  1522. func initxt_supplier_warehousing_cancel_order(tx *gorm.DB) (err error) {
  1523. err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_cancel_order as t," +
  1524. "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," +
  1525. "(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 " +
  1526. "WHERE t1.user_org_id = t2.orgid)tmp " +
  1527. "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  1528. "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error
  1529. return
  1530. }
  1531. //查询机构创建者的id
  1532. func Getcreateid(orgid int64) (createid models.UserOrg, err error) {
  1533. err = XTReadDB().Select("id,creator").Where("id = ? and status = 1", orgid).Find(&createid).Error
  1534. return
  1535. }
  1536. //根据机构id查询拥有仓库管理权限的角色
  1537. func FindRoles(orgid int64) map[string]int {
  1538. role := []models.RolePurviews{}
  1539. roles := make(map[string]int)
  1540. XTReadDB().Select("role_id").Where("purview_ids like \"%299%\" and org_id = ? and status = 1", orgid).Find(&role)
  1541. for i := 0; i < len(role); i++ {
  1542. roles[strconv.FormatInt(role[i].RoleId, 10)] = i
  1543. }
  1544. return roles
  1545. }
  1546. //判断两个切片中的元素是否有重复的,true有重复的元素,false无相同元素
  1547. func IsIdentical(str1 []string, str2 []string) (t bool) {
  1548. t = false
  1549. if len(str1) == 0 || len(str2) == 0 {
  1550. return
  1551. }
  1552. map1, map2 := make(map[string]int), make(map[string]int)
  1553. for i := 0; i < len(str1); i++ {
  1554. map1[str1[i]] = i
  1555. }
  1556. for i := 0; i < len(str2); i++ {
  1557. map2[str2[i]] = i
  1558. }
  1559. for k, _ := range map1 {
  1560. if _, ok := map2[k]; ok {
  1561. t = true
  1562. }
  1563. }
  1564. return
  1565. func ModifyWarehouseOut(id int64, orgid int64, good_id int64) error {
  1566. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  1567. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and org_id = ? and status =1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  1568. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and user_org_id = ? and status = 1 and good_id = ?", id, orgid, good_id).Update(map[string]interface{}{"status": 0}).Error
  1569. return err
  1570. }
  1571. func GetStoreDrugWarehouseOutList(drug_id int64, second_warehouse_info_id int64, orgid int64) (drugInfo []*models.DrugWarehouseOutInfo, err error) {
  1572. 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
  1573. return drugInfo, err
  1574. }
  1575. func ModifyDrugStoreWarehouseInfo(id int64, stock_max_number int64) error {
  1576. info := models.DrugWarehouseInfo{}
  1577. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  1578. return err
  1579. }
  1580. func ModifyDrugStoreWarehouseInfoOne(id int64, stock_min_number int64) error {
  1581. info := models.DrugWarehouseInfo{}
  1582. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  1583. return err
  1584. }
  1585. func GetStoreDrugWarehouseById(second_warehouse_id int64, orgid int64) (models.DrugWarehouse, error) {
  1586. warehouse := models.DrugWarehouse{}
  1587. err := XTReadDB().Where("second_warehouse_id = ? and org_id = ?", second_warehouse_id, orgid).Find(&warehouse).Error
  1588. return warehouse, err
  1589. }
  1590. func DeleteStoreWarehousingId(id int64, orgid int64, drug_id int64) error {
  1591. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  1592. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id in(?) and org_id = ? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  1593. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_id in(?) and user_org_id =? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  1594. return err
  1595. }
  1596. func GetStoreDrugWarehouseOutById(id int64, orgid int64) (models.DrugWarehouseOut, error) {
  1597. warehouse := models.DrugWarehouseOut{}
  1598. err = XTReadDB().Where("id = ? and org_id = ?", id, orgid).Find(&warehouse).Error
  1599. return warehouse, err
  1600. }
  1601. func DeleteStoreWarehouseOut(id int64, orgid int64, drug_id int64) error {
  1602. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  1603. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id in(?) and org_id = ? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  1604. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehouse_out_id in(?) and user_org_id =? and status = 1 and drug_id = ?", id, orgid, drug_id).Update(map[string]interface{}{"status": 0}).Error
  1605. return err
  1606. }
  1607. func ModifyStoreHouseById(id []string, orgid int64) error {
  1608. 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
  1609. return err
  1610. }
  1611. func GetStoreWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  1612. info := models.WarehousingInfo{}
  1613. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  1614. return info, err
  1615. }
  1616. func GetStoreHouseDrugWarehouseingInfo(id int64) (models.DrugWarehouseInfo, error) {
  1617. info := models.DrugWarehouseInfo{}
  1618. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  1619. return info, err
  1620. }
  1621. func GetDrugSumCountByStorehouseId(storehouse_id int64, orgid int64, drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  1622. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and (stock_max_number >0 or stock_min_number>0) and drug_id =?", storehouse_id, orgid, drug_id).Find(&info).Error
  1623. return info, err
  1624. }
  1625. func GetDrugSumCountByStorehouseIdSix(storehouse_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  1626. 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
  1627. return info, err
  1628. }
  1629. func GetGoodSumCountByStoreId(storehouse_id int64, goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  1630. err = XTReadDB().Where("storehouse_id = ? and good_id = ? and org_id =? and status =1 and stock_count > 0", storehouse_id, goodid, orgid).Find(&info).Error
  1631. return info, err
  1632. }
  1633. func GetGoodSumCountByStoreIdSix(storehouse_id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  1634. err = XTReadDB().Where("storehouse_id = ? and org_id =? and status =1 and stock_count > 0", storehouse_id, orgid).Find(&info).Error
  1635. return info, err
  1636. }
  1637. func GetSupplyDrugListOne(orgid int64, storehouse_id int64) (drug []*models.SpBaseDrug, err error) {
  1638. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',drug_status) = 0")
  1639. if orgid > 0 {
  1640. db = db.Where("x.org_id = ?", orgid)
  1641. }
  1642. 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
  1643. return drug, err
  1644. }
  1645. func GetSupplyGoodListOne(orgid int64, storehouse_id int64) (good []*models.SpGoodInformation, err error) {
  1646. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 AND find_in_set('停用',good_status) = 0")
  1647. if orgid > 0 {
  1648. db = db.Where("x.org_id = ?", orgid)
  1649. }
  1650. err = db.Preload("GoodWarehouseInfo", "status = 1 and org_id = ? and storehouse_id = ?", orgid, storehouse_id).Find(&good).Error
  1651. return good, err
  1652. }
  1653. func GetDrugSumSecondCount(drug_id int64, storehouse_id int64, orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1654. 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
  1655. return info, err
  1656. }
  1657. func GetGoodSumSecondCount(good_id int64, storehouse_id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  1658. 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
  1659. return info, err
  1660. }
  1661. func DeleteSecondOrderInfo(id int64) error {
  1662. err := XTWriteDB().Model(&models.XtSecondWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  1663. return err
  1664. }