Pārlūkot izejas kodu

Merge remote-tracking branch 'origin/zsl' into dev

# Conflicts:
#	coffee-system/src/main/java/com/coffee/bus/mapper/BusDeviceAlarmMapper.java
#	coffee-system/src/main/java/com/coffee/bus/mapper/BusDeviceMapper.java
#	coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceAlarmService.java
#	coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceService.java
15638522405 3 gadi atpakaļ
vecāks
revīzija
1d98cebfa5

+ 0 - 2
coffee-system/src/main/java/com/coffee/bus/controller/BusDeviceController.java

@@ -138,6 +138,4 @@ public class BusDeviceController extends BaseCrudController<BusDeviceEntity, Str
             return R.fail("同步失败");
         }
     }
-
-
 }

+ 13 - 0
coffee-system/src/main/java/com/coffee/bus/controller/BusDeviceHistoryController.java

@@ -5,9 +5,12 @@ import com.baomidou.mybatisplus.core.mapper.Mapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.coffee.bus.entity.BusDeviceAlarmEntity;
 import com.coffee.bus.entity.BusDeviceHistoryEntity;
+import com.coffee.bus.service.LocalBusDeviceAlarmService;
 import com.coffee.bus.service.LocalBusDeviceHistoryService;
 import com.coffee.bus.service.dto.AlarmQuery;
+import com.coffee.bus.service.dto.DeviceAlarmQuery;
 import com.coffee.bus.service.dto.DeviceHistoryQuery;
+import com.coffee.bus.service.dto.DeviceUse;
 import com.coffee.common.crud.BaseService;
 import com.coffee.common.crud.controller.BaseCrudController;
 import com.coffee.common.result.R;
@@ -20,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 /**
  * @author lifang
  * @version 1.0.0
@@ -33,6 +38,7 @@ import org.springframework.web.bind.annotation.RestController;
 @Api(tags = "设备历史数据管理",description = "统一权限前缀(device:history),device:history:add")
 public class BusDeviceHistoryController extends BaseCrudController<BusDeviceHistoryEntity, String> {
     private final LocalBusDeviceHistoryService historyService;
+    private final LocalBusDeviceAlarmService deviceAlarmService;
 
     /**
      * 权限控制前缀
@@ -50,6 +56,13 @@ public class BusDeviceHistoryController extends BaseCrudController<BusDeviceHist
         return R.success(historyService.pageQuery(query));
     }
 
+    @PostMapping("/deviceUse")
+    @SaCheckPermission("bus:deviceUse")
+    @ApiOperation(value = "设备使用查询",notes = "权限:【bus:deviceUse】")
+    public R<List<DeviceUse>> deviceUse(@RequestBody @Validated DeviceAlarmQuery query){
+        return R.success(deviceAlarmService.selectCountAlarm(query));
+    }
+
     @Override
     public BaseService<? extends Mapper<BusDeviceHistoryEntity>, BusDeviceHistoryEntity, String> getService() {
         return historyService;

+ 10 - 1
coffee-system/src/main/java/com/coffee/bus/mapper/BusDeviceAlarmMapper.java

@@ -1,8 +1,14 @@
 package com.coffee.bus.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+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.BusDeviceAlarmEntity;
+import com.coffee.bus.service.dto.AlarmQuery;
+import com.coffee.bus.service.dto.DeviceAlarmQuery;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * @author lifang
@@ -14,5 +20,8 @@ import org.apache.ibatis.annotations.Mapper;
 @Mapper
 public interface BusDeviceAlarmMapper extends BaseMapper<BusDeviceAlarmEntity> {
 
-    void selectCount(BusDeviceAlarmEntity busDeviceAlarmEntity);
+    IPage<BusDeviceAlarmEntity> pageQuery(Page<BusDeviceAlarmEntity> page, @Param("query") AlarmQuery query);
+
+
+    Integer selectAlarmCount(@Param("query") DeviceAlarmQuery query);
 }

+ 14 - 7
coffee-system/src/main/java/com/coffee/bus/mapper/BusDeviceMapper.java

@@ -1,8 +1,11 @@
 package com.coffee.bus.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.coffee.bus.entity.BusClinicEntity;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.coffee.bus.entity.BusDeviceEntity;
+import com.coffee.bus.service.dto.DeviceQuery;
+import com.coffee.bus.service.dto.DeviceResult;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
@@ -18,14 +21,18 @@ import org.apache.ibatis.annotations.Select;
 public interface BusDeviceMapper extends BaseMapper<BusDeviceEntity> {
 
     /**
-     * 获取病号最近一场手术信息
-     * @param hospitalId
-     * @param patientCode
+     * 获取设备,无视逻辑删除
+     * @param deviceId
      * @return
      */
