pharmacy_controller.go 47KB

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