Selaa lähdekoodia

'优化产品信息表-分页接口,解决几个统计设备值字段(设备总数、在线设备数、注册设备数)为null的问题'

james 1 vuosi sitten
vanhempi
commit
78c1a72117

+ 2 - 7
service-iot/service-iot-biz/src/main/java/com/usky/iot/domain/DmpDeviceInfo.java

@@ -37,11 +37,6 @@ public class DmpDeviceInfo implements Serializable {
      */
     private String deviceId;
 
-    /**
-     * 设备编号
-     */
-    private String deviceCode;
-
     /**
      * 设备名称
      */
@@ -112,10 +107,10 @@ public class DmpDeviceInfo implements Serializable {
      */
     @TableField(exist = false)
     private String productName;
+
     /**
-     * 业务状态;1:未激活,2:已激活,3:上线,4:下线
+     * 业务状态;1:未激活,2:已激活,3:禁用
      */
-    @TableField(exist = false)
     private Integer serviceStatus;
 
     /**

+ 1 - 10
service-iot/service-iot-biz/src/main/java/com/usky/iot/domain/DmpDeviceStatus.java

@@ -34,24 +34,15 @@ public class DmpDeviceStatus implements Serializable {
      */
     private String deviceId;
 
-    /**
-     * 设备编号
-     */
-    private String deviceCode;
-
     /**
      * 产品ID
      */
     private Integer productId;
 
     /**
-     * 设备状态;1:正常,2:故障,3:告警,4:离线
+     * 设备状态;1:在线,2:离线
      */
     private Integer deviceStatus;
-    /**
-     * 业务状态;1:未激活,2:已激活,3:上线,4:下线
-     */
-    private Integer serviceStatus;
 
     /**
      * 最后上线时间

+ 3 - 14
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -61,13 +61,10 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         if (checkNameUnique(dmpDeviceInfo)){
             throw new BusinessException("新增设备信息'" + dmpDeviceInfo.getDeviceName() + "'失败,设备信息已存在");
         }
-        //在设备状态表中新增一条设备状态记录,业务状态置为 1 待激活
         DmpDeviceStatus dmpDeviceStatus = new DmpDeviceStatus();
         dmpDeviceStatus.setDeviceId(dmpDeviceInfo.getDeviceId());
-        dmpDeviceStatus.setDeviceCode(dmpDeviceInfo.getDeviceCode());
         dmpDeviceStatus.setProductId(dmpDeviceInfo.getProductId());
-        dmpDeviceStatus.setDeviceStatus(4);
-        dmpDeviceStatus.setServiceStatus(1);
+        dmpDeviceStatus.setDeviceStatus(2);
         dmpDeviceStatus.setLastOfflineTime(LocalDateTime.now());
         dmpDeviceStatusService.save(dmpDeviceStatus);
 
@@ -83,14 +80,6 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
 //        }
         this.updateById(dmpDeviceInfo);
 
-        //更新设备状态表对应设备状态记录数据
-        LambdaQueryWrapper<DmpDeviceStatus> queryWrapper = Wrappers.lambdaQuery();
-        queryWrapper.eq(DmpDeviceStatus::getDeviceId,dmpDeviceInfo.getDeviceId());
-        DmpDeviceStatus one = dmpDeviceStatusService.getOne(queryWrapper);
-        if(null != one){
-            one.setServiceStatus(dmpDeviceInfo.getServiceStatus());
-            dmpDeviceStatusService.updateById(one);
-        }
     }
 
     @Override
@@ -123,8 +112,9 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
     @Override
     public List<Map<String, Object>> deviceCollect(List<Integer> productIds) {
         QueryWrapper<DmpDeviceInfo> query = Wrappers.query();
-        query.select("product_id as productId","count(*) as count")
+        query.select("product_id as productId","count(*) as count","count(service_status != 1 or null) as serviceCount")
                 .in(CollectionUtil.isNotEmpty(productIds),"product_id",productIds)
+                .eq("delete_flag",0)
                 .groupBy("product_id");
         return this.listMaps(query);
     }
@@ -222,7 +212,6 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                     device.setDeviceId(UUIDUtils.uuid());})
                     .filter(device -> StringUtils.isBlank(device.getDeviceName()) ||
                     StringUtils.isBlank(device.getDeviceName())
-                    || StringUtils.isBlank(device.getDeviceCode())
                     || StringUtils.isBlank(device.getSimCode())
                     || null == device.getSubscribeFlag()
             ).count();

+ 1 - 1
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceStatusServiceImpl.java

@@ -26,7 +26,7 @@ public class DmpDeviceStatusServiceImpl extends AbstractCrudService<DmpDeviceSta
     @Override
     public List<Map<String, Object>> getCollectByProduct(List<Integer> productIds) {
         QueryWrapper<DmpDeviceStatus> query = Wrappers.query();
-        query.select("product_id as productId","count(device_status != 4 or null) as deviceCount","count(service_status) as serviceCount")
+        query.select("product_id as productId","count(device_status != 2 or null) as deviceCount")
                 .in(CollectionUtil.isNotEmpty(productIds),"product_id",productIds)
                 .groupBy("product_id");
         return this.listMaps(query);

+ 53 - 15
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpProductInfoServiceImpl.java

@@ -74,31 +74,69 @@ public class DmpProductInfoServiceImpl extends AbstractCrudService<DmpProductInf
                 .eq(StringUtils.isNotBlank(piRequest.getDeviceModel()),DmpProductInfo::getDeviceModel,piRequest.getDeviceModel())
                 .eq(null != piRequest.getId(),DmpProductInfo::getId,piRequest.getId())
                 .eq(DmpProductInfo::getDeleteFlag,0);
-        List<DmpProductInfo> records = page.getRecords();
+        List<DmpProductInfo> records = this.list(lambdaQuery);
         List<Integer> productIds = records.stream().map(DmpProductInfo::getId).collect(Collectors.toList());
         List<Map<String, Object>> maps = dmpDeviceInfoService.deviceCollect(productIds);
         List<Map<String, Object>> collectByProduct = dmpDeviceStatusService.getCollectByProduct(productIds);
-        records.forEach(s -> {enhanceByDevice(s,maps);
-            enhanceByDeviceStatus(s,collectByProduct);
-        });
-        page = this.page(page,lambdaQuery);
-        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getCurrent(),page.getSize());
+
+//        maps.forEach(s -> {enhanceByDevice(records,s);});
+//        collectByProduct.forEach(s -> {enhanceByDeviceStatus(records,s);});
+        if(records.size()>0){
+            for(int i=0;i<records.size();i++){
+                if(maps.size()>0){
+                    for(int j=0;j<maps.size();j++){
+                        if(maps.get(j).get("productId")==(records.get(i).getId())){
+                            String coun = maps.get(j).get("count").toString();
+                            Integer it = Integer.parseInt(maps.get(j).get("count").toString());
+                            records.get(i).setDeviceCount(Integer.parseInt(maps.get(j).get("count").toString()));
+                            records.get(i).setServiceStatusCount(Integer.parseInt(maps.get(j).get("serviceCount").toString()));
+                            continue;
+                        }
+                    }
+                }
+                if(collectByProduct.size()>0){
+                    for(int j=0;j<collectByProduct.size();j++){
+                        if(collectByProduct.get(j).get("productId")==(records.get(i).getId())){
+                            records.get(i).setDeviceStatusCount(Integer.parseInt(collectByProduct.get(j).get("deviceCount").toString()));
+                            continue;
+                        }
+                    }
+                }
+            }
+
+        }
+        int total = 0;
+        if(records.size()>0){
+            total = records.size();
+        }
+        int current = 0;
+        Integer pageNum = piRequest.getCurrent();
+        Integer pageSize = piRequest.getSize();
+        if(pageNum != null && pageSize > 0){
+            current = (pageNum - 1)*pageSize;
+        }
+        List<DmpProductInfo> list = records.stream().skip(current).limit(pageSize).collect(Collectors.toList());
+
+        return new CommonPage<>(list,total,page.getCurrent(),page.getSize());
     }
 
-    public void enhanceByDevice(DmpProductInfo dmpProductInfo, List<Map<String,Object>> list){
-        list.stream()
-                .filter(s -> dmpProductInfo.getId().toString().equals(s.get("productId")))
+    public void enhanceByDevice(List<DmpProductInfo> dmpProductInfo, Map<String,Object> map){
+        dmpProductInfo.stream()
+                .filter(s -> map.get("productId").equals(s.getId().toString()))
                 .findAny()
-                .ifPresent(s -> dmpProductInfo.setDeviceCount(Integer.getInteger(s.get("count").toString())));
+                .ifPresent(s -> {
+                    s.setDeviceCount(Integer.parseInt(map.get("count").toString()));
+                    s.setServiceStatusCount(Integer.parseInt(map.get("serviceCount").toString()));
+                });
+
     }
 
-    public void enhanceByDeviceStatus(DmpProductInfo dmpProductInfo, List<Map<String,Object>> list){
-        list.stream()
-                .filter(s -> dmpProductInfo.getId().toString().equals(s.get("productId")))
+    public void enhanceByDeviceStatus(List<DmpProductInfo> dmpProductInfo, Map<String,Object> map){
+        dmpProductInfo.stream()
+                .filter(s -> map.get("productId").equals(s.getId().toString()))
                 .findAny()
                 .ifPresent(s -> {
-                    dmpProductInfo.setDeviceStatusCount(Integer.getInteger(s.get("deviceCount").toString()));
-                            dmpProductInfo.setServiceStatusCount(Integer.getInteger(s.get("serviceCount").toString()));
+                    s.setDeviceStatusCount(Integer.parseInt(map.get("deviceCount").toString()));
                 }
                 );
     }

+ 0 - 1
service-iot/service-iot-biz/src/main/resources/mapper/iot/DmpDeviceInfoMapper.xml

@@ -6,7 +6,6 @@
     <resultMap id="BaseResultMap" type="com.usky.iot.domain.DmpDeviceInfo">
         <id column="id" property="id" />
         <result column="device_id" property="deviceId" />
-        <result column="device_code" property="deviceCode" />
         <result column="device_name" property="deviceName" />
         <result column="device_type" property="deviceType" />
         <result column="product_id" property="productId" />

+ 0 - 1
service-iot/service-iot-biz/src/main/resources/mapper/iot/DmpDeviceStatusMapper.xml

@@ -6,7 +6,6 @@
     <resultMap id="BaseResultMap" type="com.usky.iot.domain.DmpDeviceStatus">
         <id column="id" property="id" />
         <result column="device_id" property="deviceId" />
-        <result column="device_code" property="deviceCode" />
         <result column="product_id" property="productId" />
         <result column="device_status" property="deviceStatus" />
         <result column="last_online_time" property="lastOnlineTime" />