pharmacy_controller.go 33KB

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