drug_pharmacy_management_service.go 57KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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. }