pharmacy_controller.go 46KB

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