Просмотр исходного кода

新增 获取虫情预警要素功能

zhaiyifei 9 месяцев назад
Родитель
Сommit
a40aa2cc2b

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iots/warn/mapper/IotWarnBussinessMapper.java

@@ -52,7 +52,7 @@ public interface IotWarnBussinessMapper {
 
     List<WarnConfigInfo> selectIotWarnConfigInfoList(WarnConfigInfo warnConfigInfo);
 
-    int resetReCountByDevIdAndConfigId(@Param("devBid") String devBid, @Param("wcBid") String configId);
+    int resetReCountByDevBidAndConfigId(@Param("devBid") String devBid, @Param("wcBid") String configId);
 
     IotWarnconfig selectIotWarnOfflineConfigInfo(@Param("tid") String tid);
 

+ 50 - 26
src/main/java/com/yunfeiyun/agmp/iots/warn/service/IotWarnBussinessService.java

@@ -36,42 +36,42 @@ public class IotWarnBussinessService {
     @Autowired
     private MongoTemplate mongoTemplate;
 
-    public Long selectIotWarnCountByDevAndConfig(String devId, String configId) {
-        log.info("查询设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
-        Long count = redisCacheManager.getCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
+    public Long selectIotWarnCountByDevAndConfig(String devBid, String configId) {
+        log.info("查询设备ID: {}, 配置ID: {} 的重复次数", devBid, configId);
+        Long count = redisCacheManager.getCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devBid);
         if (count != null) {
             log.info("从缓存中获取到重复次数: {}", count);
             return count;
         }
-        IotWarncount iotWarncount = iotWarnBussinessMapper.selectIotWarnCountByDevAndConfig(devId, configId);
+        IotWarncount iotWarncount = iotWarnBussinessMapper.selectIotWarnCountByDevAndConfig(devBid, configId);
         if (iotWarncount == null) {
-            log.warn("数据库中未找到设备ID: {}, 配置ID: {} 的重复次数数据", devId, configId);
+            log.warn("数据库中未找到设备ID: {}, 配置ID: {} 的重复次数数据", devBid, configId);
             return null;
         } else {
             log.info("从数据库中获取到重复次数: {}, 并将其加入缓存", iotWarncount.getWctCount());
-            redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, iotWarncount.getWctCount());
+            redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devBid, iotWarncount.getWctCount());
             return iotWarncount.getWctCount();
         }
     }
 
-    public int incrementReCount(Long reCount, String devId, String configId, String tid) {
-        log.info("增加设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
+    public int incrementReCount(Long reCount, String devBid, String configId, String tid) {
+        log.info("增加设备ID: {}, 配置ID: {} 的重复次数", devBid, configId);
         if (reCount == null) {
             log.info("初始化重复次数为1");
             IotWarncount iotWarncount = new IotWarncount();
             iotWarncount.setWctBid(IdUtils.fastUUID());
-            iotWarncount.setDevBid(devId);
+            iotWarncount.setDevBid(devBid);
             iotWarncount.setWcBid(configId);
             iotWarncount.setLastUpdateTime(DateUtils.dateTimeNow());
             iotWarncount.setWctCount(1L);
             iotWarncount.setTid(tid);
             int result = iotWarnBussinessMapper.insertIncrementReCount(iotWarncount);
-            redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, iotWarncount.getWctCount());
+            redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devBid, iotWarncount.getWctCount());
             log.info("初始化重复次数成功, 结果: {}", result);
             return result;
         } else {
-            int result = iotWarnBussinessMapper.updateIncrementReCount(devId, configId);
-            redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, reCount + 1);
+            int result = iotWarnBussinessMapper.updateIncrementReCount(devBid, configId);
+            redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devBid, reCount + 1);
             log.info("更新重复次数成功, 新的重复次数: {}", reCount + 1);
             return result;
         }
@@ -97,10 +97,10 @@ public class IotWarnBussinessService {
         return result;
     }
 
