pharmacy_controller.go 43KB

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