dialysis_api_controller_extend.go 21KB

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