Przeglądaj źródła

add 修改数据缓存方式

18339543638 3 lat temu
rodzic
commit
a25fd414cc
20 zmienionych plików z 727 dodań i 374 usunięć
  1. 7 6
      coffee-common/src/main/java/com/coffee/common/crud/BaseService.java
  2. 3 5
      coffee-common/src/main/java/com/coffee/common/entity/param/TermType.java
  3. 3 1
      coffee-framework/src/main/java/com/coffee/framework/config/mybatisplus/MybatisPlusConfig.java
  4. 7 4
      coffee-system/src/main/java/com/coffee/bus/entity/BusHospitalEntity.java
  5. 7 7
      coffee-system/src/main/java/com/coffee/bus/entity/BusPumpEntity.java
  6. 7 7
      coffee-system/src/main/java/com/coffee/bus/entity/BusPumpHistoryEntity.java
  7. 51 0
      coffee-system/src/main/java/com/coffee/bus/registry/Operator.java
  8. 59 29
      coffee-system/src/main/java/com/coffee/bus/registry/device/ClusterDeviceOperator.java
  9. 14 6
      coffee-system/src/main/java/com/coffee/bus/registry/device/ClusterDeviceRegistry.java
  10. 3 14
      coffee-system/src/main/java/com/coffee/bus/registry/device/DeviceOperator.java
  11. 0 1
      coffee-system/src/main/java/com/coffee/bus/registry/device/DeviceRegistry.java
  12. 224 0
      coffee-system/src/main/java/com/coffee/bus/registry/patient/ClusterPatientOperator.java
  13. 34 51
      coffee-system/src/main/java/com/coffee/bus/registry/patient/ClusterPatientRegistry.java
  14. 113 133
      coffee-system/src/main/java/com/coffee/bus/registry/patient/PatientOperator.java
  15. 7 0
      coffee-system/src/main/java/com/coffee/bus/registry/patient/PatientRegistry.java
  16. 35 0
      coffee-system/src/main/java/com/coffee/bus/registry/patient/bean/DeviceTimeSmallInfo.java
  17. 37 7
      coffee-system/src/main/java/com/coffee/bus/registry/patient/bean/PatientCacheInfo.java
  18. 1 1
      coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceService.java
  19. 113 102
      coffee-system/src/main/java/com/coffee/bus/websocket/listener/DeviceInfoListener.java
  20. 2 0
      coffee-system/src/main/java/com/coffee/system/common/dto/SysMenuEditDTO.java

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

@@ -205,11 +205,11 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
     public Page<E> list(QueryParamEntity<E> param) {
         //是否为分页查询
         if(ObjectUtil.isNotNull(param.getPage())){
-            param.getPage().setSearchCount(false);
+//            param.getPage().setSearchCount(false);
             QueryWrapper<E> wrapper = build(param);
-            Page<E> page = this.page(param.getPage(),wrapper);
-            page.setTotal(this.count(wrapper));
-            return this.page(param.getPage(),build(param));
+            return this.page(param.getPage(),wrapper);
+//            page.setTotal(this.count(wrapper));
+//            return this.page(param.getPage(),build(param));
         }else {
             try {
                 List<E> list = list(build(param));
@@ -253,9 +253,10 @@ public abstract class BaseService<M extends BaseMapper<E>, E,PK extends Serializ
                     queryWrapper.orderByDesc(column);
                 }
             });
-        }else {
-            queryWrapper.orderByDesc("id");
         }
