drug_pharmacy_management_service.go 58KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "XT_New/models"
  10. "XT_New/utils"
  11. "github.com/jinzhu/gorm"
  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. }
  1409. func GetDrugCodeConfig(user_org_id int64) (models.XtDrugCodeConfig, error) {
  1410. codeConfig := models.XtDrugCodeConfig{}
  1411. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&codeConfig).Error
  1412. return codeConfig, err
  1413. }
  1414. func SaveDrugCodeConfig(config models.XtDrugCodeConfig) error {
  1415. err := XTWriteDB().Save(&config).Error
  1416. return err
  1417. }
  1418. func CreateDrugCodeConfig(config models.XtDrugCodeConfig) error {
  1419. err := XTWriteDB().Create(&config).Error
  1420. return err
  1421. }