pharmacy_controller.go 45KB

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