pharmacy_controller.go 47KB

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