pharmacy_controller.go 43KB

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