Browse Source

add mq自动添加常量

18339543638 3 năm trước cách đây
mục cha
commit
29dfbbafd4

+ 1 - 1
nb-service-api/web-service-api/src/main/java/com/nb/web/api/bean/UndoDeviceConfig.java

@@ -20,7 +20,7 @@ import java.util.Objects;
 @Data
 @ApiModel("撤泵所填信息")
 public class UndoDeviceConfig implements Serializable {
-    private final long serialVersionUID=1L;
+    private static final long serialVersionUID=1L;
     @ApiModelProperty("撤泵人")
     @NotNull(message = "撤泵人不能为空")
     private String undoBy;

+ 7 - 0
nb-service-api/web-service-api/src/main/java/com/nb/web/api/entity/common/BusDeviceRunningEntity.java

@@ -3,6 +3,7 @@ package com.nb.web.api.entity.common;
 
 
 import com.baomidou.mybatisplus.annotation.TableField;
+import com.nb.web.api.enums.DataFromEnum;
 import com.nb.web.api.enums.DeviceTypeEnum;
 import com.nb.core.enums.SexEnum;
 import com.fasterxml.jackson.annotation.*;
@@ -147,6 +148,10 @@ public class BusDeviceRunningEntity extends CommonDeviceParam<String,String> {
     @TableField(exist = false)
     @JsonIgnoreProperties
     private boolean flowRestricted;
+
+    @ApiModelProperty("数据来源")
+    @JsonIgnoreProperties
+    private DataFromEnum from;
     /**
      * @author 龙三郎
      * 根据阿里云传回数据的items,设置输注的属性
@@ -154,6 +159,8 @@ public class BusDeviceRunningEntity extends CommonDeviceParam<String,String> {
      * @param items
      */
     public void updateFieldsByItems(String deviceName, Items items) {
+
+        this.setFrom(DataFromEnum.ALI);
         // 属性上传时间
         this.setUploadTime(items.getDate());
 

+ 24 - 0
nb-service-api/web-service-api/src/main/java/com/nb/web/api/enums/DataFromEnum.java

@@ -0,0 +1,24 @@
+package com.nb.web.api.enums;
+
+import com.baomidou.mybatisplus.annotation.IEnum;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName DataFromEnum.java
+ * @Description TODO
+ * @createTime 2022年09月17日 16:29:00
+ */
+@Getter
+@AllArgsConstructor
+@JsonFormat(shape = JsonFormat.Shape.OBJECT)
+public enum  DataFromEnum implements IEnum<Integer> {
+    ALI(0,"数据来自于阿里云"),
+    PC_MQ(1,"数据来自于PC端的MQ")
+    ;private Integer value;
+    private String text;
+
+}

+ 1 - 0
nb-service-api/web-service-api/src/main/java/com/nb/web/api/feign/IPatientClient.java

@@ -3,6 +3,7 @@ package com.nb.web.api.feign;
 import com.nb.web.api.enums.ClinicManageEnum;
 import com.nb.web.api.feign.query.PatientMonitorQuery;
 import com.nb.web.api.feign.result.*;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 

+ 2 - 4
nb-service/iot-service/src/main/java/com/nb/mq/entity/PcPumpEntity.java

@@ -6,10 +6,7 @@ import java.io.Serializable;
 import java.util.Date;
 import com.fasterxml.jackson.annotation.JsonSetter;
 import com.nb.web.api.entity.common.BusDeviceRunningEntity;
-import com.nb.web.api.enums.DeviceAlarmEnum;
-import com.nb.web.api.enums.DeviceStatusEnum;
-import com.nb.web.api.enums.DeviceTypeEnum;
-import com.nb.web.api.enums.FlowStatusEnum;
+import com.nb.web.api.enums.*;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
@@ -155,6 +152,7 @@ public class PcPumpEntity implements Serializable {
 
     public BusDeviceRunningEntity toDevice(){
         BusDeviceRunningEntity result = new BusDeviceRunningEntity();
+        result.setFrom(DataFromEnum.PC_MQ);
         DeviceTypeEnum deviceType=DeviceTypeEnum.other;
         switch ( this.getProductCode()){
             case "1":

+ 26 - 24
nb-service/iot-service/src/main/java/com/nb/mq/listener/RabbitMqListener.java

@@ -20,8 +20,6 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.amqp.rabbit.annotation.Queue;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Lazy;
 import org.springframework.messaging.handler.annotation.Payload;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
@@ -51,7 +49,6 @@ public class RabbitMqListener {
     private final IHospitalLogClient hospitalLogService;
     private final IConMixClient conMixClient;
     private final IConDoctorClient conDoctorClient;
-
     /**
      * 描述: 监听病人队列
      * @author lifang
@@ -68,32 +65,33 @@ public class RabbitMqListener {
         hospitalLog.setInput(body);
         long startTime = System.currentTimeMillis();
         try {
+
             PcPatientEntity source = JSONUtil.toBean(body, PcPatientEntity.class);
             hospitalLog.setIdentityCode(source.getPumpCode());
             hospitalLog.setTenantId(source.getHospitalCode());
-            String patientId = patientClient.lookPatientId(source.getHospitalCode(), source.getPatientCode());
+            String patientId =null;
+            patientId = patientClient.lookPatientId(source.getHospitalCode(), source.getPatientCode());
             BusClinicEntity clinic= source.toClinic(patientId);
             PatientMonitorDetailResult patientDetail = patientClient.lookPatientDetail(patientId);
             BusClinicEntity existClinic = patientDetail.getClinic();
-                //当上一个临床状态为已结束时,对比临床信息是否发生改变,生成新的临床信息
-                boolean sameClinic=false;
-                if(StrUtil.isNotEmpty(existClinic.getId())){
-                    sameClinic=ObjectUtil.equal(clinic.getSurgeryName(),
-                            existClinic.getSurgeryName());
-                }
-                if(!sameClinic){
-                    //为了避免网络延迟,开始时间往前推1个小时
-                    clinic.setStartTime(DateUtil.offsetHour(new Date(),0));
-                    Set<BusClinicEntity> sources = Collections.singleton(clinic);
-                    clinic=clinicClient.insertFromHis(clinic);
-                    clinicClient.setCurrentClinicByHis(clinic,Boolean.TRUE.equals(existClinic.getFinished()));
-                    conMixClient.insertUniqueCon(sources,clinic.getTenantId());
-                    conDoctorClient.insertUniqueDoctor(sources,clinic.getTenantId());
-                }else {
-                    clinic.setStartTime(existClinic.getStartTime());
-                    clinicClient.update(existClinic.getId(),clinic);
-                }
-//            }
+            //当上一个临床状态为已结束时,对比临床信息是否发生改变,生成新的临床信息
+            boolean sameClinic=false;
+            if(StrUtil.isNotEmpty(existClinic.getId())){
+                sameClinic=ObjectUtil.equal(clinic.getSurgeryName(),
+                        existClinic.getSurgeryName());
+            }
+            if(!sameClinic){
+                //为了避免网络延迟,开始时间往前推1个小时
+                clinic.setStartTime(DateUtil.offsetHour(new Date(),0));
+                Set<BusClinicEntity> sources = Collections.singleton(clinic);
+                clinic=clinicClient.insertFromHis(clinic);
+                clinicClient.setCurrentClinicByHis(clinic,Boolean.TRUE.equals(existClinic.getFinished()));
+                conMixClient.insertUniqueCon(sources,clinic.getTenantId());
+                conDoctorClient.insertUniqueDoctor(sources,clinic.getTenantId());
+            }else {
+                clinic.setStartTime(existClinic.getStartTime());
+                clinicClient.update(existClinic.getId(),clinic);
+            }
             hospitalLog.setResult(JSONUtil.toJsonStr(clinic));
             hospitalLog.setSuccess(true);
         }catch (Exception e){
@@ -105,6 +103,7 @@ public class RabbitMqListener {
         long entTime = System.currentTimeMillis();
         hospitalLog.setUseTime(entTime-startTime);
         hospitalLogService.save(hospitalLog);
+        log.error("消息队列【cloud.patient】处理消息{}完成",body);
     }
 
     /**
@@ -117,13 +116,13 @@ public class RabbitMqListener {
     @RabbitListener(queuesToDeclare = @Queue("cloud.pump"))
     @Transactional (rollbackFor = Exception.class)
     public void pumpQueue(@Payload String body) {
+        log.info("消息队列中,网络泵队列{cloud.pump},接受消息:{}",body);
         BusHospitalLogEntity hospitalLog = new BusHospitalLogEntity();
         hospitalLog.setMsgId(IdWorker.getIdStr());
         hospitalLog.setType(HospitalLogEnum.MQ_PUMP);
         hospitalLog.setInput(body);
         long startTime = System.currentTimeMillis();
         try {
-            log.info("消息队列中,网络泵队列{cloud.pump},接受消息:{}",body);
             PcPumpEntity source = JSONUtil.toBean(body, PcPumpEntity.class);
             hospitalLog.setIdentityCode(source.getPumpCode());
             hospitalLog.setTenantId(source.getHospitalCode());
@@ -139,6 +138,7 @@ public class RabbitMqListener {
                 clinicClient.finish(sync.getClinicId(),sync.getTenantId(),null);
                 deviceClient.refreshClassification(sync.getDeviceId());
             }
+
             hospitalLog.setResult(JSONUtil.toJsonStr(sync));
             hospitalLog.setSuccess(true);
         }catch (Exception e){
@@ -150,6 +150,7 @@ public class RabbitMqListener {
         long entTime = System.currentTimeMillis();
         hospitalLog.setUseTime(entTime-startTime);
         hospitalLogService.save(hospitalLog);
+        log.error("消息队列【cloud.pump】处理消息{}完成",body);
 
     }
 
@@ -200,6 +201,7 @@ public class RabbitMqListener {
         long entTime = System.currentTimeMillis();
         hospitalLog.setUseTime(entTime-startTime);
         hospitalLogService.save(hospitalLog);
+        log.error("消息队列【cloud.analgesicScore】处理消息{}完成",body);
     }
 
 }

+ 8 - 0
nb-service/web-service/src/main/java/com/nb/web/service/bus/entity/BusInfusionModifyEntity.java

@@ -158,6 +158,11 @@ public class BusInfusionModifyEntity extends TenantGenericEntity<String,String>
 
     @ApiModelProperty(value = "公共参数-电量",accessMode = ApiModelProperty.AccessMode.READ_ONLY)
     private Integer electricQuantity;
+
+
+    @ApiModelProperty(value = "公共参数-电量",accessMode = ApiModelProperty.AccessMode.READ_ONLY)
+    @TableField(exist = false)
+    private String patientCode;
     public static BusInfusionModifyEntity parseRunningInfo(BusDeviceRunningEntity running){
         BusInfusionModifyEntity entity = new BusInfusionModifyEntity();
         entity.setDeviceId(running.getDeviceId());
@@ -196,12 +201,15 @@ public class BusInfusionModifyEntity extends TenantGenericEntity<String,String>
 
         entity.setModifyTime(running.getUploadTime());
 
+        entity.setPatientCode(running.getPatientCode());
+
         return entity;
     }
 
     public String signParam(){
         //保证参数有序
         JSONObject param = new JSONObject(true);
+        param.putOpt("patientCOde",this.getPatientCode());
         param.putOpt("continueDose",this.getContinueDose());
         param.putOpt("firstDose",this.getFirstDose());
         param.putOpt("appendDose",this.getAppendDose());

+ 5 - 1
nb-service/web-service/src/main/java/com/nb/web/service/bus/entity/BusPatientEntity.java

@@ -38,7 +38,7 @@ public class BusPatientEntity extends TenantGenericEntity<String,String> {
     private String infusionId;
 
     @TableField
-    @ApiModelProperty("病人最后的手术id,若病人手术信息暂不存在,则先置为-1")
+    @ApiModelProperty("病人最后的手术id")
     private String clinicId;
 
     @TableField
@@ -46,6 +46,10 @@ public class BusPatientEntity extends TenantGenericEntity<String,String> {
     private PatientAlarmEnum alarm;
 
 
+    @TableField(exist = false)
+    @ApiModelProperty("新增病号时所绑定的输注id")
+    private String bindDeviceId;
+
     public static BusPatientEntity of(BusClinicEntity clinic){
         BusPatientEntity patient = new BusPatientEntity();
         patient.setCode(clinic.getPatientCode());

+ 14 - 6
nb-service/web-service/src/main/java/com/nb/web/service/bus/listener/DeviceInfoListener.java

@@ -10,13 +10,10 @@ import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.nb.web.api.entity.BusDeviceAlarmEntity;
 import com.nb.web.api.entity.BusDeviceHistoryEntity;
 import com.nb.web.api.entity.BusInfusionHistoryEntity;
+import com.nb.web.api.enums.*;
 import com.nb.web.service.bus.entity.*;
 import com.nb.web.api.entity.BusDeviceEntity;
 import com.nb.web.api.entity.common.BusDeviceRunningEntity;
-import com.nb.web.api.enums.DeviceStatusEnum;
-import com.nb.web.api.enums.DeviceTypeEnum;
-import com.nb.web.api.enums.FlowStatusEnum;
-import com.nb.web.api.enums.PatientAlarmEnum;
 import com.nb.web.service.bus.hospital.HospitalManagerRegister;
 import com.nb.web.service.bus.registry.device.DeviceOperator;
 import com.nb.web.service.bus.registry.device.DeviceRegistry;
@@ -90,7 +87,7 @@ public class DeviceInfoListener implements IIotMsgHandler {
     @Transactional(rollbackFor = Exception.class)
     public BusDeviceRunningEntity sync(BusDeviceRunningEntity device, String deviceId) {
         //保证统一设备数据顺序处理,若数据发送过快,为防止冲突,请在此加锁处理
-        synchronized (deviceId){
+        synchronized (device.getDeviceId()){
             log.info("接收到设备消息:{}",JSONUtil.toJsonStr(device));
             String classification = device.getClassification();
             if(StrUtil.isBlank(classification)){
@@ -274,6 +271,12 @@ public class DeviceInfoListener implements IIotMsgHandler {
         return ObjectUtil.notEqual(classification,deviceRegistry.getOperator(deviceId).getClassification());
     }
 
+
+    private boolean isNewInFusion(String patientCode, String deviceId, String classification) {
+        return ObjectUtil.notEqual(patientCode, deviceRegistry.getOperator(deviceId).getPatientCode())
+                ||isNewInFusion(deviceId,classification);
+    }
+
     /**
      * 病患信息处理,返回结果为当前病患是否存在临床信息
      * @param device 接收到的设备数据
@@ -348,7 +351,11 @@ public class DeviceInfoListener implements IIotMsgHandler {
      * @param cacheOperation 缓存操作
      */
     private BusInfusionHistoryEntity handleInfusionHistory(BusDeviceRunningEntity device, DeviceOperator deviceOperator, List<Supplier<?>> cacheOperation) {
-        boolean newInfusion=isNewInFusion(device.getDeviceId(),device.getClassification());
+        boolean newInfusion=
+                DataFromEnum.ALI.equals(device
+                        .getFrom())?
+                        isNewInFusion(device.getDeviceId(),device.getClassification())
+                        :isNewInFusion(device.getPatientCode(),device.getDeviceId(),device.getClassification());
         device.setNewInfusion(newInfusion);
         if(newInfusion){
             //新输注第一条数据不存在加档受限
@@ -407,6 +414,7 @@ public class DeviceInfoListener implements IIotMsgHandler {
         return infusionHistory;
     }
 
+
     /**
      * 描述: 处理追加量数据
      * @author lifang

+ 5 - 3
nb-service/web-service/src/main/java/com/nb/web/service/bus/registry/patient/ClusterPatientOperator.java

@@ -98,10 +98,12 @@ public class ClusterPatientOperator implements PatientOperator {
                 return null;
             }
             BusInfusionHistoryEntity infusion = infusionHistoryService.getById(patient.getInfusionId());
-            value=Value.simple(infusion.getDeviceId());
-            setBindDeviceId(infusion.getDeviceId());
+            if(infusion!=null){
+                value=Value.simple(infusion.getDeviceId());
+                setBindDeviceId(infusion.getDeviceId());
+            }
         }
-        return value.asString();
+        return value==null?null:value.asString();
     }
 
     @Override

+ 15 - 66
nb-service/web-service/src/main/java/com/nb/web/service/bus/registry/patient/ClusterPatientRegistry.java

@@ -15,7 +15,10 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
 import java.util.*;
 
@@ -32,8 +35,6 @@ import java.util.*;
 @Slf4j
 public class ClusterPatientRegistry implements PatientRegistry {
     private final LocalBusPatientService patientService;
-    private final LocalBusClinicService clinicService;
-    private final LocalBusInfusionHistoryService infusionService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -42,82 +43,30 @@ public class ClusterPatientRegistry implements PatientRegistry {
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
+    @Transactional
     public PatientOperator getOperator(String hospitalId, String patientCode, Map<String, Object> params) {
         if(StrUtil.isEmpty(hospitalId)||StrUtil.isEmpty(patientCode)){
             return null;
         }
         ClusterPatientOperator patientOperator = new ClusterPatientOperator(patientCode,hospitalId);
-        synchronized (hospitalId+patientCode){
-            //设备上传病号
-            if(!patientOperator.getExist()){
-                //将新数据存入数据库
-                BusPatientEntity patient = null;
-                patient = patientService.getOne(new QueryWrapper<BusPatientEntity>()
-                        .lambda().eq(BusPatientEntity::getCode, patientCode)
-                        .eq(BusPatientEntity::getTenantId, hospitalId));
-                boolean insert=false;
-                BusClinicEntity clinic=new BusClinicEntity();
-                if(patient==null){
-                    patient=new BusPatientEntity();
-                    patient.setOriginCode(patientCode);
-                    patient.setCode(patientCode);
-                    patient.setTenantId(hospitalId);
-                    patient.setId(IdWorker.getIdStr());
-
-                    clinic.setPatientId(patient.getId());
-                    clinic.setId(IdWorker.getIdStr());
-                    clinic.setPatientCode(patientCode);
-                    clinic.setTenantId(hospitalId);
-                    clinic.setMonitorType(true);
-                    clinic.setMonitorStartTime(Value.simple(params.get("startTime")).asDate());
-                    //当用户不存在时,创建用户,并给与用户一个空的临床信息
-                    insert=true;
-
-                }else {
-                    clinic = clinicService.getById(patient.getClinicId());
-                }
-
-                //填充临床信息
-                patient.setClinicId(clinic.getId());
-
-                patientOperator.setClinicId(clinic.getId());
-                if(insert){
-                    try {
-                        log.info("医院[{}]新增住院号号数据[{}]",hospitalId,patientCode);
-                        clinicService.save(clinic);
-                        patient.setClinicId(clinic.getId());
-                        patientService.save(patient);
-                    }catch (DuplicateKeyException e){
-                        log.warn("住院号号重复插入,医院id:[{}],病号:[{}]",hospitalId,patientCode);
-                    }
-                }else {
-                    //填充输注信息
-                    BusInfusionHistoryEntity recentInfusion = infusionService.lastInfusionByPatientCode(hospitalId, patientCode);
-                    if(recentInfusion!=null){
-                        patient.setInfusionId(recentInfusion.getId());
-                        clinic.setMonitorStartTime(recentInfusion.getStartTime());
-                        //监护已结束
-                        if(Boolean.TRUE.equals(recentInfusion.getIsUndo())){
-                            clinic.setEndTime(recentInfusion.getUndoTime());
-                            clinic.setFinished(true);
-                        }
-                        patientOperator.setBindDeviceId(recentInfusion.getDeviceId());
-                    }
-                    clinicService.updateById(clinic);
-                }
-                patientOperator.setPatientId(patient.getId());
-                patientOperator.setExist(true);
+        //设备上传病号
+        if(!patientOperator.getExist()){
+            //将新数据存入数据库
+            BusPatientEntity patient = patientService.getOneByHospitalAndPatientCode(hospitalId,patientCode,Value.simple(params.get("startTime")).asDate(),true);
+            patientOperator.setClinicId(patient.getClinicId());
+            if(StrUtil.isNotEmpty(patient.getBindDeviceId())){
+                patientOperator.setBindDeviceId(patient.getBindDeviceId());
             }
-            return patientOperator;
+            patientOperator.setPatientId(patient.getId());
+            patientOperator.setExist(true);
         }
+        return patientOperator;
     }
 
 
     @Override
     public void remove(String hospitalId, String patientCode) {
-        String key=getId()+hospitalId+":"+patientCode;
-        ClusterPatientOperator patientOperator = new ClusterPatientOperator( patientCode,hospitalId);
+        ClusterPatientOperator patientOperator = new ClusterPatientOperator(patientCode,hospitalId);
         patientOperator.clear();
     }
 

+ 6 - 2
nb-service/web-service/src/main/java/com/nb/web/service/bus/service/LocalBusDeviceService.java

@@ -162,9 +162,13 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
             // 添加设备
             try {
 //                entity.setTenantId(Constants.DefaultHospital);
-                return this.save(entity);
+                boolean result = this.save(entity);
+                if(result){
+                    log.info("新增设备{}成功",entity.getDeviceId());
+                }
+                return result;
             }catch (Exception e){
-                log.error("新增设备失败,【{}】",ExceptionUtil.getExceptionMsg(e));
+                log.error("新增设备{}失败,【{}】",entity.getDeviceId(),ExceptionUtil.getExceptionMsg(e));
             }
             return false;
 

+ 48 - 29
nb-service/web-service/src/main/java/com/nb/web/service/bus/service/LocalBusPatientService.java

@@ -38,12 +38,18 @@ import com.nb.core.exception.CustomException;
 import com.nb.core.result.R;
 import com.nb.core.utils.ExceptionUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.web.context.request.async.DeferredResult;
 
+import javax.validation.constraints.NotNull;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.util.*;
@@ -228,43 +234,56 @@ public class LocalBusPatientService extends BaseService<BusPatientMapper, BusPat
     }
 
     /**
-     * t
+     *
      * 根据医院和住院号获取一个患者
      *
      * @param tenantId
      * @param patientCode
      * @return
      */
-    @Transactional(rollbackFor = Exception.class)
-    public BusPatientEntity getOneByHospitalAndPatientCode(String tenantId, String patientCode) {
-        synchronized (tenantId+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);
-                patient.setOriginCode(patientCode);
-                patient.setId(IdWorker.getIdStr());
-
-                BusClinicEntity clinic=new BusClinicEntity();
-                clinic.setPatientId(patient.getId());
-                clinic.setId(IdWorker.getIdStr());
-                clinic.setPatientCode(patientCode);
-                clinic.setPatientId(patient.getId());
-                clinic.setTenantId(tenantId);
-                clinic.setMonitorType(true);
-                clinic.setMonitorStartTime(new Date());
-
-                patient.setClinicId(clinic.getId());
-
-                this.save(patient);
-                clinicService.save(clinic);
+    public BusPatientEntity getOneByHospitalAndPatientCode(@NotNull String tenantId, @NotNull String patientCode) {
+        return getOneByHospitalAndPatientCode(tenantId,patientCode,new Date(),true);
+    }
+
+    @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.SERIALIZABLE,rollbackFor = Exception.class)
+    public BusPatientEntity getOneByHospitalAndPatientCode(@NotNull String tenantId,@NotNull String patientCode, Date monitorStartTime, boolean monitorType) {
+        BusPatientEntity patient =null;
+        patient = this.getOne(new QueryWrapper<BusPatientEntity>().lambda()
+                .eq(BusPatientEntity::getTenantId, tenantId)
+                .eq(BusPatientEntity::getCode, patientCode));
+        BusInfusionHistoryEntity recentInfusion = infusionService.lastInfusionByPatientCode(tenantId, patientCode);
+        // 如果患者不存在则新增一个患者
+        if (Objects.isNull(patient)) {
+            patient = new BusPatientEntity();
+            patient.setTenantId(tenantId);
+            patient.setCode(patientCode);
+            patient.setOriginCode(patientCode);
+            patient.setId(IdWorker.getIdStr());
+            BusClinicEntity clinic=new BusClinicEntity();
+            clinic.setPatientId(patient.getId());
+            clinic.setId(IdWorker.getIdStr());
+            clinic.setPatientCode(patientCode);
+            clinic.setPatientId(patient.getId());
+            clinic.setTenantId(tenantId);
+            clinic.setMonitorType(monitorType);
+            clinic.setMonitorStartTime(monitorStartTime);
+            patient.setClinicId(clinic.getId());
+            if (Boolean.TRUE.equals(monitorType)&&recentInfusion!=null) {
+                patient.setInfusionId(recentInfusion.getId());
+                clinic.setMonitorStartTime(recentInfusion.getStartTime());
+                //监护已结束
+                if(Boolean.TRUE.equals(recentInfusion.getIsUndo())){
+                    clinic.setEndTime(recentInfusion.getUndoTime());
+                    clinic.setFinished(true);
+                };
             }
-            return patient;
+            this.save(patient);
+            clinicService.save(clinic);
+        }
+        if(recentInfusion!=null){
+            patient.setBindDeviceId(recentInfusion.getId());
         }
+        return patient;
     }
 
     /**

+ 4 - 2
nb-service/web-service/src/main/java/com/nb/web/service/bus/service/dto/ClinicQuery.java

@@ -1,5 +1,6 @@
 package com.nb.web.service.bus.service.dto;
 
+import cn.hutool.core.collection.CollectionUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import io.swagger.annotations.ApiModel;
@@ -58,7 +59,7 @@ public class ClinicQuery implements Serializable {
 
     @ApiModelProperty(value = "降序列",hidden = true)
     @JsonIgnore
-    private List<String> orderByDesc=new ArrayList<>();
+    private List<String> orderByDesc=Arrays.asList("monitor_start_time");
 
     @ApiModelProperty(value = "升序列",hidden = true)
     @JsonIgnore
@@ -67,7 +68,7 @@ public class ClinicQuery implements Serializable {
     public void setMonitorStartTimeDesc(Boolean monitorStartTimeDesc) {
         this.monitorStartTimeDesc = monitorStartTimeDesc;
         if (Boolean.TRUE.equals(monitorStartTimeDesc)) {
-            orderByDesc.add("monitor_start_time");
+//            orderByDesc.add("monitor_start_time");
         }else if(Boolean.FALSE.equals(monitorStartTimeDesc)){
             orderByAsc.add("monitor_start_time");
         }
@@ -76,6 +77,7 @@ public class ClinicQuery implements Serializable {
     public void setClinicFinishedDesc(Boolean clinicFinishedDesc) {
         this.clinicFinishedDesc = clinicFinishedDesc;
         if (Boolean.TRUE.equals(clinicFinishedDesc)) {
+            orderByDesc.clear();
             orderByDesc.add("finished");
         }else if(Boolean.FALSE.equals(clinicFinishedDesc)) {
             orderByAsc.add("finished");