dialysis_api_controller_extend.go 22KB

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