drug_pharmacy_management_controller.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "fmt"
  8. "github.com/astaxie/beego"
  9. "github.com/jinzhu/gorm"
  10. "math"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type PharmacyApiController struct {
  16. BaseAuthAPIController
  17. }
  18. func PharmacyApiControllersRegisterRouters() {
  19. //获取设置为药房发药的药品数据
  20. beego.Router("/api/advice/gettodayadvicelist", &PharmacyApiController{}, "Get:GetTodayAdviceList")
  21. //获取今日未发药的数据
  22. beego.Router("api/advice/getpharmacybasedrug", &PharmacyApiController{}, "Get:GetPharyMacyBaseDrug")
  23. //发药
  24. beego.Router("/api/advice/updatepharmacybasedrug", &PharmacyApiController{}, "Get:UpdatePharmacyBaseDrug")
  25. //获取今日已发药的数据
  26. beego.Router("/api/advice/getpharmacybasedruglist", &PharmacyApiController{}, "Get:GetPharmacyBaseDrugList")
  27. //退药流程
  28. beego.Router("/api/advice/getreturnpharmacybasedrug", &PharmacyApiController{}, "Get:GetReturnPharmacyBaseDrug")
  29. //设置
  30. beego.Router("/api/advice/savesetting", &PharmacyApiController{}, "Get:SaveSetting")
  31. beego.Router("/api/advice/getpharmacyconfig", &PharmacyApiController{}, "Get:GetPharmacyConfig")
  32. }
  33. func (this *PharmacyApiController) GetTodayAdviceList() {
  34. keywords := this.GetString("keywords")
  35. orgId := this.GetAdminUserInfo().CurrentOrgId
  36. //获取药房发药药品库数据
  37. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  38. list, err := service.GetPharmacyDrugList(houseConfig.DrugStorehouseOut, orgId, keywords)
  39. appId := this.GetAdminUserInfo().CurrentAppId
  40. doctorlist, _ := service.GetAllDoctorSix(orgId, appId)
  41. if err == nil {
  42. this.ServeSuccessJSON(map[string]interface{}{
  43. "list": list,
  44. "doctorlist": doctorlist,
  45. })
  46. } else {
  47. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  48. }
  49. }
  50. func (this *PharmacyApiController) GetPharyMacyBaseDrug() {
  51. start_time := this.GetString("start_time")
  52. timeLayout := "2006-01-02"
  53. loc, _ := time.LoadLocation("Local")
  54. var theStartTime int64
  55. if len(start_time) > 0 {
  56. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  57. if err != nil {
  58. utils.ErrorLog(err.Error())
  59. }
  60. theStartTime = theTime.Unix()
  61. }
  62. drug_id, _ := this.GetInt64("drug_id")
  63. orgId := this.GetAdminUserInfo().CurrentOrgId
  64. //获取血透医嘱
  65. advicelist, _ := service.GetBloodAdviceList(theStartTime, drug_id, orgId)
  66. //获取医保医嘱
  67. hisAdviceList, err := service.GetHisAdviceList(theStartTime, drug_id, orgId)
  68. patient, _ := service.GetAllPatientListSix(orgId)
  69. if err == nil {
  70. this.ServeSuccessJSON(map[string]interface{}{
  71. "advicelist": advicelist,
  72. "hisAdviceList": hisAdviceList,
  73. "patient": patient,
  74. })
  75. } else {
  76. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  77. }
  78. }
  79. func (this *PharmacyApiController) UpdatePharmacyBaseDrug() {
  80. bloodStr := this.GetString("bloodStr")
  81. hisStr := this.GetString("hisStr")
  82. admin_user_id, _ := this.GetInt64("admin_user_id")
  83. if len(bloodStr) == 0 {
  84. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  85. return
  86. }
  87. idArray := strings.Split(bloodStr, ",")
  88. if len(hisStr) == 0 {
  89. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  90. return
  91. }
  92. idArrayOne := strings.Split(hisStr, ",")
  93. fmt.Println("idArray23332233223", idArray)
  94. fmt.Println("idArrayOne23332233223", idArrayOne)
  95. orgId := this.GetAdminUserInfo().CurrentOrgId
  96. service.UpdatePharmacyAdviceBaseDrug(idArray, orgId)
  97. service.UpdatePharmacyHisAdviceBaseDrug(idArray, orgId)
  98. //出库逻辑
  99. adviceList, _ := service.GetPharmacyAdviceList(idArray, orgId)
  100. hisAdviceList, _ := service.GetPharmacyHisAdviceList(idArray, orgId)
  101. //查询是否通过药房出库
  102. pharmacyConfig, _ := service.FindPharmacyConfig(orgId)
  103. if pharmacyConfig.IsOpen == 1 {
  104. var total int64
  105. var prescribing_number_total int64
  106. //创建流水
  107. timeStr := time.Now().Format("2006-01-02")
  108. timeArr := strings.Split(timeStr, "-")
  109. totals, _ := service.FindAllPharmacyDrugWarehouseOut(orgId)
  110. totals = totals + 1
  111. warehousing_out_order := strconv.FormatInt(orgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  112. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  113. number = number + totals
  114. warehousing_out_order = "FY" + strconv.FormatInt(number, 10)
  115. //血透
  116. for _, item := range adviceList {
  117. pharmacy := models.Pharmacy{
  118. UserOrgId: item.UserOrgId,
  119. PatientId: item.PatientId,
  120. PrescriptionId: 0,
  121. XtAdviceId: item.ID,
  122. HisOrXt: 1,
  123. Ctime: time.Now().Unix(),
  124. Mtime: 0,
  125. Status: 1,
  126. HisAdviceId: 0,
  127. DrugId: item.DrugId,
  128. RecordDate: item.AdviceDate,
  129. OrdreNumber: warehousing_out_order,
  130. Creater: admin_user_id,
  131. }
  132. service.CreatePharmacy(pharmacy)
  133. lastPharmacy, _ := service.GetLastPharmary(item.UserOrgId, item.ID, item.AdviceDate)
  134. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  135. ////查询该药品是否有库存
  136. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  137. //
  138. ////查询改药品信息
  139. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  140. ////判断单位是否相等
  141. if medical.MaxUnit == item.PrescribingNumberUnit {
  142. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  143. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  144. //转化为最小单位
  145. total = list.Count*medical.MinNumber + list.StockMinNumber
  146. prescribing_number_total = count * medical.MinNumber
  147. }
  148. if medical.MinUnit == item.PrescribingNumberUnit {
  149. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  150. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  151. total = list.Count*medical.MinNumber + list.StockMinNumber
  152. prescribing_number_total = count
  153. }
  154. if (list.Count*medical.MinNumber + list.StockMinNumber) == 0 {
  155. this.ServeSuccessJSON(map[string]interface{}{
  156. "msg": "3",
  157. "medical": medical,
  158. })
  159. return
  160. }
  161. if prescribing_number_total > total {
  162. this.ServeSuccessJSON(map[string]interface{}{
  163. "msg": "2",
  164. "medical": medical,
  165. })
  166. return
  167. }
  168. if prescribing_number_total <= total {
  169. //查询是否门诊处方和临时医嘱同步到透析医嘱的开关是否开启
  170. adviceSetting, _ := service.FindAdviceSettingById(item.UserOrgId)
  171. if adviceSetting.IsAdviceOpen == 1 {
  172. if medical.IsUse == 2 {
  173. service.PharmacyDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmacy.ID)
  174. //查询默认仓库
  175. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  176. //查询默认仓库剩余多少库存
  177. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  178. var sum_count int64
  179. var sum_in_count int64
  180. for _, it := range list {
  181. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  182. if it.MaxUnit == baseDrug.MaxUnit {
  183. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  184. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  185. }
  186. sum_count += it.StockMaxNumber + it.StockMinNumber
  187. sum_in_count += it.WarehousingCount
  188. }
  189. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  190. break
  191. this.ServeSuccessJSON(map[string]interface{}{
  192. "msg": "1",
  193. "medical": medical,
  194. })
  195. return
  196. }
  197. if medical.IsUse == 1 {
  198. this.ServeSuccessJSON(map[string]interface{}{
  199. "msg": "1",
  200. })
  201. return
  202. }
  203. } else {
  204. if medical.IsUse == 2 {
  205. service.PharmacyDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmacy.ID)
  206. //查询默认仓库
  207. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  208. //查询默认仓库剩余多少库存
  209. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  210. var sum_count int64
  211. var sum_in_count int64
  212. for _, it := range list {
  213. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  214. if it.MaxUnit == baseDrug.MaxUnit {
  215. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  216. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  217. }
  218. sum_count += it.StockMaxNumber + it.StockMinNumber
  219. sum_in_count += it.WarehousingCount
  220. }
  221. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  222. this.ServeSuccessJSON(map[string]interface{}{
  223. "msg": "1",
  224. "medical": medical,
  225. })
  226. return
  227. }
  228. if medical.IsUse == 1 {
  229. this.ServeSuccessJSON(map[string]interface{}{
  230. "msg": "1",
  231. "medical": medical,
  232. })
  233. return
  234. }
  235. }
  236. }
  237. }
  238. //血透
  239. for _, item := range hisAdviceList {
  240. pharmacy := models.Pharmacy{
  241. UserOrgId: item.UserOrgId,
  242. PatientId: item.PatientId,
  243. PrescriptionId: 0,
  244. XtAdviceId: 0,
  245. HisOrXt: 2,
  246. Ctime: time.Now().Unix(),
  247. Mtime: 0,
  248. Status: 1,
  249. HisAdviceId: item.ID,
  250. DrugId: item.DrugId,
  251. RecordDate: item.AdviceDate,
  252. OrdreNumber: warehousing_out_order,
  253. Creater: admin_user_id,
  254. }
  255. service.CreatePharmacy(pharmacy)
  256. lastPharmary, _ := service.GetLastHisPharmary(item.UserOrgId, item.ID, item.AdviceDate)
  257. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  258. ////查询该药品是否有库存
  259. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  260. //
  261. ////查询改药品信息
  262. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  263. ////判断单位是否相等
  264. if medical.MaxUnit == item.PrescribingNumberUnit {
  265. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  266. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  267. //转化为最小单位
  268. total = list.Count*medical.MinNumber + list.StockMinNumber
  269. prescribing_number_total = count * medical.MinNumber
  270. }
  271. if medical.MinUnit == item.PrescribingNumberUnit {
  272. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  273. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  274. total = list.Count*medical.MinNumber + list.StockMinNumber
  275. prescribing_number_total = count
  276. }
  277. if (list.Count*medical.MinNumber + list.StockMinNumber) == 0 {
  278. this.ServeSuccessJSON(map[string]interface{}{
  279. "msg": "3",
  280. "medical": medical,
  281. })
  282. return
  283. }
  284. if prescribing_number_total > total {
  285. this.ServeSuccessJSON(map[string]interface{}{
  286. "msg": "2",
  287. "medical": medical,
  288. })
  289. return
  290. }
  291. if prescribing_number_total <= total {
  292. //查询是否门诊处方和临时医嘱同步到透析医嘱的开关是否开启
  293. adviceSetting, _ := service.FindAdviceSettingById(item.UserOrgId)
  294. if adviceSetting.IsAdviceOpen == 1 {
  295. if medical.IsUse == 2 {
  296. service.PharmacyHisDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmary.ID)
  297. //查询默认仓库
  298. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  299. //查询默认仓库剩余多少库存
  300. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  301. var sum_count int64
  302. var sum_in_count int64
  303. for _, it := range list {
  304. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  305. if it.MaxUnit == baseDrug.MaxUnit {
  306. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  307. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  308. }
  309. sum_count += it.StockMaxNumber + it.StockMinNumber
  310. sum_in_count += it.WarehousingCount
  311. }
  312. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  313. break
  314. this.ServeSuccessJSON(map[string]interface{}{
  315. "msg": "1",
  316. "medical": medical,
  317. })
  318. return
  319. }
  320. if medical.IsUse == 1 {
  321. this.ServeSuccessJSON(map[string]interface{}{
  322. "msg": "1",
  323. })
  324. return
  325. }
  326. } else {
  327. if medical.IsUse == 2 {
  328. service.PharmacyHisDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmary.ID)
  329. //查询默认仓库
  330. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  331. //查询默认仓库剩余多少库存
  332. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  333. var sum_count int64
  334. var sum_in_count int64
  335. for _, it := range list {
  336. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  337. if it.MaxUnit == baseDrug.MaxUnit {
  338. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  339. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  340. }
  341. sum_count += it.StockMaxNumber + it.StockMinNumber
  342. sum_in_count += it.WarehousingCount
  343. }
  344. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  345. this.ServeSuccessJSON(map[string]interface{}{
  346. "msg": "1",
  347. "medical": medical,
  348. })
  349. return
  350. }
  351. if medical.IsUse == 1 {
  352. this.ServeSuccessJSON(map[string]interface{}{
  353. "msg": "1",
  354. "medical": medical,
  355. })
  356. return
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. func (this *PharmacyApiController) GetPharmacyBaseDrugList() {
  364. start_time := this.GetString("start_time")
  365. timeLayout := "2006-01-02"
  366. loc, _ := time.LoadLocation("Local")
  367. var theStartTime int64
  368. if len(start_time) > 0 {
  369. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  370. if err != nil {
  371. utils.ErrorLog(err.Error())
  372. }
  373. theStartTime = theTime.Unix()
  374. }
  375. drug_id, _ := this.GetInt64("drug_id")
  376. orgId := this.GetAdminUserInfo().CurrentOrgId
  377. //获取血透医嘱
  378. advicelist, _ := service.GetUseredBloodAdviceList(theStartTime, drug_id, orgId)
  379. //获取医保医嘱
  380. hisAdviceList, err := service.GetUseredHisAdviceList(theStartTime, drug_id, orgId)
  381. patient, _ := service.GetAllPatientListSix(orgId)
  382. if err == nil {
  383. this.ServeSuccessJSON(map[string]interface{}{
  384. "advicelist": advicelist,
  385. "hisAdviceList": hisAdviceList,
  386. "patient": patient,
  387. })
  388. } else {
  389. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  390. }
  391. }
  392. func (this *PharmacyApiController) GetReturnPharmacyBaseDrug() {
  393. bloodStr := this.GetString("bloodStr")
  394. hisStr := this.GetString("hisStr")
  395. if len(bloodStr) == 0 {
  396. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  397. return
  398. }
  399. idArray := strings.Split(bloodStr, ",")
  400. if len(hisStr) == 0 {
  401. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  402. return
  403. }
  404. idArrayOne := strings.Split(hisStr, ",")
  405. fmt.Println("idArray23332233223", idArray)
  406. fmt.Println("idArrayOne23332233223", idArrayOne)
  407. orgId := this.GetAdminUserInfo().CurrentOrgId
  408. service.UpdateReturnPharmacyAdviceBaseDrug(idArray, orgId)
  409. service.UpdateReturnPharmacyHisAdviceBaseDrug(idArray, orgId)
  410. }
  411. func (this *PharmacyApiController) SaveSetting() {
  412. is_open, _ := this.GetInt64("is_open")
  413. orgId := this.GetAdminUserInfo().CurrentOrgId
  414. config := models.PharmacyConfig{
  415. UserOrgId: orgId,
  416. IsOpen: is_open,
  417. Status: 1,
  418. Ctime: time.Now().Unix(),
  419. }
  420. //查找是否存在
  421. _, errcode := service.GetConfigSettingIsExsit(orgId)
  422. if errcode == gorm.ErrRecordNotFound {
  423. service.CreatePharmacyConfig(&config)
  424. } else if errcode == nil {
  425. service.UpdatePharmacyConfig(orgId, &config)
  426. }
  427. this.ServeSuccessJSON(map[string]interface{}{
  428. "config": config,
  429. })
  430. }
  431. func (this *PharmacyApiController) GetPharmacyConfig() {
  432. orgId := this.GetAdminUserInfo().CurrentOrgId
  433. config, err := service.GetPharmacyConfig(orgId)
  434. if err == nil {
  435. this.ServeSuccessJSON(map[string]interface{}{
  436. "config": config,
  437. })
  438. } else {
  439. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  440. }
  441. }