LocalBusPatientService.java 13 KB

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