pharmacy_controller.go 47KB

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