-    @Select("select * from bus_clinic where tenant_id=#{hospitalId} and patient_code=#{patientCode} order by start_time DESC limit 0,1")
-    BusClinicEntity recentClinic(@Param("hospitalId") String hospitalId, @Param("patientCode") String patientCode);
-
+    BusDeviceEntity selectOneByDeviceId(String deviceId);
 
+    /**
+     * 去掉逻辑删除标志
+     * @param deviceId
+     * @return
+     */
+    Integer notDelete(String deviceId);
 
+    IPage<DeviceResult> pageQuery(Page<DeviceResult> page,@Param("query") DeviceQuery query);
 }

+ 41 - 16
coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceAlarmService.java

@@ -3,14 +3,19 @@ package com.coffee.bus.service;
 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.controller.vo.DeviceUse;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.coffee.bus.entity.BusClinicEntity;
 import com.coffee.bus.entity.BusDeviceAlarmEntity;
 import com.coffee.bus.entity.BusHospitalEntity;
 import com.coffee.bus.enums.DeviceAlarmEnum;
 import com.coffee.bus.mapper.BusDeviceAlarmMapper;
 import com.coffee.bus.mapper.BusHospitalMapper;
+import com.coffee.bus.service.dto.AlarmQuery;
+import com.coffee.bus.service.dto.DeviceAlarmQuery;
+import com.coffee.bus.service.dto.DeviceUse;
 import com.coffee.common.crud.BaseService;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -29,8 +34,10 @@ import java.util.Map;
 @Service
 @AllArgsConstructor
 public class LocalBusDeviceAlarmService extends BaseService<BusDeviceAlarmMapper, BusDeviceAlarmEntity,String> {
-    private final BusDeviceAlarmMapper deviceAlarmMapper;
-    private final BusHospitalMapper hospitalMapper;
+    @Autowired
+    private BusDeviceAlarmMapper busDeviceAlarmMapper;
+    @Autowired
+    BusHospitalMapper hospitalMapper;
 
     @Override
     public void validateBeforeSave(BusDeviceAlarmEntity entity) {
@@ -47,7 +54,9 @@ public class LocalBusDeviceAlarmService extends BaseService<BusDeviceAlarmMapper
 
     }
 
-
+    public IPage<BusDeviceAlarmEntity> pageQuery(AlarmQuery query){
+        return this.baseMapper.pageQuery(query.getPage(),query);
+    }
     /**
      * 添加报警原因
      * @param id
@@ -64,33 +73,48 @@ public class LocalBusDeviceAlarmService extends BaseService<BusDeviceAlarmMapper
         }
         this.update(new UpdateWrapper<BusDeviceAlarmEntity>().lambda().eq(BusDeviceAlarmEntity::getId,id).set(BusDeviceAlarmEntity::getCause,cause));
     }
-
     /**
      * 查看报警数量
      */
     @Transactional(rollbackFor = Exception.class)
-    public List<DeviceUse> countAlarm(){
-        List<BusHospitalEntity> hospitalList = hospitalMapper.selectList(new QueryWrapper<>());
+    public List<DeviceUse> selectCountAlarm(DeviceAlarmQuery query){
+        QueryWrapper<BusHospitalEntity> busHospitalEntityQueryWrapper = new QueryWrapper<>();
+        if(query.getHositalName()!=null){
+            busHospitalEntityQueryWrapper.getEntity().setName(query.getHositalName());
+        }
+        List<BusHospitalEntity> hospitalEntities = hospitalMapper.selectList(busHospitalEntityQueryWrapper);
         List<DeviceUse> deviceUseList = new ArrayList<>();
 
         for (BusHospitalEntity hospital:
-             hospitalList) {
+                hospitalEntities) {
             DeviceUse deviceUse = new DeviceUse();
+            Map<String, Integer> deviceAlarms = new HashMap<>();
+
             deviceUse.setName(hospital.getName());
             deviceUse.setAddress(hospital.getAddress());
-            Map<DeviceAlarmEnum, Long> deviceAlarms = new HashMap<>();
             deviceUse.setDeviceAlarms(deviceAlarms);
 
-            QueryWrapper<BusDeviceAlarmEntity> busDeviceAlarmEntityQueryWrapper = new QueryWrapper<>();
-            BusDeviceAlarmEntity busDeviceAlarmEntity = new BusDeviceAlarmEntity();
-            busDeviceAlarmEntityQueryWrapper.setEntity(busDeviceAlarmEntity);
-            busDeviceAlarmEntity.setTenantId(hospital.getId());
+
+            query.setTenantId(hospital.getId());
+
             for (DeviceAlarmEnum alarmEnum:
                     DeviceAlarmEnum.values()) {
-                busDeviceAlarmEntity.setAlarm(alarmEnum);
-                deviceAlarms.put(alarmEnum,deviceAlarmMapper.selectCount(busDeviceAlarmEntityQueryWrapper));
+                query.setAlarmState(alarmEnum.getValue());
+                deviceAlarms.put(alarmEnum.getText(),busDeviceAlarmMapper.selectAlarmCount(query));
             }
-
+            query.setAlarmState(null);
+            /*输注即将结束提醒*/
+            query.setWarnWillFinished(1);
+            deviceAlarms.put("warnWillFinished",busDeviceAlarmMapper.selectAlarmCount(query));
+            query.setWarnWillFinished(null);
+            /*镇痛不足提醒*/
+            query.setWarnAnalgesicPoor(1);
+            deviceAlarms.put("warnAnalgesicPoor",busDeviceAlarmMapper.selectAlarmCount(query));
+            query.setWarnAnalgesicPoor(null);
+            /*电量偏低提醒*/
+            query.setWarnWillFinished(1);
+            deviceAlarms.put("warnLowBattery",busDeviceAlarmMapper.selectAlarmCount(query));
+            query.setWarnWillFinished(null);
 
             deviceUseList.add(deviceUse);
 
@@ -98,4 +122,5 @@ public class LocalBusDeviceAlarmService extends BaseService<BusDeviceAlarmMapper
         return deviceUseList;
 
     }
+
 }

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

@@ -1,15 +1,33 @@
 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.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+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;
 import com.coffee.bus.registry.device.DeviceOperator;
+import com.coffee.bus.service.dto.DeviceQuery;
+import com.coffee.bus.service.dto.DeviceResult;
 import com.coffee.common.crud.BaseService;
+import com.coffee.common.exception.CustomException;
 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.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * @author lifang
@@ -25,9 +43,19 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
     @Autowired
     @Lazy
     private DeviceRegistry deviceRegistry;
+
+    @Autowired
+    private BusDeviceMapper deviceMapper;
+
+    @Autowired
+    private AliyunIotSdk aliyunIotSdk;
+
+
     @Override
     public void validateBeforeSave(BusDeviceEntity entity) {
-
+       if(entity.getTenantId()==null){
+           entity.setTenantId("1");
+       }
     }
 
     @Override
@@ -37,7 +65,8 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
 
     @Override
     public void validateBeforeDelete(String id) {
-     }
+
+    }
 
     @Override
     public void postSave(BusDeviceEntity entity) {
@@ -57,6 +86,9 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
         if(entity.getEnable()!=null){
             deviceOperator.setEnable(entity.getEnable());
         }
+        if(StrUtil.isNotEmpty(entity.getTenantId())){
+            deviceOperator.setTenantId(entity.getTenantId());
+        }
     }
 
     @Override
@@ -65,5 +97,176 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
         deviceRegistry.remove(registeredEntity.getDeviceId());
     }
 
