pharmacy_controller.go 49KB

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