pharmacy_controller.go 47KB

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