+//        else {
+//            queryWrapper.orderByDesc("create_time");
+//        }
         if(!StrUtil.isNullOrUndefined(param.getTenantId())){
             LoginUser loginUser = (LoginUser) StpUtil.getTokenSession().get(com.coffee.common.Constants.LOGIN_USER_KEY);
             //是否为系统用户

+ 3 - 5
coffee-common/src/main/java/com/coffee/common/entity/param/TermType.java

@@ -28,7 +28,7 @@ public enum  TermType {
             {
                 @Override
                 public void build(QueryWrapper<?> queryWrapper,String column, Object value) {
-                    value="%"+value+"%";
+//                    value="%"+value+"%";
                     queryWrapper.like(column,value);
                 }
             },
@@ -36,16 +36,14 @@ public enum  TermType {
             {
                 @Override
                 public void build(QueryWrapper<?> queryWrapper,String column, Object value) {
-                    value="%"+value;
-                    queryWrapper.eq(column,value);
+                    queryWrapper.likeLeft(column,value);
                 }
             },
     rlike
             {
                 @Override
                 public void build(QueryWrapper<?> queryWrapper,String column, Object value) {
-                    value=value+"%";
-                    queryWrapper.eq(column,value);
+                    queryWrapper.likeRight(column,value);
                 }
             },
     gt

+ 3 - 1
coffee-framework/src/main/java/com/coffee/framework/config/mybatisplus/MybatisPlusConfig.java

@@ -54,7 +54,9 @@ public class MybatisPlusConfig {
         this.tenantIdManager=tenantIdManager;
         Set<Class<?>> classes = ClassUtil.scanPackage("com.coffee");
         //找到所有@TableName修饰的类
-        tableClass = classes.stream().filter(aClass -> ObjectUtil.isNotNull(aClass.getAnnotation(TableName.class))).collect(Collectors.toList());
+        tableClass = classes.stream()
+                .filter(aClass -> ObjectUtil.isNotNull(aClass.getAnnotation(TableName.class)))
+                .collect(Collectors.toList());
         if (CollectionUtil.isNotEmpty(tableClass)) {
             ignoreTableName =
                     tableClass

+ 7 - 4
coffee-system/src/main/java/com/coffee/bus/entity/BusHospitalEntity.java

@@ -10,8 +10,11 @@ import com.coffee.common.entity.RecordModifierEntity;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
+import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
 import org.hibernate.validator.constraints.Length;
 
 import java.util.Date;
@@ -23,13 +26,13 @@ import java.util.Date;
  * @Description TODO
  * @createTime 2022年03月19日 09:15:00
  */
-@EqualsAndHashCode(callSuper = true)
 @Data
 @TableName(value = "bus_hospital",autoResultMap = true)
 @ApiModel(value="医院", description="医院实体类")
-public class BusHospitalEntity extends GenericEntity<String> implements RecordModifierEntity, RecordCreationEntity {
-
-
+public class BusHospitalEntity implements RecordModifierEntity, RecordCreationEntity {
+    @TableId(type = IdType.ASSIGN_ID,value = "tenant_id")
+    @Schema(description = "tenant_id")
+    private String id;
     /**
      * 医院所在区域编码
      */

+ 7 - 7
coffee-system/src/main/java/com/coffee/bus/entity/BusPumpEntity.java

@@ -62,16 +62,16 @@ public class BusPumpEntity extends TenantGenericEntity<String,String> {
     private String alias;
 
     @ApiModelProperty(value = "剩余量")
-    private BigDecimal remainQuantity;
+    private BigDecimal remainDose;
 
     @ApiModelProperty(value = "持续量")
-    private BigDecimal continueQuantity;
+    private BigDecimal continueDose;
 
     @ApiModelProperty(value = "自控锁时")
     private BigDecimal selfControlLockTime;
 
     @ApiModelProperty(value = "已输入量")
-    private BigDecimal inputQuantity;
+    private BigDecimal inputDose;
 
     @ApiModelProperty(value = "有效次数")
     private Integer validTime;
@@ -80,16 +80,16 @@ public class BusPumpEntity extends TenantGenericEntity<String,String> {
     private Integer invalidTime;
 
     @ApiModelProperty(value = "极限量")
-    private BigDecimal maxQuantity;
+    private BigDecimal maxDose;
 
     @ApiModelProperty(value = "首次量")
-    private Integer firstQuantity;
+    private Integer firstDose;
 
     @ApiModelProperty(value = "追加量")
-    private BigDecimal singleQuantity;
+    private BigDecimal singleDose;
 
     @ApiModelProperty(value = "总量")
-    private Integer totalQuantity;
+    private Integer totalDose;
 
     @ApiModelProperty(value = "总按次数")
     private Integer totalCount;

+ 7 - 7
coffee-system/src/main/java/com/coffee/bus/entity/BusPumpHistoryEntity.java

@@ -169,16 +169,16 @@ public class BusPumpHistoryEntity extends TenantGenericEntity<String,String> {
         history.setPatientName(pump.getPatientName());
         history.setPatientSex(pump.getPatientSex());
         history.setAlias(pump.getAlias());
-        history.setRemainQuantity(pump.getRemainQuantity());
-        history.setContinueQuantity(pump.getContinueQuantity());
+        history.setRemainQuantity(pump.getRemainDose());
+        history.setContinueQuantity(pump.getContinueDose());
         history.setSelfControlLockTime(pump.getSelfControlLockTime());
-        history.setInputQuantity(pump.getInputQuantity());
+        history.setInputQuantity(pump.getInputDose());
         history.setValidTime(pump.getValidTime());
         history.setInvalidTime(pump.getInvalidTime());
-        history.setMaxQuantity(pump.getMaxQuantity());
-        history.setFirstQuantity(pump.getFirstQuantity());
-        history.setSingleQuantity(pump.getSingleQuantity());
-        history.setTotalQuantity(pump.getTotalQuantity());
+        history.setMaxQuantity(pump.getMaxDose());
+        history.setFirstQuantity(pump.getFirstDose());
+        history.setSingleQuantity(pump.getSingleDose());
+        history.setTotalQuantity(pump.getTotalDose());
         history.setTotalCount(pump.getTotalCount());
         history.setRunState(pump.getRunState());
         history.setRegisterTime(pump.getRegisterTime());

+ 51 - 0
coffee-system/src/main/java/com/coffee/bus/registry/Operator.java

@@ -0,0 +1,51 @@
+package com.coffee.bus.registry;
+
+import com.coffee.common.cache.ConfigStorage;
+import com.coffee.common.cache.value.Value;
+import org.springframework.cache.support.SimpleValueWrapper;
+
+import java.util.*;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName Operator.java
+ * @Description TODO
+ * @createTime 2022年04月07日 16:07:00
+ */
+public interface Operator<T> {
+    /**
+     * 获取所有缓存数据
+     * @return
+     */
+    T get();
+
+    void set(T all);
+
+    ConfigStorage getConfig();
+
+    List<String> getAllKeys();
+
+    default Map<String, Value> getAll() {
+        Map<String, Value> result = getConfig().getKeys(getAllKeys());
+        List<String> keys = new LinkedList<>();
+        result.forEach((k,v)->{
+            if(v==null||v.get()==null){
+                keys.add(k);
+            }
+        });
+        keys.forEach(result::remove);
+        return result;
+    }
+
+    default Value getValue(String name){
+        return parseValue(getConfig().getConfig(name));
+    }
+
+    default Value parseValue(Value value){
+        if(value==null||value.get()==null){
+            return value;
+        }
+        return Value.simple(value.as(SimpleValueWrapper.class).get());
+    }
+}

+ 59 - 29
coffee-system/src/main/java/com/coffee/bus/registry/device/ClusterDeviceOperator.java

@@ -3,7 +3,6 @@ package com.coffee.bus.registry.device;
 import cn.hutool.core.util.StrUtil;
 import com.coffee.bus.enums.NetPumpStatusEnum;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
-import com.coffee.bus.registry.device.bean.DeviceOperator;
 import com.coffee.common.cache.ConfigStorage;
 import com.coffee.common.cache.value.Value;
 import lombok.AllArgsConstructor;
@@ -22,22 +21,56 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     private final ConfigStorage configStorage;
 
+    @Override
+    public ConfigStorage getConfig() {
+        return configStorage;
+    }
 
     @Override
     public DeviceCacheInfo get() {
         Map<String, Value> result = configStorage.getKeys(getAllKeys());
-        return DeviceCacheInfo.builder()
-                .deviceId(result.get("deviceId").asString())
-                .alias(result.get("alias").asString())
-                .enable(result.get("enable").asBoolean())
-                .tenantId(result.get("tenantId").asString())
-                .usingId(result.get("usingId").asString())
-                .startTime(result.get("startTime").asDate())
-                .patientCode(result.get("patientCode").asString())
-                .status(result.get("status").as(NetPumpStatusEnum.class))
-                .master(result.get("master").asBoolean())
-                .mark(result.get("mark").asInt())
-                .build();
+        DeviceCacheInfo cacheInfo = DeviceCacheInfo.builder().build();
+        Value deviceId =parseValue(result.get("deviceId")) ;
+        if(deviceId!=null&&deviceId.get()!=null){
+            cacheInfo.setDeviceId(deviceId.asString());
+        }
+        Value alias =parseValue(result.get("alias"));
+        if(alias!=null&&alias.get()!=null){
+            cacheInfo.setAlias(alias.asString());
+        }
+        Value enable =parseValue( result.get("enable"));
+        if(enable!=null&&enable.get()!=null){
+            cacheInfo.setEnable(enable.asBoolean());
+        }
+        Value tenantId =parseValue(result.get("tenantId")) ;
+        if(tenantId!=null&&tenantId.get()!=null){
+            cacheInfo.setTenantId(tenantId.asString());
+        }
+        Value usingId =parseValue(result.get("usingId")) ;
+        if(usingId!=null&&usingId.get()!=null){
+            cacheInfo.setUsingId(usingId.asString());
+        }
+        Value startTime = parseValue(result.get("startTime"));
+        if(startTime!=null&&startTime.get()!=null){
+            cacheInfo.setStartTime(startTime.asDate());
+        }
+        Value patientCode = parseValue(result.get("patientCode"));
+        if(patientCode!=null&&patientCode.get()!=null){
+            cacheInfo.setPatientCode(patientCode.asString());
+        }
+        Value status =parseValue( result.get("status"));
+        if(status!=null&&status.get()!=null){
+            cacheInfo.setStatus(status.as(NetPumpStatusEnum.class));
+        }
+        Value master = parseValue(result.get("master"));
+        if(master!=null&&master.get()!=null){
+            cacheInfo.setMaster(master.asBoolean());
+        }
+        Value mark = parseValue(result.get("mark"));
+        if(mark!=null&&mark.get()!=null){
+            cacheInfo.setMark(mark.asInt());
+        }
+        return cacheInfo;
     }
 
     @Override
@@ -76,11 +109,6 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
         configStorage.setConfigs(map);
     }
 
-    @Override
-    public   Map<String, Value> getAll() {
-        return configStorage.getKeys(getAllKeys());
-    }
-
     @Override
     public void setDeviceId(String deviceId) {
         configStorage.setConfig("deviceId",deviceId);
@@ -88,7 +116,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public String getDeviceId() {
-        return configStorage.getConfig("deviceId").asString();
+        return getValue("deviceId").asString();
     }
 
     @Override
@@ -98,7 +126,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public String getAlias() {
-        return configStorage.getConfig("alias").asString();
+        return getValue("alias").asString();
     }
 
     @Override
@@ -108,7 +136,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public boolean getEnable() {
-        return configStorage.getConfig("enable").asBoolean();
+        return getValue("enable").asBoolean();
     }
 
     @Override
@@ -118,7 +146,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public String getTenantId() {
-        return configStorage.getConfig("tenantId").asString();
+        return getValue("tenantId").asString();
     }
 
     @Override
@@ -128,7 +156,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public String getUsingId() {
-        return configStorage.getConfig("usingId").asString();
+        return getValue("usingId").asString();
     }
 
     @Override
@@ -138,7 +166,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public Date getStartTime() {
-        return configStorage.getConfig("startTime").asDate();
+        return getValue("startTime").asDate();
     }
 
     @Override
@@ -148,12 +176,12 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public String getPatientCode() {
-        return configStorage.getConfig("patientCode").asString();
+        return getValue("patientCode").asString();
     }
 
     @Override
     public NetPumpStatusEnum getStatus() {
-        return configStorage.getConfig("status").as(NetPumpStatusEnum.class);
+        return getValue("status").as(NetPumpStatusEnum.class);
     }
 
     @Override
@@ -168,7 +196,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public boolean getMaster() {
-        return configStorage.getConfig("master").asBoolean();
+        return getValue("master").asBoolean();
     }
 
     @Override
@@ -178,7 +206,7 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
 
     @Override
     public Integer getMark() {
-        return configStorage.getConfig("mark").asInt();
+        return getValue("mark").asInt();
     }
 
     @Override
@@ -186,7 +214,9 @@ public class ClusterDeviceOperator implements DeviceOperator<DeviceCacheInfo> {
         configStorage.clear();
     }
 
-    private List<String> getAllKeys(){
+
+    @Override
+    public List<String> getAllKeys(){
         return Arrays.asList("deviceId","alias","enable","tenantId","usingId","startTime","patientCode","status","master","mark");
     }
 }

+ 14 - 6
coffee-system/src/main/java/com/coffee/bus/registry/device/ClusterDeviceRegistry.java

@@ -2,16 +2,15 @@ package com.coffee.bus.registry.device;
 
 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.BusDeviceEntity;
 import com.coffee.bus.entity.BusPumpEntity;
 import com.coffee.bus.registry.RegistryConstant;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
-import com.coffee.bus.registry.device.bean.DeviceOperator;
 import com.coffee.bus.service.LocalBusDeviceService;
 import com.coffee.bus.service.LocalBusPumpService;
 import com.coffee.common.cache.manager.ClusterConfigStorageManager;
 import com.coffee.common.cache.value.Value;
-import com.coffee.common.redis.RedisUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
@@ -28,7 +27,7 @@ import java.util.Map;
 @Service
 public class ClusterDeviceRegistry implements DeviceRegistry {
     private final LocalBusPumpService pumpService;
-    private final LocalBusDeviceService deviceRegisteredService;
+    private final LocalBusDeviceService deviceService;
     private final ClusterConfigStorageManager configStorageManager;
 
     @Override
@@ -42,16 +41,25 @@ public class ClusterDeviceRegistry implements DeviceRegistry {
         if(result==null||result.size()==0){
             //从数据库中获取数据
             BusPumpEntity pump = pumpService.getOne(new QueryWrapper<BusPumpEntity>().lambda().eq(BusPumpEntity::getDeviceId, deviceId));
-            BusDeviceEntity device = deviceRegisteredService.getOne(new QueryWrapper<BusDeviceEntity>().lambda()
+            BusDeviceEntity device = deviceService.getOne(new QueryWrapper<BusDeviceEntity>().lambda()
                     .eq(BusDeviceEntity::getDeviceId, deviceId));
             if(device==null){
                 //设备不存在,即缓存操作不存在
                 return null;
             }
+            deviceOperator.setEnable(device.getEnable()==1);
+            deviceOperator.setDeviceId(deviceId);
+            deviceOperator.setTenantId(device.getTenantId());
             if(pump==null){
                 deviceOperator.setAlias(device.getAlias());
-                deviceOperator.setTenantId(device.getTenantId());
-                deviceOperator.setDeviceId(device.getDeviceId());
+            }else {
+                deviceOperator.setUsingId(pump.getId());
+                //判断泵是否医院绑定发生变化
+                if(device.getTenantId().equals(pump.getTenantId())){
+                    pumpService.update(new UpdateWrapper<BusPumpEntity>().lambda()
+                            .eq(BusPumpEntity::getDeviceId,deviceId).set(BusPumpEntity::getTenantId,device.getTenantId()));
+                }
+
             }
             //数据同步到缓存中
             return deviceOperator;

+ 3 - 14
coffee-system/src/main/java/com/coffee/bus/registry/device/bean/DeviceOperator.java → coffee-system/src/main/java/com/coffee/bus/registry/device/DeviceOperator.java

@@ -1,10 +1,9 @@
-package com.coffee.bus.registry.device.bean;
+package com.coffee.bus.registry.device;
 
 import com.coffee.bus.enums.NetPumpStatusEnum;
-import com.coffee.common.cache.value.Value;
+import com.coffee.bus.registry.Operator;
 
 import java.util.Date;
-import java.util.Map;
 
 /**
  * @author lifang
@@ -13,19 +12,9 @@ import java.util.Map;
  * @Description 设备操作符
  * @createTime 2022年04月06日 15:16:00
  */
-public interface DeviceOperator<T> {
+public interface DeviceOperator<T> extends Operator<T> {
 
 
-    /**
-     * 获取所有缓存数据
-     * @return
-     */
-    T get();
-
-    void set(T  all);
-
-    Map<String, Value> getAll();
-
     /**
      * 设置设备号
      * @param deviceId

+ 0 - 1
coffee-system/src/main/java/com/coffee/bus/registry/device/DeviceRegistry.java

@@ -2,7 +2,6 @@ package com.coffee.bus.registry.device;
 
 import com.coffee.bus.registry.Registry;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
-import com.coffee.bus.registry.device.bean.DeviceOperator;
 
 /**
  * @author lifang

+ 224 - 0
coffee-system/src/main/java/com/coffee/bus/registry/patient/ClusterPatientOperator.java

@@ -0,0 +1,224 @@
+package com.coffee.bus.registry.patient;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.StrUtil;
+import com.coffee.bus.registry.patient.bean.DeviceTimeSmallInfo;
+import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
+import com.coffee.common.cache.ConfigStorage;
+import com.coffee.common.cache.value.Value;
+import com.coffee.common.enums.SexEnum;
+import lombok.AllArgsConstructor;
+
+import java.util.*;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName ClusterPatientOperator.java
+ * @Description 病号操作符
+ * @createTime 2022年04月07日 15:25:00
+ */
+@AllArgsConstructor
+public class ClusterPatientOperator implements PatientOperator<PatientCacheInfo> {
+    private final ConfigStorage configStorage;
+
+    @Override
+    public ConfigStorage getConfig() {
+        return configStorage;
+    }
+
+    @Override
+    public PatientCacheInfo get() {
+        Map<String, Value> result = configStorage.getKeys(getAllKeys());
+        PatientCacheInfo cacheInfo = PatientCacheInfo.builder().build();
+        Value code =parseValue(result.get("code")) ;
+        if(code!=null&&code.get()!=null){
+            cacheInfo.setCode(code.asString());
+        }
+        Value gender =parseValue(result.get("gender"));
+        if(gender!=null&&gender.get()!=null){
+            cacheInfo.setGender(gender.as(SexEnum.class));
+        }
+        Value name =parseValue( result.get("name"));
+        if(name!=null&&name.get()!=null){
+            cacheInfo.setName(name.asString());
+        }
+        Value tenantId =parseValue(result.get("tenantId")) ;
+        if(tenantId!=null&&tenantId.get()!=null){
+            cacheInfo.setTenantId(tenantId.asString());
+        }
+        Value clinicId =parseValue(result.get("clinicId")) ;
+        if(clinicId!=null&&clinicId.get()!=null){
+            cacheInfo.setClinicId(clinicId.asString());
+        }
+        Value startTime = parseValue(result.get("startTime"));
+        if(startTime!=null&&startTime.get()!=null){
+            cacheInfo.setStartTime(startTime.asDate());
+        }
+        Value finished = parseValue(result.get("finished"));
+        if(finished!=null&&finished.get()!=null){
+            cacheInfo.setIsFinished(finished.asBoolean());
+        }
+        Value bindDeviceId =parseValue( result.get("bindDeviceId"));
+        if(bindDeviceId!=null&&bindDeviceId.get()!=null){
+            cacheInfo.setBindDeviceId(bindDeviceId.asString());
+        }
+        Value devices = parseValue(result.get("devices"));
+        if(devices!=null&&devices.get()!=null){
+            cacheInfo.setDevices(code.as(HashSet.class));
+        }
+        return cacheInfo;
+    }
+
+    @Override
+    public void set(PatientCacheInfo all) {
+        Map<String, Object> map = new HashMap<>();
+        if(StrUtil.isNotEmpty(all.getCode())){
+            map.put("code",all.getCode());
+        }
+        if(all.getGender()!=null){
+            map.put("gender",all.getGender().ordinal());
+        }
+        if(StrUtil.isNotEmpty(all.getName())){
+            map.put("name",all.getName());
+        }
+        if(StrUtil.isNotEmpty(all.getTenantId())){
+            map.put("tenantId",all.getTenantId());
+        }
+        if(StrUtil.isNotEmpty(all.getClinicId())){
+            map.put("clinicId",all.getClinicId());
+        }
+        if(all.getStartTime()!=null){
+            map.put("startTime",all.getStartTime());
+        }
+        if(all.getIsFinished()!=null){
+            map.put("finished",all.getIsFinished());
+        }
+        if(StrUtil.isNotEmpty(all.getBindDeviceId())){
+            map.put("bindDeviceId",all.getBindDeviceId());
+        }
+        if(CollectionUtil.isNotEmpty(all.getDevices())){
+            map.put("devices",all.getDevices());
+        }
+        configStorage.setConfigs(map);
+
+    }
+
+
+    @Override
+    public void setCode(String code) {
+        configStorage.setConfig("code",code);
+    }
+
+    @Override
+    public String getCode() {
+        return getValue("code").as(String.class);
+    }
+
+    @Override
+    public void setGender(SexEnum gender) {
+        configStorage.setConfig("gender",gender.ordinal());
+    }
+
+    @Override
+    public SexEnum getGender() {
+        return getValue("gender").as(SexEnum.class);
+    }
+
+    @Override
+    public void setName(String name) {
+        configStorage.setConfig("name",name);
+    }
+
+    @Override
+    public String getName() {
+        return getValue("name").as(String.class);
+    }
+
+    @Override
+    public void setTenantId(String tenantId) {
+        configStorage.setConfig("tenantId",tenantId);
+    }
+
+    @Override
+    public String getTenantId() {
+        return getValue("tenantId").as(String.class);
+    }
+
+    @Override
+    public void setClinicId(String clinicId) {
+        configStorage.setConfig("clinicId",clinicId);
+    }
+
+    @Override
+    public Boolean getFinished() {
+        return getValue("finished").as(Boolean.class);
+    }
+
+    @Override
+    public void setFinished(Boolean finished) {
+        configStorage.setConfig("finished",finished);
+    }
+
+    @Override
+    public String getClinicId() {
+        return getValue("clinicId").as(String.class);
+    }
+
+    @Override
+    public void setStartTime(Date startTime) {
+        configStorage.setConfig("startTime",startTime);
+    }
+
+    @Override
+    public Date getStartTime() {
+        return getValue("startTime").as(Date.class);
+    }
+
+    @Override
+    public void setBindDeviceId(String deviceId) {
+        configStorage.setConfig("bindDeviceId",deviceId);
+    }
+
+    @Override
+    public String getBindDeviceId() {
+        return getValue("bindDeviceId").asString();
+    }
+
+    @Override
+    public Set<DeviceTimeSmallInfo> getAllDevice() {
+        return getValue("devices").as(HashSet.class);
+    }
+
+    @Override
+    public void setAllDevice(Collection<DeviceTimeSmallInfo> devices) {
+        configStorage.setConfig("devices",devices);
+    }
+
+    @Override
+    public void add(Collection<DeviceTimeSmallInfo> devices) {
+        Set<DeviceTimeSmallInfo> deviceList = getValue("devices").as(HashSet.class);
+        deviceList.addAll(devices);
+        configStorage.setConfig("devices",deviceList);
+    }
+
+    @Override
+    public void clear() {
+        configStorage.clear();
+    }
+
+    @Override
+    public void clearClinic() {
+        configStorage.remove(Arrays.asList("clinicId","startTime","finished"));
+    }
+
+    @Override
+    public void clearDevice() {
+        configStorage.remove(Arrays.asList("bindDeviceId","devices"));
+    }
+
+    @Override
+    public List<String> getAllKeys(){
+        return Arrays.asList("code","gender","name","tenantId","clinicId","startTime","finished","bindDeviceId","devices");
+    }
+}

+ 34 - 51
coffee-system/src/main/java/com/coffee/bus/registry/patient/ClusterPatientRegistry.java

@@ -7,9 +7,12 @@ import com.coffee.bus.entity.BusPatientEntity;
 import com.coffee.bus.registry.RegistryConstant;
 import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
 import com.coffee.bus.service.LocalBusPatientService;
+import com.coffee.common.cache.manager.ClusterConfigStorageManager;
+import com.coffee.common.cache.value.Value;
 import com.coffee.common.redis.RedisUtils;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.python.antlr.ast.Str;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -27,75 +30,55 @@ import java.util.Map;
 @Service
 @Slf4j
 public class ClusterPatientRegistry implements PatientRegistry {
-    private final RedisUtils redisUtils;
     private final LocalBusPatientService patientService;
+    private final ClusterConfigStorageManager configStorageManager;
     @Override
     @Transactional(rollbackFor = Exception.class)
     public PatientOperator getPatient(String hospitalId, String patientCode) {
         if(StrUtil.isEmpty(hospitalId)||StrUtil.isEmpty(patientCode)){
-            return PatientOperator.of(new PatientCacheInfo(),redisUtils,null);
+            return null;
         }
         String key=getId()+hospitalId+":"+patientCode;
-        Map<Object, Object> result = redisUtils.hmget(key);
-        PatientCacheInfo currentInfo = new PatientCacheInfo();
-        currentInfo.setKey(key);
+        ClusterPatientOperator patientOperator = new ClusterPatientOperator( configStorageManager.getStorage(key));
+        Map<String, Value> result = patientOperator.getAll();
         if(result==null||result.size()==0){
             //将新数据存入数据库
-            BusPatientEntity patient = new BusPatientEntity();
-            patient.setCode(patientCode);
-            patient.setTenantId(hospitalId);
-            try {
-                //插入前二次判断
-                result=redisUtils.hmget(key);
-                if(result==null||result.size()==0){
+            BusPatientEntity patient = null;
+            patient = patientService.getOne(new QueryWrapper<BusPatientEntity>()
+                    .lambda().eq(BusPatientEntity::getCode, patientCode)
+                    .eq(BusPatientEntity::getTenantId, hospitalId));
+            if(patient==null){
+                patient=new BusPatientEntity();
+                patient.setCode(patientCode);
+                patient.setTenantId(hospitalId);
+                try {
                     log.info("医院[{}]新增病号数据[{}]",hospitalId,patientCode);
                     patientService.save(patient);
+                }catch (DuplicateKeyException e){
+                    log.warn("病号重复插入,医院id:[{}],病号:[{}]",hospitalId,patientCode);
+                    return patientOperator;
                 }
-            }catch (DuplicateKeyException e){
-                patient = patientService.getOne(new QueryWrapper<BusPatientEntity>()
-                        .lambda().eq(BusPatientEntity::getCode, patientCode)
-                        .eq(BusPatientEntity::getTenantId, hospitalId));
-                currentInfo.setPatientCode(patientCode);
-                currentInfo.setTenantId(hospitalId);
-                currentInfo.setPatientName(patient.getName());
-                currentInfo.setPatientGender(patient.getGender());
-                PatientOperator patientOperator = PatientOperator.of(currentInfo, redisUtils, null);
-                patientOperator.syncCache();
-                return patientOperator;
             }
-            currentInfo.setPatientCode(patientCode);
-            currentInfo.setTenantId(hospitalId);
-            PatientOperator patientOperator = PatientOperator.of(currentInfo, redisUtils, null);
-            patientOperator.syncCache();
-            return patientOperator;
-
+            patientOperator.setCode(patientCode);
+            patientOperator.setTenantId(hospitalId);
+            patientOperator.setName(patient.getName());
+            patientOperator.setGender(patient.getGender());
+            //将临床数据拉入缓存,获取最新的临床信息 todo
         }
-        return PatientOperator.of(JSONUtil.toBean(JSONUtil.toJsonStr(result), PatientCacheInfo.class),redisUtils, patientCacheInfo ->{
-            //从数据库中刷新数据 todo
-            BusPatientEntity patient = patientService.getOne(
-                    new QueryWrapper<BusPatientEntity>().lambda().eq(BusPatientEntity::getCode, patientCacheInfo.getPatientCode())
-                            .eq(BusPatientEntity::getTenantId, patientCacheInfo.getTenantId()));
-            if(patient==null){
-                //存储新的病人数据
-                patient = new BusPatientEntity();
-                patient.setCode(patientCode);
-                patient.setTenantId(hospitalId);
-                patientService.save(patient);
-                //同步数据 todo
-            }else if(StrUtil.isNotEmpty(patient.getName())){
-                //同步数据 todo
-            }else {
-                patientCacheInfo.setPatientGender(patient.getGender());
-                patientCacheInfo.setPatientName(patient.getName());
-                //同步临床数据 todo
-            }
-            return patientCacheInfo;
-        });
+        return patientOperator;
     }
 
     @Override
-    public void register(PatientCacheInfo basicInfo) {
+    public void register(PatientCacheInfo cacheInfo) {
+        ClusterPatientOperator patientOperator = new ClusterPatientOperator( configStorageManager.getStorage(cacheInfo.getKey()));
+        patientOperator.set(cacheInfo);
+    }
 
+    @Override
+    public void unRegister(String hospitalId, String patientCode) {
+        String key=getId()+hospitalId+":"+patientCode;
+        ClusterPatientOperator patientOperator = new ClusterPatientOperator( configStorageManager.getStorage(key));
+        patientOperator.clear();
     }
 
     @Override

+ 113 - 133
coffee-system/src/main/java/com/coffee/bus/registry/patient/PatientOperator.java

@@ -1,167 +1,147 @@
 package com.coffee.bus.registry.patient;
 
-import cn.hutool.core.util.StrUtil;
-import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
+import com.coffee.bus.registry.Operator;
+import com.coffee.bus.registry.patient.bean.DeviceTimeSmallInfo;
 import com.coffee.common.enums.SexEnum;
-import com.coffee.common.redis.RedisUtils;
-import lombok.AllArgsConstructor;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.CompletableFuture;
-import java.util.function.Function;
+import java.util.*;
 
 /**
  * @author lifang
  * @version 1.0.0
  * @ClassName PatientOperator.java
- * @Description 病人信息操作符
- * @createTime 2022年04月02日 16:16:00
+ * @Description 病人信息操作符
+ * @createTime 2022年04月07日 14:26:00
  */
-@AllArgsConstructor(staticName = "of")
-public class PatientOperator {
-    private transient final PatientCacheInfo patientCacheInfo;
+public interface PatientOperator<T> extends Operator<T> {
 
-    private transient final RedisUtils redisUtils;
+    /**
+     * 设置病号
+     * @param patientCode
+     */
+    void setCode(String patientCode);
+
+    /**
+     * 获取病号
+     * @return
+     */
+    String getCode();
+
+    /**
+     * 设置性别
+     * @param gender
+     */
+    void setGender(SexEnum gender);
+
+    /**
+     * 获取性别
+     * @return
+     */
+    SexEnum getGender();
+
+    /**
+     * 设置病人名称
+     * @param name
+     */
+    void setName(String name);
+
+    /**
+     * 获取病人名称
+     * @return
+     */
+    String getName();
+
+    /**
+     * 设置医院id
+     * @param tenantId
+     */
+    void setTenantId(String tenantId);
+
+    /**
+     * 获取医院id
+     * @return
+     */
+    String getTenantId();
 
-    private transient final Function<PatientCacheInfo, PatientCacheInfo> syncCurrentInfo;
+    /**
+     * 设置当前临床信息id
+     * @param clinicId
+     */
+    void setClinicId(String clinicId);
 
     /**
-     * 清除缓存
+     * 判断当前临床是否结束
+     * @return
      */
-    public PatientOperator clear(){
-        redisUtils.del(patientCacheInfo.getKey());
-        return this;
-    }
+    Boolean getFinished();
+
+    /**
+     * 设置当前临床是否结束
+     */
+    void setFinished(Boolean finished);
+
     /**
-     * 将自身同步至redis
-     * @param
+     * 获取当前临床信息id
+     * @return
      */
-    public PatientOperator syncCache(){
-        redisUtils.hmset(patientCacheInfo.getKey(),getMap());
-        return this;
-    }
+    String getClinicId();
 
     /**
-     * 是否存在病人
+     * 设置临床开始时间
+     * @param startTime
      */
-    public boolean existPatient(){
-        return patientCacheInfo !=null&& StrUtil.isNotEmpty(patientCacheInfo.getPatientCode());
-    }
+    void setStartTime(Date startTime);
 
     /**
-     * 是否存在临床信息
+     * 获取临床开始时间
      * @return
      */
-    public boolean existClinic(){
-        return patientCacheInfo !=null&&StrUtil.isNotEmpty(patientCacheInfo.getClinicId());
-    }
+    Date getStartTime();
 
     /**
-     * 异步从his获取临床数据
-     * @param
+     * 设置当前病号绑定主泵号
+     * @param deviceId
      */
-    public PatientOperator asyncClinicHis(){
-        CompletableFuture.runAsync(()->{
-            // todo his
-        });
-        return this;
-    }
+    void setBindDeviceId(String deviceId);
 
     /**
-     *
-     * @param isNullRefresh 当数据为null时刷新数据
+     * 获取当前病号绑定主泵号
      * @return
      */
-    public String getPatientName(boolean isNullRefresh){
-        String patientName = patientCacheInfo.getPatientName();
-        if(StrUtil.isEmpty(patientName)&&isNullRefresh){
-            freshInfo();
-        }
-        return patientCacheInfo.getPatientName();
-    }
-
-    public String getTenantId(){
-        return patientCacheInfo.getTenantId();
-    }
-
-    public String getClinicId(boolean isNullRefresh){
-        String clinicId = patientCacheInfo.getClinicId();
-        if(StrUtil.isEmpty(clinicId)&&isNullRefresh){
-            freshInfo();
-        }
-        return patientCacheInfo.getClinicId();
-    }
-
-    public Date getClinicStartTime(boolean isNullRefresh){
-        Date clinicStartTime = patientCacheInfo.getClinicStartTime();
-        if(null!=clinicStartTime&&isNullRefresh){
-            freshInfo();
-        }
-        return patientCacheInfo.getClinicStartTime();
-    }
-
-    public Boolean getIsFinished(boolean isNullRefresh){
-        Boolean isFinished = patientCacheInfo.getIsFinished();
-        if(null!=isFinished&&isNullRefresh){
-            freshInfo();
-        }
-        return patientCacheInfo.getIsFinished();
-    }
-
-    public String getDeviceId(){
-        return patientCacheInfo.getCurrentDeviceId();
-    }
-
-    public SexEnum getPatientGender(boolean isNullRefresh){
-        SexEnum patientGender = patientCacheInfo.getPatientGender();
-        if(patientGender==null&&isNullRefresh){
-            freshInfo();
-        }
-        return patientCacheInfo.getPatientGender();
-    }
-
-    private void freshInfo(){
-        if(syncCurrentInfo!=null
-                && StrUtil.isNotEmpty(patientCacheInfo.getPatientCode())
-                &&StrUtil.isNotEmpty(patientCacheInfo.getTenantId())){
-            syncCurrentInfo.apply(patientCacheInfo);
-
-        }
-    }
-
-    public void updateDeviceId(String deviceId) {
-        redisUtils.hset(patientCacheInfo.getKey(),"currentDeviceId",deviceId);
-    }
-
-    public Map<String,Object> getMap(){
-        Map<String, Object> result = new HashMap<>();
-        if(StrUtil.isNotEmpty(patientCacheInfo.getPatientCode())){
-            result.put("patientCode", patientCacheInfo.getPatientCode());
-        }
-        if(null!= patientCacheInfo.getPatientGender()){
-            result.put("patientGender", patientCacheInfo.getPatientGender());
-        }
-        if(StrUtil.isNotEmpty(patientCacheInfo.getPatientName())){
-            result.put("patientName", patientCacheInfo.getPatientName());
-        }
-        if(StrUtil.isNotEmpty(patientCacheInfo.getTenantId())){
-            result.put("tenantId", patientCacheInfo.getTenantId());
-        }
-        if(StrUtil.isNotEmpty(patientCacheInfo.getClinicId())){
-            result.put("clinicId", patientCacheInfo.getClinicId());
-        }
-        if(null!= patientCacheInfo.getClinicStartTime()){
-            result.put("clinicStartTime", patientCacheInfo.getClinicStartTime());
-        }if(null!= patientCacheInfo.getIsFinished()){
-            result.put("isFinished", patientCacheInfo.getIsFinished());
-        }
-        if(StrUtil.isNotEmpty(patientCacheInfo.getCurrentDeviceId())){
-            result.put("currentDeviceId", patientCacheInfo.getCurrentDeviceId());
-        }
-        return result;
-    }
+    String getBindDeviceId();
+
+    /**
+     * 获取当前病号临床绑定所有泵信息
+     * @return
+     */
+    Set<DeviceTimeSmallInfo> getAllDevice();
+
+    /**
+     * 设置当前病号临床绑定所有泵信息
+     * @param devices
+     */
+    void setAllDevice(Collection<DeviceTimeSmallInfo> devices);
+
+    /**
+     * 为当前临床添加泵信息
+     * @param devices
+     */
+    void add(Collection<DeviceTimeSmallInfo> devices);
+
+    /**
+     * 情空所有信息
+     */
+    void clear();
+
+    /**
+     * 清空病号临床信息
+     */
+    void clearClinic();
+
+    /**
+     * 情空病号泵信息
+     */
+    void clearDevice();
 
 
 }

+ 7 - 0
coffee-system/src/main/java/com/coffee/bus/registry/patient/PatientRegistry.java

@@ -24,4 +24,11 @@ public interface PatientRegistry extends Registry {
      * @param basicInfo
      */
     void register(PatientCacheInfo basicInfo);
+
+    /**
+     * 取消注册病人信息
+     * @param hospitalId
+     * @param patientCode
+     */
+    void unRegister(String hospitalId, String patientCode);
 }

+ 35 - 0
coffee-system/src/main/java/com/coffee/bus/registry/patient/bean/DeviceTimeSmallInfo.java

@@ -0,0 +1,35 @@
+package com.coffee.bus.registry.patient.bean;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.Objects;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName DeviceTimeSmallInfo.java
+ * @Description 记录设备信息时间
+ * @createTime 2022年04月07日 14:16:00
+ */
+@Data
+@AllArgsConstructor(staticName = "of")
+public class DeviceTimeSmallInfo implements Serializable {
+    private String deviceId;
+    private Date startTime;
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        DeviceTimeSmallInfo that = (DeviceTimeSmallInfo) o;
+        return Objects.equals(getDeviceId(), that.getDeviceId());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(getDeviceId());
+    }
+}

+ 37 - 7
coffee-system/src/main/java/com/coffee/bus/registry/patient/bean/PatientCacheInfo.java

@@ -1,10 +1,12 @@
 package com.coffee.bus.registry.patient.bean;
 
 import com.coffee.bus.registry.CacheInfo;
+import com.coffee.bus.registry.RegistryConstant;
 import com.coffee.common.enums.SexEnum;
+import lombok.Builder;
 import lombok.Data;
 
-import java.util.Date;
+import java.util.*;
 
 /**
  * @author lifang
@@ -14,13 +16,13 @@ import java.util.Date;
  * @createTime 2022年04月02日 16:19:00
  */
 @Data
+@Builder
 public class PatientCacheInfo implements CacheInfo {
-    private String key;
 
     /******病人信息***********/
-    private String patientCode;
-    private SexEnum patientGender;
-    private String patientName;
+    private String code;
+    private SexEnum gender;
+    private String name;
     private String tenantId;
     /******病人信息***********/
 
@@ -28,7 +30,7 @@ public class PatientCacheInfo implements CacheInfo {
 
     /******临床信息***********/
     private String clinicId;
-    private Date clinicStartTime;
+    private Date startTime;
     private Boolean isFinished;
     /******临床信息***********/
 
@@ -37,6 +39,34 @@ public class PatientCacheInfo implements CacheInfo {
     /**
      * 当前绑定泵id
      */
-    private String currentDeviceId;
+    private String bindDeviceId;
+
+    /**
+     * 当前病人所绑定的泵信息,用于处理泵重复以及无泵状态信息
+     */
+    private Set<DeviceTimeSmallInfo> devices;
+
+    @Override
+    public String getKey() {
+        return RegistryConstant.Patient+tenantId+"-"+code;
+    }
+
+    private PatientCacheInfo() {
+
+    }
+
     /******泵信息***********/
+
+
+    private PatientCacheInfo(String code, SexEnum gender, String name, String tenantId, String clinicId, Date startTime, Boolean isFinished, String bindDeviceId, Set<DeviceTimeSmallInfo> devices) {
+        this.code = code;
+        this.gender = gender;
+        this.name = name;
+        this.tenantId = tenantId;
+        this.clinicId = clinicId;
+        this.startTime = startTime;
+        this.isFinished = isFinished;
+        this.bindDeviceId = bindDeviceId;
+        this.devices = devices;
+    }
 }

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

@@ -4,7 +4,7 @@ import cn.hutool.core.util.StrUtil;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.entity.BusDeviceEntity;
 import com.coffee.bus.mapper.BusDeviceMapper;
-import com.coffee.bus.registry.device.bean.DeviceOperator;
+import com.coffee.bus.registry.device.DeviceOperator;
 import com.coffee.common.crud.BaseService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;

+ 113 - 102
coffee-system/src/main/java/com/coffee/bus/websocket/listener/DeviceInfoListener.java

@@ -14,9 +14,11 @@ import com.coffee.bus.enums.NetPumpStatusEnum;
 import com.coffee.bus.listener.event.bean.DeviceAlarmEvent;
 import com.coffee.bus.listener.event.bean.DeviceInfoEvent;
 import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
-import com.coffee.bus.registry.device.bean.DeviceOperator;
+import com.coffee.bus.registry.device.DeviceOperator;
 import com.coffee.bus.registry.patient.PatientOperator;
 import com.coffee.bus.registry.patient.PatientRegistry;
+import com.coffee.bus.registry.patient.bean.DeviceTimeSmallInfo;
+import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
 import com.coffee.bus.service.LocalBusPumpService;
 import com.coffee.bus.service.LocalBusPatientService;
 import com.coffee.common.config.websocket.WebSocketConstant;
@@ -25,16 +27,13 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.event.EventListener;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.Async;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 import java.util.function.Supplier;
+import java.util.stream.Collectors;
 
 /**
  * @author lifang
@@ -69,101 +68,116 @@ public class DeviceInfoListener {
     @Transactional(rollbackFor = Exception.class)
     public void deviceInfoDetail(DeviceInfoEvent infoEvent){
         //若数据发送过快,为防止冲突,请在此加锁处理 todo
-        /****************处理泵数据****************/
-        BusPumpEntity device = infoEvent.getContent();
-        //1、判断该设备是否已和医院绑定并开启使用
-        String deviceId = device.getDeviceId();
-        DeviceOperator<DeviceCacheInfo> deviceOperator = deviceRegistry.getDevice(deviceId);
-        PatientOperator patientOperator =null;
-        if (deviceOperator==null||!Boolean.TRUE.equals(deviceOperator.getEnable())) {
-            log.warn("设备[{}]暂不可用,数据已丢弃",deviceId);
-            return ;
-        }
-        log.info("接收到设备数据:[{}]",infoEvent.getContent().toString());
-        device.setMonitorType(1);
-        //不存在运行id,即首次运行,需要与病人、医院进行绑定
-        String usingId = deviceOperator.getUsingId();
-        if(StrUtil.isEmpty(usingId)){
-            initDevice(device,deviceOperator);
-            //todo
-            patientOperator = patientRegistry.getPatient(device.getTenantId(), device.getPatientCode());
-            initPatient(device,patientOperator);
-            deviceUsingService.save(device);
-        }
-        else {
-            device.setId(usingId);
-            deviceUsingService.updateById(device);
-        }
-        /****************处理泵数据****************/
+        synchronized (infoEvent.getDeviceId()){
+            /****************处理泵数据****************/
+            BusPumpEntity device = infoEvent.getContent();
+            //1、判断该设备是否已和医院绑定并开启使用
+            String deviceId = device.getDeviceId();
+            DeviceOperator<DeviceCacheInfo> deviceOperator = deviceRegistry.getDevice(deviceId);
+            if (deviceOperator==null||!Boolean.TRUE.equals(deviceOperator.getEnable())) {
+                log.warn("设备[{}]暂不可用,数据已丢弃",deviceId);
+                return ;
+            }
+            log.info("接收到设备数据:[{}]",infoEvent.getContent().toString());
+            String tenantId = deviceOperator.getTenantId();
+            PatientOperator<PatientCacheInfo> patientOperator =patientRegistry.getPatient(tenantId, device.getPatientCode());
+            device.setTenantId(tenantId);
+            device.setMonitorType(1);
+            String usingId = deviceOperator.getUsingId();
+            if(StrUtil.isNullOrUndefined(usingId)){
+                //不存在运行id,即首次运行,需要与病人、医院进行绑定
+                initDevice(device,deviceOperator);
+                initPatient(device,patientOperator);
+                deviceUsingService.save(device);
+            }
+            else {
+                device.setId(usingId);
+                deviceUsingService.updateById(device);
+            }
+            /****************处理泵数据****************/
 
-        /****************处理泵与患者关系****************/
-        if(patientOperator==null){
-            patientOperator = patientRegistry.getPatient(device.getTenantId(), device.getPatientCode());
-        }
-        //泵绑定重复判定
-        if(StrUtil.isNotEmpty(patientOperator.getDeviceId())&&deviceId.equals(patientOperator.getDeviceId())){
-            //泵号发生改变,判断泵的开始时间,将开始时间稍后的泵设置为主泵再与设备绑定
-            DeviceOperator currentBindDevice = deviceRegistry.getDevice(patientOperator.getDeviceId());
-            if (currentBindDevice.getStartTime().before(device.getStartTime())) {
-                //设置当前上传信息的泵为主泵,将旧泵设置为副泵吗,并更新病人绑定泵的信息
-                deviceUsingService.update(new UpdateWrapper<BusPumpEntity>().lambda().eq(BusPumpEntity::getDeviceId,deviceId).set(BusPumpEntity::getMaster,1));
-                deviceUsingService.update(new UpdateWrapper<BusPumpEntity>().lambda().eq(BusPumpEntity::getDeviceId,patientOperator.getDeviceId()).set(BusPumpEntity::getMaster,0));
-                //todo 缓存更新放在最后
-                patientOperator.updateDeviceId(deviceId);
+            /****************处理泵与患者关系****************/
+            //泵绑定重复判定
+            //获取病号当前绑定的泵号
+            String bindDeviceId = patientOperator.getBindDeviceId();
+            if(StrUtil.isNullOrUndefined(bindDeviceId)&&deviceId.equals(bindDeviceId)){
+                //泵号发生改变,判断泵的开始时间,将开始时间稍后的泵设置为主泵再与设备绑定
+                DeviceOperator currentBindDevice = deviceRegistry.getDevice(bindDeviceId);
+                if (currentBindDevice.getStartTime().before(device.getStartTime())) {
+                    //设置当前上传信息的泵为主泵,将旧泵设置为副泵吗,并更新病人绑定泵的信息, todo 发生泵重复报警
+                    deviceUsingService.update(new UpdateWrapper<BusPumpEntity>().lambda().eq(BusPumpEntity::getDeviceId,deviceId).set(BusPumpEntity::getMaster,1));
+                    deviceUsingService.update(new UpdateWrapper<BusPumpEntity>().lambda().eq(BusPumpEntity::getDeviceId,bindDeviceId).set(BusPumpEntity::getMaster,0));
+                }
+            }
+            if (StrUtil.isNullOrUndefined(bindDeviceId)) {
+                //之前的病号为无泵状态,无泵 -》 有泵 做处理 todo
             }
-        }else {
-            patientOperator.updateDeviceId(deviceId);
-        }
 
-        //当泵所绑定的病号发生变化时,进行无泵判定
-        if(device.getPatientCode().equals(deviceOperator.getPatientCode())){
-            //临床无泵绑定时,查看是否存在副泵,若存在将开始时间稍后的泵设置为副泵,若不存在,则报无泵异常
-            List<BusPumpEntity> usingDevices = deviceUsingService.list(new QueryWrapper<BusPumpEntity>().lambda()
-                    .eq(BusPumpEntity::getTenantId, device.getTenantId())
-                    .eq(BusPumpEntity::getPatientCode, device.getPatientCode()));
-            if(CollectionUtil.isEmpty(usingDevices)){
-                //todo 发起无泵报警
-
-            }else {
-                //将开始时间最大的泵设置为主泵
-                BusPumpEntity master = usingDevices.stream().max(new Comparator<BusPumpEntity>() {
-                    @Override
-                    public int compare(BusPumpEntity o1, BusPumpEntity o2) {
-                        return o1.getStartTime().before(o2.getStartTime()) ? 1 : 0;
+            //无泵判断处理
+            //上条记录所绑定的病号
+            String originPatientCode=deviceOperator.getPatientCode();
+            //当泵所绑定的病号发生变化时,进行原先泵号无泵判定
+            if(!device.getPatientCode().equals(originPatientCode)){
+                //检查是原先的病号是否发生了无泵异常
+                PatientOperator originPatientOperator =patientRegistry.getPatient(tenantId, originPatientCode);
+                Set<DeviceTimeSmallInfo> originPatientBindDevices = originPatientOperator.getAllDevice();
+                Set<DeviceTimeSmallInfo> originRemainPatientBindDevices = originPatientBindDevices.stream().filter(bindDevice -> !bindDevice.getDeviceId().equals(deviceId)).collect(Collectors.toSet());
+                //临床无泵绑定时,查看是否存在副泵,若存在将开始时间稍后的泵设置为副泵,若不存在,则报无泵异常
+                if(CollectionUtil.isEmpty(originRemainPatientBindDevices)){
+                    //todo 发起无泵报警,处理原先泵的无泵信息
+                    originPatientOperator.setBindDeviceId(null);
+                    originPatientOperator.setAllDevice(new ArrayList<>());
+                }
+                else  {
+                    //将开始时间最大的泵设置为主泵
+                    Optional<DeviceTimeSmallInfo> master = originRemainPatientBindDevices.stream().max((o1,o2)->o1.getStartTime().before(o2.getStartTime()) ? 1 : 0);
+                    if(master.isPresent()){
+                        deviceUsingService
+                                .update(new UpdateWrapper<BusPumpEntity>().lambda()
+                                        .eq(BusPumpEntity::getDeviceId,master.get().getDeviceId())
+                                        .set(BusPumpEntity::getMaster,1));
+                        //更新泵原来绑定病人缓存信息
+                        originPatientOperator.setBindDeviceId(master.get().getDeviceId());
                     }
-                })
-                        .orElseGet(new Supplier<BusPumpEntity>() {
-                            @Override
-                            public BusPumpEntity get() {
-                                //todo 发起无泵报警
-                                return null;
-                            }
-                        });
-
-                if(master!=null){
-                    master.setMaster(1);
-                    deviceUsingService.updateById(master);
                 }
-
+                //更新泵原来绑定病人缓存信息
+                originPatientOperator.setAllDevice(originRemainPatientBindDevices);
             }
+
+            /****************处理泵与患者关系****************/
+
+            //则推送设备上报消息
+            String topic = WebSocketConstant.getDeviceInfoDetailTopic(null, device.getId(), device.getTenantId());
+            redisTemplate.convertAndSend(topic, device);
+
+            //更新设备缓存信息
+            DeviceCacheInfo deviceCacheInfo = DeviceCacheInfo.builder()
+                    .tenantId(device.getTenantId())
+                    .usingId(device.getId())
+                    .status(device.getRunState())
+                    .startTime(device.getStartTime())
+                    .master(device.getMaster() == 1)
+                    .patientCode(device.getPatientCode())
+                    .mark(device.getMark())
+                    .build();
+            deviceOperator.set(deviceCacheInfo);
+            //更新病人缓存信息
+            Set allDevice = patientOperator.getAllDevice();
+            allDevice.add(DeviceTimeSmallInfo.of(deviceId,device.getStartTime()));
+            PatientCacheInfo patientCacheInfo = PatientCacheInfo.builder()
+                    .bindDeviceId(deviceId)
+//                    .clinicId()
+                    .code(device.getPatientCode())
+//                    .isFinished()
+//                    .name()
+//                    .startTime()
+                    .tenantId(device.getTenantId())
+//                    .gender()
+                    .devices(allDevice)
+                    .build();
+            patientOperator.set(patientCacheInfo);
         }
 
-        /****************处理泵与患者关系****************/
-
-        //则推送设备上报消息
-        String topic = WebSocketConstant.getDeviceInfoDetailTopic(null, device.getId(), device.getTenantId());
-        redisTemplate.convertAndSend(topic, device);
-
-        //更新设备缓存信息
-        DeviceCacheInfo cacheInfo = DeviceCacheInfo.builder()
-                .usingId(device.getId())
-                .status(device.getRunState())
-                .startTime(device.getStartTime())
-                .master(device.getMaster() == 1)
-                .patientCode(device.getPatientCode())
-                .mark(device.getMark())
-                .build();
-        deviceOperator.set(cacheInfo);
     }
 
     /**
@@ -173,8 +187,8 @@ public class DeviceInfoListener {
      */
     private void initPatient(BusPumpEntity pump, PatientOperator patientOperator) {
         //填充病人名称和病人性别
-        pump.setPatientName(patientOperator.getPatientName(true));
-        pump.setPatientSex(patientOperator.getPatientGender(true));
+        pump.setPatientName(patientOperator.getName());
+        pump.setPatientSex(patientOperator.getGender());
     }
 
 
@@ -218,22 +232,19 @@ public class DeviceInfoListener {
         pump.setRegisterTime(now);
         pump.setStartTime(now);
         pump.setAlias(deviceOperator.getAlias());
-        //设备绑定医院
-        pump.setTenantId(deviceOperator.getTenantId());
-
     }
 
-    @Scheduled(cron = "0/50 * * * * ?")
+    //    @Scheduled(cron = "0/10 * * * * ?")
     public void send(){
 //        List<BusPumpEntity> list = deviceUsingService.list();
 //        list.forEach(pump->{
 
-        ThreadUtil.concurrencyTest(5,()->  {
+        ThreadUtil.concurrencyTest(1,()->  {
             BusPumpEntity pump = new BusPumpEntity();
             pump.setDeviceId("123");
             pump.setPatientCode("456");
-            pump.setRemainQuantity(BigDecimal.valueOf(RandomUtil.randomInt(100)));
-            pump.setContinueQuantity(BigDecimal.valueOf(RandomUtil.randomInt(100)));
+            pump.setRemainDose(BigDecimal.valueOf(RandomUtil.randomInt(100)));
+            pump.setContinueDose(BigDecimal.valueOf(RandomUtil.randomInt(100)));
             pump.setSelfControlLockTime(BigDecimal.valueOf(RandomUtil.randomInt(100)));
             pump.setValidTime(RandomUtil.randomInt(100));
             pump.setInvalidTime(RandomUtil.randomInt(100));

+ 2 - 0
coffee-system/src/main/java/com/coffee/system/common/dto/SysMenuEditDTO.java

@@ -113,4 +113,6 @@ public class SysMenuEditDTO implements Serializable {
     @NotBlank(message = "状态不能为空")
     private String status;
 
+    private String tenantId;
+
 }