+    /**
+     * @author 龙三郎
+     * 根据deviceId删除设备
+     * @param deviceId
+     */
+    public void removeByDeviceId(String deviceId){
+        this.remove(new QueryWrapper<BusDeviceEntity>().lambda()
+                .eq(BusDeviceEntity::getDeviceId,deviceId));
+    }
+
+    /**
+     * @author 龙三郎
+     * 根据deviceId更新设备在线状态
+     * @param device
+     */
+    public boolean updateDevice(BusDeviceEntity device){
+        return this.update(device,new QueryWrapper<BusDeviceEntity>().lambda()
+                .eq(BusDeviceEntity::getDeviceId,device.getDeviceId()));
+    }
+
+    /**
+     * @author 龙三郎
+     * 保存一个设备,系统中存在时就更新,不存在时则插入。
+     * @param entity
+     */
+    public boolean saveDevice(BusDeviceEntity entity) {
+        // 查询设备是否存在,无视逻辑删除
+        BusDeviceEntity device = deviceMapper.selectOneByDeviceId(entity.getDeviceId());
+        // 判断设备是否存在
+        boolean isExists = Objects.nonNull(device);
+        // 设备存在,且处于删除状态,首先去掉逻辑删除标志 ?? 逻辑删除后设备数据是否不再接收
+        if (isExists && device.getIsDelete() == 1){
+            deviceMapper.notDelete(entity.getDeviceId());
+        }
+        // 设备存在
+        if (isExists){
+            // 更新设备
+            return this.updateDevice(entity);
+        }else {
+            // 添加设备
+            return this.save(entity);
+        }
+    }
+
+    /**
+     * @author 龙三郎
+     * 更新一个系统中现有的设备
+     * @param deviceId
+     */
+    public boolean updateDeviceByDeviceId(String deviceId) {
+        // 查询设备是否存在
+        BusDeviceEntity device = getByDeviceId(deviceId);
+        // 设备不存在直接退出
+        if (Objects.isNull(device)){
+            return false;
+        }
+        // 从阿里云物联网查询设备
+        QueryDeviceDetailResponse response = aliyunIotSdk.queryDeviceDetail(deviceId);
+        // 设备存在
+        if (response.getSuccess()){
+            // 更新设备参数
+            device.updateFields(response.getData());
+            // 更新设备
+            return this.updateDevice(device);
+        }else {
+            return false;
+        }
+    }
+
+    /**
+     * @author 龙三郎
+     * 根据deviceId获取设备
+     * @param deviceId
+     * @return
+     */
+    public BusDeviceEntity getByDeviceId(String deviceId) {
+        BusDeviceEntity device = getOne(new QueryWrapper<BusDeviceEntity>().lambda()
+                .eq(BusDeviceEntity::getDeviceId,deviceId));
+        return device;
+    }
+
+    /**
+     * @author 龙三郎
+     * 该方法用于从阿里云同步设备,系统暂时不允许创建非阿里云物联网平台设备。意思是系统中的设备必须存在于阿里云物联网平台。
+     * 通过deviceId从阿里云查询设备,如果设备在阿里云平台不存在,则创建失败,如果存在,则获取设备数据,插入或更新设备数据,表示创建成功。
+     * @param deviceId
+     * @return
+     */
+    public BusDeviceEntity addDevice(String deviceId) {
+        BusDeviceEntity device = new BusDeviceEntity();
+        device.setConfig(new AliIotConfig());
+        // 从阿里云物联网查询设备
+        QueryDeviceDetailResponse response = aliyunIotSdk.queryDeviceDetail(deviceId);
+        // 设备存在
+        if (response.getSuccess()){
+            device.updateFields(response.getData());
+            // 存储设备
+            this.saveDevice(device);
+            return device;
+        }
+        // 不存在返回null
+        return null;
+    }
+
+
+    public IPage<DeviceResult> pageQuery(Page<DeviceResult> page, DeviceQuery query){
+        return this.baseMapper.pageQuery(page,query);
+    }
+    /**
+     * @author 龙三郎
+     * 从阿里云平台获取全部的设备,同步到系统数据库
+     * @return
+     */
+    public int syncAllDevice(){
+        Integer m = 0;
+        // 创建异步执行任务,有返回值
+        CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(()->{
+            AtomicReference<Integer> n = new AtomicReference<>(0);
+            // 同步所有的设备
+            aliyunIotSdk.queryDevice().forEach(item ->{
+                BusDeviceEntity device = new BusDeviceEntity();
+                device.setConfig(new AliIotConfig());
+                // 更新设备属性
+                device.updateFields(item);
+                n.updateAndGet(v -> v + (this.saveDevice(device)?1:0));
+            });
+            return n.get();
+        });
+        //等待子任务执行完成
+        try {
+            m = cf.get();
+        }catch (Exception e){
+            throw new CustomException(e.getMessage());
+        }
+        return m;
+    }
 
