drug_pharmacy_management_controller.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. HisPrescriptionId: 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. UserOrgId: item.UserOrgId,
  132. PatientId: item.PatientId,
  133. HisAdviceInfoId: 0,
  134. XtAdviceId: item.ID,
  135. HisOrXt: 1,
  136. Ctime: time.Now().Unix(),
  137. Mtime: 0,
  138. Status: 1,
  139. HisAdviceId: 0,
  140. DrugId: item.DrugId,
  141. RecordDate: item.AdviceDate,
  142. OrdreNumber: warehousing_out_order,
  143. Creater: creater,
  144. }
  145. service.CreatePharmacy(pharmacy)
  146. lastPharmacy, _ := service.GetLastPharmary(item.UserOrgId, item.ID, item.AdviceDate)
  147. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  148. ////查询该药品是否有库存
  149. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  150. //
  151. ////查询改药品信息
  152. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  153. ////判断单位是否相等
  154. if medical.MaxUnit == item.PrescribingNumberUnit {
  155. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  156. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  157. //转化为最小单位
  158. total = list.Count*medical.MinNumber + list.StockMinNumber
  159. prescribing_number_total = count * medical.MinNumber
  160. }
  161. if medical.MinUnit == item.PrescribingNumberUnit {
  162. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  163. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  164. total = list.Count*medical.MinNumber + list.StockMinNumber
  165. prescribing_number_total = count
  166. }
  167. if (list.Count*medical.MinNumber + list.StockMinNumber) == 0 {
  168. this.ServeSuccessJSON(map[string]interface{}{
  169. "msg": "3",
  170. "medical": medical,
  171. })
  172. return
  173. }
  174. if prescribing_number_total > total {
  175. this.ServeSuccessJSON(map[string]interface{}{
  176. "msg": "2",
  177. "medical": medical,
  178. })
  179. return
  180. }
  181. if prescribing_number_total <= total {
  182. //查询是否门诊处方和临时医嘱同步到透析医嘱的开关是否开启
  183. adviceSetting, _ := service.FindAdviceSettingById(item.UserOrgId)
  184. if adviceSetting.IsAdviceOpen == 1 {
  185. if medical.IsUse == 2 {
  186. service.PharmacyDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmacy.ID)
  187. //查询默认仓库
  188. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  189. //查询默认仓库剩余多少库存
  190. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  191. var sum_count int64
  192. var sum_in_count int64
  193. for _, it := range list {
  194. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  195. if it.MaxUnit == baseDrug.MaxUnit {
  196. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  197. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  198. }
  199. sum_count += it.StockMaxNumber + it.StockMinNumber
  200. sum_in_count += it.WarehousingCount
  201. }
  202. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  203. break
  204. this.ServeSuccessJSON(map[string]interface{}{
  205. "msg": "1",
  206. "medical": medical,
  207. })
  208. return
  209. }
  210. if medical.IsUse == 1 {
  211. this.ServeSuccessJSON(map[string]interface{}{
  212. "msg": "1",
  213. })
  214. return
  215. }
  216. } else {
  217. if medical.IsUse == 2 {
  218. service.PharmacyDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmacy.ID)
  219. //查询默认仓库
  220. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  221. //查询默认仓库剩余多少库存
  222. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  223. var sum_count int64
  224. var sum_in_count int64
  225. for _, it := range list {
  226. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  227. if it.MaxUnit == baseDrug.MaxUnit {
  228. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  229. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  230. }
  231. sum_count += it.StockMaxNumber + it.StockMinNumber
  232. sum_in_count += it.WarehousingCount
  233. }
  234. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  235. this.ServeSuccessJSON(map[string]interface{}{
  236. "msg": "1",
  237. "medical": medical,
  238. })
  239. return
  240. }
  241. if medical.IsUse == 1 {
  242. this.ServeSuccessJSON(map[string]interface{}{
  243. "msg": "1",
  244. "medical": medical,
  245. })
  246. return
  247. }
  248. }
  249. }
  250. }
  251. //血透
  252. for _, item := range hisAdviceList {
  253. pharmacy := models.Pharmacy{
  254. UserOrgId: item.UserOrgId,
  255. PatientId: item.PatientId,
  256. HisPrescriptionId: 0,
  257. XtAdviceId: 0,
  258. HisOrXt: 2,
  259. Ctime: time.Now().Unix(),
  260. Mtime: 0,
  261. Status: 1,
  262. HisAdviceId: item.ID,
  263. DrugId: item.DrugId,
  264. RecordDate: item.AdviceDate,
  265. OrdreNumber: warehousing_out_order,
  266. Creater: admin_user_id,
  267. UserOrgId: item.UserOrgId,
  268. PatientId: item.PatientId,
  269. HisAdviceInfoId: 0,
  270. XtAdviceId: 0,
  271. HisOrXt: 2,
  272. Ctime: time.Now().Unix(),
  273. Mtime: 0,
  274. Status: 1,
  275. HisAdviceId: item.ID,
  276. DrugId: item.DrugId,
  277. RecordDate: item.AdviceDate,
  278. OrdreNumber: warehousing_out_order,
  279. Creater: creater,
  280. }
  281. service.CreatePharmacy(pharmacy)
  282. lastPharmary, _ := service.GetLastHisPharmary(item.UserOrgId, item.ID, item.AdviceDate)
  283. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  284. ////查询该药品是否有库存
  285. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  286. //
  287. ////查询改药品信息
  288. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  289. ////判断单位是否相等
  290. if medical.MaxUnit == item.PrescribingNumberUnit {
  291. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  292. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  293. //转化为最小单位
  294. total = list.Count*medical.MinNumber + list.StockMinNumber
  295. prescribing_number_total = count * medical.MinNumber
  296. }
  297. if medical.MinUnit == item.PrescribingNumberUnit {
  298. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  299. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  300. total = list.Count*medical.MinNumber + list.StockMinNumber
  301. prescribing_number_total = count
  302. }
  303. if (list.Count*medical.MinNumber + list.StockMinNumber) == 0 {
  304. this.ServeSuccessJSON(map[string]interface{}{
  305. "msg": "3",
  306. "medical": medical,
  307. })
  308. return
  309. }
  310. if prescribing_number_total > total {
  311. this.ServeSuccessJSON(map[string]interface{}{
  312. "msg": "2",
  313. "medical": medical,
  314. })
  315. return
  316. }
  317. if prescribing_number_total <= total {
  318. //查询是否门诊处方和临时医嘱同步到透析医嘱的开关是否开启
  319. adviceSetting, _ := service.FindAdviceSettingById(item.UserOrgId)
  320. if adviceSetting.IsAdviceOpen == 1 {
  321. if medical.IsUse == 2 {
  322. service.PharmacyHisDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmary.ID)
  323. //查询默认仓库
  324. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  325. //查询默认仓库剩余多少库存
  326. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  327. var sum_count int64
  328. var sum_in_count int64
  329. for _, it := range list {
  330. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  331. if it.MaxUnit == baseDrug.MaxUnit {
  332. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  333. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  334. }
  335. sum_count += it.StockMaxNumber + it.StockMinNumber
  336. sum_in_count += it.WarehousingCount
  337. }
  338. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  339. break
  340. this.ServeSuccessJSON(map[string]interface{}{
  341. "msg": "1",
  342. "medical": medical,
  343. })
  344. return
  345. }
  346. if medical.IsUse == 1 {
  347. this.ServeSuccessJSON(map[string]interface{}{
  348. "msg": "1",
  349. })
  350. return
  351. }
  352. } else {
  353. if medical.IsUse == 2 {
  354. service.PharmacyHisDrugsDelivery(item.UserOrgId, item.ExecutionStaff, item, lastPharmary.ID)
  355. //查询默认仓库
  356. houseConfig, _ := service.GetAllStoreHouseConfig(item.UserOrgId)
  357. //查询默认仓库剩余多少库存
  358. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.UserOrgId, item.DrugId)
  359. var sum_count int64
  360. var sum_in_count int64
  361. for _, it := range list {
  362. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  363. if it.MaxUnit == baseDrug.MaxUnit {
  364. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  365. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  366. }
  367. sum_count += it.StockMaxNumber + it.StockMinNumber
  368. sum_in_count += it.WarehousingCount
  369. }
  370. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.UserOrgId)
  371. this.ServeSuccessJSON(map[string]interface{}{
  372. "msg": "1",
  373. "medical": medical,
  374. })
  375. return
  376. }
  377. if medical.IsUse == 1 {
  378. this.ServeSuccessJSON(map[string]interface{}{
  379. "msg": "1",
  380. "medical": medical,
  381. })
  382. return
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. func (this *PharmacyApiController) GetPharmacyBaseDrugList() {
  390. start_time := this.GetString("start_time")
  391. timeLayout := "2006-01-02"
  392. loc, _ := time.LoadLocation("Local")
  393. var theStartTime int64
  394. if len(start_time) > 0 {
  395. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  396. if err != nil {
  397. utils.ErrorLog(err.Error())
  398. }
  399. theStartTime = theTime.Unix()
  400. }
  401. drug_id, _ := this.GetInt64("drug_id")
  402. orgId := this.GetAdminUserInfo().CurrentOrgId
  403. //获取血透医嘱
  404. advicelist, _ := service.GetUseredBloodAdviceList(theStartTime, drug_id, orgId)
  405. //获取医保医嘱
  406. hisAdviceList, err := service.GetUseredHisAdviceList(theStartTime, drug_id, orgId)
  407. patient, _ := service.GetAllPatientListSix(orgId)
  408. if err == nil {
  409. this.ServeSuccessJSON(map[string]interface{}{
  410. "advicelist": advicelist,
  411. "hisAdviceList": hisAdviceList,
  412. "patient": patient,
  413. })
  414. } else {
  415. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  416. }
  417. }
  418. func (this *PharmacyApiController) GetReturnPharmacyBaseDrug() {
  419. bloodStr := this.GetString("bloodStr")
  420. hisStr := this.GetString("hisStr")
  421. if len(bloodStr) == 0 {
  422. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  423. return
  424. }
  425. idArray := strings.Split(bloodStr, ",")
  426. if len(hisStr) == 0 {
  427. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  428. return
  429. }
  430. idArrayOne := strings.Split(hisStr, ",")
  431. fmt.Println("idArray23332233223", idArray)
  432. fmt.Println("idArrayOne23332233223", idArrayOne)
  433. orgId := this.GetAdminUserInfo().CurrentOrgId
  434. service.UpdateReturnPharmacyAdviceBaseDrug(idArray, orgId)
  435. service.UpdateReturnPharmacyHisAdviceBaseDrug(idArray, orgId)
  436. }
  437. func (this *PharmacyApiController) SaveSetting() {
  438. is_open, _ := this.GetInt64("is_open")
  439. orgId := this.GetAdminUserInfo().CurrentOrgId
  440. config := models.PharmacyConfig{
  441. UserOrgId: orgId,
  442. IsOpen: is_open,
  443. Status: 1,
  444. Ctime: time.Now().Unix(),
  445. }
  446. //查找是否存在
  447. _, errcode := service.GetConfigSettingIsExsit(orgId)
  448. if errcode == gorm.ErrRecordNotFound {
  449. service.CreatePharmacyConfig(&config)
  450. } else if errcode == nil {
  451. service.UpdatePharmacyConfig(orgId, &config)
  452. }
  453. this.ServeSuccessJSON(map[string]interface{}{
  454. "config": config,
  455. })
  456. }
  457. func (this *PharmacyApiController) GetPharmacyConfig() {
  458. orgId := this.GetAdminUserInfo().CurrentOrgId
  459. config, err := service.GetPharmacyConfig(orgId)
  460. if err == nil {
  461. this.ServeSuccessJSON(map[string]interface{}{
  462. "config": config,
  463. })
  464. } else {
  465. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  466. }
  467. }