dialysis_api_controller_extend.go 25KB

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