| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package com.coffee.bus.service;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.date.LocalDateTimeUtil;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.coffee.bus.entity.BusClinicEntity;
- import com.coffee.bus.entity.BusPatientEntity;
- import com.coffee.bus.entity.PatientDeviceRepeatDomain;
- import com.coffee.bus.enums.DeviceAlarmEnum;
- import com.coffee.bus.enums.DeviceStatusEnum;
- import com.coffee.bus.service.dto.*;
- import com.coffee.bus.mapper.BusPatientMapper;
- import com.coffee.bus.registry.patient.PatientOperator;
- import com.coffee.bus.registry.patient.PatientRegistry;
- import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
- import com.coffee.bus.utils.WsPublishUtils;
- import com.coffee.common.config.websocket.WebSocketConstant;
- import com.coffee.common.crud.BaseService;
- import com.coffee.common.enums.SexEnum;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- /**
- * @author lifang
- * @version 1.0.0
- * @ClassName LocalBusHospitalService.java
- * @Description TODO
- * @createTime 2022年03月19日 09:27:00
- */
- @Service
- @Slf4j
- public class LocalBusPatientService extends BaseService<BusPatientMapper, BusPatientEntity,String> {
- @Autowired
- @Lazy
- private LocalBusClinicService clinicService;
- @Autowired
- @Lazy
- private PatientRegistry patientRegistry;
- @Autowired
- private WsPublishUtils wsPublishUtils;
- private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
- @Autowired
- @Lazy
- private LocalBusPatientService patientService;
- @Override
- public void validateBeforeSave(BusPatientEntity entity) {
- if(entity.getGender()==null){
- entity.setGender(SexEnum.UNKNOW);
- }
- }
- @Override
- public void validateBeforeUpdate(BusPatientEntity entity) {
- }
- @Override
- public void validateBeforeDelete(String id) {
- }
- @Override
- public void postSave(BusPatientEntity entity) {
- super.postSave(entity);
- //新增病人后推送主题,延迟推送,保证处理逻辑已全部完成
- executorService.schedule(()->{
- wsPublishUtils.publish(WebSocketConstant.getPatientAdd(entity.getTenantId()).getTopic(),
- new JSONObject().putOpt("patientCode",entity.getCode()));
- }
- ,3, TimeUnit.SECONDS);
- }
- /**
- * 异步获取病人信息 todo
- * @param hospitalId 医院id
- * @param patientCode 病号
- */
- @Async
- public void syncGetPatientInfoFromHis(String hospitalId,String patientCode){
- }
- /**
- * 获取给定医院下所有的挂载多个设备的病人信息
- *
- */
- public List<PatientDeviceRepeatResult> repeatDevice() {
- List<PatientDeviceRepeatDomain> patientDeviceRepeats = this.baseMapper.selectRepeatDevice();
- Map<String, PatientDeviceRepeatResult> resultMap = new HashMap<>();
- patientDeviceRepeats.forEach(deviceRepeat->{
- PatientDeviceRepeatResult repeatResult = resultMap.computeIfAbsent(deviceRepeat.getCode(),k->
- PatientDeviceRepeatResult.of(
- deviceRepeat.getName(),
- deviceRepeat.getGender(),
- deviceRepeat.getCode(),
- deviceRepeat.getAge(),
- deviceRepeat.getWard(),
- deviceRepeat.getBedNo(),
- deviceRepeat.getClinicName(),
- new ArrayList<>())
- );
- List<PatientDeviceRepeatResult.DeviceRunningSmallInfo> deviceRunningSmallInfos = Optional.ofNullable(repeatResult.getDevices()).orElse(new ArrayList<>());
- deviceRunningSmallInfos.add(PatientDeviceRepeatResult.DeviceRunningSmallInfo.of(
- deviceRepeat.getDeviceRunningId(),
- deviceRepeat.getDeviceId(),
- deviceRepeat.getDeviceAlias(),
- deviceRepeat.getDeviceRunState(),
- deviceRepeat.getDeviceAlarm(),
- deviceRepeat.getInfusionStartTime(),
- deviceRepeat.getMaster()
- ));
- repeatResult.setDevices(deviceRunningSmallInfos);
- });
- return new ArrayList<>(resultMap.values());
- }
- /**
- * 获取无设备绑定的临床手术信息
- * @return
- */
- public List<PatientDeviceNoneResult> noneDevice() {
- return this.baseMapper.selectNoneDevice();
- }
- public List<PatientMonitorResult> selectAll(PatientMonitorQuery query) {
- Page<PatientMonitorResult> page = new Page<>(0, 500, false);
- IPage<PatientMonitorResult> result = this.baseMapper.selectMonitor(page,query);
- if(CollectionUtil.isNotEmpty(result.getRecords())){
- result.getRecords().forEach(PatientMonitorResult::handleWarn);
- }
- return result.getRecords();
- }
- /**
- * 根据医院和住院号获取一个患者
- * @param tenantId
- * @param patientCode
- * @return
- */
- public BusPatientEntity getOneByHospitalAndPatientCode(String tenantId, String patientCode) {
- BusPatientEntity patient = this.getOne(new QueryWrapper<BusPatientEntity>().lambda()
- .eq(BusPatientEntity::getTenantId,tenantId)
- .eq(BusPatientEntity::getCode,patientCode));
- // 如果患者不存在则新增一个患者
- if (Objects.isNull(patient)){
- patient = new BusPatientEntity();
- patient.setTenantId(tenantId);
- patient.setCode(patientCode);
- this.save(patient);
- }
- return patient;
- }
- /**
- *
- * 病人手动更新当前监控的临床信息 todo
- * @param clinic
- */
- @Transactional(rollbackFor = Exception.class)
- public void manualEdit(BusClinicEntity clinic) {
- //先更新手术信息 todo
- clinicService.saveOrUpdate(clinic);
- //后更新病人信息
- BusPatientEntity patient = BusPatientEntity.of(clinic);
- PatientOperator<PatientCacheInfo> patientOperator = patientRegistry.getOperator(patient.getTenantId(), patient.getCode());
- if (StrUtil.isEmpty(patientOperator.getCode())) {
- this.save(patient);
- patientOperator.setCode(patient.getCode());
- patientOperator.setTenantId(patient.getTenantId());
- }else {
- BusPatientEntity existPatient = this.getOne(new QueryWrapper<BusPatientEntity>().lambda().eq(BusPatientEntity::getCode, patient.getCode()).last("limit 1"));
- patient.setId(existPatient.getId());
- this.updateById(patient);
- }
- patientOperator.setClinicId(patient.getClinicId());
- patientOperator.setName(patient.getName());
- patientOperator.setBedNo(clinic.getBedNo());
- patientOperator.setWard(clinic.getWard());
- CompletableFuture.runAsync(()->{
- wsPublishUtils.publish(WebSocketConstant.getPatientMonitor(null,patient.getCode(), patient.getTenantId()).getTopic(),
- patientService.lookMonitorByPatientCode(patient.getCode(),patient.getTenantId()));
- });
- }
- /**
- * 根据病号查询临床监控记录
- * @author lifang
- * @param patientCode 病号
- * @param tenantId 医院id
- * @return
- */
- public PatientMonitorResult lookMonitorByPatientCode(String patientCode,String tenantId) {
- try {
- return this.baseMapper.findByPatientCode(tenantId, patientCode);
- } catch (Exception e) {
- log.error("根据病号查询临床失败,",e.getMessage());
- return null;
- }
- }
- /**
- * 描述: 设备状态数量统计
- * @author lifang
- * @date 2022/5/8 21:52
- * @param tenantId 医院id 用户请求时传输null
- * @return MonitorStatusStatsCountResult
- */
- public MonitorStatusStatsCountResult statusStats(String tenantId) {
- PatientMonitorQuery query = new PatientMonitorQuery();
- query.setTenantId(tenantId);
- List<PatientMonitorResult> patientMonitorResults = this.selectAll(query);
- MonitorStatusStatsCountResult result = new MonitorStatusStatsCountResult();
- if(CollectionUtil.isNotEmpty(patientMonitorResults)){
- patientMonitorResults.forEach(monitor->{
- //运行数量
- if(DeviceStatusEnum.Running.equals(monitor.getDeviceRunState())){
- result.setRunningCount(result.getRunningCount()+1);
- }
- //todo 待结束数量
- //报警数量
- if(monitor.getDeviceAlarm()!=null&&!monitor.getDeviceAlarm().equals(DeviceAlarmEnum.None)){
- result.setAlarmCount(result.getAlarmCount()+1);
- }
- //提醒数量
- if(Boolean.TRUE.equals(monitor.getWarnAnalgesicPoor())
- ||Boolean.TRUE.equals(monitor.getWarnLowBattery())
- ||Boolean.TRUE.equals(monitor.getWarnWillFinished())
- ||monitor.getWarnFlow()!=null){
- result.setWarnCount(result.getWarnCount()+1);
- }
- });
- }
- return result;
- }
- /**
- * 描述: 按照时间对输注监控进行统计
- * @author lifang
- * @date 2022/5/8 22:40
- * @param tenantId 医院id
- * @return MonitorTimeStatsCountResult
- */
- public MonitorTimeStatsCountResult timeStats(String tenantId) {
- PatientMonitorQuery query = new PatientMonitorQuery();
- query.setTenantId(tenantId);
- List<PatientMonitorResult> patientMonitorResults = this.selectAll(query);
- MonitorTimeStatsCountResult result = new MonitorTimeStatsCountResult();
- patientMonitorResults.forEach(monitor->{
- Date startTime = monitor.getMonitorStartTime();
- if(startTime==null){
- return;
- }
- if (includeTimes(startTime, 0)) {
- result.setToday(result.getToday()+1);
- return;
- }
- if (includeTimes(startTime, 1)) {
- result.setOneDay(result.getOneDay()+1);
- return;
- }
- if (includeTimes(startTime, 2)) {
- result.setTwoDay(result.getTwoDay()+1);
- return;
- }
- if (includeTimes(startTime, 3)) {
- result.setThreeDay(result.getThreeDay()+1);
- return;
- }
- result.setBeyondThreeDay(result.getBeyondThreeDay()+1);
- });
- return result;
- }
- /**
- * 描述: 判断所给时间是否在存在于某一时间段内
- * @author lifang
- * @date 2022/5/8 22:47
- * @param time 时间
- * @param offset 时间偏移量,单位:天 以当天时间为基准进行偏移
- * @return boolean
- */
- private boolean includeTimes(Date time,int offset){
- if(time==null){
- return false;
- }
- LocalDateTime dateTime = LocalDateTime.now().plusDays(offset);
- LocalDateTime beginTime = LocalDateTimeUtil.beginOfDay(dateTime);
- LocalDateTime endTime = LocalDateTimeUtil.endOfDay(dateTime);
- return beginTime.getSecond()<time.getSeconds()
- && time.getSeconds()<endTime.getSecond();
- }
- }
|