pharmacy_controller.go 48KB

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