drug_pharmacy_management_controller.go 17KB

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