-    public int resetReCountByDevIdAndConfigId(String devId, String configId) {
-        log.info("重置设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
-        redisCacheManager.deleteObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
-        int result = iotWarnBussinessMapper.resetReCountByDevIdAndConfigId(devId, configId);
+    public int resetReCountByDevBidAndConfigId(String devBid, String configId) {
+        log.info("重置设备ID: {}, 配置ID: {} 的重复次数", devBid, configId);
+        redisCacheManager.deleteObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devBid);
+        int result = iotWarnBussinessMapper.resetReCountByDevBidAndConfigId(devBid, configId);
         log.info("重置结果: {}", result);
         return result;
     }
@@ -167,11 +167,11 @@ public class IotWarnBussinessService {
     /**
      * 设备若正常上报数据后,则离线告警自动处理
      *
-     * @param devId
+     * @param devBid
      */
-    public void autoDealWarnOfflineLog(String devId) {
+    public void autoDealWarnOfflineLog(String devBid) {
         IotWarnlog iotWarnlog = new IotWarnlog();
-        iotWarnlog.setDevBid(devId);
+        iotWarnlog.setDevBid(devBid);
         iotWarnlog.setWlDealtime(DateUtils.dateTimeNow());
         iotWarnlog.setWlDealresult("该设备已于" + DateUtils.dateTimeNow() + "上报数据,离线告警自动恢复。");
         iotWarnlog.setStatus("1");
@@ -194,16 +194,16 @@ public class IotWarnBussinessService {
     /**
      * 统计设备的虫子种类数量
      *
-     * @param devId     设备业务标识
+     * @param devBid     设备业务标识
      * @param startTime 开始时间
      * @param endTime   结束时间
      * @return 虫子种类数量
      */
-    public int statPestTypeCountByDevId(String devId, String startTime, String endTime) {
+    public int statPestTypeCountByDevBid(String devBid, String startTime, String endTime) {
         Query query = new Query();
 
         // 添加条件:设备业务标识
-        query.addCriteria(Criteria.where("devBid").is(devId));
+        query.addCriteria(Criteria.where("devBid").is(devBid));
 
         // 添加条件:创建时间在指定范围内
         query.addCriteria(Criteria.where("pestrecogCreatedDate").gte(startTime).lte(endTime));
@@ -221,23 +221,47 @@ public class IotWarnBussinessService {
     /**
      * 统计设备的虫子总数量
      *
-     * @param devId     设备业务标识
+     * @param devBid     设备业务标识
      * @param startTime 开始时间
      * @param endTime   结束时间
      * @return 虫子总数量
      */
-    public int statPestCountByDevId(String devId, String startTime, String endTime) {
+    public int statPestCountByDevBid(String devBid, String startTime, String endTime) {
         Query query = new Query();
 
         // 添加条件:设备业务标识
-        query.addCriteria(Criteria.where("devBid").is(devId));
+        query.addCriteria(Criteria.where("devBid").is(devBid));
 
         // 添加条件:创建时间在指定范围内
         query.addCriteria(Criteria.where("pestrecogCreatedDate").gte(startTime).lte(endTime));
 
         // 使用聚合查询统计害虫数量
         Aggregation aggregation = Aggregation.newAggregation(
-                Aggregation.match(Criteria.where("devBid").is(devId)
+                Aggregation.match(Criteria.where("devBid").is(devBid)
+                        .and("pestrecogCreatedDate").gte(startTime).lte(endTime)),
+                Aggregation.group().sum("pestrecogNum").as("totalPestCount")
+        );
+
+        // 执行聚合查询
+        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "IotPestrecog", Map.class);
+        Map<String, Object> result = results.getUniqueMappedResult();
+
+        // 获取统计结果,默认为0
+        return result != null ? ((Number) result.get("totalPestCount")).intValue() : 0;
+    }
+
+    public int statPestDetailByDevBid(String devBid, String startTime, String endTime) {
+        Query query = new Query();
+
+        // 添加条件:设备业务标识
+        query.addCriteria(Criteria.where("devBid").is(devBid));
+
+        // 添加条件:创建时间在指定范围内
+        query.addCriteria(Criteria.where("pestrecogCreatedDate").gte(startTime).lte(endTime));
+
+        // 使用聚合查询统计害虫数量
+        Aggregation aggregation = Aggregation.newAggregation(
+                Aggregation.match(Criteria.where("devBid").is(devBid)
                         .and("pestrecogCreatedDate").gte(startTime).lte(endTime)),
                 Aggregation.group().sum("pestrecogNum").as("totalPestCount")
         );

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iots/warn/service/ReCountService.java

@@ -127,7 +127,7 @@ public class ReCountService {
         }
         if (count != 0) {
             log.info("【设备预警】消息标识{}:重置设备ID:{}, 配置ID:{} 的重复计数", messageId, devId, configId);
-            iotWarnBussinessService.resetReCountByDevIdAndConfigId(devId, configId);
+            iotWarnBussinessService.resetReCountByDevBidAndConfigId(devId, configId);
         }
     }
 

+ 9 - 4
src/main/java/com/yunfeiyun/agmp/iots/warn/service/WarnPestService.java

@@ -85,8 +85,8 @@ public class WarnPestService {
         validateParam(iotWarnindicator);
         String startTime = DateUtils.getDate() + " 00:00:00";
         String endTime = DateUtils.getDate() + " 23:59:59";
-        String wdBid = iotWarnindicator.getWdBid();
-        int currentValue = iotWarnBussinessService.statPestTypeCountByDevId(wdBid, startTime, endTime);
+        String devBid = iotWarnindicator.getDevBid();
+        int currentValue = iotWarnBussinessService.statPestTypeCountByDevBid(devBid, startTime, endTime);
         return buildWarnStatusDto(currentValue, iotWarnindicator);
     }
 
@@ -97,8 +97,8 @@ public class WarnPestService {
         validateParam(iotWarnindicator);
         String startTime = DateUtils.getDate() + " 00:00:00";
         String endTime = DateUtils.getDate() + " 23:59:59";
-        String wdBid = iotWarnindicator.getWdBid();
-        int currentValue = iotWarnBussinessService.statPestCountByDevId(wdBid, startTime, endTime);
+        String devBid = iotWarnindicator.getDevBid();
+        int currentValue = iotWarnBussinessService.statPestCountByDevBid(devBid, startTime, endTime);
         return buildWarnStatusDto(currentValue, iotWarnindicator);
     }
 
@@ -134,6 +134,11 @@ public class WarnPestService {
      */
     private WarnStatusDto processPestByDesignatePest(IotWarnindicator iotWarnindicator) {
         validateParam(iotWarnindicator);
+        String startTime = DateUtils.getDate() + " 00:00:00";
+        String endTime = DateUtils.getDate() + " 23:59:59";
+        String devBid = iotWarnindicator.getDevBid();
+
+        int currentValue = iotWarnBussinessService.statPestDetailByDevBid(devBid, startTime, endTime);
         //根据自己需要的父子结构单独再写一个方法
         //iotWarnBussinessService.selectIotWarnPestConfigInfoList("pestDetail");
         return null;

+ 1 - 1
src/main/resources/mapper/IotWarnBusinessMapper.xml

@@ -70,7 +70,7 @@
     <update id="updateIncrementReCount">
              update IotWarncount set wctCount=wctCount+1 where devBid=#{devBid} and wcBid=#{wcBid};
     </update>
-    <update id="resetReCountByDevIdAndConfigId">
+    <update id="resetReCountByDevBidAndConfigId">
               update IotWarncount set wctCount=0 where devBid=#{devBid} and wcBid=#{wcBid};
     </update>