pharmacy_controller.go 45KB

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