LocalBusPatientService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package com.coffee.bus.service;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.date.LocalDateTimeUtil;
  5. import cn.hutool.json.JSONObject;
  6. import cn.hutool.json.JSONUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import cn.hutool.core.util.StrUtil;
  9. import com.baomidou.mybatisplus.core.metadata.IPage;
  10. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  11. import com.coffee.bus.entity.BusClinicEntity;
  12. import com.coffee.bus.entity.BusPatientEntity;
  13. import com.coffee.bus.entity.PatientDeviceRepeatDomain;
  14. import com.coffee.bus.enums.DeviceAlarmEnum;
  15. import com.coffee.bus.enums.DeviceStatusEnum;
  16. import com.coffee.bus.enums.PatientAlarmEnum;
  17. import com.coffee.bus.his.HisScriptSession;
  18. import com.coffee.bus.his.HisScriptSessionManager;
  19. import com.coffee.bus.his.strategy.HisStrategyManager;
  20. import com.coffee.bus.service.dto.*;
  21. import com.coffee.bus.mapper.BusPatientMapper;
  22. import com.coffee.bus.registry.patient.PatientOperator;
  23. import com.coffee.bus.registry.patient.PatientRegistry;
  24. import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
  25. import com.coffee.bus.utils.WsPublishUtils;
  26. import com.coffee.common.config.websocket.WebSocketConstant;
  27. import com.coffee.common.crud.BaseService;
  28. import com.coffee.common.enums.SexEnum;
  29. import com.coffee.common.result.R;
  30. import lombok.extern.slf4j.Slf4j;
  31. import net.bytebuddy.asm.Advice;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.context.annotation.Lazy;
  34. import org.springframework.scheduling.annotation.Async;
  35. import org.springframework.stereotype.Service;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import org.springframework.web.context.request.async.DeferredResult;
  38. import java.time.LocalDateTime;
  39. import java.time.ZoneOffset;
  40. import java.util.*;
  41. import java.util.concurrent.CompletableFuture;
  42. import java.util.concurrent.Executors;
  43. import java.util.concurrent.ScheduledExecutorService;
  44. import java.util.concurrent.TimeUnit;
  45. /**
  46. * @author lifang
  47. * @version 1.0.0
  48. * @ClassName LocalBusHospitalService.java
  49. * @Description TODO
  50. * @createTime 2022年03月19日 09:27:00
  51. */
  52. @Service
  53. @Slf4j
  54. public class LocalBusPatientService extends BaseService<BusPatientMapper, BusPatientEntity,String> {
  55. @Autowired
  56. @Lazy
  57. private LocalBusClinicService clinicService;
  58. @Autowired
  59. @Lazy
  60. private PatientRegistry patientRegistry;
  61. @Autowired
  62. @Lazy
  63. private HisScriptSessionManager scriptSessionManager;
  64. @Autowired
  65. @Lazy
  66. private WsPublishUtils wsPublishUtils;
  67. private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  68. @Override
  69. public void validateBeforeSave(BusPatientEntity entity) {
  70. if(entity.getGender()==null){
  71. entity.setGender(SexEnum.UNKNOW);
  72. }
  73. if(entity.getAlarm()==null){
  74. entity.setAlarm(PatientAlarmEnum.NONE);
  75. }
  76. }
  77. @Override
  78. public void validateBeforeUpdate(BusPatientEntity entity) {
  79. }
  80. @Override
  81. public void validateBeforeDelete(String id) {
  82. }
  83. @Override
  84. public void postSave(BusPatientEntity entity) {
  85. super.postSave(entity);
  86. //新增病人后推送主题,延迟推送,保证处理逻辑已全部完成
  87. executorService.schedule(()->{
  88. wsPublishUtils.publishPatientAdd(entity.getCode(),entity.getTenantId());
  89. wsPublishUtils.publishMonitorTotalCount(entity.getTenantId());
  90. }
  91. ,2, TimeUnit.SECONDS);
  92. }
  93. /**
  94. * 获取病人信息
  95. * @param hospitalId 医院id
  96. * @param patientCode 病号
  97. * @param sync 是否为同步操作
  98. */
  99. public DeferredResult<R<BusClinicEntity>> getPatientInfoFromHis(String hospitalId, String patientCode, long timeout,boolean sync){
  100. HisScriptSession hisScriptSession = scriptSessionManager.get(hospitalId);
  101. return sync?hisScriptSession.syncGetPatientInfo(patientCode,timeout,TimeUnit.SECONDS):hisScriptSession.asyncGetPatientInfo(patientCode,timeout,TimeUnit.SECONDS,true);
  102. }
  103. /**
  104. * 获取给定医院下所有的挂载多个设备的病人信息
  105. *
  106. */
  107. public List<PatientDeviceRepeatResult> repeatDevice() {
  108. List<PatientDeviceRepeatDomain> patientDeviceRepeats = this.baseMapper.selectRepeatDevice();
  109. Map<String, PatientDeviceRepeatResult> resultMap = new HashMap<>();
  110. patientDeviceRepeats.forEach(deviceRepeat->{
  111. PatientDeviceRepeatResult repeatResult = resultMap.computeIfAbsent(deviceRepeat.getCode()+deviceRepeat.getClinicId(),k->
  112. PatientDeviceRepeatResult.of(
  113. deviceRepeat.getName(),
  114. deviceRepeat.getGender(),
  115. deviceRepeat.getCode(),
  116. deviceRepeat.getAge(),
  117. deviceRepeat.getWard(),
  118. deviceRepeat.getBedNo(),
  119. deviceRepeat.getClinicName(),
  120. deviceRepeat.getClinicId(),
  121. new ArrayList<>())
  122. );
  123. List<PatientDeviceRepeatResult.DeviceRunningSmallInfo> deviceRunningSmallInfos = Optional.ofNullable(repeatResult.getDevices()).orElse(new ArrayList<>());
  124. deviceRunningSmallInfos.add(PatientDeviceRepeatResult.DeviceRunningSmallInfo.of(
  125. deviceRepeat.getDeviceRunningId(),
  126. deviceRepeat.getDeviceId(),
  127. deviceRepeat.getDeviceAlias(),
  128. deviceRepeat.getDeviceRunState(),
  129. deviceRepeat.getDeviceAlarm(),
  130. deviceRepeat.getInfusionStartTime(),
  131. deviceRepeat.getMaster()
  132. ));
  133. repeatResult.setDevices(deviceRunningSmallInfos);
  134. });
  135. return new ArrayList<>(resultMap.values());
  136. }
  137. /**
  138. * 获取无设备绑定的临床手术信息
  139. * @return
  140. */
  141. public List<PatientDeviceNoneResult> noneDevice() {
  142. return this.baseMapper.selectNoneDevice();
  143. }
  144. public List<PatientMonitorResult> selectAll(PatientMonitorQuery query) {
  145. Page<PatientMonitorResult> page = new Page<>(0, 500, false);
  146. IPage<PatientMonitorResult> result = this.baseMapper.selectMonitor(page,query);
  147. if(CollectionUtil.isNotEmpty(result.getRecords())){
  148. result.getRecords().forEach(PatientMonitorResult::handleWarn);
  149. }
  150. return result.getRecords();
  151. }
  152. /**
  153. * 根据医院和住院号获取一个患者
  154. * @param tenantId
  155. * @param patientCode
  156. * @return
  157. */
  158. public BusPatientEntity getOneByHospitalAndPatientCode(String tenantId, String patientCode) {
  159. BusPatientEntity patient = this.getOne(new QueryWrapper<BusPatientEntity>().lambda()
  160. .eq(BusPatientEntity::getTenantId,tenantId)
  161. .eq(BusPatientEntity::getCode,patientCode));
  162. // 如果患者不存在则新增一个患者
  163. if (Objects.isNull(patient)){
  164. patient = new BusPatientEntity();
  165. patient.setTenantId(tenantId);
  166. patient.setCode(patientCode);
  167. this.save(patient);
  168. }
  169. return patient;
  170. }
  171. /**
  172. *
  173. * 病人手动更新当前监控的临床信息 todo
  174. * @param clinic
  175. */
  176. @Transactional(rollbackFor = Exception.class)
  177. public void manualEdit(BusClinicEntity clinic) {
  178. //先更新手术信息 todo
  179. clinicService.saveOrUpdate(clinic);
  180. //后更新病人信息
  181. BusPatientEntity patient = BusPatientEntity.of(clinic);
  182. PatientOperator<PatientCacheInfo> patientOperator = patientRegistry.getOperator(patient.getTenantId(), patient.getCode());
  183. if (StrUtil.isEmpty(patientOperator.getCode())) {
  184. this.save(patient);
  185. patientOperator.setCode(patient.getCode());
  186. patientOperator.setTenantId(patient.getTenantId());
  187. }else {
  188. BusPatientEntity existPatient = this.getOne(new QueryWrapper<BusPatientEntity>().lambda().eq(BusPatientEntity::getCode, patient.getCode()).last("limit 1"));
  189. patient.setId(existPatient.getId());
  190. this.updateById(patient);
  191. }
  192. patientOperator.setClinicId(patient.getClinicId());
  193. patientOperator.setName(patient.getName());
  194. patientOperator.setBedNo(clinic.getBedNo());
  195. patientOperator.setWard(clinic.getWard());
  196. CompletableFuture.runAsync(()->{
  197. wsPublishUtils.publishPatientMonitor(patient.getCode(),patient.getTenantId());
  198. });
  199. }
  200. /**
  201. * 根据病号查询临床监控记录
  202. * @author lifang
  203. * @param patientCode 病号
  204. * @param tenantId 医院id
  205. * @return
  206. */
  207. public PatientMonitorResult lookMonitorByPatientCode(String patientCode,String tenantId) {
  208. try {
  209. return this.baseMapper.findByPatientCode(tenantId, patientCode);
  210. } catch (Exception e) {
  211. log.error("根据病号查询临床失败,",e.getMessage());
  212. return null;
  213. }
  214. }
  215. /**
  216. * 描述: 设备状态数量统计
  217. * @author lifang
  218. * @date 2022/5/8 21:52
  219. * @param tenantId 医院id 用户请求时传输null
  220. * @return MonitorStatusStatsCountResult
  221. */
  222. public MonitorStatusStatsCountResult statusStats(String tenantId) {
  223. PatientMonitorQuery query = new PatientMonitorQuery();
  224. query.setTenantId(tenantId);
  225. List<PatientMonitorResult> patientMonitorResults = this.selectAll(query);
  226. MonitorStatusStatsCountResult result = new MonitorStatusStatsCountResult();
  227. if(CollectionUtil.isNotEmpty(patientMonitorResults)){
  228. patientMonitorResults.forEach(monitor->{
  229. //运行数量
  230. if(DeviceStatusEnum.Running.equals(monitor.getDeviceRunState())){
  231. result.setRunningCount(result.getRunningCount()+1);
  232. }
  233. //todo 待结束数量
  234. //报警数量
  235. if(monitor.getDeviceAlarm()!=null&&!monitor.getDeviceAlarm().equals(DeviceAlarmEnum.None)){
  236. result.setAlarmCount(result.getAlarmCount()+1);
  237. }
  238. //提醒数量
  239. if(Boolean.TRUE.equals(monitor.getWarnAnalgesicPoor())
  240. ||Boolean.TRUE.equals(monitor.getWarnLowBattery())
  241. ||Boolean.TRUE.equals(monitor.getWarnWillFinished())
  242. ||monitor.getWarnFlow()!=null){
  243. result.setWarnCount(result.getWarnCount()+1);
  244. }
  245. });
  246. }
  247. return result;
  248. }
  249. /**
  250. * 描述: 按照时间对输注监控进行统计
  251. * @author lifang
  252. * @date 2022/5/8 22:40
  253. * @param tenantId 医院id
  254. * @return MonitorTimeStatsCountResult
  255. */
  256. public MonitorTimeStatsCountResult timeStats(String tenantId) {
  257. PatientMonitorQuery query = new PatientMonitorQuery();
  258. query.setTenantId(tenantId);
  259. List<PatientMonitorResult> patientMonitorResults = this.selectAll(query);
  260. MonitorTimeStatsCountResult result = new MonitorTimeStatsCountResult();
  261. patientMonitorResults.forEach(monitor->{
  262. Date startTime = monitor.getMonitorStartTime();
  263. if(startTime==null){
  264. return;
  265. }
  266. if (includeTimes(startTime, 0)) {
  267. result.setToday(result.getToday()+1);
  268. return;
  269. }
  270. if (includeTimes(startTime, -1)) {
  271. result.setOneDay(result.getOneDay()+1);
  272. return;
  273. }
  274. if (includeTimes(startTime, -2)) {
  275. result.setTwoDay(result.getTwoDay()+1);
  276. return;
  277. }
  278. if (includeTimes(startTime, -3)) {
  279. result.setThreeDay(result.getThreeDay()+1);
  280. return;
  281. }
  282. result.setBeyondThreeDay(result.getBeyondThreeDay()+1);
  283. });
  284. return result;
  285. }
  286. /**
  287. * 描述: 判断所给时间是否在存在于某一时间段内
  288. * @author lifang
  289. * @date 2022/5/8 22:47
  290. * @param time 时间
  291. * @param offset 时间偏移量,单位:天 以当天时间为基准进行偏移
  292. * @return boolean
  293. */
  294. private boolean includeTimes(Date time,int offset){
  295. if(time==null){
  296. return false;
  297. }
  298. LocalDateTime dateTime = LocalDateTime.now().plusDays(offset);
  299. LocalDateTime beginTime = LocalDateTimeUtil.beginOfDay(dateTime);
  300. LocalDateTime endTime = LocalDateTimeUtil.endOfDay(dateTime);
  301. return beginTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()<time.getTime()
  302. && time.getTime()<endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  303. }
  304. }