pharmacy_controller.go 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. fmt.Println("list1---------------------------", listll)
  704. if err != nil {
  705. utils.ErrorLog(err.Error())
  706. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  707. return
  708. }
  709. total, err := service.CalculateTheTotalAmount(listll, drug_id)
  710. if err != nil {
  711. utils.ErrorLog(err.Error())
  712. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  713. return
  714. }
  715. this.ServeSuccessJSON(map[string]interface{}{
  716. "list": listll,
  717. "total": total,
  718. })
  719. return
  720. }
  721. // 药品管理发药按钮点击
  722. func (this *PharmacyController) MedicineDeparture() {
  723. var err error
  724. defer func() {
  725. if rec := recover(); rec != nil {
  726. err = fmt.Errorf("程序异常:%v", rec)
  727. }
  728. if err != nil {
  729. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  730. }
  731. }()
  732. creater, _ := this.GetInt64("creater", 0) //领药人
  733. ids := this.GetString("ids", "") //发药的数据
  734. orgid := this.GetAdminUserInfo().CurrentOrgId
  735. if ids == "" {
  736. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数不足")
  737. return
  738. }
  739. err = service.MedicineDeparture(ids, creater, orgid)
  740. if err != nil {
  741. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  742. return
  743. }
  744. this.ServeSuccessJSON(map[string]interface{}{
  745. "list": "修改成功",
  746. })
  747. return
  748. }
  749. func (this *PharmacyController) GetPartitionList() {
  750. var err error
  751. defer func() {
  752. if rec := recover(); rec != nil {
  753. err = fmt.Errorf("程序异常:%v", rec)
  754. }
  755. if err != nil {
  756. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  757. }
  758. }()
  759. orgid := this.GetAdminUserInfo().CurrentOrgId
  760. tmp := []*models.DeviceZone{{ID: 0, Name: "全部分区"}}
  761. list, err := service.GetAllValidDeviceZones02(orgid)
  762. if err != nil {
  763. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  764. return
  765. }
  766. this.ServeSuccessJSON(map[string]interface{}{
  767. "list": append(tmp, list...),
  768. })
  769. return
  770. }
  771. func (this *PharmacyController) RouteOfAdministration() {
  772. var err error
  773. defer func() {
  774. if rec := recover(); rec != nil {
  775. err = fmt.Errorf("程序异常:%v", rec)
  776. }
  777. if err != nil {
  778. service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
  779. }
  780. }()
  781. orgid := this.GetAdminUserInfo().CurrentOrgId
  782. tmp := []*models.DrugwayDic{{ID: 0, Name: "全部"}}
  783. list, _, err := service.GetDrugWayDics(orgid)
  784. if err != nil {
  785. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  786. return
  787. }
  788. this.ServeSuccessJSON(map[string]interface{}{
  789. "list": append(tmp, list...),
  790. })
  791. return
  792. }
  793. func (this *PharmacyController) GetGoodInventoryList() {
  794. keyword := this.GetString("keywords")
  795. orgId := this.GetAdminUserInfo().CurrentOrgId
  796. list, _ := service.GetGoodInventoryList(keyword, orgId)
  797. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  798. this.ServeSuccessJSON(map[string]interface{}{
  799. "list": list,
  800. "manufacturerList": manufacturerList,
  801. })
  802. return
  803. }
  804. func (this *PharmacyController) GetGoodInventoryByGoodId() {
  805. id, _ := this.GetInt64("id")
  806. infoList, _ := service.GetGoodInventoryByGoodId(id)
  807. this.ServeSuccessJSON(map[string]interface{}{
  808. "infoList": infoList,
  809. })
  810. }
  811. func (this *PharmacyController) UpdateInventoryWarehouseInfo() {
  812. id, _ := this.GetInt64("id")
  813. stock_count, _ := this.GetInt64("stock_count")
  814. last_stock_count, _ := this.GetInt64("last_stock_count")
  815. good_id, _ := this.GetInt64("good_id")
  816. orgId := this.GetAdminUserInfo().CurrentOrgId
  817. storehouse_id, _ := this.GetInt64("storehouse_id")
  818. is_type, _ := this.GetInt64("is_type")
  819. warehouseInfo, _ := service.GetInventoryWarehouseInfo(id)
  820. goodInfo, _ := service.GetGoodInformationByGoodIdThirty(good_id)
  821. manufacturer, _ := service.GetManufactureById(warehouseInfo.Manufacturer)
  822. //更新库存
  823. service.UpdateWarehouseInfoById(last_stock_count, id)
  824. recordDateStr := time.Now().Format("2006-01-02")
  825. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  826. goodListOne, _ := service.GetSumGoodList(orgId, storehouse_id, good_id)
  827. var flush_count int64
  828. for _, it := range goodListOne {
  829. flush_count += it.StockCount
  830. }
  831. //盘盈
  832. if last_stock_count > stock_count {
  833. var total_count int64
  834. total_count = last_stock_count - stock_count
  835. creater := this.GetAdminUserInfo().AdminUser.Id
  836. inventory := models.XtStockInventory{
  837. GoodName: goodInfo.GoodName,
  838. SpecificationName: goodInfo.SpecificationName,
  839. WarehousingUnit: goodInfo.PackingUnit,
  840. Count: total_count,
  841. BuyPrice: goodInfo.BuyPrice,
  842. PackingPrice: goodInfo.PackingPrice,
  843. NewPrice: 0,
  844. Manufacturer: manufacturer.ManufacturerName,
  845. Dealer: "",
  846. Remark: "",
  847. GoodId: good_id,
  848. UserOrgId: orgId,
  849. Ctime: time.Now().Unix(),
  850. Mtime: 0,
  851. Status: 1,
  852. WarehousingOrder: warehouseInfo.WarehousingOrder,
  853. LicenseNumber: warehouseInfo.LicenseNumber,
  854. StartTime: time.Now().Unix(),
  855. Creater: creater,
  856. Checker: 0,
  857. CheckerStatus: 0,
  858. CheckerTime: 0,
  859. Total: 0,
  860. Number: warehouseInfo.Number,
  861. WarehousingInfoId: id,
  862. ExpireDate: warehouseInfo.ExpiryDate,
  863. ProductDate: warehouseInfo.ProductDate,
  864. GoodOriginPlace: "",
  865. Type: 10,
  866. InventoryType: is_type,
  867. LastStockCount: last_stock_count,
  868. StockCount: stock_count,
  869. StorehouseId: storehouse_id,
  870. RecordDate: recordDate.Unix(),
  871. }
  872. service.CreateInentory(inventory)
  873. stockFlow := models.VmStockFlow{
  874. WarehousingId: 0,
  875. GoodId: good_id,
  876. Number: warehouseInfo.Number,
  877. LicenseNumber: "",
  878. Count: total_count,
  879. UserOrgId: orgId,
  880. PatientId: 0,
  881. SystemTime: recordDate.Unix(),
  882. ConsumableType: 10,
  883. IsSys: 0,
  884. WarehousingOrder: warehouseInfo.WarehousingOrder,
  885. WarehouseOutId: 0,
  886. WarehouseOutOrderNumber: "",
  887. IsEdit: 0,
  888. CancelStockId: 0,
  889. CancelOrderNumber: "",
  890. Manufacturer: warehouseInfo.Manufacturer,
  891. Dealer: 0,
  892. Creator: creater,
  893. UpdateCreator: 0,
  894. Status: 1,
  895. Ctime: time.Now().Unix(),
  896. Mtime: 0,
  897. Price: warehouseInfo.Price,
  898. WarehousingDetailId: 0,
  899. WarehouseOutDetailId: 0,
  900. CancelOutDetailId: 0,
  901. ProductDate: warehouseInfo.ProductDate,
  902. ExpireDate: warehouseInfo.ExpiryDate,
  903. ReturnCount: 0,
  904. StorehouseId: storehouse_id,
  905. OverCount: flush_count,
  906. }
  907. service.CreateStockFlowOne(stockFlow)
  908. service.ReduceSumOutCount(good_id, total_count, storehouse_id, orgId)
  909. }
  910. //盘亏
  911. if last_stock_count < stock_count {
  912. var total_count int64
  913. total_count = stock_count - last_stock_count
  914. creater := this.GetAdminUserInfo().AdminUser.Id
  915. inventory := models.XtStockInventory{
  916. GoodName: goodInfo.GoodName,
  917. SpecificationName: goodInfo.SpecificationName,
  918. WarehousingUnit: goodInfo.PackingUnit,
  919. Count: total_count,
  920. BuyPrice: goodInfo.BuyPrice,
  921. PackingPrice: goodInfo.PackingPrice,
  922. NewPrice: 0,
  923. Manufacturer: manufacturer.ManufacturerName,
  924. Dealer: "",
  925. Remark: "",
  926. GoodId: good_id,
  927. UserOrgId: orgId,
  928. Ctime: time.Now().Unix(),
  929. Mtime: 0,
  930. Status: 1,
  931. WarehousingOrder: warehouseInfo.WarehousingOrder,
  932. LicenseNumber: warehouseInfo.LicenseNumber,
  933. StartTime: time.Now().Unix(),
  934. Creater: creater,
  935. Checker: 0,
  936. CheckerStatus: 0,
  937. CheckerTime: 0,
  938. Total: 0,
  939. Number: warehouseInfo.Number,
  940. WarehousingInfoId: id,
  941. ExpireDate: warehouseInfo.ExpiryDate,
  942. ProductDate: warehouseInfo.ProductDate,
  943. GoodOriginPlace: "",
  944. Type: 10,
  945. InventoryType: is_type,
  946. LastStockCount: last_stock_count,
  947. StockCount: stock_count,
  948. StorehouseId: storehouse_id,
  949. RecordDate: recordDate.Unix(),
  950. }
  951. service.CreateInentory(inventory)
  952. stockFlow := models.VmStockFlow{
  953. WarehousingId: 0,
  954. GoodId: good_id,
  955. Number: warehouseInfo.Number,
  956. LicenseNumber: "",
  957. Count: total_count,
  958. UserOrgId: orgId,
  959. PatientId: 0,
  960. SystemTime: recordDate.Unix(),
  961. ConsumableType: 11,
  962. IsSys: 0,
  963. WarehousingOrder: warehouseInfo.WarehousingOrder,
  964. WarehouseOutId: 0,
  965. WarehouseOutOrderNumber: "",
  966. IsEdit: 0,
  967. CancelStockId: 0,
  968. CancelOrderNumber: "",
  969. Manufacturer: warehouseInfo.Manufacturer,
  970. Dealer: 0,
  971. Creator: creater,
  972. UpdateCreator: 0,
  973. Status: 1,
  974. Ctime: time.Now().Unix(),
  975. Mtime: 0,
  976. Price: warehouseInfo.Price,
  977. WarehousingDetailId: 0,
  978. WarehouseOutDetailId: 0,
  979. CancelOutDetailId: 0,
  980. ProductDate: warehouseInfo.ProductDate,
  981. ExpireDate: warehouseInfo.ExpiryDate,
  982. ReturnCount: 0,
  983. StorehouseId: storehouse_id,
  984. OverCount: flush_count,
  985. }
  986. service.CreateStockFlowOne(stockFlow)
  987. service.ReduceSumInCount(good_id, total_count, storehouse_id, orgId)
  988. }
  989. //查询已审核单据的剩余库存
  990. goodList, _ := service.GetGoodSumCountByStoreId(storehouse_id, good_id, orgId)
  991. var sum_count int64
  992. var sum_in_count int64
  993. for _, item := range goodList {
  994. sum_count += item.StockCount
  995. sum_in_count += item.WarehousingCount
  996. }
  997. service.UpdateGoodByGoodId(good_id, sum_count, sum_in_count, orgId)
  998. service.UpdateSumGood(orgId, storehouse_id, good_id, flush_count)
  999. this.ServeSuccessJSON(map[string]interface{}{
  1000. "msg": "msg",
  1001. })
  1002. }
  1003. func (this *PharmacyController) GetDrugInventoryList() {
  1004. keywords := this.GetString("keywords")
  1005. orgId := this.GetAdminUserInfo().CurrentOrgId
  1006. drugList, _ := service.GetDrugNewInventoryList(orgId, keywords)
  1007. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  1008. this.ServeSuccessJSON(map[string]interface{}{
  1009. "drugList": drugList,
  1010. "manufacturerList": manufacturerList,
  1011. })
  1012. }
  1013. func (this *PharmacyController) GetDrugInventoryByDrugId() {
  1014. orgId := this.GetAdminUserInfo().CurrentOrgId
  1015. id, _ := this.GetInt64("id")
  1016. infoList, _ := service.GetDrugInvetoryByDrugId(orgId, id)
  1017. this.ServeSuccessJSON(map[string]interface{}{
  1018. "infoList": infoList,
  1019. })
  1020. }
  1021. func (this *PharmacyController) UpdateDrugInventoryWarehouseInfo() {
  1022. id, _ := this.GetInt64("id")
  1023. stock_max_number, _ := this.GetInt64("stock_max_number")
  1024. stock_min_number, _ := this.GetInt64("stock_min_number")
  1025. last_stock_max_number, _ := this.GetInt64("last_stock_max_number")
  1026. last_stock_min_number, _ := this.GetInt64("last_stock_min_number")
  1027. drug_id, _ := this.GetInt64("drug_id")
  1028. storehouse_id, _ := this.GetInt64("storehouse_id")
  1029. is_type, _ := this.GetInt64("is_type")
  1030. specification_name := this.GetString("specification_name")
  1031. orgId := this.GetAdminUserInfo().CurrentOrgId
  1032. base, _ := service.GetBasedrugByIdOne(drug_id, orgId)
  1033. var ord_total int64
  1034. var new_total int64
  1035. var consumable_count int64
  1036. ord_total = stock_max_number*base.MinNumber + stock_min_number
  1037. new_total = last_stock_max_number*base.MinNumber + last_stock_min_number
  1038. //更新入库单
  1039. service.UpdateNewDrugWarehouseInfoById(id, last_stock_max_number, last_stock_min_number)
  1040. //查询默认仓库
  1041. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  1042. //查询默认仓库剩余多少库存
  1043. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgId, drug_id)
  1044. var sum_count int64
  1045. for _, it := range list {
  1046. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1047. if it.MaxUnit == baseDrug.MaxUnit {
  1048. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1049. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1050. }
  1051. sum_count += it.StockMaxNumber + it.StockMinNumber
  1052. }
  1053. //更新剩余库存
  1054. service.UpdateMedicalSumCountOne(drug_id, sum_count, orgId)
  1055. //更新剩余库存
  1056. service.UpdateDrugStockCount(drug_id, orgId, houseConfig.DrugStorehouseOut, sum_count)
  1057. drugWarehouseInfoOne, _ := service.GetDrugWarehouseInfoOne(id)
  1058. manufacturer, _ := service.GetManufactureById(drugWarehouseInfoOne.Manufacturer)
  1059. recordDateStr := time.Now().Format("2006-01-02")
  1060. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  1061. fmt.Println("ord_total-----------------------------", ord_total)
  1062. fmt.Println("new_total-----------------------------", new_total)
  1063. //盘亏
  1064. if ord_total > new_total {
  1065. consumable_count = ord_total - new_total
  1066. inventory := models.XtDrugInventory{
  1067. DrugName: base.DrugName,
  1068. SpecificationName: specification_name,
  1069. WarehousingUnit: drugWarehouseInfoOne.MaxUnit,
  1070. Count: consumable_count,
  1071. LastPrice: drugWarehouseInfoOne.Price,
  1072. RetailPrice: drugWarehouseInfoOne.RetailPrice,
  1073. NewPrice: 0,
  1074. Manufacturer: manufacturer.ManufacturerName,
  1075. Dealer: "",
  1076. Remark: "",
  1077. DrugId: drug_id,
  1078. UserOrgId: orgId,
  1079. Ctime: time.Now().Unix(),
  1080. Mtime: 0,
  1081. Status: 1,
  1082. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1083. LicenseNumber: "",
  1084. StartTime: recordDate.Unix(),
  1085. Creater: this.GetAdminUserInfo().AdminUser.Id,
  1086. Checker: 0,
  1087. CheckerStatus: 0,
  1088. CheckerTime: 0,
  1089. ExpiryDate: drugWarehouseInfoOne.ExpiryDate,
  1090. ProductDate: drugWarehouseInfoOne.ProductDate,
  1091. Number: "",
  1092. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1093. Total: "",
  1094. DrugOriginPlace: "",
  1095. WarehouseInfoId: id,
  1096. ProofCount: 0,
  1097. StockMaxNumber: stock_max_number,
  1098. StockMinNumber: stock_min_number,
  1099. MinCount: 0,
  1100. MinUnit: drugWarehouseInfoOne.MinUnit,
  1101. LastStockMaxNumber: last_stock_max_number,
  1102. LastStockMinNumber: last_stock_min_number,
  1103. InventoryType: 11,
  1104. Type: is_type,
  1105. StorehouseId: storehouse_id,
  1106. RecordDate: recordDate.Unix(),
  1107. }
  1108. service.CreateDrugInventory(inventory)
  1109. flow := models.DrugFlow{
  1110. WarehousingId: id,
  1111. DrugId: drug_id,
  1112. Number: "",
  1113. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1114. Count: consumable_count,
  1115. UserOrgId: orgId,
  1116. PatientId: 0,
  1117. SystemTime: recordDate.Unix(),
  1118. ConsumableType: 11,
  1119. IsSys: 1,
  1120. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1121. WarehouseOutId: 0,
  1122. WarehouseOutOrderNumber: "",
  1123. IsEdit: 0,
  1124. CancelStockId: 0,
  1125. CancelOrderNumber: "",
  1126. Manufacturer: manufacturer.ID,
  1127. Dealer: 0,
  1128. Creator: this.GetAdminUserInfo().AdminUser.Id,
  1129. UpdateCreator: 0,
  1130. Status: 1,
  1131. Ctime: time.Now().Unix(),
  1132. Mtime: 0,
  1133. Price: drugWarehouseInfoOne.Price,
  1134. WarehousingDetailId: id,
  1135. WarehouseOutDetailId: 0,
  1136. CancelOutDetailId: 0,
  1137. ExpireDate: drugWarehouseInfoOne.ExpiryDate,
  1138. ProductDate: 0,
  1139. MaxUnit: drugWarehouseInfoOne.MaxUnit,
  1140. MinUnit: drugWarehouseInfoOne.MinUnit,
  1141. StorehouseId: storehouse_id,
  1142. OverCount: sum_count,
  1143. }
  1144. service.CreateDrugFlowOne(flow)
  1145. //添加出库数据
  1146. service.AddDrugWarehouseOut(drug_id, consumable_count, storehouse_id, orgId)
  1147. }
  1148. //盘盈
  1149. if ord_total < new_total {
  1150. consumable_count = new_total - ord_total
  1151. inventory := models.XtDrugInventory{
  1152. DrugName: base.DrugName,
  1153. SpecificationName: specification_name,
  1154. WarehousingUnit: drugWarehouseInfoOne.MaxUnit,
  1155. Count: consumable_count,
  1156. LastPrice: drugWarehouseInfoOne.Price,
  1157. RetailPrice: drugWarehouseInfoOne.RetailPrice,
  1158. NewPrice: 0,
  1159. Manufacturer: manufacturer.ManufacturerName,
  1160. Dealer: "",
  1161. Remark: "",
  1162. DrugId: drug_id,
  1163. UserOrgId: orgId,
  1164. Ctime: time.Now().Unix(),
  1165. Mtime: 0,
  1166. Status: 1,
  1167. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1168. LicenseNumber: "",
  1169. StartTime: recordDate.Unix(),
  1170. Creater: this.GetAdminUserInfo().AdminUser.Id,
  1171. Checker: 0,
  1172. CheckerStatus: 0,
  1173. CheckerTime: 0,
  1174. ExpiryDate: drugWarehouseInfoOne.ExpiryDate,
  1175. ProductDate: drugWarehouseInfoOne.ProductDate,
  1176. Number: "",
  1177. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1178. Total: "",
  1179. DrugOriginPlace: "",
  1180. WarehouseInfoId: id,
  1181. ProofCount: 0,
  1182. StockMaxNumber: stock_max_number,
  1183. StockMinNumber: stock_min_number,
  1184. MinCount: 0,
  1185. MinUnit: drugWarehouseInfoOne.MinUnit,
  1186. LastStockMaxNumber: last_stock_max_number,
  1187. LastStockMinNumber: last_stock_min_number,
  1188. InventoryType: 10,
  1189. Type: is_type,
  1190. StorehouseId: storehouse_id,
  1191. RecordDate: recordDate.Unix(),
  1192. }
  1193. service.CreateDrugInventory(inventory)
  1194. flow := models.DrugFlow{
  1195. WarehousingId: id,
  1196. DrugId: drug_id,
  1197. Number: "",
  1198. BatchNumber: drugWarehouseInfoOne.BatchNumber,
  1199. Count: consumable_count,
  1200. UserOrgId: orgId,
  1201. PatientId: 0,
  1202. SystemTime: recordDate.Unix(),
  1203. ConsumableType: 10,
  1204. IsSys: 1,
  1205. WarehousingOrder: drugWarehouseInfoOne.WarehousingOrder,
  1206. WarehouseOutId: 0,
  1207. WarehouseOutOrderNumber: "",
  1208. IsEdit: 0,
  1209. CancelStockId: 0,
  1210. CancelOrderNumber: "",
  1211. Manufacturer: manufacturer.ID,
  1212. Dealer: 0,
  1213. Creator: this.GetAdminUserInfo().AdminUser.Id,
  1214. UpdateCreator: 0,
  1215. Status: 1,
  1216. Ctime: time.Now().Unix(),
  1217. Mtime: 0,
  1218. Price: drugWarehouseInfoOne.Price,
  1219. WarehousingDetailId: id,
  1220. WarehouseOutDetailId: 0,
  1221. CancelOutDetailId: 0,
  1222. ExpireDate: drugWarehouseInfoOne.ExpiryDate,
  1223. ProductDate: 0,
  1224. MaxUnit: drugWarehouseInfoOne.MaxUnit,
  1225. MinUnit: drugWarehouseInfoOne.MinUnit,
  1226. StorehouseId: storehouse_id,
  1227. OverCount: sum_count,
  1228. }
  1229. service.CreateDrugFlowOne(flow)
  1230. //减少出库数据
  1231. service.ReduceDrugWarehouseOut(drug_id, consumable_count, storehouse_id, orgId)
  1232. }
  1233. this.ServeSuccessJSON(map[string]interface{}{
  1234. "msg": "msg",
  1235. })
  1236. }