drug_pharmacy_management_service.go 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "errors"
  6. "fmt"
  7. "github.com/jinzhu/gorm"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func GetPharmacyDrugList(storehouse_id int64, orgid int64, keywords string) (drug []*models.PharmacyBaseDrug, err error) {
  14. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and drug_status<>'停用 and is_pharmacy = 1' ", orgid)
  15. if len(keywords) > 0 {
  16. likekey := "%" + keywords + "%"
  17. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  18. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  19. }).Find(&drug).Error
  20. } else {
  21. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  22. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  23. }).Find(&drug).Error
  24. }
  25. return
  26. }
  27. func GetBloodAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyDoctorAdvice, err error) {
  28. err = XTReadDB().Where("advice_date = ? and drug_id = ? and user_org_id = ? and status = 1 and advice_type =2 and is_medicine= 0", advice_date, drug_id, org_id).Find(&advice).Error
  29. return advice, err
  30. }
  31. func GetHisAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyHisDoctorAdviceInfo, err error) {
  32. err = XTReadDB().Where("advice_date = ? and drug_id =? and user_org_id =? and status = 1 and is_medicine = 0", advice_date, drug_id, org_id).Order("prescription_id asc").Find(&advice).Error
  33. return advice, err
  34. }
  35. func UpdatePharmacyAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyDoctorAdvice, error) {
  36. drug := models.PharmacyDoctorAdvice{}
  37. err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 1}).Error
  38. return drug, err
  39. }
  40. func UpdatePharmacyHisAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyHisDoctorAdviceInfo, error) {
  41. info := models.PharmacyHisDoctorAdviceInfo{}
  42. err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 1}).Error
  43. return info, err
  44. }
  45. func GetPharmacyAdviceList(ids []string, orgid int64) (advice []*models.PharmacyDoctorAdvice, err error) {
  46. err = XTReadDB().Where("id in(?) and user_org_id = ?", ids, orgid).Find(&advice).Error
  47. return advice, err
  48. }
  49. func GetPharmacyHisAdviceList(ids []string, orgid int64) (advice []*models.PharmacyHisDoctorAdviceInfo, err error) {
  50. err = XTReadDB().Where("id in(?) and user_org_id = ?", ids, orgid).Find(&advice).Error
  51. return advice, err
  52. }
  53. func FindPharmacyConfig(orgid int64) (models.PharmacyConfig, error) {
  54. config := models.PharmacyConfig{}
  55. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  56. return config, err
  57. }
  58. // 药品出库
  59. func PharmacyDrugsDelivery(orgID int64, creater int64, advice *models.PharmacyDoctorAdvice, id int64) (err error) {
  60. // 1.判断药品是否来自专用字典的药品库
  61. // 2.判断当天当前机构有没有创建出库单,没有则创建
  62. // 3.创建出库流程
  63. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  64. isHasWay := false
  65. record_time := int64(0)
  66. if advice.Way == 1 {
  67. isHasWay = true
  68. record_time = advice.RecordDate
  69. }
  70. if isHasWay {
  71. //判断当天当前机构有没有创建出库单,没有则创建
  72. out, err := FindDrugStockOutByIsSys(orgID, 15, record_time)
  73. if err == gorm.ErrRecordNotFound {
  74. timeStr := time.Now().Format("2006-01-02")
  75. timeArr := strings.Split(timeStr, "-")
  76. total, _ := FindAllDrugWarehouseOut(orgID)
  77. total = total + 1
  78. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  79. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  80. number = number + total
  81. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  82. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  83. warehouseOut := models.DrugWarehouseOut{
  84. WarehouseOutOrderNumber: warehousing_out_order,
  85. OperationTime: time.Now().Unix(),
  86. OrgId: orgID,
  87. Creater: creater,
  88. Ctime: time.Now().Unix(),
  89. Status: 1,
  90. WarehouseOutTime: record_time,
  91. Dealer: 0,
  92. Manufacturer: 0,
  93. Type: 1,
  94. IsSys: 15,
  95. StorehouseId: storeConfig.DrugStorehouseOut,
  96. }
  97. err := AddSigleDrugWarehouseOut(&warehouseOut)
  98. if err != nil {
  99. utils.TraceLog("创建出库单失败 err = %v", err)
  100. return err
  101. } else {
  102. out = warehouseOut
  103. }
  104. }
  105. // 出库流程
  106. // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  107. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  108. if drup.ID > 0 {
  109. prescribingNumber := advice.PrescribingNumber
  110. PharmacyDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, id)
  111. } else {
  112. return errors.New("药品信息不存在")
  113. }
  114. }
  115. return
  116. }
  117. // 药品自动出库 递归方式
  118. func PharmacyDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyDoctorAdvice, id int64) (err error) {
  119. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  120. var deliver_number int64 = 0
  121. var stock_number int64 = 0
  122. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  123. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  124. var drug_price float64
  125. if advice.PrescribingNumberUnit == drup.MaxUnit {
  126. deliver_number = count * drup.MinNumber
  127. } else {
  128. deliver_number = count
  129. }
  130. if advice.PrescribingNumberUnit == drup.MaxUnit {
  131. drug_price = drup.RetailPrice
  132. }
  133. fmt.Println(drug_price)
  134. if advice.PrescribingNumberUnit == drup.MinUnit {
  135. drug_price = drup.MinPrice
  136. }
  137. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  138. drug_price = drup.RetailPrice
  139. }
  140. // 根据先进先出原则,查询最先入库的批次,进行出库
  141. // 如果没有对应的库存,则报错
  142. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  143. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  144. if err != nil {
  145. return err
  146. }
  147. // 将该批次的剩余库存数量转换为拆零数量
  148. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  149. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  150. if stock_number >= deliver_number {
  151. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  152. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  153. WarehouseOutId: warehouseout.ID,
  154. Status: 1,
  155. Ctime: time.Now().Unix(),
  156. Remark: warehouse.Remark,
  157. OrgId: orgID,
  158. Type: 1,
  159. Manufacturer: warehouse.Manufacturer,
  160. Dealer: warehouse.Dealer,
  161. IsSys: 15,
  162. SysRecordTime: advice.RecordDate,
  163. DrugId: advice.DrugId,
  164. Number: warehouse.Number,
  165. BatchNumber: warehouse.BatchNumber,
  166. Price: warehouse.RetailPrice,
  167. CountUnit: drup.MinUnit,
  168. RetailPrice: warehouse.RetailPrice,
  169. ProductDate: warehouse.ProductDate,
  170. ExpiryDate: warehouse.ExpiryDate,
  171. PatientId: advice.PatientId,
  172. Count: deliver_number, //出最小单位
  173. WarehouseInfoId: warehouse.ID,
  174. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  175. StorehouseId: storeConfig.DrugStorehouseOut,
  176. PharmacyId: id,
  177. }
  178. drugflow := models.DrugFlow{
  179. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  180. WarehouseOutId: warehouseout.ID,
  181. DrugId: advice.DrugId,
  182. Number: warehouse.Number,
  183. ProductDate: warehouse.ProductDate,
  184. ExpireDate: warehouse.ExpiryDate,
  185. Count: deliver_number, //按最小单位计算,
  186. Price: warehouse.RetailPrice,
  187. Status: 1,
  188. Ctime: time.Now().Unix(),
  189. UserOrgId: orgID,
  190. Manufacturer: warehouse.Manufacturer,
  191. Dealer: warehouse.Dealer,
  192. BatchNumber: warehouse.BatchNumber,
  193. MaxUnit: drup.MinUnit,
  194. ConsumableType: 3,
  195. IsEdit: 1,
  196. Creator: advice.ExecutionStaff,
  197. IsSys: 15,
  198. PatientId: advice.PatientId,
  199. SystemTime: advice.AdviceDate,
  200. WarehousingDetailId: warehouse.ID,
  201. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  202. StorehouseId: storeConfig.DrugStorehouseOut,
  203. PharmacyId: id,
  204. }
  205. CreateDrugFlowOne(drugflow)
  206. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  207. fmt.Println(errOne)
  208. //查询是否存在数据
  209. details := &models.DrugAutomaticReduceDetail{
  210. WarehouseOutId: warehouseout.ID,
  211. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  212. PatientId: advice.PatientId,
  213. Ctime: time.Now().Unix(),
  214. Mtime: time.Now().Unix(),
  215. Status: 1,
  216. RecordTime: advice.RecordDate,
  217. OrgId: orgID,
  218. DrugId: advice.DrugId,
  219. Count: count,
  220. CountUnit: advice.PrescribingNumberUnit,
  221. WarehouseInfoId: warehouse.ID,
  222. StorehouseId: storeConfig.DrugStorehouseOut,
  223. }
  224. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  225. if errTwo != nil {
  226. return errTwo
  227. }
  228. var maxNumber int64 = 0
  229. var minNumber int64 = 0
  230. var stock_max_number int64 = 0
  231. stock_max_number = warehouse.StockMaxNumber
  232. maxNumber = deliver_number / drup.MinNumber
  233. minNumber = deliver_number % drup.MinNumber
  234. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  235. minNumber = maxNumber
  236. }
  237. if drup.MaxUnit != drup.MinUnit {
  238. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  239. return errors.New("库存数量不足")
  240. }
  241. }
  242. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  243. if warehouse.StockMaxNumber < 0 {
  244. warehouse.StockMaxNumber = 0
  245. }
  246. if warehouse.StockMinNumber < 0 {
  247. warehouse.StockMinNumber = 0
  248. }
  249. warehouse.Mtime = time.Now().Unix()
  250. if warehouse.StockMinNumber < minNumber {
  251. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  252. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  253. } else {
  254. if minNumber > 0 {
  255. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  256. }
  257. if minNumber == 0 && maxNumber != 1 {
  258. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  259. }
  260. }
  261. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  262. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  263. if warehouse.StockMinNumber == 0 {
  264. warehouse.StockMaxNumber = stock_max_number
  265. }
  266. }
  267. if drup.MaxUnit != drup.MinUnit {
  268. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  269. return errors.New("库存数量不足")
  270. }
  271. }
  272. if warehouse.StockMinNumber <= 0 {
  273. warehouse.StockMinNumber = 0
  274. }
  275. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  276. if errThree != nil {
  277. return errThree
  278. }
  279. return nil
  280. } else {
  281. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  282. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  283. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  284. WarehouseOutId: warehouseout.ID,
  285. Status: 1,
  286. Ctime: time.Now().Unix(),
  287. Remark: warehouse.Remark,
  288. OrgId: orgID,
  289. Type: 1,
  290. Manufacturer: warehouse.Manufacturer,
  291. Dealer: warehouse.Dealer,
  292. IsSys: 15,
  293. SysRecordTime: advice.RecordDate,
  294. DrugId: advice.DrugId,
  295. Number: warehouse.Number,
  296. BatchNumber: warehouse.BatchNumber,
  297. Price: warehouse.RetailPrice,
  298. CountUnit: drup.MinUnit,
  299. ProductDate: warehouse.ProductDate,
  300. ExpiryDate: warehouse.ExpiryDate,
  301. PatientId: advice.PatientId,
  302. Count: deliver_number,
  303. WarehouseInfoId: warehouse.ID,
  304. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  305. StorehouseId: storeConfig.DrugStorehouseOut,
  306. PharmacyId: id,
  307. }
  308. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  309. if errOne != nil {
  310. return errOne
  311. }
  312. details := &models.DrugAutomaticReduceDetail{
  313. WarehouseOutId: warehouseout.ID,
  314. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  315. PatientId: advice.PatientId,
  316. Ctime: time.Now().Unix(),
  317. Mtime: time.Now().Unix(),
  318. Status: 1,
  319. RecordTime: advice.RecordDate,
  320. OrgId: orgID,
  321. DrugId: advice.DrugId,
  322. Count: deliver_number,
  323. CountUnit: drup.MinUnit,
  324. WarehouseInfoId: warehouse.ID,
  325. StorehouseId: storeConfig.DrugStorehouseOut,
  326. }
  327. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  328. if errTwo != nil {
  329. return errTwo
  330. }
  331. info := models.XtDrugWarehouseInfo{
  332. ID: warehouse.ID,
  333. WarehousingId: warehouse.WarehousingId,
  334. DrugId: warehouse.DrugId,
  335. Number: warehouse.Number,
  336. ProductDate: warehouse.ProductDate,
  337. ExpiryDate: warehouse.ExpiryDate,
  338. WarehousingCount: warehouse.WarehousingCount,
  339. Price: warehouse.RetailPrice,
  340. TotalPrice: warehouse.TotalPrice,
  341. Dealer: warehouse.Dealer,
  342. Manufacturer: warehouse.Manufacturer,
  343. Remark: warehouse.Remark,
  344. Ctime: warehouse.Ctime,
  345. Mtime: warehouse.Mtime,
  346. Status: 1,
  347. OrgId: warehouse.OrgId,
  348. IsReturn: warehouse.IsReturn,
  349. WarehousingOrder: warehouse.WarehousingOrder,
  350. Type: warehouse.Type,
  351. RetailPrice: warehouse.RetailPrice,
  352. RetailTotalPrice: warehouse.RetailPrice,
  353. StockMaxNumber: 0,
  354. StockMinNumber: 0,
  355. BatchNumber: warehouse.BatchNumber,
  356. MaxUnit: warehouse.MaxUnit,
  357. WarehousingInfoId: warehouse.WarehousingInfoId,
  358. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  359. }
  360. //扣减库存
  361. errThree := UpDateDrugWarehouseInfoByStock(&info)
  362. if errThree != nil {
  363. return errThree
  364. }
  365. drugflow := models.DrugFlow{
  366. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  367. WarehouseOutId: warehouseout.ID,
  368. DrugId: advice.DrugId,
  369. Number: warehouse.Number,
  370. ProductDate: warehouse.ProductDate,
  371. ExpireDate: warehouse.ExpiryDate,
  372. Count: stock_number,
  373. Price: warehouse.RetailPrice,
  374. Status: 1,
  375. Ctime: time.Now().Unix(),
  376. UserOrgId: orgID,
  377. Manufacturer: warehouse.Manufacturer,
  378. Dealer: warehouse.Dealer,
  379. BatchNumber: warehouse.BatchNumber,
  380. MaxUnit: drup.MinUnit,
  381. ConsumableType: 3,
  382. IsEdit: 1,
  383. Creator: advice.ExecutionStaff,
  384. IsSys: 15,
  385. PatientId: advice.PatientId,
  386. WarehousingDetailId: warehouse.ID,
  387. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  388. StorehouseId: storeConfig.DrugStorehouseOut,
  389. PharmacyId: id,
  390. }
  391. CreateDrugFlowOne(drugflow)
  392. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  393. prescribingNumber_two_temp := deliver_number - stock_number
  394. overPlusNumber := float64(prescribingNumber_two_temp)
  395. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  396. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  397. NewPharmacyDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
  398. }
  399. return
  400. }
  401. // 药品出库 递归方式
  402. func NewPharmacyDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyDoctorAdvice, id int64) (err error) {
  403. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  404. var deliver_number int64 = 0
  405. var stock_number int64 = 0
  406. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  407. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  408. var drug_price float64
  409. //按最小单位数据计算
  410. deliver_number = count
  411. if advice.PrescribingNumberUnit == drup.MaxUnit {
  412. drug_price = drup.RetailPrice
  413. }
  414. if advice.PrescribingNumberUnit == drup.MinUnit {
  415. drug_price = drup.MinPrice
  416. }
  417. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  418. drug_price = drup.RetailPrice
  419. }
  420. // 根据先进先出原则,查询最先入库的批次,进行出库
  421. // 如果没有对应的库存,则报错
  422. //查询默认仓库
  423. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  424. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  425. if err != nil {
  426. return err
  427. }
  428. // 将该批次的剩余库存数量转换为拆零数量
  429. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  430. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  431. if stock_number >= deliver_number {
  432. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  433. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  434. WarehouseOutId: warehouseout.ID,
  435. Status: 1,
  436. Ctime: time.Now().Unix(),
  437. Remark: warehouse.Remark,
  438. OrgId: orgID,
  439. Type: 1,
  440. Manufacturer: warehouse.Manufacturer,
  441. Dealer: warehouse.Dealer,
  442. IsSys: 15,
  443. SysRecordTime: advice.RecordDate,
  444. DrugId: advice.DrugId,
  445. Number: warehouse.Number,
  446. BatchNumber: warehouse.BatchNumber,
  447. Price: warehouse.RetailPrice,
  448. CountUnit: drup.MinUnit,
  449. RetailPrice: drug_price,
  450. ProductDate: warehouse.ProductDate,
  451. ExpiryDate: warehouse.ExpiryDate,
  452. PatientId: advice.PatientId,
  453. Count: deliver_number, //出最小单位
  454. WarehouseInfoId: warehouse.ID,
  455. AdviceId: advice.ID,
  456. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  457. StorehouseId: storeConfig.DrugStorehouseOut,
  458. PharmacyId: id,
  459. }
  460. drugflow := models.DrugFlow{
  461. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  462. WarehouseOutId: warehouseout.ID,
  463. DrugId: advice.DrugId,
  464. Number: warehouse.Number,
  465. ProductDate: warehouse.ProductDate,
  466. ExpireDate: warehouse.ExpiryDate,
  467. Count: deliver_number, //按最小单位计算,
  468. Price: warehouse.RetailPrice,
  469. Status: 1,
  470. Ctime: time.Now().Unix(),
  471. UserOrgId: orgID,
  472. Manufacturer: warehouse.Manufacturer,
  473. Dealer: warehouse.Dealer,
  474. BatchNumber: warehouse.BatchNumber,
  475. MaxUnit: drup.MinUnit,
  476. ConsumableType: 3,
  477. IsEdit: 1,
  478. Creator: advice.ExecutionStaff,
  479. IsSys: 15,
  480. PatientId: advice.PatientId,
  481. WarehousingDetailId: warehouse.ID,
  482. AdviceId: advice.ID,
  483. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  484. StorehouseId: storeConfig.DrugStorehouseOut,
  485. PharmacyId: id,
  486. }
  487. CreateDrugFlowOne(drugflow)
  488. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  489. if errOne != nil {
  490. return errOne
  491. }
  492. details := &models.DrugAutomaticReduceDetail{
  493. WarehouseOutId: warehouseout.ID,
  494. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  495. PatientId: advice.PatientId,
  496. Ctime: time.Now().Unix(),
  497. Mtime: time.Now().Unix(),
  498. Status: 1,
  499. RecordTime: advice.RecordDate,
  500. OrgId: orgID,
  501. DrugId: advice.DrugId,
  502. Count: count,
  503. CountUnit: advice.PrescribingNumberUnit,
  504. WarehouseInfoId: warehouse.ID,
  505. StorehouseId: storeConfig.DrugStorehouseOut,
  506. }
  507. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  508. if errTwo != nil {
  509. return errTwo
  510. }
  511. // 出库完成后,要减去对应批次的库存数量
  512. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  513. var maxNumber int64 = 0
  514. var minNumber int64 = 0
  515. var stock_max_number int64 = 0
  516. stock_max_number = warehouse.StockMaxNumber
  517. maxNumber = deliver_number / drup.MinNumber
  518. minNumber = deliver_number % drup.MinNumber
  519. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  520. minNumber = maxNumber
  521. }
  522. if drup.MaxUnit != drup.MinUnit {
  523. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  524. return errors.New("库存数量不足")
  525. }
  526. }
  527. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  528. if warehouse.StockMaxNumber < 0 {
  529. warehouse.StockMaxNumber = 0
  530. }
  531. if warehouse.StockMinNumber < 0 {
  532. warehouse.StockMinNumber = 0
  533. }
  534. warehouse.Mtime = time.Now().Unix()
  535. if warehouse.StockMinNumber < minNumber {
  536. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  537. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  538. } else {
  539. if minNumber > 0 {
  540. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  541. }
  542. if minNumber == 0 && maxNumber != 1 {
  543. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  544. }
  545. }
  546. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  547. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  548. if warehouse.StockMinNumber == 0 {
  549. warehouse.StockMaxNumber = stock_max_number
  550. }
  551. }
  552. if drup.MaxUnit != drup.MinUnit {
  553. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  554. return errors.New("库存数量不足")
  555. }
  556. }
  557. if warehouse.StockMinNumber <= 0 {
  558. warehouse.StockMinNumber = 0
  559. }
  560. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  561. if errThree != nil {
  562. return errThree
  563. }
  564. return nil
  565. } else {
  566. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  567. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  568. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  569. WarehouseOutId: warehouseout.ID,
  570. Status: 1,
  571. Ctime: time.Now().Unix(),
  572. Remark: warehouse.Remark,
  573. OrgId: orgID,
  574. Type: 1,
  575. Manufacturer: warehouse.Manufacturer,
  576. Dealer: warehouse.Dealer,
  577. IsSys: 15,
  578. SysRecordTime: advice.RecordDate,
  579. DrugId: advice.DrugId,
  580. Number: warehouse.Number,
  581. BatchNumber: warehouse.BatchNumber,
  582. Price: warehouse.RetailPrice,
  583. CountUnit: drup.MinUnit,
  584. ProductDate: warehouse.ProductDate,
  585. ExpiryDate: warehouse.ExpiryDate,
  586. PatientId: advice.PatientId,
  587. Count: deliver_number,
  588. WarehouseInfoId: warehouse.ID,
  589. AdviceId: advice.ID,
  590. StorehouseId: storeConfig.DrugStorehouseOut,
  591. PharmacyId: id,
  592. }
  593. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  594. if errOne != nil {
  595. return errOne
  596. }
  597. //查询是否存在数据
  598. details := &models.DrugAutomaticReduceDetail{
  599. WarehouseOutId: warehouseout.ID,
  600. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  601. PatientId: advice.PatientId,
  602. Ctime: time.Now().Unix(),
  603. Mtime: time.Now().Unix(),
  604. Status: 1,
  605. RecordTime: advice.RecordDate,
  606. OrgId: orgID,
  607. DrugId: advice.DrugId,
  608. Count: deliver_number,
  609. CountUnit: drup.MinUnit,
  610. WarehouseInfoId: warehouse.ID,
  611. StorehouseId: storeConfig.DrugStorehouseOut,
  612. }
  613. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  614. if errTwo != nil {
  615. return errTwo
  616. }
  617. // 出库完成后,要将该批次库存清零
  618. //warehouse.StockMaxNumber = 0
  619. //warehouse.StockMinNumber = 0
  620. //warehouse.Mtime = time.Now().Unix()
  621. info := models.XtDrugWarehouseInfo{
  622. ID: warehouse.ID,
  623. WarehousingId: warehouse.WarehousingId,
  624. DrugId: warehouse.DrugId,
  625. Number: warehouse.Number,
  626. ProductDate: warehouse.ProductDate,
  627. ExpiryDate: warehouse.ExpiryDate,
  628. WarehousingCount: warehouse.WarehousingCount,
  629. Price: warehouse.Price,
  630. TotalPrice: warehouse.TotalPrice,
  631. Dealer: warehouse.Dealer,
  632. Manufacturer: warehouse.Manufacturer,
  633. Remark: warehouse.Remark,
  634. Ctime: warehouse.Ctime,
  635. Mtime: warehouse.Mtime,
  636. Status: 1,
  637. OrgId: warehouse.OrgId,
  638. IsReturn: warehouse.IsReturn,
  639. WarehousingOrder: warehouse.WarehousingOrder,
  640. Type: warehouse.Type,
  641. RetailPrice: warehouse.RetailPrice,
  642. RetailTotalPrice: warehouse.RetailPrice,
  643. StockMaxNumber: 0,
  644. StockMinNumber: 0,
  645. BatchNumber: warehouse.BatchNumber,
  646. MaxUnit: warehouse.MaxUnit,
  647. WarehousingInfoId: warehouse.WarehousingInfoId,
  648. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  649. StorehouseId: storeConfig.DrugStorehouseOut,
  650. }
  651. //扣减库存
  652. errThree := UpDateDrugWarehouseInfoByStock(&info)
  653. if errThree != nil {
  654. return errThree
  655. }
  656. drugflow := models.DrugFlow{
  657. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  658. WarehouseOutId: warehouseout.ID,
  659. DrugId: advice.DrugId,
  660. Number: warehouse.Number,
  661. ProductDate: warehouse.ProductDate,
  662. ExpireDate: warehouse.ExpiryDate,
  663. Count: deliver_number,
  664. Price: warehouse.RetailPrice,
  665. Status: 1,
  666. Ctime: time.Now().Unix(),
  667. UserOrgId: orgID,
  668. Manufacturer: warehouse.Manufacturer,
  669. Dealer: warehouse.Dealer,
  670. BatchNumber: warehouse.BatchNumber,
  671. MaxUnit: drup.MinUnit,
  672. ConsumableType: 3,
  673. IsEdit: 1,
  674. Creator: advice.ExecutionStaff,
  675. IsSys: 15,
  676. PatientId: advice.PatientId,
  677. WarehousingDetailId: warehouse.ID,
  678. AdviceId: advice.ID,
  679. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  680. StorehouseId: storeConfig.DrugStorehouseOut,
  681. PharmacyId: id,
  682. }
  683. CreateDrugFlowOne(drugflow)
  684. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  685. prescribingNumber_two_temp := deliver_number - stock_number
  686. overPlusNumber := float64(prescribingNumber_two_temp)
  687. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  688. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  689. NewPharmacyDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
  690. }
  691. return
  692. }
  693. func FindAllPharmacyDrugWarehouseOut(org_id int64) (total int64, err error) {
  694. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ? and is_sys = 15", org_id).Count(&total).Error
  695. return total, err
  696. }
  697. func CreatePharmacy(pharmacy models.Pharmacy) error {
  698. err := XTWriteDB().Create(&pharmacy).Error
  699. return err
  700. }
  701. // 药品出库
  702. func PharmacyHisDrugsDelivery(orgID int64, creater int64, advice *models.PharmacyHisDoctorAdviceInfo, id int64) (err error) {
  703. // 1.判断药品是否来自专用字典的药品库
  704. // 2.判断当天当前机构有没有创建出库单,没有则创建
  705. // 3.创建出库流程
  706. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  707. isHasWay := false
  708. record_time := int64(0)
  709. if advice.Way == 1 {
  710. isHasWay = true
  711. record_time = advice.RecordDate
  712. }
  713. if isHasWay {
  714. //判断当天当前机构有没有创建出库单,没有则创建
  715. out, err := FindDrugStockOutByIsSys(orgID, 15, record_time)
  716. if err == gorm.ErrRecordNotFound {
  717. timeStr := time.Now().Format("2006-01-02")
  718. timeArr := strings.Split(timeStr, "-")
  719. total, _ := FindAllDrugWarehouseOut(orgID)
  720. total = total + 1
  721. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  722. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  723. number = number + total
  724. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  725. storeConfig, _ := GetAllStoreHouseConfig(orgID)
  726. warehouseOut := models.DrugWarehouseOut{
  727. WarehouseOutOrderNumber: warehousing_out_order,
  728. OperationTime: time.Now().Unix(),
  729. OrgId: orgID,
  730. Creater: creater,
  731. Ctime: time.Now().Unix(),
  732. Status: 1,
  733. WarehouseOutTime: record_time,
  734. Dealer: 0,
  735. Manufacturer: 0,
  736. Type: 1,
  737. IsSys: 15,
  738. StorehouseId: storeConfig.DrugStorehouseOut,
  739. }
  740. err := AddSigleDrugWarehouseOut(&warehouseOut)
  741. if err != nil {
  742. utils.TraceLog("创建出库单失败 err = %v", err)
  743. return err
  744. } else {
  745. out = warehouseOut
  746. }
  747. }
  748. // 出库流程
  749. // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  750. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  751. if drup.ID > 0 {
  752. prescribingNumber := advice.PrescribingNumber
  753. PharmacyHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, id)
  754. } else {
  755. return errors.New("药品信息不存在")
  756. }
  757. }
  758. return
  759. }
  760. // 药品自动出库 递归方式
  761. func PharmacyHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyHisDoctorAdviceInfo, id int64) (err error) {
  762. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  763. var deliver_number int64 = 0
  764. var stock_number int64 = 0
  765. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  766. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  767. var drug_price float64
  768. if advice.PrescribingNumberUnit == drup.MaxUnit {
  769. deliver_number = count * drup.MinNumber
  770. } else {
  771. deliver_number = count
  772. }
  773. if advice.PrescribingNumberUnit == drup.MaxUnit {
  774. drug_price = drup.RetailPrice
  775. }
  776. fmt.Println(drug_price)
  777. if advice.PrescribingNumberUnit == drup.MinUnit {
  778. drug_price = drup.MinPrice
  779. }
  780. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  781. drug_price = drup.RetailPrice
  782. }
  783. // 根据先进先出原则,查询最先入库的批次,进行出库
  784. // 如果没有对应的库存,则报错
  785. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  786. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  787. if err != nil {
  788. return err
  789. }
  790. // 将该批次的剩余库存数量转换为拆零数量
  791. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  792. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  793. if stock_number >= deliver_number {
  794. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  795. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  796. WarehouseOutId: warehouseout.ID,
  797. Status: 1,
  798. Ctime: time.Now().Unix(),
  799. Remark: warehouse.Remark,
  800. OrgId: orgID,
  801. Type: 1,
  802. Manufacturer: warehouse.Manufacturer,
  803. Dealer: warehouse.Dealer,
  804. IsSys: 15,
  805. SysRecordTime: advice.RecordDate,
  806. DrugId: advice.DrugId,
  807. Number: warehouse.Number,
  808. BatchNumber: warehouse.BatchNumber,
  809. Price: warehouse.RetailPrice,
  810. CountUnit: drup.MinUnit,
  811. RetailPrice: warehouse.RetailPrice,
  812. ProductDate: warehouse.ProductDate,
  813. ExpiryDate: warehouse.ExpiryDate,
  814. PatientId: advice.PatientId,
  815. Count: deliver_number, //出最小单位
  816. WarehouseInfoId: warehouse.ID,
  817. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  818. StorehouseId: storeConfig.DrugStorehouseOut,
  819. ID: id,
  820. }
  821. drugflow := models.DrugFlow{
  822. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  823. WarehouseOutId: warehouseout.ID,
  824. DrugId: advice.DrugId,
  825. Number: warehouse.Number,
  826. ProductDate: warehouse.ProductDate,
  827. ExpireDate: warehouse.ExpiryDate,
  828. Count: deliver_number, //按最小单位计算,
  829. Price: warehouse.RetailPrice,
  830. Status: 1,
  831. Ctime: time.Now().Unix(),
  832. UserOrgId: orgID,
  833. Manufacturer: warehouse.Manufacturer,
  834. Dealer: warehouse.Dealer,
  835. BatchNumber: warehouse.BatchNumber,
  836. MaxUnit: drup.MinUnit,
  837. ConsumableType: 3,
  838. IsEdit: 1,
  839. Creator: advice.ExecutionStaff,
  840. IsSys: 15,
  841. PatientId: advice.PatientId,
  842. SystemTime: advice.AdviceDate,
  843. WarehousingDetailId: warehouse.ID,
  844. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  845. StorehouseId: storeConfig.DrugStorehouseOut,
  846. ID: id,
  847. }
  848. CreateDrugFlowOne(drugflow)
  849. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  850. fmt.Println(errOne)
  851. //查询是否存在数据
  852. details := &models.DrugAutomaticReduceDetail{
  853. WarehouseOutId: warehouseout.ID,
  854. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  855. PatientId: advice.PatientId,
  856. Ctime: time.Now().Unix(),
  857. Mtime: time.Now().Unix(),
  858. Status: 1,
  859. RecordTime: advice.RecordDate,
  860. OrgId: orgID,
  861. DrugId: advice.DrugId,
  862. Count: count,
  863. CountUnit: advice.PrescribingNumberUnit,
  864. WarehouseInfoId: warehouse.ID,
  865. StorehouseId: storeConfig.DrugStorehouseOut,
  866. }
  867. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  868. if errTwo != nil {
  869. return errTwo
  870. }
  871. var maxNumber int64 = 0
  872. var minNumber int64 = 0
  873. var stock_max_number int64 = 0
  874. stock_max_number = warehouse.StockMaxNumber
  875. maxNumber = deliver_number / drup.MinNumber
  876. minNumber = deliver_number % drup.MinNumber
  877. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  878. minNumber = maxNumber
  879. }
  880. if drup.MaxUnit != drup.MinUnit {
  881. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  882. return errors.New("库存数量不足")
  883. }
  884. }
  885. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  886. if warehouse.StockMaxNumber < 0 {
  887. warehouse.StockMaxNumber = 0
  888. }
  889. if warehouse.StockMinNumber < 0 {
  890. warehouse.StockMinNumber = 0
  891. }
  892. warehouse.Mtime = time.Now().Unix()
  893. if warehouse.StockMinNumber < minNumber {
  894. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  895. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  896. } else {
  897. if minNumber > 0 {
  898. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  899. }
  900. if minNumber == 0 && maxNumber != 1 {
  901. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  902. }
  903. }
  904. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  905. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  906. if warehouse.StockMinNumber == 0 {
  907. warehouse.StockMaxNumber = stock_max_number
  908. }
  909. }
  910. if drup.MaxUnit != drup.MinUnit {
  911. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  912. return errors.New("库存数量不足")
  913. }
  914. }
  915. if warehouse.StockMinNumber <= 0 {
  916. warehouse.StockMinNumber = 0
  917. }
  918. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  919. if errThree != nil {
  920. return errThree
  921. }
  922. return nil
  923. } else {
  924. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  925. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  926. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  927. WarehouseOutId: warehouseout.ID,
  928. Status: 1,
  929. Ctime: time.Now().Unix(),
  930. Remark: warehouse.Remark,
  931. OrgId: orgID,
  932. Type: 1,
  933. Manufacturer: warehouse.Manufacturer,
  934. Dealer: warehouse.Dealer,
  935. IsSys: 15,
  936. SysRecordTime: advice.RecordDate,
  937. DrugId: advice.DrugId,
  938. Number: warehouse.Number,
  939. BatchNumber: warehouse.BatchNumber,
  940. Price: warehouse.RetailPrice,
  941. CountUnit: drup.MinUnit,
  942. ProductDate: warehouse.ProductDate,
  943. ExpiryDate: warehouse.ExpiryDate,
  944. PatientId: advice.PatientId,
  945. Count: deliver_number,
  946. WarehouseInfoId: warehouse.ID,
  947. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  948. StorehouseId: storeConfig.DrugStorehouseOut,
  949. ID: id,
  950. }
  951. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  952. if errOne != nil {
  953. return errOne
  954. }
  955. details := &models.DrugAutomaticReduceDetail{
  956. WarehouseOutId: warehouseout.ID,
  957. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  958. PatientId: advice.PatientId,
  959. Ctime: time.Now().Unix(),
  960. Mtime: time.Now().Unix(),
  961. Status: 1,
  962. RecordTime: advice.RecordDate,
  963. OrgId: orgID,
  964. DrugId: advice.DrugId,
  965. Count: deliver_number,
  966. CountUnit: drup.MinUnit,
  967. WarehouseInfoId: warehouse.ID,
  968. StorehouseId: storeConfig.DrugStorehouseOut,
  969. }
  970. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  971. if errTwo != nil {
  972. return errTwo
  973. }
  974. info := models.XtDrugWarehouseInfo{
  975. ID: warehouse.ID,
  976. WarehousingId: warehouse.WarehousingId,
  977. DrugId: warehouse.DrugId,
  978. Number: warehouse.Number,
  979. ProductDate: warehouse.ProductDate,
  980. ExpiryDate: warehouse.ExpiryDate,
  981. WarehousingCount: warehouse.WarehousingCount,
  982. Price: warehouse.RetailPrice,
  983. TotalPrice: warehouse.TotalPrice,
  984. Dealer: warehouse.Dealer,
  985. Manufacturer: warehouse.Manufacturer,
  986. Remark: warehouse.Remark,
  987. Ctime: warehouse.Ctime,
  988. Mtime: warehouse.Mtime,
  989. Status: 1,
  990. OrgId: warehouse.OrgId,
  991. IsReturn: warehouse.IsReturn,
  992. WarehousingOrder: warehouse.WarehousingOrder,
  993. Type: warehouse.Type,
  994. RetailPrice: warehouse.RetailPrice,
  995. RetailTotalPrice: warehouse.RetailPrice,
  996. StockMaxNumber: 0,
  997. StockMinNumber: 0,
  998. BatchNumber: warehouse.BatchNumber,
  999. MaxUnit: warehouse.MaxUnit,
  1000. WarehousingInfoId: warehouse.WarehousingInfoId,
  1001. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1002. }
  1003. //扣减库存
  1004. errThree := UpDateDrugWarehouseInfoByStock(&info)
  1005. if errThree != nil {
  1006. return errThree
  1007. }
  1008. drugflow := models.DrugFlow{
  1009. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1010. WarehouseOutId: warehouseout.ID,
  1011. DrugId: advice.DrugId,
  1012. Number: warehouse.Number,
  1013. ProductDate: warehouse.ProductDate,
  1014. ExpireDate: warehouse.ExpiryDate,
  1015. Count: stock_number,
  1016. Price: warehouse.RetailPrice,
  1017. Status: 1,
  1018. Ctime: time.Now().Unix(),
  1019. UserOrgId: orgID,
  1020. Manufacturer: warehouse.Manufacturer,
  1021. Dealer: warehouse.Dealer,
  1022. BatchNumber: warehouse.BatchNumber,
  1023. MaxUnit: drup.MinUnit,
  1024. ConsumableType: 3,
  1025. IsEdit: 1,
  1026. Creator: advice.ExecutionStaff,
  1027. IsSys: 15,
  1028. PatientId: advice.PatientId,
  1029. WarehousingDetailId: warehouse.ID,
  1030. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1031. StorehouseId: storeConfig.DrugStorehouseOut,
  1032. ID: id,
  1033. }
  1034. CreateDrugFlowOne(drugflow)
  1035. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1036. prescribingNumber_two_temp := deliver_number - stock_number
  1037. overPlusNumber := float64(prescribingNumber_two_temp)
  1038. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1039. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  1040. NewPharmacyHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
  1041. }
  1042. return
  1043. }
  1044. // 药品出库 递归方式
  1045. func NewPharmacyHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyHisDoctorAdviceInfo, id int64) (err error) {
  1046. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1047. var deliver_number int64 = 0
  1048. var stock_number int64 = 0
  1049. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1050. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1051. var drug_price float64
  1052. //按最小单位数据计算
  1053. deliver_number = count
  1054. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1055. drug_price = drup.RetailPrice
  1056. }
  1057. if advice.PrescribingNumberUnit == drup.MinUnit {
  1058. drug_price = drup.MinPrice
  1059. }
  1060. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1061. drug_price = drup.RetailPrice
  1062. }
  1063. // 根据先进先出原则,查询最先入库的批次,进行出库
  1064. // 如果没有对应的库存,则报错
  1065. //查询默认仓库
  1066. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1067. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1068. if err != nil {
  1069. return err
  1070. }
  1071. // 将该批次的剩余库存数量转换为拆零数量
  1072. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1073. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1074. if stock_number >= deliver_number {
  1075. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1076. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1077. WarehouseOutId: warehouseout.ID,
  1078. Status: 1,
  1079. Ctime: time.Now().Unix(),
  1080. Remark: warehouse.Remark,
  1081. OrgId: orgID,
  1082. Type: 1,
  1083. Manufacturer: warehouse.Manufacturer,
  1084. Dealer: warehouse.Dealer,
  1085. IsSys: 15,
  1086. SysRecordTime: advice.RecordDate,
  1087. DrugId: advice.DrugId,
  1088. Number: warehouse.Number,
  1089. BatchNumber: warehouse.BatchNumber,
  1090. Price: warehouse.RetailPrice,
  1091. CountUnit: drup.MinUnit,
  1092. RetailPrice: drug_price,
  1093. ProductDate: warehouse.ProductDate,
  1094. ExpiryDate: warehouse.ExpiryDate,
  1095. PatientId: advice.PatientId,
  1096. Count: deliver_number, //出最小单位
  1097. WarehouseInfoId: warehouse.ID,
  1098. AdviceId: advice.ID,
  1099. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1100. StorehouseId: storeConfig.DrugStorehouseOut,
  1101. ID: id,
  1102. }
  1103. drugflow := models.DrugFlow{
  1104. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1105. WarehouseOutId: warehouseout.ID,
  1106. DrugId: advice.DrugId,
  1107. Number: warehouse.Number,
  1108. ProductDate: warehouse.ProductDate,
  1109. ExpireDate: warehouse.ExpiryDate,
  1110. Count: deliver_number, //按最小单位计算,
  1111. Price: warehouse.RetailPrice,
  1112. Status: 1,
  1113. Ctime: time.Now().Unix(),
  1114. UserOrgId: orgID,
  1115. Manufacturer: warehouse.Manufacturer,
  1116. Dealer: warehouse.Dealer,
  1117. BatchNumber: warehouse.BatchNumber,
  1118. MaxUnit: drup.MinUnit,
  1119. ConsumableType: 3,
  1120. IsEdit: 1,
  1121. Creator: advice.ExecutionStaff,
  1122. IsSys: 15,
  1123. PatientId: advice.PatientId,
  1124. WarehousingDetailId: warehouse.ID,
  1125. AdviceId: advice.ID,
  1126. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1127. StorehouseId: storeConfig.DrugStorehouseOut,
  1128. ID: id,
  1129. }
  1130. CreateDrugFlowOne(drugflow)
  1131. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1132. if errOne != nil {
  1133. return errOne
  1134. }
  1135. details := &models.DrugAutomaticReduceDetail{
  1136. WarehouseOutId: warehouseout.ID,
  1137. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1138. PatientId: advice.PatientId,
  1139. Ctime: time.Now().Unix(),
  1140. Mtime: time.Now().Unix(),
  1141. Status: 1,
  1142. RecordTime: advice.RecordDate,
  1143. OrgId: orgID,
  1144. DrugId: advice.DrugId,
  1145. Count: count,
  1146. CountUnit: advice.PrescribingNumberUnit,
  1147. WarehouseInfoId: warehouse.ID,
  1148. StorehouseId: storeConfig.DrugStorehouseOut,
  1149. }
  1150. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1151. if errTwo != nil {
  1152. return errTwo
  1153. }
  1154. // 出库完成后,要减去对应批次的库存数量
  1155. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  1156. var maxNumber int64 = 0
  1157. var minNumber int64 = 0
  1158. var stock_max_number int64 = 0
  1159. stock_max_number = warehouse.StockMaxNumber
  1160. maxNumber = deliver_number / drup.MinNumber
  1161. minNumber = deliver_number % drup.MinNumber
  1162. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1163. minNumber = maxNumber
  1164. }
  1165. if drup.MaxUnit != drup.MinUnit {
  1166. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1167. return errors.New("库存数量不足")
  1168. }
  1169. }
  1170. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1171. if warehouse.StockMaxNumber < 0 {
  1172. warehouse.StockMaxNumber = 0
  1173. }
  1174. if warehouse.StockMinNumber < 0 {
  1175. warehouse.StockMinNumber = 0
  1176. }
  1177. warehouse.Mtime = time.Now().Unix()
  1178. if warehouse.StockMinNumber < minNumber {
  1179. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1180. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1181. } else {
  1182. if minNumber > 0 {
  1183. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1184. }
  1185. if minNumber == 0 && maxNumber != 1 {
  1186. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1187. }
  1188. }
  1189. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  1190. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1191. if warehouse.StockMinNumber == 0 {
  1192. warehouse.StockMaxNumber = stock_max_number
  1193. }
  1194. }
  1195. if drup.MaxUnit != drup.MinUnit {
  1196. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1197. return errors.New("库存数量不足")
  1198. }
  1199. }
  1200. if warehouse.StockMinNumber <= 0 {
  1201. warehouse.StockMinNumber = 0
  1202. }
  1203. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1204. if errThree != nil {
  1205. return errThree
  1206. }
  1207. return nil
  1208. } else {
  1209. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1210. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1211. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1212. WarehouseOutId: warehouseout.ID,
  1213. Status: 1,
  1214. Ctime: time.Now().Unix(),
  1215. Remark: warehouse.Remark,
  1216. OrgId: orgID,
  1217. Type: 1,
  1218. Manufacturer: warehouse.Manufacturer,
  1219. Dealer: warehouse.Dealer,
  1220. IsSys: 15,
  1221. SysRecordTime: advice.RecordDate,
  1222. DrugId: advice.DrugId,
  1223. Number: warehouse.Number,
  1224. BatchNumber: warehouse.BatchNumber,
  1225. Price: warehouse.RetailPrice,
  1226. CountUnit: drup.MinUnit,
  1227. ProductDate: warehouse.ProductDate,
  1228. ExpiryDate: warehouse.ExpiryDate,
  1229. PatientId: advice.PatientId,
  1230. Count: deliver_number,
  1231. WarehouseInfoId: warehouse.ID,
  1232. AdviceId: advice.ID,
  1233. StorehouseId: storeConfig.DrugStorehouseOut,
  1234. ID: id,
  1235. }
  1236. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1237. if errOne != nil {
  1238. return errOne
  1239. }
  1240. //查询是否存在数据
  1241. details := &models.DrugAutomaticReduceDetail{
  1242. WarehouseOutId: warehouseout.ID,
  1243. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1244. PatientId: advice.PatientId,
  1245. Ctime: time.Now().Unix(),
  1246. Mtime: time.Now().Unix(),
  1247. Status: 1,
  1248. RecordTime: advice.RecordDate,
  1249. OrgId: orgID,
  1250. DrugId: advice.DrugId,
  1251. Count: deliver_number,
  1252. CountUnit: drup.MinUnit,
  1253. WarehouseInfoId: warehouse.ID,
  1254. StorehouseId: storeConfig.DrugStorehouseOut,
  1255. }
  1256. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1257. if errTwo != nil {
  1258. return errTwo
  1259. }
  1260. // 出库完成后,要将该批次库存清零
  1261. //warehouse.StockMaxNumber = 0
  1262. //warehouse.StockMinNumber = 0
  1263. //warehouse.Mtime = time.Now().Unix()
  1264. info := models.XtDrugWarehouseInfo{
  1265. ID: warehouse.ID,
  1266. WarehousingId: warehouse.WarehousingId,
  1267. DrugId: warehouse.DrugId,
  1268. Number: warehouse.Number,
  1269. ProductDate: warehouse.ProductDate,
  1270. ExpiryDate: warehouse.ExpiryDate,
  1271. WarehousingCount: warehouse.WarehousingCount,
  1272. Price: warehouse.Price,
  1273. TotalPrice: warehouse.TotalPrice,
  1274. Dealer: warehouse.Dealer,
  1275. Manufacturer: warehouse.Manufacturer,
  1276. Remark: warehouse.Remark,
  1277. Ctime: warehouse.Ctime,
  1278. Mtime: warehouse.Mtime,
  1279. Status: 1,
  1280. OrgId: warehouse.OrgId,
  1281. IsReturn: warehouse.IsReturn,
  1282. WarehousingOrder: warehouse.WarehousingOrder,
  1283. Type: warehouse.Type,
  1284. RetailPrice: warehouse.RetailPrice,
  1285. RetailTotalPrice: warehouse.RetailPrice,
  1286. StockMaxNumber: 0,
  1287. StockMinNumber: 0,
  1288. BatchNumber: warehouse.BatchNumber,
  1289. MaxUnit: warehouse.MaxUnit,
  1290. WarehousingInfoId: warehouse.WarehousingInfoId,
  1291. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1292. StorehouseId: storeConfig.DrugStorehouseOut,
  1293. }
  1294. //扣减库存
  1295. errThree := UpDateDrugWarehouseInfoByStock(&info)
  1296. if errThree != nil {
  1297. return errThree
  1298. }
  1299. drugflow := models.DrugFlow{
  1300. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1301. WarehouseOutId: warehouseout.ID,
  1302. DrugId: advice.DrugId,
  1303. Number: warehouse.Number,
  1304. ProductDate: warehouse.ProductDate,
  1305. ExpireDate: warehouse.ExpiryDate,
  1306. Count: deliver_number,
  1307. Price: warehouse.RetailPrice,
  1308. Status: 1,
  1309. Ctime: time.Now().Unix(),
  1310. UserOrgId: orgID,
  1311. Manufacturer: warehouse.Manufacturer,
  1312. Dealer: warehouse.Dealer,
  1313. BatchNumber: warehouse.BatchNumber,
  1314. MaxUnit: drup.MinUnit,
  1315. ConsumableType: 3,
  1316. IsEdit: 1,
  1317. Creator: advice.ExecutionStaff,
  1318. IsSys: 15,
  1319. PatientId: advice.PatientId,
  1320. WarehousingDetailId: warehouse.ID,
  1321. AdviceId: advice.ID,
  1322. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1323. StorehouseId: storeConfig.DrugStorehouseOut,
  1324. ID: id,
  1325. }
  1326. CreateDrugFlowOne(drugflow)
  1327. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1328. prescribingNumber_two_temp := deliver_number - stock_number
  1329. overPlusNumber := float64(prescribingNumber_two_temp)
  1330. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1331. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  1332. NewPharmacyHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
  1333. }
  1334. return
  1335. }
  1336. func GetUseredBloodAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyDoctorAdvice, err error) {
  1337. err = XTReadDB().Where("advice_date = ? and drug_id = ? and user_org_id = ? and status = 1 and advice_type =2 and is_medicine= 1", advice_date, drug_id, org_id).Find(&advice).Error
  1338. return advice, err
  1339. }
  1340. func GetUseredHisAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyHisDoctorAdviceInfo, err error) {
  1341. err = XTReadDB().Where("advice_date = ? and drug_id =? and user_org_id =? and status = 1 and is_medicine = 1", advice_date, drug_id, org_id).Order("prescription_id asc").Find(&advice).Error
  1342. return advice, err
  1343. }
  1344. func UpdateReturnPharmacyAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyDoctorAdvice, error) {
  1345. advice := models.PharmacyDoctorAdvice{}
  1346. err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 0}).Error
  1347. return advice, err
  1348. }
  1349. func UpdateReturnPharmacyHisAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyHisDoctorAdviceInfo, error) {
  1350. advice := models.PharmacyHisDoctorAdviceInfo{}
  1351. err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 0}).Error
  1352. return advice, err
  1353. }
  1354. func GetLastPharmary(orgid int64, advice_id int64, record_date int64) (models.Pharmacy, error) {
  1355. pharmacy := models.Pharmacy{}
  1356. err := XTReadDB().Where("user_org_id = ? and advice_id = ? and record_date =? and status =1 ", orgid, advice_id, record_date).Find(&pharmacy).Error
  1357. return pharmacy, err
  1358. }
  1359. func GetLastHisPharmary(orgid int64, advice_id int64, record_date int64) (models.Pharmacy, error) {
  1360. pharmacy := models.Pharmacy{}
  1361. err := XTReadDB().Where("user_org_id = ? and his_advice_id = ? and record_date =? and status =1 ", orgid, advice_id, record_date).Find(&pharmacy).Error
  1362. return pharmacy, err
  1363. }
  1364. func GetConfigSettingIsExsit(orgid int64) (*models.PharmacyConfig, error) {
  1365. var pharmacy models.PharmacyConfig
  1366. var err error
  1367. err = readDb.Model(&models.PharmacyConfig{}).Where("user_org_id=? and status = 1", orgid).First(&pharmacy).Error
  1368. if err == gorm.ErrRecordNotFound {
  1369. return nil, nil
  1370. }
  1371. if err != nil {
  1372. return nil, err
  1373. }
  1374. return &pharmacy, nil
  1375. }
  1376. func CreatePharmacyConfig(config *models.PharmacyConfig) error {
  1377. err := XTWriteDB().Create(&config).Error
  1378. return err
  1379. }
  1380. func UpdatePharmacyConfig(orgid int64, config *models.PharmacyConfig) error {
  1381. err := XTWriteDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  1382. return err
  1383. }
  1384. func GetPharmacyConfig(orgid int64) (models.PharmacyConfig, error) {
  1385. config := models.PharmacyConfig{}
  1386. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  1387. if err == gorm.ErrRecordNotFound {
  1388. tmp := models.PharmacyConfig{
  1389. UserOrgId: orgid,
  1390. IsOpen: 2,
  1391. Status: 1,
  1392. Ctime: time.Now().Unix(),
  1393. Mtime: time.Now().Unix(),
  1394. }
  1395. errs := XTWriteDB().Create(&tmp).Error
  1396. if errs != nil {
  1397. err = errs
  1398. } else {
  1399. err = nil
  1400. }
  1401. return tmp, err
  1402. }
  1403. return config, err
  1404. }
  1405. func CreateDrugError(drugError models.XtDrugError) error {
  1406. err := XTWriteDB().Create(&drugError).Error
  1407. return err
  1408. }