+    /**
+     * @author 龙三郎
+     * 根据ids获取指定的设备,更新本地设备
+     * @return
+     */
+    public int syncDevice(List<String> ids) {
+        AtomicReference<Integer> n = new AtomicReference<>(0);
+        // ids不能为空
+        if (Objects.isNull(ids) || ids.size() <= 0){
+            throw new CustomException("设备id不能为空");
+        }
+        // 同步指定的设备
+        ids.forEach(id->{
+            n.updateAndGet(v -> v + (this.updateDeviceByDeviceId(id)?1:0));
+        });
+        return n.get();
+    }
+
+    /**
+     * 描述: 设备医院换绑操作
+     * @author lifang
+     * @date 2022/5/8 21:34
+     * @param deviceIds 换绑的设备id
+     * @param afterTenantId 换绑之后的医院id
+     * @return void
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void shift(List<String> deviceIds, String afterTenantId) {
+        this.update(new UpdateWrapper<BusDeviceEntity>().lambda()
+                .in(BusDeviceEntity::getDeviceId,deviceIds)
+                .set(BusDeviceEntity::getTenantId,afterTenantId));
+        deviceIds.stream()
+                .map(deviceRegistry::getOperator)
+                .forEach(deviceOperator -> deviceOperator.setTenantId(afterTenantId));
+    }
 }

+ 37 - 0
coffee-system/src/main/resources/mapper/bus/BusDeviceAlarmMapper.xml

@@ -10,5 +10,42 @@
         on a.infusion_id=h.id
     </select>
 
+    <select id="selectAlarmCount" parameterType="com.coffee.bus.service.dto.DeviceAlarmQuery" resultType="java.lang.Integer">
+        SELECT
+            count( 1 )
+        FROM
+            bus_device_alarm AS da
+                LEFT JOIN bus_device AS d ON da.device_id = d.id
+        WHERE
+              1=1
+        <if test="query.uploadTimeMin != null">
+        and da.update_time >= #{query.uploadTimeMin}
+        </if>
+        <if test="query.uploadTimeMax != null">
+        AND da.update_time &lt;= #{query.uploadTimeMax}
+        </if>
+        <if test="query.alarm != null">
+        AND da.alarm = #{query.alarm}
+        </if>
+        <if test="query.alarmState != null">
+        AND da.alarm_state = #{query.alarmState}
+        </if>
+        <if test="query.warnWillFinished != null">
+        AND da.warn_will_finished = #{query.warnWillFinished}
+        </if>
+        <if test="query.warnLowBattery != null">
+        AND da.warn_low_battery = #{query.warnLowBattery}
+        </if>
+        <if test="query.warnAnalgesicPoor != null">
+        AND da.warn_analgesic_poor = #{query.warnAnalgesicPoor}
+        </if>
+        <if test="query.tenantId != null">
+        AND da.tenant_id = #{query.tenantId}
+        </if>
+        <if test="query.type != null">
+        AND d.type = #{query.type}
+        </if>
+
+    </select>
 
 </mapper>

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

@@ -12,4 +12,7 @@
     </select>
 
 
+
+
+
 </mapper>