drug_pharmacy_management_controller.go 17KB

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