pharmacy_controller.go 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. package controllers
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. "time"
  7. "XT_New/enums"
  8. "XT_New/models"
  9. "XT_New/service"
  10. "XT_New/utils"
  11. "github.com/astaxie/beego"
  12. )
  13. type PharmacyController struct {
  14. BaseAuthAPIController
  15. }
  16. func PharmacyApiRegistRouters() {
  17. beego.Router("/api/pharmacy/ceshili", &PharmacyController{}, "get:Tlili")
  18. beego.Router("/api/pharmacy/todaynumber", &PharmacyController{}, "get:TodayNumber") //查询今天的待发药,已发药人数(
  19. beego.Router("/api/pharmacy/waitingdrug", &PharmacyController{}, "get:WaitingDrug") //获取当天待发药的所有患者(
  20. beego.Router("/api/pharmacy/issueddrugs", &PharmacyController{}, "get:IssuedDrug") //获取当天已发药的所有患者(
  21. beego.Router("/api/pharmacy/getpharmacycontent", &PharmacyController{}, "get:GetPharmacyContent") //获取当天该患者的所有信息(
  22. beego.Router("/api/pharmacy/dispensingmedicine", &PharmacyController{}, "get:DispensingMedicine") //患者发药按钮点击(
  23. beego.Router("/api/pharmacy/drugwithdrawal", &PharmacyController{}, "get:DrugWithdrawal") //退药按钮点击
  24. beego.Router("/api/pharmacy/dispensingdetails", &PharmacyController{}, "get:DispensingDetails") //获取发药明细的患者列表(
  25. beego.Router("/api/pharmacy/prescriptiondetails", &PharmacyController{}, "get:PrescriptionDetails") //发药明细-详情(
  26. beego.Router("/api/pharmacy/dispensemedicine", &PharmacyController{}, "get:DispenseMedicine") //获取当天已发药的药品(
  27. beego.Router("/api/pharmacy/waitingmedicine", &PharmacyController{}, "get:WaitingMedicine") //获取当天待发药的药品(
  28. beego.Router("/api/pharmacy/getpatientswithdrugs", &PharmacyController{}, "get:GetPatientsWithDrugs") //获取当天该药品的所有患者(
  29. beego.Router("/api/pharmacy/medicinedeparture", &PharmacyController{}, "get:MedicineDeparture") //药品发药按钮点击(
  30. beego.Router("/api/pharmacy/getcurrentname", &PharmacyController{}, "get:GetCurrentName") //获取当前登录账号的名字(
  31. beego.Router("/api/pharmacy/getpartitionlist", &PharmacyController{}, "get:GetPartitionList") //获取当前机构的分区列表
  32. beego.Router("/api/pharmacy/routeofadministration", &PharmacyController{}, "get:RouteOfAdministration") //获取当前机构的给药途径
  33. beego.Router("/api/pharmacy/getgoodinventorylist", &PharmacyController{}, "Get:GetGoodInventoryList")
  34. beego.Router("/api/pharmacy/getgoodinventorybygoodid", &PharmacyController{}, "Get:GetGoodInventoryByGoodId")
  35. beego.Router("/api/pharmacy/updateInventoryWarehouseInfo", &PharmacyController{}, "Get:UpdateInventoryWarehouseInfo")
  36. beego.Router("/api/pharmacy/getdrugnewinventoryList", &PharmacyController{}, "Get:GetDrugInventoryList")
  37. beego.Router("/api/pharmacy/getdruginventorybydrugid", &PharmacyController{}, "Get:GetDrugInventoryByDrugId")
  38. beego.Router("/api/pharmacy/updatedruginventorywarehouseinfo", &PharmacyController{}, "Get:UpdateDrugInventoryWarehouseInfo")
  39. }
  40. // 测试
  41. func (this *PharmacyController) Tlili() {
  42. var err error
  43. defer func() {
  44. if rec := recover(); rec != nil {
  45. err = fmt.Errorf("程序异常:%v", rec)
  46. }
  47. if err != nil {
  48. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  49. }
  50. }()
  51. var list2 []*models.ReplacementDrugs
  52. list2, err = service.ReplacementDrugs(9675, false)
  53. if err != nil {
  54. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  55. return
  56. }
  57. this.ServeSuccessJSON(map[string]interface{}{
  58. "list": list2,
  59. })
  60. return
  61. }
  62. func (this *PharmacyController) GetCurrentName() {
  63. create := this.GetAdminUserInfo().AdminUser.Id
  64. this.ServeSuccessJSON(map[string]interface{}{
  65. "list": create,
  66. })
  67. return
  68. }
  69. // 查询今天的待发药,已发药人数
  70. func (this *PharmacyController) TodayNumber() {
  71. var err error
  72. defer func() {
  73. if rec := recover(); rec != nil {
  74. err = fmt.Errorf("程序异常:%v", rec)
  75. }
  76. if err != nil {
  77. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  78. }
  79. }()
  80. orgid := this.GetAdminUserInfo().CurrentOrgId
  81. times := this.GetString("time", "")
  82. timeLayout := "2006-01-02"
  83. loc, _ := time.LoadLocation("Local")
  84. var stime, etime int64
  85. if times == "" {
  86. stime, etime = service.GetNowTime()
  87. } else {
  88. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  89. stime = stmp.Unix()
  90. etime = stime + 86399
  91. }
  92. //if orgid != 9671 && orgid != 10188 && orgid != 10217 && orgid != 3877 && orgid != 10164 && orgid != 10480 {
  93. // //查询表里当天的数据
  94. // var wtotal int
  95. // wtotal, err = service.GetTodayPharmacy(stime, etime, orgid, 1)
  96. //
  97. // if err != nil {
  98. // utils.ErrorLog(err.Error())
  99. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  100. // return
  101. // }
  102. //
  103. // var itotal int
  104. // itotal, err = service.GetTodayPharmacy(stime, etime, orgid, 0)
  105. //
  106. // drug, _ := service.GetAllBaseDrugList(orgid)
  107. // if err != nil {
  108. // utils.ErrorLog(err.Error())
  109. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  110. // return
  111. // }
  112. // this.ServeSuccessJSON(map[string]interface{}{
  113. // "wtotal": wtotal,
  114. // "itotal": itotal,
  115. // "drug": drug,
  116. // })
  117. // return
  118. //}
  119. var wtotal int
  120. var itotal int
  121. //查找出库数量
  122. wtotal, _ = service.GetTodayAdviceCount(stime, etime, orgid, 1)
  123. itotal, _ = service.GetTodayAdviceCount(stime, etime, orgid, 0)
  124. drug, _ := service.GetAllBaseDrugList(orgid)
  125. this.ServeSuccessJSON(map[string]interface{}{
  126. "wtotal": wtotal,
  127. "itotal": itotal,
  128. "drug": drug,
  129. })
  130. return
  131. }
  132. func (this *PharmacyController) IssuedDrug() {
  133. var err error
  134. defer func() {
  135. if rec := recover(); rec != nil {
  136. err = fmt.Errorf("程序异常:%v", rec)
  137. }
  138. if err != nil {
  139. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  140. }
  141. }()
  142. keyword := this.GetString("keyword", "")
  143. fmt.Println(keyword)
  144. times := this.GetString("time", "")
  145. orgid := this.GetAdminUserInfo().CurrentOrgId
  146. shift, err := this.GetInt64("shift", 0) //班次
  147. if err != nil {
  148. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  149. return
  150. }
  151. partition, err := this.GetInt64("partition", 0) //分区
  152. if err != nil {
  153. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  154. return
  155. }
  156. timeLayout := "2006-01-02"
  157. loc, _ := time.LoadLocation("Local")
  158. var stime, etime int64
  159. if times == "" {
  160. stime, etime = service.GetNowTime()
  161. } else {
  162. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  163. stime = stmp.Unix()
  164. etime = stime + 86399
  165. }
  166. //获取排班班次
  167. schedule, _ := service.GetSchedulePatientId(stime, etime, orgid, shift, partition)
  168. var ids []int64
  169. for _, item := range schedule {
  170. ids = append(ids, item.PatientId)
  171. }
  172. list, _ := service.GetTodayAdviceCountOne(stime, etime, orgid, 1, ids)
  173. var flist []models.TmpPatientOne
  174. if len(list) > 0 {
  175. for _, item := range list {
  176. patientlist, _ := service.GetPatientByAdviceId(item.PatientId)
  177. flist = append(flist, patientlist)
  178. }
  179. }
  180. this.ServeSuccessJSON(map[string]interface{}{
  181. "list": flist,
  182. })
  183. //if orgid != 10164 && orgid == 3877 && orgid != 10188 && orgid != 10217 && orgid != 9671 && orgid != 10387 && orgid != 10375 && orgid != 10480 && orgid == 10344 {
  184. // //查询表里当天的数据
  185. // var flist []*models.TmpPatient
  186. // flist, err = service.GetTodayDrug(stime, etime, orgid, 1, keyword)
  187. // if err != nil {
  188. // utils.ErrorLog(err.Error())
  189. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  190. // return
  191. // }
  192. // listt, err := service.PartitionAndLayout(stime, etime, orgid, shift, partition, flist)
  193. // if err != nil {
  194. // utils.ErrorLog(err.Error())
  195. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  196. // return
  197. // }
  198. // this.ServeSuccessJSON(map[string]interface{}{
  199. // "list": listt,
  200. // })
  201. // return
  202. //}
  203. }
  204. func (this *PharmacyController) WaitingDrug() {
  205. var err error
  206. defer func() {
  207. if rec := recover(); rec != nil {
  208. err = fmt.Errorf("程序异常:%v", rec)
  209. }
  210. if err != nil {
  211. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  212. }
  213. }()
  214. keyword := this.GetString("keyword", "")
  215. times := this.GetString("time", "")
  216. shift, err := this.GetInt64("shift", 0) //班次
  217. if err != nil {
  218. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  219. return
  220. }
  221. partition, err := this.GetInt64("partition", 0) //分区
  222. if err != nil {
  223. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  224. return
  225. }
  226. orgid := this.GetAdminUserInfo().CurrentOrgId
  227. timeLayout := "2006-01-02"
  228. loc, _ := time.LoadLocation("Local")
  229. var stime, etime int64
  230. if times == "" {
  231. stime, etime = service.GetNowTime()
  232. } else {
  233. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  234. stime = stmp.Unix()
  235. etime = stime + 86399
  236. }
  237. //if orgid != 9671 && orgid != 10188 && orgid != 10217 && orgid != 3877 && orgid != 10164 && orgid != 10387 && orgid != 10375 && orgid != 10480 && orgid != 10344 {
  238. // //查询表里当天的数据
  239. // var flist []*models.TmpPatient
  240. // flist, err = service.GetTodayDrug(stime, etime, orgid, 0, keyword)
  241. //
  242. // if err != nil {
  243. // utils.ErrorLog(err.Error())
  244. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  245. // return
  246. // }
  247. // listt, err := service.PartitionAndLayout(stime, etime, orgid, shift, partition, flist)
  248. //
  249. // if err != nil {
  250. // utils.ErrorLog(err.Error())
  251. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  252. // return
  253. // }
  254. // baseList, _ := service.GetAllBaseDrugListTwo(orgid)
  255. // this.ServeSuccessJSON(map[string]interface{}{
  256. // "list": listt,
  257. // "baseList": baseList,
  258. // })
  259. // return
  260. //}
  261. fmt.Println(keyword)
  262. //当未发药的人数
  263. //获取排班班次
  264. schedule, _ := service.GetSchedulePatientId(stime, etime, orgid, shift, partition)
  265. var ids []int64
  266. for _, item := range schedule {
  267. ids = append(ids, item.PatientId)
  268. }
  269. list, _ := service.GetTodayAdviceCountOne(stime, etime, orgid, 0, ids)
  270. var flist []models.TmpPatientOne
  271. if len(list) > 0 {
  272. for _, item := range list {
  273. patientlist, _ := service.GetPatientByAdviceId(item.PatientId)
  274. flist = append(flist, patientlist)
  275. }
  276. }
  277. baseList, _ := service.GetAllBaseDrugListTwo(orgid)
  278. this.ServeSuccessJSON(map[string]interface{}{
  279. "list": flist,
  280. "baseList": baseList,
  281. })
  282. return
  283. }
  284. func (this *PharmacyController) GetPharmacyContent() {
  285. var err error
  286. defer func() {
  287. if rec := recover(); rec != nil {
  288. err = fmt.Errorf("程序异常:%v", rec)
  289. }
  290. if err != nil {
  291. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  292. }
  293. }()
  294. patient_id, _ := this.GetInt64("patient_id", 0)
  295. is_medicine, _ := this.GetInt64("is_medicine", 0) //0:待发药,1:已发药
  296. times := this.GetString("time", "")
  297. orgid := this.GetAdminUserInfo().CurrentOrgId
  298. timeLayout := "2006-01-02"
  299. loc, _ := time.LoadLocation("Local")
  300. var stime, etime int64
  301. if times == "" {
  302. stime, etime = service.GetNowTime()
  303. } else {
  304. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  305. stime = stmp.Unix()
  306. etime = stime + 86399
  307. }
  308. var list []*models.PharmacyContent
  309. list, err = service.GetPatientMedication(orgid, patient_id, stime, etime, is_medicine)
  310. baseList, _ := service.GetAllBaseDrugListTwo(orgid)
  311. patients, _ := service.GetPatientByIDOne(orgid, patient_id)
  312. order, _ := service.GetMobiledialysiOrder(orgid, patient_id, stime)
  313. numberList, _ := service.GetAllBedNumberList(orgid)
  314. zoneList, _ := service.GetAllZoneByList(orgid)
  315. appId := this.GetAdminUserInfo().CurrentAppId
  316. adminUserES, _ := service.GetAllAdminUserES(orgid, appId)
  317. allDoctor, _ := service.GetAllDoctor(orgid, appId)
  318. prescriptionListByToDay, _ := service.GetPrescriptionListByToDay(orgid, patient_id, stime)
  319. diagnose, _ := service.FindAllDiagnose(orgid)
  320. if err != nil {
  321. utils.ErrorLog(err.Error())
  322. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  323. return
  324. }
  325. return_value := make(map[string]interface{})
  326. return_value["list"] = list
  327. return_value["baseList"] = baseList
  328. return_value["patients"] = patients
  329. return_value["order"] = order
  330. return_value["numberList"] = numberList
  331. return_value["zoneList"] = zoneList
  332. return_value["adminUserES"] = adminUserES
  333. return_value["doctors"] = allDoctor
  334. return_value["prescription"] = prescriptionListByToDay
  335. return_value["diagnose"] = diagnose
  336. //if is_medicine == 1{发药时间先不展示
  337. // return_value["time"] = time
  338. //}
  339. this.ServeSuccessJSON(return_value)
  340. return
  341. }
  342. // 发药按钮点击
  343. func (this *PharmacyController) DispensingMedicine() {
  344. var err error
  345. defer func() {
  346. if rec := recover(); rec != nil {
  347. err = fmt.Errorf("程序异常:%v", rec)
  348. }
  349. if err != nil {
  350. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  351. }
  352. }()
  353. patient_id, _ := this.GetInt64("patient_id", 0)
  354. times := this.GetString("time", "")
  355. orgid := this.GetAdminUserInfo().CurrentOrgId
  356. creater := this.GetAdminUserInfo().AdminUser.Id
  357. timeLayout := "2006-01-02"
  358. loc, _ := time.LoadLocation("Local")
  359. var stime, etime int64
  360. if times == "" {
  361. stime, etime = service.GetNowTime()
  362. } else {
  363. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  364. stime = stmp.Unix()
  365. etime = stime + 86399
  366. }
  367. tmp_bool := service.IsPharmacyConfig(orgid)
  368. if tmp_bool == true {
  369. advicelist, _ := service.FindeHisAdviceDocAdvice(orgid, patient_id, stime, etime)
  370. if len(advicelist) > 0 {
  371. var total int64
  372. var prescribing_number_total int64
  373. for _, item := range advicelist {
  374. //查询改药品信息
  375. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  376. //如果是药房发药的方式
  377. if medical.IsPharmacy == 1 {
  378. houseConfig, _ := service.GetAllStoreHouseConfig(orgid)
  379. //查询该药品是否有库存
  380. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  381. //判断单位是否相等
  382. if medical.MaxUnit == item.PrescribingNumberUnit {
  383. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  384. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  385. //转化为最小单位
  386. total = list.Count*medical.MinNumber + list.StockMinNumber
  387. prescribing_number_total = count * medical.MinNumber
  388. }
  389. if medical.MinUnit == item.PrescribingNumberUnit {
  390. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  391. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  392. total = list.Count*medical.MinNumber + list.StockMinNumber
  393. prescribing_number_total = count
  394. }
  395. if medical.IsUse != 1 {
  396. //如果出库数量大于库存数量
  397. if prescribing_number_total > total {
  398. err := fmt.Errorf(service.FindDrugsName(item.DrugId) + "库存不足")
  399. if err != nil {
  400. utils.ErrorLog(err.Error())
  401. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  402. return
  403. }
  404. }
  405. }
  406. }
  407. }
  408. }
  409. }
  410. if orgid != 10480 && orgid != 10188 && orgid != 10217 && orgid != 10164 && orgid != 10666 {
  411. //发药
  412. err = service.DispensingMedicine(orgid, patient_id, stime, etime, creater)
  413. if err != nil {
  414. utils.ErrorLog(err.Error())
  415. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  416. return
  417. }
  418. this.ServeSuccessJSON(map[string]interface{}{
  419. "list": "操作成功",
  420. })
  421. return
  422. }
  423. if orgid == 10480 || orgid == 10188 || orgid == 10217 || orgid == 10164 || orgid == 10666 {
  424. //发药逻辑
  425. service.DispensingMedicineOne(orgid, patient_id, stime, etime, creater)
  426. //针对桑植盛康
  427. if orgid == 10387 || orgid == 3877 {
  428. advicelist, _ := service.FindeHisAdviceDocAdvice(orgid, patient_id, stime, etime)
  429. if len(advicelist) > 0 {
  430. for _, item := range advicelist {
  431. //查询改药品信息
  432. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  433. if medical.IsPharmacy == 1 {
  434. flow, _ := service.GetFlow(item.DrugId, item.AdviceDate, orgid, item.PatientId)
  435. if len(flow) > 0 {
  436. service.UpdateAdviceFlow(item.ID)
  437. }
  438. }
  439. }
  440. }
  441. }
  442. if err != nil {
  443. utils.ErrorLog(err.Error())
  444. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  445. return
  446. }
  447. this.ServeSuccessJSON(map[string]interface{}{
  448. "list": "操作成功",
  449. })
  450. return
  451. }
  452. }
  453. // 退药按钮点击
  454. func (this *PharmacyController) DrugWithdrawal() {
  455. var err error
  456. defer func() {
  457. if rec := recover(); rec != nil {
  458. err = fmt.Errorf("程序异常:%v", rec)
  459. }
  460. if err != nil {
  461. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  462. }
  463. }()
  464. patient_id, _ := this.GetInt64("patient_id", 0)
  465. times := this.GetString("time", "")
  466. orgid := this.GetAdminUserInfo().CurrentOrgId
  467. creater := this.GetAdminUserInfo().AdminUser.Id
  468. timeLayout := "2006-01-02"
  469. loc, _ := time.LoadLocation("Local")
  470. var stime, etime int64
  471. if times == "" {
  472. stime, etime = service.GetNowTime()
  473. } else {
  474. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  475. stime = stmp.Unix()
  476. etime = stime + 86399
  477. }
  478. err = service.DrugWithdrawal(orgid, patient_id, stime, etime, creater)
  479. if err != nil {
  480. utils.ErrorLog(err.Error())
  481. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  482. return
  483. }
  484. this.ServeSuccessJSON(map[string]interface{}{
  485. "list": "操作成功",
  486. })
  487. return
  488. }
  489. // 发药明细列表
  490. func (this *PharmacyController) DispensingDetails() {
  491. var err error
  492. defer func() {
  493. if rec := recover(); rec != nil {
  494. err = fmt.Errorf("程序异常:%v", rec)
  495. }
  496. if err != nil {
  497. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  498. }
  499. }()
  500. keyword := this.GetString("keyword", "") //患者名称
  501. start_time := this.GetString("start_time", "") //开始时间
  502. end_time := this.GetString("end_time", "") //结束时间
  503. page, _ := this.GetInt64("page", 1) //页码
  504. limit, _ := this.GetInt64("limit", 10) //每一页查出来的条数
  505. orgid := this.GetAdminUserInfo().CurrentOrgId
  506. timeLayout := "2006-01-02"
  507. loc, _ := time.LoadLocation("Local")
  508. var stime, etime int64
  509. if start_time == "" {
  510. stime = 1
  511. } else {
  512. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  513. stime = stmp.Unix()
  514. }
  515. if end_time == "" {
  516. _, etime = service.GetNowTime()
  517. } else {
  518. etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  519. etime = etmp.Unix()
  520. }
  521. var dislist []*models.DispensingList
  522. var total int64
  523. dislist, total, err = service.DispensingDetailsList(stime, etime, orgid, page, limit, keyword)
  524. if err != nil {
  525. utils.ErrorLog(err.Error())
  526. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  527. return
  528. }
  529. this.ServeSuccessJSON(map[string]interface{}{
  530. "list": dislist,
  531. "total": total,
  532. })
  533. return
  534. }
  535. // 处方详情
  536. func (this *PharmacyController) PrescriptionDetails() {
  537. var err error
  538. defer func() {
  539. if rec := recover(); rec != nil {
  540. err = fmt.Errorf("程序异常:%v", rec)
  541. }
  542. if err != nil {
  543. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  544. }
  545. }()
  546. //患者姓名
  547. patient_id, _ := this.GetInt64("patient_id", 0)
  548. //发药时间
  549. record_date, _ := this.GetInt64("record_date", 0)
  550. orgid := this.GetAdminUserInfo().CurrentOrgId
  551. if record_date == 0 || patient_id == 0 {
  552. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数不足")
  553. return
  554. }
  555. var list []*models.PrescripDetails
  556. list, err = service.PrescriptionDetails(patient_id, record_date, orgid)
  557. if err != nil {
  558. utils.ErrorLog(err.Error())
  559. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  560. return
  561. }
  562. this.ServeSuccessJSON(map[string]interface{}{
  563. "list": list,
  564. })
  565. return
  566. }
  567. // 已发药品的信息
  568. func (this *PharmacyController) DispenseMedicine() {
  569. var err error
  570. defer func() {
  571. if rec := recover(); rec != nil {
  572. err = fmt.Errorf("程序异常:%v", rec)
  573. }
  574. if err != nil {
  575. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  576. }
  577. }()
  578. keyword := this.GetString("keyword", "")
  579. times := this.GetString("time", "")
  580. orgid := this.GetAdminUserInfo().CurrentOrgId
  581. timeLayout := "2006-01-02"
  582. loc, _ := time.LoadLocation("Local")
  583. deliveryway := this.GetString("deliveryway", "")
  584. var stime, etime int64
  585. if times == "" {
  586. stime, etime = service.GetNowTime()
  587. } else {
  588. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  589. stime = stmp.Unix()
  590. etime = stime + 86399
  591. }
  592. //查询表里当天的数据
  593. var flist []*models.ListOfDrugs
  594. flist, err = service.GetTodayMedicine(stime, etime, orgid, 1, keyword)
  595. if err != nil {
  596. utils.ErrorLog(err.Error())
  597. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  598. return
  599. }
  600. llist, err := service.Administration(deliveryway, orgid, flist)
  601. if err != nil {
  602. utils.ErrorLog(err.Error())
  603. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  604. return
  605. }
  606. this.ServeSuccessJSON(map[string]interface{}{
  607. "list": llist,
  608. })
  609. return
  610. }
  611. // 待发药的药品信息
  612. func (this *PharmacyController) WaitingMedicine() {
  613. var err error
  614. defer func() {
  615. if rec := recover(); rec != nil {
  616. err = fmt.Errorf("程序异常:%v", rec)
  617. }
  618. if err != nil {
  619. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  620. }
  621. }()
  622. keyword := this.GetString("keyword", "")
  623. times := this.GetString("time", "")
  624. orgid := this.GetAdminUserInfo().CurrentOrgId
  625. timeLayout := "2006-01-02"
  626. loc, _ := time.LoadLocation("Local")
  627. deliveryway := this.GetString("deliveryway", "")
  628. var stime, etime int64
  629. if times == "" {
  630. stime, etime = service.GetNowTime()
  631. } else {
  632. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  633. stime = stmp.Unix()
  634. etime = stime + 86399
  635. }
  636. //查询表里当天的数据
  637. var flist []*models.ListOfDrugs
  638. flist, err = service.GetTodayMedicine(stime, etime, orgid, 0, keyword)
  639. if err != nil {
  640. utils.ErrorLog(err.Error())
  641. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  642. return
  643. }
  644. llist, err := service.Administration(deliveryway, orgid, flist)
  645. if err != nil {
  646. utils.ErrorLog(err.Error())
  647. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  648. return
  649. }
  650. this.ServeSuccessJSON(map[string]interface{}{
  651. "list": llist,
  652. })
  653. return
  654. }
  655. // 获取药品的所有患者信息
  656. func (this *PharmacyController) GetPatientsWithDrugs() {
  657. var err error
  658. defer func() {
  659. if rec := recover(); rec != nil {
  660. err = fmt.Errorf("程序异常:%v", rec)
  661. }
  662. if err != nil {
  663. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  664. }
  665. }()
  666. drug_id, _ := this.GetInt64("drug_id", 0)
  667. is_medicine, _ := this.GetInt64("is_medicine", 0) //0:待发药,1:已发药
  668. times := this.GetString("time", "")
  669. orgid := this.GetAdminUserInfo().CurrentOrgId
  670. deliveryway := this.GetString("deliveryway", "")
  671. if deliveryway == "" {
  672. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  673. return
  674. }
  675. shift, err := this.GetInt64("shift", 0) //班次
  676. if err != nil {
  677. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  678. return
  679. }
  680. partition, err := this.GetInt64("partition", 0) //分区
  681. if err != nil {
  682. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  683. return
  684. }
  685. timeLayout := "2006-01-02"
  686. loc, _ := time.LoadLocation("Local")
  687. var stime, etime int64
  688. if times == "" {
  689. stime, etime = service.GetNowTime()
  690. } else {
  691. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
  692. stime = stmp.Unix()
  693. etime = stime + 86399
  694. }
  695. var list []*models.PatientInformation
  696. list, err = service.FindMedicationList(orgid, drug_id, stime, etime, is_medicine)
  697. if err != nil {
  698. utils.ErrorLog(err.Error())
  699. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  700. return
  701. }
  702. listll, err := service.PartitionAndLayoutDrug(deliveryway, stime, etime, orgid, shift, partition, list)
  703. if err != nil {
  704. utils.ErrorLog(err.Error())
  705. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  706. return
  707. }
  708. total, err := service.CalculateTheTotalAmount(listll, drug_id)
  709. if err != nil {
  710. utils.ErrorLog(err.Error())
  711. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  712. return
  713. }
  714. this.ServeSuccessJSON(map[string]interface{}{
  715. "list": listll,
  716. "total": total,
  717. })
  718. return
  719. }
  720. // 药品管理发药按钮点击
  721. func (this *PharmacyController) MedicineDeparture() {
  722. var err error
  723. defer func() {
  724. if rec := recover(); rec != nil {
  725. err = fmt.Errorf("程序异常:%v", rec)
  726. }
  727. if err != nil {
  728. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  729. }
  730. }()
  731. creater, _ := this.GetInt64("creater", 0) //领药人
  732. ids := this.GetString("ids", "") //发药的数据
  733. orgid := this.GetAdminUserInfo().CurrentOrgId
  734. if ids == "" {
  735. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数不足")
  736. return
  737. }
  738. err = service.MedicineDeparture(ids, creater, orgid)
  739. if err != nil {
  740. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  741. return
  742. }
  743. this.ServeSuccessJSON(map[string]interface{}{
  744. "list": "修改成功",
  745. })
  746. return
  747. }
  748. func (this *PharmacyController) GetPartitionList() {
  749. var err error
  750. defer func() {
  751. if rec := recover(); rec != nil {
  752. err = fmt.Errorf("程序异常:%v", rec)
  753. }
  754. if err != nil {
  755. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  756. }
  757. }()
  758. orgid := this.GetAdminUserInfo().CurrentOrgId
  759. tmp := []*models.DeviceZone{{ID: 0, Name: "全部分区"}}
  760. list, err := service.GetAllValidDeviceZones02(orgid)
  761. if err != nil {
  762. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  763. return
  764. }
  765. this.ServeSuccessJSON(map[string]interface{}{
  766. "list": append(tmp, list...),
  767. })
  768. return
  769. }
  770. func (this *PharmacyController) RouteOfAdministration() {
  771. var err error
  772. defer func() {
  773. if rec := recover(); rec != nil {
  774. err = fmt.Errorf("程序异常:%v", rec)
  775. }
  776. if err != nil {
  777. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  778. }
  779. }()
  780. orgid := this.GetAdminUserInfo().CurrentOrgId
  781. tmp := []*models.DrugwayDic{{ID: 0, Name: "全部"}}
  782. list, _, err := service.GetDrugWayDics(orgid)
  783. if err != nil {
  784. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  785. return
  786. }
  787. this.ServeSuccessJSON(map[string]interface{}{
  788. "list": append(tmp, list...),
  789. })
  790. return
  791. }
  792. func (this *PharmacyController) GetGoodInventoryList() {
  793. keyword := this.GetString("keywords")
  794. orgId := this.GetAdminUserInfo().CurrentOrgId
  795. list, _ := service.GetGoodInventoryList(keyword, orgId)
  796. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  797. this.ServeSuccessJSON(map[string]interface{}{
  798. "list": list,
  799. "manufacturerList": manufacturerList,
  800. })
  801. return
  802. }
  803. func (this *PharmacyController) GetGoodInventoryByGoodId() {
  804. id, _ := this.GetInt64("id")
  805. infoList, _ := service.GetGoodInventoryByGoodId(id)
  806. this.ServeSuccessJSON(map[string]interface{}{
  807. "infoList": infoList,
  808. })
  809. }
  810. func (this *PharmacyController) UpdateInventoryWarehouseInfo() {
  811. id, _ := this.GetInt64("id")
  812. stock_count, _ := this.GetInt64("stock_count")
  813. last_stock_count, _ := this.GetInt64("last_stock_count")
  814. good_id, _ := this.GetInt64("good_id")
  815. orgId := this.GetAdminUserInfo().CurrentOrgId
  816. storehouse_id, _ := this.GetInt64("storehouse_id")
  817. is_type, _ := this.GetInt64("is_type")
  818. warehouseInfo, _ := service.GetInventoryWarehouseInfo(id)
  819. goodInfo, _ := service.GetGoodInformationByGoodIdThirty(good_id)
  820. manufacturer, _ := service.GetManufactureById(warehouseInfo.Manufacturer)
  821. //更新库存
  822. service.UpdateWarehouseInfoById(last_stock_count, id)
  823. recordDateStr := time.Now().Format("2006-01-02")
  824. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  825. goodListOne, _ := service.GetSumGoodList(orgId, storehouse_id, good_id)
  826. var flush_count int64
  827. for _, it := range goodListOne {
  828. flush_count += it.StockCount
  829. }
  830. //盘盈
  831. if last_stock_count > stock_count {
  832. var total_count int64
  833. total_count = last_stock_count - stock_count
  834. creater := this.GetAdminUserInfo().AdminUser.Id
  835. inventory := models.XtStockInventory{
  836. GoodName: goodInfo.GoodName,
  837. SpecificationName: goodInfo.SpecificationName,
  838. WarehousingUnit: goodInfo.PackingUnit,
  839. Count: total_count,
  840. BuyPrice: goodInfo.BuyPrice,
  841. PackingPrice: goodInfo.PackingPrice,
  842. NewPrice: 0,
  843. Manufacturer: manufacturer.ManufacturerName,
  844. Dealer: "",
  845. Remark: "",
  846. GoodId: good_id,
  847. UserOrgId: orgId,
  848. Ctime: time.Now().Unix(),
  849. Mtime: 0,
  850. Status: 1,
  851. WarehousingOrder: warehouseInfo.WarehousingOrder,
  852. LicenseNumber: warehouseInfo.LicenseNumber,
  853. StartTime: time.Now().Unix(),
  854. Creater: creater,
  855. Checker: 0,
  856. CheckerStatus: 0,
  857. CheckerTime: 0,
  858. Total: 0,
  859. Number: warehouseInfo.Number,
  860. WarehousingInfoId: id,
  861. ExpireDate: warehouseInfo.ExpiryDate,
  862. ProductDate: warehouseInfo.ProductDate,
  863. GoodOriginPlace: "",
  864. Type: 10,
  865. InventoryType: is_type,
  866. LastStockCount: last_stock_count,
  867. StockCount: stock_count,
  868. StorehouseId: storehouse_id,
  869. RecordDate: recordDate.Unix(),
  870. }
  871. service.CreateInentory(inventory)
  872. stockFlow := models.VmStockFlow{
  873. WarehousingId: 0,
  874. GoodId: good_id,
  875. Number: warehouseInfo.Number,
  876. LicenseNumber: "",
  877. Count: total_count,
  878. UserOrgId: orgId,
  879. PatientId: 0,
  880. SystemTime: recordDate.Unix(),
  881. ConsumableType: 10,
  882. IsSys: 0,
  883. WarehousingOrder: warehouseInfo.WarehousingOrder,
  884. WarehouseOutId: 0,
  885. WarehouseOutOrderNumber: "",
  886. IsEdit: 0,
  887. CancelStockId: 0,
  888. CancelOrderNumber: "",
  889. Manufacturer: warehouseInfo.Manufacturer,
  890. Dealer: 0,
  891. Creator: creater,
  892. UpdateCreator: 0,
  893. Status: 1,
  894. Ctime: time.Now().Unix(),
  895. Mtime: 0,
  896. Price: warehouseInfo.Price,
  897. WarehousingDetailId: 0,
  898. WarehouseOutDetailId: 0,
  899. CancelOutDetailId: 0,
  900. ProductDate: warehouseInfo.ProductDate,
  901. ExpireDate: warehouseInfo.ExpiryDate,
  902. ReturnCount: 0,
  903. StorehouseId: storehouse_id,
  904. OverCount: flush_count,
  905. }
  906. service.CreateStockFlowOne(stockFlow)
  907. service.ReduceSumOutCount(good_id, total_count, storehouse_id, orgId)
  908. }
  909. //盘亏
  910. if last_stock_count < stock_count {
  911. var total_count int64
  912. total_count = stock_count - last_stock_count
  913. creater := this.GetAdminUserInfo().AdminUser.Id
  914. inventory := models.XtStockInventory{
  915. GoodName: goodInfo.GoodName,
  916. SpecificationName: goodInfo.SpecificationName,
  917. WarehousingUnit: goodInfo.PackingUnit,
  918. Count: total_count,
  919. BuyPrice: goodInfo.BuyPrice,
  920. PackingPrice: goodInfo.PackingPrice,
  921. NewPrice: 0,
  922. Manufacturer: manufacturer.ManufacturerName,
  923. Dealer: "",
  924. Remark: "",
  925. GoodId: good_id,
  926. UserOrgId: orgId,
  927. Ctime: time.Now().Unix(),
  928. Mtime: 0,
  929. Status: 1,
  930. WarehousingOrder: warehouseInfo.WarehousingOrder,
  931. LicenseNumber: warehouseInfo.LicenseNumber,
  932. StartTime: time.Now().Unix(),
  933. Creater: creater,
  934. Checker: 0,
  935. CheckerStatus: 0,
  936. CheckerTime: 0,
  937. Total: 0,
  938. Number: warehouseInfo.Number,
  939. WarehousingInfoId: id,
  940. ExpireDate: warehouseInfo.ExpiryDate,
  941. ProductDate: warehouseInfo.ProductDate,
  942. GoodOriginPlace: "",
  943. Type: 10,
  944. InventoryType: is_type,
  945. LastStockCount: last_stock_count,
  946. StockCount: stock_count,
  947. StorehouseId: storehouse_id,
  948. RecordDate: recordDate.Unix(),
  949. }
  950. service.CreateInentory(inventory)
  951. stockFlow := models.VmStockFlow{
  952. WarehousingId: 0,
  953. GoodId: good_id,
  954. Number: warehouseInfo.Number,
  955. LicenseNumber: "",
  956. Count: total_count,
  957. UserOrgId: orgId,
  958. PatientId: 0,
  959. SystemTime: recordDate.Unix(),
  960. ConsumableType: 11,
  961. IsSys: 0,
  962. WarehousingOrder: warehouseInfo.WarehousingOrder,
  963. WarehouseOutId: 0,
  964. WarehouseOutOrderNumber: "",
  965. IsEdit: 0,
  966. CancelStockId: 0,
  967. CancelOrderNumber: "",
  968. Manufacturer: warehouseInfo.Manufacturer,
  969. Dealer: 0,
  970. Creator: creater,
  971. UpdateCreator: 0,
  972. Status: 1,
  973. Ctime: time.Now().Unix(),
  974. Mtime: 0,
  975. Price: warehouseInfo.Price,
  976. WarehousingDetailId: 0,
  977. WarehouseOutDetailId: 0,
  978. CancelOutDetailId: 0,
  979. ProductDate: warehouseInfo.ProductDate,
  980. ExpireDate: warehouseInfo.ExpiryDate,
  981. ReturnCount: 0,
  982. StorehouseId: storehouse_id,
  983. OverCount: flush_count,
  984. }
  985. service.CreateStockFlowOne(stockFlow)
  986. service.ReduceSumInCount(good_id, total_count, storehouse_id, orgId)
  987. }
  988. //查询已审核单据的剩余库存
  989. goodList, _ := service.GetGoodSumCountByStoreId(storehouse_id, good_id, orgId)
  990. var sum_count int64
  991. var sum_in_count int64
  992. for _, item := range goodList {
  993. sum_count += item.StockCount
  994. sum_in_count += item.WarehousingCount
  995. }
  996. service.UpdateGoodByGoodId(good_id, sum_count, sum_in_count, orgId)
  997. service.UpdateSumGood(orgId, storehouse_id, good_id, flush_count)
  998. this.ServeSuccessJSON(map[string]interface{}{
  999. "msg": "msg",
  1000. })
  1001. }
  1002. func (this *PharmacyController) GetDrugInventoryList() {
  1003. keywords := this.GetString("keywords")
  1004. orgId := this.GetAdminUserInfo().CurrentOrgId
  1005. drugList, _ := service.GetDrugNewInventoryList(orgId, keywords)
  1006. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  1007. this.ServeSuccessJSON(map[string]interface{}{
  1008. "drugList": drugList,
  1009. "manufacturerList": manufacturerList,
  1010. })
  1011. }
  1012. func (this *PharmacyController) GetDrugInventoryByDrugId() {
  1013. orgId := this.GetAdminUserInfo().CurrentOrgId
  1014. id, _ := this.GetInt64("id")
  1015. infoList, _ := service.GetDrugInvetoryByDrugId(orgId, id)
  1016. this.ServeSuccessJSON(map[string]interface{}{
  1017. "infoList": infoList,
  1018. })
  1019. }
  1020. func (this *PharmacyController) UpdateDrugInventoryWarehouseInfo() {
  1021. id, _ := this.GetInt64("id")
  1022. stock_max_number, _ := this.GetInt64("stock_max_number")
  1023. stock_min_number, _ := this.GetInt64("stock_min_number")
  1024. last_stock_max_number, _ := this.GetInt64("last_stock_max_number")
  1025. last_stock_min_number, _ := this.GetInt64("last_stock_min_number")
  1026. drug_id, _ := this.GetInt64("drug_id")
  1027. storehouse_id, _ := this.GetInt64("storehouse_id")
  1028. is_type, _ := this.GetInt64("is_type")
  1029. specification_name := this.GetString("specification_name")
  1030. orgId := this.GetAdminUserInfo().CurrentOrgId
  1031. base, _ := service.GetBasedrugByIdOne(drug_id, orgId)
  1032. var ord_total int64
  1033. var new_total int64
  1034. var consumable_count int64
  1035. ord_total = stock_max_number*base.MinNumber + stock_min_number
  1036. new_total = last_stock_max_number*base.MinNumber + last_stock_min_number
  1037. //更新入库单
  1038. service.UpdateNewDrugWarehouseInfoById(id, last_stock_max_number, last_stock_min_number)
  1039. //查询默认仓库
  1040. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  1041. //查询默认仓库剩余多少库存
  1042. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgId, drug_id)
  1043. var sum_count int64
  1044. for _, it := range list {
  1045. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1046. if it.MaxUnit == baseDrug.MaxUnit {
  1047. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1048. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1049. }
  1050. sum_count += it.StockMaxNumber + it.StockMinNumber
  1051. }
  1052. //更新剩余库存
  1053. service.UpdateMedicalSumCountOne(drug_id, sum_count, orgId)
  1054. //更新剩余库存
  1055. service.UpdateDrugStockCount(drug_id, orgId, houseConfig.DrugStorehouseOut, sum_count)
  1056. drugWarehouseInfoOne, _ := service.GetDrugWarehouseInfoOne(id)
  1057. manufacturer, _ := service.GetManufactureById(drugWarehouseInfoOne.Manufacturer)
  1058. recordDateStr := time.Now().Format("2006-01-02")
  1059. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  1060. fmt.Println("ord_total-----------------------------", ord_total)
  1061. fmt.Println("new_total-----------------------------", new_total)
  1062. //盘亏
  1063. if ord_total > new_total {
  1064. consumable_count = ord_total - new_total
  1065. inventory := models.XtDrugInventory{
  1066. DrugName: base.DrugName,
  1067. SpecificationName: specification_name,
  1068. WarehousingUnit: drugWarehouseInfoOne.MaxUnit,
  1069. Count: consumable_count,
  1070. LastPrice: drugWarehouseInfoOne.Price,
  1071. RetailPrice: drugWarehouseInfoOne.RetailPrice,
  1072. NewPrice: 0,
  1073. Manufacturer: manufacturer.ManufacturerName,
  1074. Dealer: "",
  1075. Remark: "",
  1076. DrugId: drug_id,
  1077. UserOrgId: orgId,
  1078. Ctime: time.Now().Unix(),
  1079. Mtime: 0,
  1080. Status: 1,
  1081. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1082. LicenseNumber: "",
  1083. StartTime: recordDate.Unix(),
  1084. Creater: this.GetAdminUserInfo().AdminUser.Id,
  1085. Checker: 0,
  1086. CheckerStatus: 0,
  1087. CheckerTime: 0,
  1088. ExpiryDate: drugWarehouseInfoOne.ExpiryDate,
  1089. ProductDate: drugWarehouseInfoOne.ProductDate,
  1090. Number: "",
  1091. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1092. Total: "",
  1093. DrugOriginPlace: "",
  1094. WarehouseInfoId: id,
  1095. ProofCount: 0,
  1096. StockMaxNumber: stock_max_number,
  1097. StockMinNumber: stock_min_number,
  1098. MinCount: 0,
  1099. MinUnit: drugWarehouseInfoOne.MinUnit,
  1100. LastStockMaxNumber: last_stock_max_number,
  1101. LastStockMinNumber: last_stock_min_number,
  1102. InventoryType: 11,
  1103. Type: is_type,
  1104. StorehouseId: storehouse_id,
  1105. RecordDate: recordDate.Unix(),
  1106. }
  1107. service.CreateDrugInventory(inventory)
  1108. flow := models.DrugFlow{
  1109. WarehousingId: id,
  1110. DrugId: drug_id,
  1111. Number: "",
  1112. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1113. Count: consumable_count,
  1114. UserOrgId: orgId,
  1115. PatientId: 0,
  1116. SystemTime: recordDate.Unix(),
  1117. ConsumableType: 11,
  1118. IsSys: 1,
  1119. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1120. WarehouseOutId: 0,
  1121. WarehouseOutOrderNumber: "",
  1122. IsEdit: 0,
  1123. CancelStockId: 0,
  1124. CancelOrderNumber: "",
  1125. Manufacturer: manufacturer.ID,
  1126. Dealer: 0,
  1127. Creator: this.GetAdminUserInfo().AdminUser.Id,
  1128. UpdateCreator: 0,
  1129. Status: 1,
  1130. Ctime: time.Now().Unix(),
  1131. Mtime: 0,
  1132. Price: drugWarehouseInfoOne.Price,
  1133. WarehousingDetailId: id,
  1134. WarehouseOutDetailId: 0,
  1135. CancelOutDetailId: 0,
  1136. ExpireDate: drugWarehouseInfoOne.ExpiryDate,
  1137. ProductDate: 0,
  1138. MaxUnit: drugWarehouseInfoOne.MaxUnit,
  1139. MinUnit: drugWarehouseInfoOne.MinUnit,
  1140. StorehouseId: storehouse_id,
  1141. OverCount: sum_count,
  1142. }
  1143. service.CreateDrugFlowOne(flow)
  1144. //添加出库数据
  1145. service.AddDrugWarehouseOut(drug_id, consumable_count, storehouse_id, orgId)
  1146. }
  1147. //盘盈
  1148. if ord_total < new_total {
  1149. consumable_count = new_total - ord_total
  1150. inventory := models.XtDrugInventory{
  1151. DrugName: base.DrugName,
  1152. SpecificationName: specification_name,
  1153. WarehousingUnit: drugWarehouseInfoOne.MaxUnit,
  1154. Count: consumable_count,
  1155. LastPrice: drugWarehouseInfoOne.Price,
  1156. RetailPrice: drugWarehouseInfoOne.RetailPrice,
  1157. NewPrice: 0,
  1158. Manufacturer: manufacturer.ManufacturerName,
  1159. Dealer: "",
  1160. Remark: "",
  1161. DrugId: drug_id,
  1162. UserOrgId: orgId,
  1163. Ctime: time.Now().Unix(),
  1164. Mtime: 0,
  1165. Status: 1,
  1166. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1167. LicenseNumber: "",
  1168. StartTime: recordDate.Unix(),
  1169. Creater: this.GetAdminUserInfo().AdminUser.Id,
  1170. Checker: 0,
  1171. CheckerStatus: 0,
  1172. CheckerTime: 0,
  1173. ExpiryDate: drugWarehouseInfoOne.ExpiryDate,
  1174. ProductDate: drugWarehouseInfoOne.ProductDate,
  1175. Number: "",
  1176. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1177. Total: "",
  1178. DrugOriginPlace: "",
  1179. WarehouseInfoId: id,
  1180. ProofCount: 0,
  1181. StockMaxNumber: stock_max_number,
  1182. StockMinNumber: stock_min_number,
  1183. MinCount: 0,
  1184. MinUnit: drugWarehouseInfoOne.MinUnit,
  1185. LastStockMaxNumber: last_stock_max_number,
  1186. LastStockMinNumber: last_stock_min_number,
  1187. InventoryType: 10,
  1188. Type: is_type,
  1189. StorehouseId: storehouse_id,
  1190. RecordDate: recordDate.Unix(),
  1191. }
  1192. service.CreateDrugInventory(inventory)
  1193. flow := models.DrugFlow{
  1194. WarehousingId: id,
  1195. DrugId: drug_id,
  1196. Number: "",
  1197. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1198. Count: consumable_count,
  1199. UserOrgId: orgId,
  1200. PatientId: 0,
  1201. SystemTime: recordDate.Unix(),
  1202. ConsumableType: 10,
  1203. IsSys: 1,
  1204. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1205. WarehouseOutId: 0,
  1206. WarehouseOutOrderNumber: "",
  1207. IsEdit: 0,
  1208. CancelStockId: 0,
  1209. CancelOrderNumber: "",
  1210. Manufacturer: manufacturer.ID,
  1211. Dealer: 0,
  1212. Creator: this.GetAdminUserInfo().AdminUser.Id,
  1213. UpdateCreator: 0,
  1214. Status: 1,
  1215. Ctime: time.Now().Unix(),
  1216. Mtime: 0,
  1217. Price: drugWarehouseInfoOne.Price,
  1218. WarehousingDetailId: id,
  1219. WarehouseOutDetailId: 0,
  1220. CancelOutDetailId: 0,
  1221. ExpireDate: drugWarehouseInfoOne.ExpiryDate,
  1222. ProductDate: 0,
  1223. MaxUnit: drugWarehouseInfoOne.MaxUnit,
  1224. MinUnit: drugWarehouseInfoOne.MinUnit,
  1225. StorehouseId: storehouse_id,
  1226. OverCount: sum_count,
  1227. }
  1228. service.CreateDrugFlowOne(flow)
  1229. //减少出库数据
  1230. service.ReduceDrugWarehouseOut(drug_id, consumable_count, storehouse_id, orgId)
  1231. }
  1232. this.ServeSuccessJSON(map[string]interface{}{
  1233. "msg": "msg",
  1234. })
  1235. }