drug_pharmacy_management_controller.go 17KB

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