dialysis_api_controller_extend.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. package mobile_api_controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "fmt"
  8. "github.com/jinzhu/gorm"
  9. "strconv"
  10. "time"
  11. )
  12. // /m/api/monitor/add [post]
  13. // @param patient_id:int
  14. // @param order_id?:int 透析记录 ID
  15. // @param date?:int 日期(10位时间戳)
  16. // @param operate_time?:int 实际测量日期(10位时间戳)
  17. // @param time?:string 时间(hh:mm) 废弃
  18. // @param pulse_frequency?:float 脉率(P)(次/min)
  19. // @param breathing_rated?:float 呼吸频率(R)(次/min)
  20. // @param systolic_bp?:float 收缩压
  21. // @param diastolic_bp?:float 舒张压
  22. // @param bp_type?:int 血压测量类型
  23. // @param blood_flow_volume?:float 血流量(ml/min)
  24. // @param venous_pressure?:float 静脉压
  25. // @param venous_pressure_unit?:string 静脉压单位
  26. // @param arterial_pressure?:float 动脉压
  27. // @param transmembrane_pressure?:float 跨膜压
  28. // @param transmembrane_pressure_unit?:string 跨膜压单位
  29. // @param ultrafiltration_rate?:float 超滤率(ml/h)
  30. // @param ultrafiltration_volume?:float 超滤量(ml)
  31. // @param sodium_concentration?:float 钠浓度(mmol/L)
  32. // @param dialysate_temperature?:float 透析液温度(℃)
  33. // @param replacement_rate?:float 置换率(ml/min)
  34. // @param displacement_quantity?:float 置换量(L)
  35. // @param ktv?:float KT/V(在线)
  36. // @param symptom?:string 症状
  37. // @param dispose?:string 处理
  38. // @param result?:string 结果
  39. // @param monitoring_nurse?:int 监测人
  40. func (this *DialysisAPIController) AddMonitorRecord() {
  41. patientID, _ := this.GetInt64("patient_id")
  42. if patientID <= 0 {
  43. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  44. return
  45. }
  46. orderID, _ := this.GetInt64("order_id")
  47. if orderID < 0 {
  48. orderID = 0
  49. }
  50. date, _ := this.GetInt64("date")
  51. operateTime, _ := this.GetInt64("operate_time")
  52. // recordTime := this.GetString("time")
  53. if date <= 0 || operateTime <= 0 {
  54. date = 0
  55. operateTime = 0
  56. // recordTime = ""
  57. }
  58. fmt.Println(date)
  59. fmt.Println(operateTime)
  60. pulseFrequency, _ := this.GetFloat("pulse_frequency")
  61. breathingRated, _ := this.GetFloat("breathing_rated")
  62. systolicBP, _ := this.GetFloat("systolic_bp")
  63. diastolicBP, _ := this.GetFloat("diastolic_bp")
  64. BPType, _ := this.GetInt("bp_type")
  65. bloodFlowVolume, _ := this.GetFloat("blood_flow_volume")
  66. venousPressure, _ := this.GetFloat("venous_pressure")
  67. venousPressureType, _ := this.GetInt64("venous_pressure_type", 1)
  68. arterialPressure, _ := this.GetFloat("arterial_pressure")
  69. arterialPressureType, _ := this.GetInt64("arterial_pressure_type", 1)
  70. transmembranePressure, _ := this.GetFloat("transmembrane_pressure", 0)
  71. transmembranePressureType, _ := this.GetInt64("transmembrane_pressure_type", 1)
  72. ultrafiltrationRate, _ := this.GetFloat("ultrafiltration_rate")
  73. ultrafiltrationVolume, _ := this.GetFloat("ultrafiltration_volume")
  74. sodiumConcentration, _ := this.GetFloat("sodium_concentration")
  75. dialysateTemperature, _ := this.GetFloat("dialysate_temperature")
  76. temperature, _ := this.GetFloat("temperature")
  77. replacementRate, _ := this.GetFloat("replacement_rate")
  78. displacementQuantity, _ := this.GetFloat("displacement_quantity")
  79. ktv, _ := this.GetFloat("ktv")
  80. symptom := this.GetString("symptom")
  81. dispose := this.GetString("dispose")
  82. conductivity, _ := this.GetFloat("conductivity")
  83. displacement_flow_quantity, _ := this.GetFloat("displacement_flow_quantity")
  84. blood_oxygen_saturation := this.GetString("blood_oxygen_saturation")
  85. result := this.GetString("result")
  86. monitoringNurse, _ := this.GetInt64("monitoring_nurse")
  87. heparin, _ := this.GetFloat("heparin")
  88. dialysate_flow, _ := this.GetFloat("dialysate_flow")
  89. urr := this.GetString("urr")
  90. blood_sugar, _ := this.GetFloat("blood_sugar")
  91. monitor_anticoagulant, _ := this.GetInt64("monitor_anticoagulant")
  92. monitor_anticoagulant_value := this.GetString("monitor_anticoagulant_value")
  93. heparin_amount, _ := this.GetFloat("heparin_amount")
  94. blood_pressure_monitoring_site, _ := this.GetInt64("blood_pressure_monitoring_site")
  95. complication, _ := this.GetInt64("complication")
  96. accumulated_blood_volume, _ := this.GetFloat("accumulated_blood_volume")
  97. blood_temperature, _ := this.GetFloat("blood_temperature")
  98. urea_monitoring, _ := this.GetFloat("urea_monitoring")
  99. blood_thickness, _ := this.GetFloat("blood_thickness")
  100. blood_monitor, _ := this.GetFloat("blood_monitor")
  101. adminInfo := this.GetMobileAdminUserInfo()
  102. patient, getPatientErr := service.MobileGetPatientById(adminInfo.Org.Id, patientID)
  103. if getPatientErr != nil {
  104. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  105. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  106. return
  107. } else if patient == nil {
  108. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  109. return
  110. }
  111. record := models.MonitoringRecord{
  112. UserOrgId: adminInfo.Org.Id,
  113. PatientId: patientID,
  114. DialysisOrderId: orderID,
  115. MonitoringDate: date,
  116. OperateTime: operateTime,
  117. // MonitoringTime: recordTime,
  118. PulseFrequency: pulseFrequency,
  119. BreathingRate: breathingRated,
  120. SystolicBloodPressure: systolicBP,
  121. DiastolicBloodPressure: diastolicBP,
  122. BloodPressureType: int64(BPType),
  123. BloodFlowVolume: bloodFlowVolume,
  124. VenousPressure: venousPressure,
  125. VenousPressureType: venousPressureType,
  126. ArterialPressure: arterialPressure,
  127. ArterialPressureType: arterialPressureType,
  128. TransmembranePressure: transmembranePressure,
  129. TransmembranePressureType: transmembranePressureType,
  130. UltrafiltrationRate: ultrafiltrationRate,
  131. UltrafiltrationVolume: ultrafiltrationVolume,
  132. SodiumConcentration: sodiumConcentration,
  133. DialysateTemperature: dialysateTemperature,
  134. Temperature: temperature,
  135. ReplacementRate: replacementRate,
  136. DisplacementQuantity: displacementQuantity,
  137. Ktv: ktv,
  138. Symptom: symptom,
  139. Dispose: dispose,
  140. Result: result,
  141. MonitoringNurse: monitoringNurse,
  142. Status: 1,
  143. CreatedTime: time.Now().Unix(),
  144. UpdatedTime: time.Now().Unix(),
  145. Conductivity: conductivity,
  146. DisplacementFlowQuantity: displacement_flow_quantity,
  147. BloodOxygenSaturation: blood_oxygen_saturation,
  148. Creator: adminInfo.AdminUser.Id,
  149. Modify: 0,
  150. Heparin: heparin,
  151. DialysateFlow: dialysate_flow,
  152. Urr: urr,
  153. BloodSugar: blood_sugar,
  154. MonitorAnticoagulant: monitor_anticoagulant,
  155. MonitorAnticoagulantValue: monitor_anticoagulant_value,
  156. BloodPressureMonitoringSite: blood_pressure_monitoring_site,
  157. Complication: complication,
  158. AccumulatedBloodVolume: accumulated_blood_volume,
  159. BloodTemperature: blood_temperature,
  160. UreaMonitoring: urea_monitoring,
  161. BloodThickness: blood_thickness,
  162. BloodMonitor: blood_monitor,
  163. HeparinAmount: heparin_amount,
  164. }
  165. err := service.CreateMonitor(&record)
  166. key := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_records"
  167. redis := service.RedisClient()
  168. //清空key 值
  169. redis.Set(key, "", time.Second)
  170. keyOne := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_record_list_all"
  171. redis.Set(keyOne, "", time.Second)
  172. defer redis.Close()
  173. if err != nil {
  174. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMonitorCreate)
  175. return
  176. }
  177. this.ServeSuccessJSON(map[string]interface{}{
  178. "monitor": record,
  179. })
  180. }
  181. // /m/api/monitor/edit [post]
  182. // @param patient_id:int
  183. // @param order_id?:int 透析记录 ID
  184. // @param date?:int 日期(10位时间戳)
  185. // @param operate_time?:int 实际测量日期(10位时间戳)
  186. // @param time?:string 时间(hh:mm)废弃
  187. // @param pulse_frequency?:float 脉率(P)(次/min)
  188. // @param breathing_rated?:float 呼吸频率(R)(次/min)
  189. // @param systolic_bp?:float 收缩压
  190. // @param diastolic_bp?:float 舒张压
  191. // @param bp_type?:int 血压测量类型
  192. // @param blood_flow_volume?:float 血流量(ml/min)
  193. // @param venous_pressure?:float 静脉压
  194. // @param venous_pressure_unit?:string 静脉压单位
  195. // @param arterial_pressure?:float 动脉压
  196. // @param transmembrane_pressure?:float 跨膜压
  197. // @param transmembrane_pressure_unit?:string 跨膜压单位
  198. // @param ultrafiltration_rate?:float 超滤率(ml/h)
  199. // @param ultrafiltration_volume?:float 超滤量(ml)
  200. // @param sodium_concentration?:float 钠浓度(mmol/L)
  201. // @param dialysate_temperature?:float 透析液温度(℃)
  202. // @param replacement_rate?:float 置换率(ml/min)
  203. // @param displacement_quantity?:float 置换量(L)
  204. // @param ktv?:float KT/V(在线)
  205. // @param symptom?:string 症状
  206. // @param dispose?:string 处理
  207. // @param result?:string 结果
  208. // @param monitoring_nurse?:int 监测人
  209. func (this *DialysisAPIController) EditMonitorRecord() {
  210. patientID, _ := this.GetInt64("patient_id")
  211. if patientID <= 0 {
  212. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  213. return
  214. }
  215. id, _ := this.GetInt64("id")
  216. if id <= 0 {
  217. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  218. return
  219. }
  220. orderID, _ := this.GetInt64("order_id")
  221. if orderID < 0 {
  222. orderID = 0
  223. }
  224. date, _ := this.GetInt64("date")
  225. operateTime, _ := this.GetInt64("operate_time")
  226. if date <= 0 {
  227. date = 0
  228. operateTime = 0
  229. }
  230. pulseFrequency, _ := this.GetFloat("pulse_frequency")
  231. breathingRated, _ := this.GetFloat("breathing_rated")
  232. systolicBP, _ := this.GetFloat("systolic_bp")
  233. diastolicBP, _ := this.GetFloat("diastolic_bp")
  234. BPType, _ := this.GetInt("bp_type")
  235. bloodFlowVolume, _ := this.GetFloat("blood_flow_volume")
  236. venousPressure, _ := this.GetFloat("venous_pressure")
  237. venousPressureType, _ := this.GetInt64("venous_pressure_type", 1)
  238. arterialPressure, _ := this.GetFloat("arterial_pressure")
  239. arterialPressureType, _ := this.GetInt64("arterial_pressure", 1)
  240. transmembranePressure, _ := this.GetFloat("transmembrane_pressure")
  241. transmembranePressureType, _ := this.GetInt64("transmembrane_pressure_type", 1)
  242. ultrafiltrationRate, _ := this.GetFloat("ultrafiltration_rate")
  243. ultrafiltrationVolume, _ := this.GetFloat("ultrafiltration_volume")
  244. sodiumConcentration, _ := this.GetFloat("sodium_concentration")
  245. dialysateTemperature, _ := this.GetFloat("dialysate_temperature")
  246. temperature, _ := this.GetFloat("temperature")
  247. replacementRate, _ := this.GetFloat("replacement_rate")
  248. displacementQuantity, _ := this.GetFloat("displacement_quantity")
  249. ktv, _ := this.GetFloat("ktv")
  250. symptom := this.GetString("symptom")
  251. dispose := this.GetString("dispose")
  252. result := this.GetString("result")
  253. blood_oxygen_saturation := this.GetString("blood_oxygen_saturation")
  254. monitoringNurse, _ := this.GetInt64("monitoring_nurse")
  255. conductivity, _ := this.GetFloat("conductivity")
  256. displacement_flow_quantity, _ := this.GetFloat("displacement_flow_quantity")
  257. heparin, _ := this.GetFloat("heparin")
  258. dialysate_flow, _ := this.GetFloat("dialysate_flow")
  259. urr := this.GetString("urr")
  260. blood_sugar, _ := this.GetFloat("blood_sugar")
  261. adminInfo := this.GetMobileAdminUserInfo()
  262. monitor_anticoagulant, _ := this.GetInt64("monitor_anticoagulant")
  263. monitor_anticoagulant_value := this.GetString("monitor_anticoagulant_value")
  264. blood_pressure_monitoring_site, _ := this.GetInt64("blood_pressure_monitoring_site")
  265. complication, _ := this.GetInt64("complication")
  266. accumulated_blood_volume, _ := this.GetFloat("accumulated_blood_volume")
  267. blood_temperature, _ := this.GetFloat("blood_temperature")
  268. urea_monitoring, _ := this.GetFloat("urea_monitoring")
  269. blood_thickness, _ := this.GetFloat("blood_thickness")
  270. blood_monitor, _ := this.GetFloat("blood_monitor")
  271. monitor, err := service.GetMonitor(adminInfo.Org.Id, patientID, id)
  272. if err != nil {
  273. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  274. return
  275. }
  276. if monitor == nil {
  277. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMonitorNotExist)
  278. return
  279. }
  280. monitor.Conductivity = conductivity
  281. monitor.DialysisOrderId = orderID
  282. monitor.MonitoringDate = date
  283. monitor.OperateTime = operateTime
  284. monitor.PulseFrequency = pulseFrequency
  285. monitor.BreathingRate = breathingRated
  286. monitor.SystolicBloodPressure = systolicBP
  287. monitor.DiastolicBloodPressure = diastolicBP
  288. monitor.BloodPressureType = int64(BPType)
  289. monitor.BloodFlowVolume = bloodFlowVolume
  290. monitor.VenousPressure = venousPressure
  291. monitor.VenousPressureType = venousPressureType
  292. monitor.ArterialPressure = arterialPressure
  293. monitor.ArterialPressureType = arterialPressureType
  294. monitor.TransmembranePressure = transmembranePressure
  295. monitor.TransmembranePressureType = transmembranePressureType
  296. monitor.UltrafiltrationRate = ultrafiltrationRate
  297. monitor.UltrafiltrationVolume = ultrafiltrationVolume
  298. monitor.SodiumConcentration = sodiumConcentration
  299. monitor.DialysateTemperature = dialysateTemperature
  300. monitor.Temperature = temperature
  301. monitor.ReplacementRate = replacementRate
  302. monitor.DisplacementQuantity = displacementQuantity
  303. monitor.Ktv = ktv
  304. monitor.Symptom = symptom
  305. monitor.Dispose = dispose
  306. monitor.Result = result
  307. monitor.MonitoringNurse = monitoringNurse
  308. monitor.Status = 1
  309. monitor.UpdatedTime = time.Now().Unix()
  310. monitor.DisplacementFlowQuantity = displacement_flow_quantity
  311. monitor.BloodOxygenSaturation = blood_oxygen_saturation
  312. monitor.Modify = adminInfo.AdminUser.Id
  313. monitor.Heparin = heparin
  314. monitor.DialysateFlow = dialysate_flow
  315. monitor.Urr = urr
  316. monitor.BloodSugar = blood_sugar
  317. monitor.MonitorAnticoagulant = monitor_anticoagulant
  318. monitor.MonitorAnticoagulantValue = monitor_anticoagulant_value
  319. monitor.BloodPressureMonitoringSite = blood_pressure_monitoring_site
  320. monitor.Complication = complication
  321. monitor.AccumulatedBloodVolume = accumulated_blood_volume
  322. monitor.BloodMonitor = blood_monitor
  323. monitor.UreaMonitoring = urea_monitoring
  324. monitor.BloodThickness = blood_thickness
  325. monitor.BloodTemperature = blood_temperature
  326. err = service.UpdateMonitor(monitor)
  327. key := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_records"
  328. redis := service.RedisClient()
  329. //清空key 值
  330. redis.Set(key, "", time.Second)
  331. keyOne := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_record_list_all"
  332. redis.Set(keyOne, "", time.Second)
  333. defer redis.Close()
  334. if err != nil {
  335. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMonitorUpdate)
  336. return
  337. }
  338. this.ServeSuccessJSON(map[string]interface{}{
  339. "monitor": monitor,
  340. })
  341. }
  342. // /m/api/monitor/delete [post]
  343. // @param record_id:int
  344. // @param patient_id:int
  345. func (this *DialysisAPIController) DeleteMonitor() {
  346. recordID, _ := this.GetInt64("record_id")
  347. patientID, _ := this.GetInt64("patient_id")
  348. if recordID <= 0 || patientID <= 0 {
  349. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  350. return
  351. }
  352. adminInfo := this.GetMobileAdminUserInfo()
  353. patient, getPatientErr := service.MobileGetPatientById(adminInfo.Org.Id, patientID)
  354. if getPatientErr != nil {
  355. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  356. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  357. return
  358. } else if patient == nil {
  359. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  360. return
  361. }
  362. monitor, getMonitorErr := service.GetMonitor(adminInfo.Org.Id, patientID, recordID)
  363. if getMonitorErr != nil {
  364. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  365. return
  366. }
  367. err := service.DisableMonitor(adminInfo.Org.Id, patientID, recordID, adminInfo.AdminUser.Id)
  368. orgid := this.GetMobileAdminUserInfo().Org.Id
  369. redis := service.RedisClient()
  370. key := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(monitor.MonitoringDate, 10) + ":monitor_record_list_all"
  371. redis.Set(key, "", time.Second)
  372. keyOne := strconv.FormatInt(orgid, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(monitor.MonitoringDate, 10) + ":monitor_records"
  373. redis.Set(keyOne, "", time.Second)
  374. fmt.Println("keyo呢32332322332332232332", keyOne)
  375. redis.Close()
  376. if err != nil {
  377. this.ErrorLog("删除透析监测记录失败:%v", err)
  378. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBDelete)
  379. return
  380. }
  381. this.ServeSuccessJSON(map[string]interface{}{
  382. "record_id": monitor.ID,
  383. })
  384. }
  385. // /m/api/schedule/urgentinit [get]
  386. func (this *DialysisAPIController) UrgentScheduleInitData() {
  387. schedule_type, _ := this.GetInt("type", 0)
  388. adminUserInfo := this.GetMobileAdminUserInfo()
  389. today := utils.ZeroHourTimeOfDay(time.Now())
  390. //获取所有病人且没有上机记录的病人
  391. patients, getPatientsErr := service.MobileGetAllPatientsForUrgentSchedule(adminUserInfo.Org.Id, today.Unix())
  392. if getPatientsErr != nil {
  393. this.ErrorLog("获取所有患者失败:%v", getPatientsErr)
  394. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  395. return
  396. }
  397. modes, getModesErr := service.MobileGetAllTrearmentModesForUrgentSchedule()
  398. if getModesErr != nil {
  399. this.ErrorLog("获取所有治疗模式失败:%v", getModesErr)
  400. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  401. return
  402. }
  403. deviceNumbers, getDeviceNumbersErr := service.GetAllAvaildDeviceNumbers(adminUserInfo.Org.Id, today.Unix(), schedule_type)
  404. if getDeviceNumbersErr != nil {
  405. this.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr)
  406. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  407. return
  408. }
  409. schedules, getSchedulesErr := service.MobileGetOtherSchedulesForUrgentSchedule(adminUserInfo.Org.Id, today.Unix(), schedule_type)
  410. if getSchedulesErr != nil {
  411. this.ErrorLog("获取所有排班失败:%v", getSchedulesErr)
  412. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  413. return
  414. }
  415. this.ServeSuccessJSON(map[string]interface{}{
  416. "patients": patients,
  417. "modes": modes,
  418. "device_numbers": deviceNumbers,
  419. "schedules": schedules,
  420. })
  421. }
  422. // /m/api/schedule/urgentadd [post]
  423. // @param patient_id:int
  424. // @param schedule_type:int
  425. // @param mode:int
  426. // @param bed:int
  427. func (this *DialysisAPIController) AddUrgentSchedule() {
  428. patientID, _ := this.GetInt64("patient_id")
  429. scheduleType, _ := this.GetInt64("schedule_type")
  430. modeID, _ := this.GetInt64("mode")
  431. bedID, _ := this.GetInt64("bed")
  432. if patientID <= 0 || scheduleType < 1 || scheduleType > 3 || modeID < 1 || modeID > 14 || bedID <= 0 {
  433. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  434. return
  435. }
  436. adminUserInfo := this.GetMobileAdminUserInfo()
  437. patient, getPatientErr := service.GetPatientByID(adminUserInfo.Org.Id, patientID)
  438. if getPatientErr != nil {
  439. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  440. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  441. return
  442. } else if patient == nil {
  443. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  444. return
  445. }
  446. deviceNumber, getDeviceNumberErr := service.GetDeviceNumberByID(adminUserInfo.Org.Id, bedID)
  447. if getDeviceNumberErr != nil {
  448. this.ErrorLog("获取床位失败:%v", getDeviceNumberErr)
  449. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  450. return
  451. } else if deviceNumber == nil {
  452. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
  453. return
  454. }
  455. scheduleDate := utils.ZeroHourTimeOfDay(time.Now())
  456. weekday := int64(scheduleDate.Weekday())
  457. if weekday == 0 {
  458. weekday = 7
  459. }
  460. //判断这个人今天是否有排班
  461. daySchedule, dayScheduleErr := service.GetDayScheduleTwo(adminUserInfo.Org.Id, scheduleDate.Unix(), patientID)
  462. //判断当前时段该床位是否有排班
  463. schedule, err := service.GetDayScheduleByBedid(adminUserInfo.Org.Id, scheduleDate.Unix(), bedID, scheduleType)
  464. if dayScheduleErr == gorm.ErrRecordNotFound { //今天没有排班
  465. if err == gorm.ErrRecordNotFound { //空床位
  466. // 创建排班数据
  467. newSchedule := &models.Schedule{
  468. UserOrgId: adminUserInfo.Org.Id,
  469. PartitionId: deviceNumber.ZoneID,
  470. BedId: deviceNumber.ID,
  471. PatientId: patientID,
  472. ScheduleDate: scheduleDate.Unix(),
  473. ScheduleType: scheduleType,
  474. ScheduleWeek: weekday,
  475. ModeId: modeID,
  476. Status: 1,
  477. CreatedTime: time.Now().Unix(),
  478. UpdatedTime: time.Now().Unix(),
  479. }
  480. createErr := service.CreateSchedule(newSchedule)
  481. redis := service.RedisClient()
  482. timeStr := time.Now().Format("2006-01-02")
  483. key := "scheduals_" + timeStr + "_" + strconv.FormatInt(adminUserInfo.Org.Id, 10)
  484. redis.Set(key, "", time.Second)
  485. //处方
  486. keyOne := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":prescriptions_list_all"
  487. redis.Set(keyOne, "", time.Second)
  488. //医嘱
  489. keyTwo := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":advice_list_all"
  490. redis.Set(keyTwo, "", time.Second)
  491. keySix := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":assessment_befores_list_all"
  492. redis.Set(keySix, "", time.Second)
  493. keyThree := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":assessment_after_dislysis_list_all"
  494. redis.Set(keyThree, "", time.Second)
  495. keyFour := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":monitor_record_list_all"
  496. redis.Set(keyFour, "", time.Second)
  497. keyFive := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":treatment_summarys_list_all"
  498. redis.Set(keyFive, "", time.Second)
  499. keySeven := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":dialysis_orders_list_all"
  500. redis.Set(keySeven, "", time.Second)
  501. redis.Close()
  502. if createErr != nil {
  503. this.ErrorLog("紧急排班失败:%v", createErr)
  504. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateScheduleFail)
  505. return
  506. } else {
  507. this.ServeSuccessJSON(map[string]interface{}{
  508. "schedule": newSchedule,
  509. "patient": patient,
  510. })
  511. }
  512. } else if err == nil {
  513. if schedule.ID > 0 && schedule.DialysisOrder.ID == 0 { //该床位有排班没上机记录
  514. this.ServeFailJSONWithSGJErrorCode(enums.ErrorSchedualcRepeatBed)
  515. } else if schedule.ID > 0 && schedule.DialysisOrder.ID > 0 { //有排班且有上机记录
  516. this.ServeFailJSONWithSGJErrorCode(enums.ErrorDialysisOrderRepeatBed)
  517. return
  518. }
  519. } else if err != nil {
  520. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  521. return
  522. }
  523. } else if dayScheduleErr == nil { //今天有排班
  524. if daySchedule.ID > 0 && daySchedule.DialysisOrder.ID <= 0 { //今天有排班但是没上机
  525. if err == gorm.ErrRecordNotFound { //空床位
  526. // 修改了床位逻辑
  527. daySchedule, _ := service.GetDayScheduleTwo(adminUserInfo.Org.Id, scheduleDate.Unix(), patientID)
  528. if daySchedule.ID > 0 {
  529. daySchedule.PartitionId = deviceNumber.ZoneID
  530. daySchedule.BedId = bedID
  531. daySchedule.ModeId = modeID
  532. daySchedule.ScheduleType = scheduleType
  533. daySchedule.UpdatedTime = time.Now().Unix()
  534. err := service.UpdateSchedule(&daySchedule)
  535. redis := service.RedisClient()
  536. timeStr := time.Now().Format("2006-01-02")
  537. key := "scheduals_" + timeStr + "_" + strconv.FormatInt(adminUserInfo.Org.Id, 10)
  538. redis.Set(key, "", time.Second)
  539. //处方
  540. keyOne := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":prescriptions_list_all"
  541. redis.Set(keyOne, "", time.Second)
  542. //医嘱
  543. keyTwo := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":advice_list_all"
  544. redis.Set(keyTwo, "", time.Second)
  545. keySix := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":assessment_befores_list_all"
  546. redis.Set(keySix, "", time.Second)
  547. keyThree := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":assessment_after_dislysis_list_all"
  548. redis.Set(keyThree, "", time.Second)
  549. keyFour := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":monitor_record_list_all"
  550. redis.Set(keyFour, "", time.Second)
  551. keyFive := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":treatment_summarys_list_all"
  552. redis.Set(keyFive, "", time.Second)
  553. keySeven := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":dialysis_orders_list_all"
  554. redis.Set(keySeven, "", time.Second)
  555. redis.Close()
  556. if err != nil {
  557. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  558. return
  559. }
  560. this.ServeSuccessJSON(map[string]interface{}{
  561. "schedule": daySchedule,
  562. "patient": patient,
  563. })
  564. }
  565. } else if err == nil { //该床位被人霸占
  566. if schedule.ID > 0 && schedule.DialysisOrder.ID == 0 { //霸占该床位的病人有排班没上机记录
  567. this.ServeFailJSONWithSGJErrorCode(enums.ErrorSchedualcRepeatBed)
  568. return
  569. } else if schedule.ID > 0 && schedule.DialysisOrder.ID > 0 { //霸占该床位的病人有排班且有上机记录
  570. this.ServeFailJSONWithSGJErrorCode(enums.ErrorDialysisOrderRepeatBed)
  571. return
  572. }
  573. } else if err != nil {
  574. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  575. return
  576. }
  577. } else if daySchedule.ID > 0 && daySchedule.DialysisOrder.ID > 0 { //今天有排班且上机
  578. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientDialysisOrder)
  579. return
  580. }
  581. }
  582. }