Browse Source

add 病人当前监护状态统计

A17404李放 3 years ago
parent
commit
ce934fdc57

+ 5 - 7
coffee-common/src/main/java/com/coffee/common/config/websocket/WebSocketConstant.java

@@ -6,7 +6,7 @@ import com.coffee.common.config.websocket.handler.TopicWrapper;
 /**
  * @author lifang
  * @version 1.0.0
- * @ClassName WebSocketContants.java
+ * @ClassName WebSocketConstants.java
  * @Description websocket订阅所用常量
  * @createTime 2022年03月25日 14:25:00
  */
@@ -14,7 +14,7 @@ public class WebSocketConstant {
 
     public static final String ALARM_COUNT="alarm-count";
     public static final String DEVICE_INFO_DETAIL="device-info-detail";
-    public static final String DEVICE_STATE_COUNT="device-state-count";
+    public static final String MONITOR_STATE_COUNT ="monitor-state-count";
     public static final String CLINIC_INFO = "clinic-info";
     public static final String DEVICE_CONFLICT = "device-conflict";
     /**
@@ -61,13 +61,11 @@ public class WebSocketConstant {
 
     /**
      * 获取 设备状态变化主题
-     * @param productName
-     * @param param
-     * @param tenantId
+     * @param tenantId 设备所属医院
      * @return
      */
-    public static TopicWrapper getDeviceStateCount(String productName,String param,String tenantId){
-        return getTopic(DEVICE_STATE_COUNT,productName,param,tenantId);
+    public static TopicWrapper getDeviceStateCount(String tenantId){
+        return getTopic(MONITOR_STATE_COUNT,null,null,tenantId);
     }
 
 

+ 6 - 0
coffee-common/src/main/java/com/coffee/common/crud/BaseService.java

@@ -27,6 +27,7 @@ import com.coffee.common.exception.CustomException;
 import org.apache.ibatis.binding.MapperMethod;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.jdbc.BadSqlGrammarException;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.io.Serializable;
 import java.sql.SQLSyntaxErrorException;
@@ -63,6 +64,7 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean saveBatch(Collection<E> entityList, int batchSize) {
         entityList.forEach(this::validateBeforeSave);
         if (super.saveBatch(entityList, batchSize)) {
@@ -82,6 +84,7 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
 
     @Override
     @Deprecated
+    @Transactional(rollbackFor = Exception.class)
     public boolean saveOrUpdate(E entity) {
         if (null != entity) {
             TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
@@ -114,6 +117,7 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
      * @return boolean
      **/
     @Override
+    @Transactional(rollbackFor = Exception.class)
     @Deprecated
     public boolean saveOrUpdateBatch(Collection<E> entityList, int batchSize) {
         TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
@@ -187,6 +191,7 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean updateById(E entity) {
         validateBeforeUpdate(entity);
         if (super.updateById(entity)) {
@@ -197,6 +202,7 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean updateBatchById(Collection<E> entityList) {
         entityList.forEach(this::validateBeforeUpdate);
         if (super.updateBatchById(entityList)) {

+ 6 - 17
coffee-system/src/main/java/com/coffee/bus/controller/BusDeviceController.java

@@ -19,6 +19,7 @@ import com.coffee.common.exception.CustomException;
 import com.coffee.common.result.R;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
@@ -42,37 +43,25 @@ import java.util.Objects;
 @Api(tags = "设备管理",description = "统一权限前缀(device:info),device:info:add")
 public class BusDeviceController extends BaseCrudController<BusDeviceEntity, String> {
     private final LocalBusDeviceService deviceService;
-    private final DeviceRegistry deviceRegistry;
 
     @PostMapping("/shift/bind")
     @ApiOperation(value = "设备换绑",notes = "权限【device:info:shift】")
-    public R shift(@RequestBody DeviceBindVo vo){
+    public R shift(@RequestBody DeviceBindVo vo,@RequestAttribute("tenantId")@ApiParam(hidden = true) String tenantId){
         if(StrUtil.isEmpty(vo.getBindTenantId())|| CollectionUtil.isEmpty(vo.getDeviceIds())){
             throw new CustomException("选择的医院、设备不能为空");
         }
-        List<String> deviceIds = vo.getDeviceIds();
-        deviceService.update(new UpdateWrapper<BusDeviceEntity>().lambda()
-                .in(BusDeviceEntity::getDeviceId,deviceIds)
-                .set(BusDeviceEntity::getTenantId,vo.getBindTenantId()));
-        deviceIds.stream()
-                .map(deviceRegistry::getOperator)
-                .forEach(deviceOperator -> deviceOperator.setTenantId(vo.getBindTenantId()));
+        deviceService.shift(tenantId,vo.getDeviceIds(),vo.getBindTenantId());
         return R.success(DeviceAlarmEnum.values());
     }
 
     @PostMapping("/undo/bind")
     @ApiOperation(value = "设备解绑",notes = "即自动将该设备绑定到默认医院上,权限【device:info:undo】")
-    public R undo(@RequestBody DeviceBindVo vo){
+    public R undo(@RequestBody DeviceBindVo vo,@RequestAttribute("tenantId")@ApiParam(hidden = true) String tenantId){
         if( CollectionUtil.isEmpty(vo.getDeviceIds())){
             throw new CustomException("选择的设备不能为空");
         }
-        List<String> deviceIds = vo.getDeviceIds();
-        deviceService.update(new UpdateWrapper<BusDeviceEntity>().lambda()
-                .in(BusDeviceEntity::getDeviceId,deviceIds)
-                .set(BusDeviceEntity::getTenantId,"1"));
-        deviceIds.stream()
-                .map(deviceRegistry::getOperator)
-                .forEach(deviceOperator -> deviceOperator.setTenantId("1"));
+        //1为默认医院id
+        deviceService.shift(tenantId,vo.getDeviceIds(),"1");
         return R.success(DeviceAlarmEnum.values());
     }
 

+ 7 - 0
coffee-system/src/main/java/com/coffee/bus/controller/BusPatientController.java

@@ -214,6 +214,13 @@ public class BusPatientController  {
         return R.success(result);
     }
 
+    @PostMapping("/stats")
+    @SaCheckPermission("device:patient:query")
+    @ApiOperation(value = "病人监控当前状态统计",notes = "权限【device:patient:query】")
+    public R<DeviceStatusStatsCountResult> stats(){
+        return R.success(patientService.statusStats(null));
+    }
+
     //todo 使用
     @PostMapping("/syn/{hospitalId}/{patientCode}")
     @ApiOperation(value = "同步更新患者信息,即等待更新完成后返回更新结果")

+ 5 - 0
coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceRunningService.java

@@ -1,15 +1,19 @@
 package com.coffee.bus.service;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.coffee.bus.entity.BusPatientEntity;
+import com.coffee.bus.enums.DeviceAlarmEnum;
+import com.coffee.bus.enums.DeviceStatusEnum;
 import com.coffee.bus.registry.device.DeviceOperator;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
 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.service.dto.DeviceStatusStatsCountResult;
 import com.coffee.bus.service.dto.UndoDeviceConfig;
 import com.coffee.bus.service.dto.DeviceShiftConfig;
 import com.coffee.bus.service.dto.ManualUndoConfig;
@@ -185,4 +189,5 @@ public class LocalBusDeviceRunningService extends BaseService<BusDeviceRunningMa
         masterOperator.setMaster(true);
         patientOperator.setBindDeviceId(shiftConfig.getMasterDeviceId());
     }
+
 }

+ 33 - 1
coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceService.java

@@ -3,8 +3,10 @@ package com.coffee.bus.service;
 import cn.hutool.core.util.StrUtil;
 import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailResponse;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.coffee.aliyun.sdk.AliyunIotSdk;
 import com.coffee.bus.bean.AliIotConfig;
+import com.coffee.bus.entity.BusHospitalEntity;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.entity.BusDeviceEntity;
 import com.coffee.bus.mapper.BusDeviceMapper;
@@ -14,7 +16,9 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -39,6 +43,10 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
     @Autowired
     private AliyunIotSdk aliyunIotSdk;
 
+    @Autowired
+    @Lazy
+    private LocalBusHospitalService hospitalService;
+
     @Override
     public void validateBeforeSave(BusDeviceEntity entity) {
 
@@ -56,7 +64,8 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
 
     @Override
     public void postSave(BusDeviceEntity entity) {
-
+        //设备新增成功后,更新医院设备数量信息
+        hospitalService.update(new UpdateWrapper<BusHospitalEntity>().lambda().eq(BusHospitalEntity::getId,entity.getTenantId()).setSql("device_count=device_count+1"));
     }
 
     @Override
@@ -176,4 +185,27 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
         });
         return n.get();
     }
+
+    /**
+     * 描述: 设备医院换绑操作
+     * @author lifang
+     * @date 2022/5/8 21:34
+     * @param beforeTenantId  换绑之前的医院id
+     * @param deviceIds 换绑的设备id
+     * @param afterTenantId 换绑之后的医院id
+     * @return void
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void shift(String beforeTenantId,List<String> deviceIds, String afterTenantId) {
+        hospitalService.update(new UpdateWrapper<BusHospitalEntity>().lambda().eq(BusHospitalEntity::getId,beforeTenantId).setSql("device_count=device_count-"+deviceIds.size()));
+        this.update(new UpdateWrapper<BusDeviceEntity>().lambda()
+                .in(BusDeviceEntity::getDeviceId,deviceIds)
+                .set(BusDeviceEntity::getTenantId,afterTenantId));
+
+        hospitalService.update(new UpdateWrapper<BusHospitalEntity>().lambda().eq(BusHospitalEntity::getId,afterTenantId).setSql("device_count=device_count+"+deviceIds.size()));
+
+        deviceIds.stream()
+                .map(deviceRegistry::getOperator)
+                .forEach(deviceOperator -> deviceOperator.setTenantId(afterTenantId));
+    }
 }

+ 39 - 4
coffee-system/src/main/java/com/coffee/bus/service/LocalBusPatientService.java

@@ -6,16 +6,16 @@ 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.BusDeviceRunningEntity;
 import com.coffee.bus.entity.BusPatientEntity;
 import com.coffee.bus.entity.PatientDeviceRepeatDomain;
-import com.coffee.bus.service.dto.PatientMonitorResult;
+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.service.dto.PatientDeviceNoneResult;
-import com.coffee.bus.service.dto.PatientDeviceRepeatResult;
-import com.coffee.bus.service.dto.PatientMonitorQuery;
 import com.coffee.common.crud.BaseService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -183,4 +183,39 @@ public class LocalBusPatientService extends BaseService<BusPatientMapper, BusPat
             return null;
         }
     }
+
+    /**
+     * 描述: 设备状态数量统计
+     * @author lifang
+     * @date 2022/5/8 21:52
+     * @param tenantId 医院id 用户请求时传输null
+     * @return DeviceStatusStatsCountResult
+     */
+    public DeviceStatusStatsCountResult statusStats(String tenantId) {
+        PatientMonitorQuery query = new PatientMonitorQuery();
+        query.setTenantId(tenantId);
+        List<PatientMonitorResult> patientMonitorResults = this.selectAll(query);
+        DeviceStatusStatsCountResult result = new DeviceStatusStatsCountResult();
+        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;
+    }
 }

+ 0 - 2
coffee-system/src/main/java/com/coffee/bus/service/dto/DeviceStatusStatsCountResult.java

@@ -14,8 +14,6 @@ import lombok.Data;
 @ApiModel("设备状态数量统计结果")
 @Data
 public class DeviceStatusStatsCountResult {
-    @ApiModelProperty("全部数量")
-    private int totalCount;
 
     @ApiModelProperty("运行数量")
     private int runningCount;

+ 3 - 0
coffee-system/src/main/java/com/coffee/bus/service/dto/PatientMonitorQuery.java

@@ -92,6 +92,9 @@ public class PatientMonitorQuery {
     @ApiModelProperty("查询时间区间")
     private List<Timestamp> timeRange;
 
+    @ApiModelProperty(value = "医院id",hidden = true)
+    @JsonIgnore
+    private String tenantId;
     /**
      * 将前端的提醒查询做统一封装,做请求处理
      * @param warns

+ 2 - 3
coffee-system/src/main/java/com/coffee/bus/websocket/DeviceStateCountHandler.java → coffee-system/src/main/java/com/coffee/bus/websocket/MonitorStateCountHandler.java

@@ -1,6 +1,5 @@
 package com.coffee.bus.websocket;
 
-import com.coffee.common.config.websocket.MessagingRequest;
 import com.coffee.common.config.websocket.WebSocketConstant;
 import com.coffee.common.config.websocket.handler.Subscribe;
 import org.springframework.stereotype.Component;
@@ -14,11 +13,11 @@ import org.tio.core.ChannelContext;
  * @createTime 2022年03月25日 14:21:00
  */
 @Component
-public class DeviceStateCountHandler  extends Subscribe {
+public class MonitorStateCountHandler extends Subscribe {
 
     @Override
     public String getId() {
-        return WebSocketConstant.DEVICE_STATE_COUNT;
+        return WebSocketConstant.MONITOR_STATE_COUNT;
     }
 
     @Override

+ 6 - 5
coffee-system/src/main/java/com/coffee/bus/websocket/listener/DeviceInfoListener.java

@@ -52,8 +52,6 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 public class DeviceInfoListener {
 
-    private final RedisTemplate redisTemplate;
-
     private final LocalBusDeviceRunningService deviceUsingService;
 
     private final DeviceRegistry deviceRegistry;
@@ -75,6 +73,7 @@ public class DeviceInfoListener {
     private final LocalBusClinicService clinicService;
 
     private final WsPublishUtils wsPublishUtils;
+
     /**
      * 监听上传的数据信息,
      * 若设备详情发生变化,则及时通知相应的ws通道
@@ -116,7 +115,7 @@ public class DeviceInfoListener {
             //处理设备运行数据,     判断是否为注册后第一次开机,判断是否为新的输注信息
             boolean first = handleRunningInfo(device, deviceOperator,newInfusion,cacheOperation);
             //处理输注参数
-            BusInfusionHistoryEntity infusionHistory = handleInfusion(device, deviceOperator, newInfusion, cacheOperation);
+            handleInfusion(device, deviceOperator, newInfusion, cacheOperation);
             //处理历史运行数据
             BusDeviceHistoryEntity history=handleRunningHistory(device);
             //处理报警信息
@@ -136,12 +135,14 @@ public class DeviceInfoListener {
             //医院相关配置处理
             handleHospitalConfig(device,cacheOperation);
 
-            //则推送设备上报输注消息
+            //异步推送
             cacheOperation.add(()->{
-                //异步推送
                 CompletableFuture.runAsync(()->{
+                    //推送设备上报输注消息
                     wsPublishUtils.publish(WebSocketConstant.getPatientMonitor(null, device.getPatientCode(), device.getTenantId()).getTopic(),
                             patientService.lookMonitorByPatientCode(device.getPatientCode(),device.getTenantId()));
+                    //推送设备状态
+                    wsPublishUtils.publish(WebSocketConstant.getDeviceStateCount(device.getTenantId()).getTopic(),patientService.statusStats(device.getTenantId()));
                 });
                 return null;
             });

+ 3 - 0
coffee-system/src/main/resources/mapper/bus/BusPatientMapper.xml

@@ -189,6 +189,9 @@
         from
         (select * from bus_patient
         <where>
+            <if test="query.tenantId!=null">
+              and tenant_id=#{query.tenantId}
+            </if>
             <if test="query.timeRange != null and query.timeRange.size >0">
                 and tmp_finished &gt; #{query.timeRange[0]} and  tmp_finished &lt; #{query.timeRange[1]}
             </if>