فهرست منبع

新增 获取虫情指定害虫预警功能

zhaiyifei 9 ماه پیش
والد
کامیت
63ce97d250

+ 2 - 2
src/main/java/com/yunfeiyun/agmp/iots/warn/job/WarnJob.java

@@ -39,8 +39,8 @@ public class WarnJob {
     /**
      * 病虫害告警定时检查方法
      */
-    //@Scheduled(cron = "0 * * * * ?")
-    @Scheduled(cron = "0 0/20 * * * ?")
+    @Scheduled(cron = "* * * * * ?")
+//    @Scheduled(cron = "0 0/20 * * * ?")
     public void pestWarnJob() {
         // 处理虫害
         warnService.processWarningPestData();

+ 9 - 0
src/main/java/com/yunfeiyun/agmp/iots/warn/model/WarnPestDetailStatDto.java

@@ -0,0 +1,9 @@
+package com.yunfeiyun.agmp.iots.warn.model;
+
+import lombok.Data;
+
+@Data
+public class WarnPestDetailStatDto {
+    private String pestBusid;
+    private int totalPestCount;
+}

+ 18 - 15
src/main/java/com/yunfeiyun/agmp/iots/warn/service/IotWarnBussinessService.java

@@ -259,28 +259,31 @@ public class IotWarnBussinessService {
         return result != null ? ((Number) result.get("totalPestCount")).intValue() : 0;
     }
 
-    public int statPestDetailByDevBid(String devBid, String startTime, String endTime) {
-        Query query = new Query();
+    public List<WarnPestDetailStatDto> statPestDetailByDevBid(IotDevice iotDevice, String startTime, String endTime) {
+        String devBid = iotDevice.getDevBid();
+        String devCbdrecogtype = iotDevice.getDevCbdrecogtype();
 
         // 添加条件:设备业务标识
-        query.addCriteria(Criteria.where("devBid").is(devBid));
-
-        // 添加条件:创建时间在指定范围内
-        query.addCriteria(Criteria.where("pestrecogCreatedDate").gte(startTime).lte(endTime));
-
+        Criteria criteria = Criteria.where("devBid").is(devBid)
+                .and("cbdrecogType").is(devCbdrecogtype)
+                .and("pestrecogCreatedDate").gte(startTime).lte(endTime);
+
+        MatchOperation matchOperation = Aggregation.match(criteria);
+        GroupOperation groupOperation = Aggregation.group("pestBusid")
+                .sum("pestrecogNum").as("totalPestCount");
+        ProjectionOperation projectionOperation = Aggregation.project()
+                .and("_id").as("pestBusid")
+                .and("totalPestCount").as("totalPestCount");
         // 使用聚合查询统计害虫数量
         Aggregation aggregation = Aggregation.newAggregation(
-                Aggregation.match(Criteria.where("devBid").is(devBid)
-                        .and("pestrecogCreatedDate").gte(startTime).lte(endTime)),
-                Aggregation.group().sum("pestrecogNum").as("totalPestCount")
+                matchOperation,
+                groupOperation,
+                projectionOperation
         );
 
         // 执行聚合查询
-        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;
+        AggregationResults<WarnPestDetailStatDto> results = mongoTemplate.aggregate(aggregation, "IotPestrecog", WarnPestDetailStatDto.class);
+        return results.getMappedResults();
     }
 
     /**

+ 105 - 36
src/main/java/com/yunfeiyun/agmp/iots/warn/service/WarnPestService.java

@@ -4,11 +4,15 @@ import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.exception.BizException;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotPest;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarnindicator;
 import com.yunfeiyun.agmp.iot.common.enums.EnumWarnRuleOp;
+import com.yunfeiyun.agmp.iots.service.IIotPestService;
 import com.yunfeiyun.agmp.iots.warn.model.IotWarnconfigCbdInfoVo;
+import com.yunfeiyun.agmp.iots.warn.model.WarnPestDetailStatDto;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
 import com.yunfeiyun.agmp.iots.warn.model.WarnStatusDto;
 import com.yunfeiyun.agmp.iots.warn.util.CompareUtil;
@@ -18,8 +22,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 
 @Service
@@ -30,6 +33,9 @@ public class WarnPestService {
     @Autowired
     private ReCountService reCountService;
 
+    @Autowired
+    private IIotPestService iotPestService;
+
     /**
      * 虫害处理入口
      */
@@ -59,7 +65,7 @@ public class WarnPestService {
                     }
                 }
                 //底层调用:统一处理一个虫情设备的三个要素
-                processPestIotWarnindicators(devBid, iotWarnconfig, pestByTypeWarnindicator, pestByCountWarnindicator, designatePestWarnindicator);
+                processPestIotWarnindicators(iotDevice, iotWarnconfig, pestByTypeWarnindicator, pestByCountWarnindicator, designatePestWarnindicator);
             } catch (Exception e) {
                 log.error("{}", e);
             }
@@ -75,18 +81,19 @@ public class WarnPestService {
      * @param pestByCountWarnindicator
      * @param designatePestWarnindicator
      */
-    private void processPestIotWarnindicators(String devId, IotWarnconfig iotWarnconfig, IotWarnindicator pestByTypeWarnindicator, IotWarnindicator pestByCountWarnindicator, IotWarnindicator designatePestWarnindicator) {
-        WarnStatusDto pestByType = processPestByType(pestByTypeWarnindicator);
-        WarnStatusDto pestByCount = processPestByCount(pestByCountWarnindicator);
-        WarnStatusDto designatePest = processPestByDesignatePest(designatePestWarnindicator);
+    private void processPestIotWarnindicators(IotDevice iotDevice, IotWarnconfig iotWarnconfig, IotWarnindicator pestByTypeWarnindicator, IotWarnindicator pestByCountWarnindicator, IotWarnindicator designatePestWarnindicator) {
+        WarnStatusDto pestByType = processPestByType(iotDevice, pestByTypeWarnindicator);
+        WarnStatusDto pestByCount = processPestByCount(iotDevice, pestByCountWarnindicator);
+        List<WarnStatusDto> warnStatusDtoList = processPestByDesignatePest(iotDevice, designatePestWarnindicator);
         //合并结果
-        handlerAllWarnLog(devId, iotWarnconfig, pestByType, pestByCount, designatePest);
+        String devBid = iotDevice.getDevBid();
+        handlerAllWarnLog(devBid, iotWarnconfig, pestByType, pestByCount, warnStatusDtoList);
     }
 
     /**
      * 根据虫害类型处理
      */
-    private WarnStatusDto processPestByType(IotWarnindicator iotWarnindicator) {
+    private WarnStatusDto processPestByType(IotDevice iotDevice, IotWarnindicator iotWarnindicator) {
         validateParam(iotWarnindicator);
         String datePre = getDateBefore();
         String startTime = datePre + " 00:00:00";
@@ -103,12 +110,12 @@ public class WarnPestService {
     /**
      * 根据虫害总数处理
      */
-    private WarnStatusDto processPestByCount(IotWarnindicator iotWarnindicator) {
+    private WarnStatusDto processPestByCount(IotDevice iotDevice, IotWarnindicator iotWarnindicator) {
         validateParam(iotWarnindicator);
         String datePre = getDateBefore();
         String startTime = datePre + " 00:00:00";
         String endTime = datePre + " 23:59:59";
-        String devBid = iotWarnindicator.getDevBid();
+        String devBid = iotDevice.getDevBid();
         int currentValue = iotWarnBussinessService.statPestCountByDevBid(devBid, startTime, endTime);
         return buildWarnStatusDto(currentValue, iotWarnindicator);
     }
@@ -124,10 +131,12 @@ public class WarnPestService {
         String statue = iotWarnindicator.getWiStatus();
         //开启的处理
         if ("0".equals(statue)) {
+            String devtypeBid = iotWarnindicator.getDevtypeBid();
             EnumWarnRuleOp warnRuleOp = EnumWarnRuleOp.findEnumByCode(expression);
+            String devType = IotDeviceDictEnum.getLv1NameByCode(devtypeBid);
             boolean tempSuccess = CompareUtil.comp(currentValue + "", expression, targetValue);
             WarnStatusDto warnStatusDto = new WarnStatusDto();
-            warnStatusDto.setDevType(iotWarnindicator.getDevtypeBid());
+            warnStatusDto.setDevType(devType);
             warnStatusDto.setDevCode(iotWarnindicator.getDevCode());
             warnStatusDto.setName(iotWarnindicator.getWiName());
             warnStatusDto.setValue(currentValue + "");
@@ -143,16 +152,52 @@ public class WarnPestService {
     /**
      * 根据指定虫害处理
      */
-    private WarnStatusDto processPestByDesignatePest(IotWarnindicator iotWarnindicator) {
+    private List<WarnStatusDto> processPestByDesignatePest(IotDevice iotDevice, IotWarnindicator iotWarnindicator) {
         validateParam(iotWarnindicator);
-        String startTime = DateUtils.getDate() + " 00:00:00";
-        String endTime = DateUtils.getDate() + " 23:59:59";
-        String devBid = iotWarnindicator.getDevBid();
+        List<IotWarnindicator> childrenList = iotWarnindicator.getChildrenList();
+        List<WarnStatusDto> warnStatusDtoList = new ArrayList<>();
 
-        int currentValue = iotWarnBussinessService.statPestDetailByDevBid(devBid, startTime, endTime);
-        //根据自己需要的父子结构单独再写一个方法
-        //iotWarnBussinessService.selectIotWarnPestConfigInfoList("pestDetail");
-        return null;
+        if(Objects.equals(iotWarnindicator.getWiStatus(), "1")){
+            return warnStatusDtoList;
+        }
+
+        if(childrenList == null || childrenList.isEmpty()){
+            return warnStatusDtoList;
+        }
+
+        HashMap<String, IotPest> iotPestHashMap = iotPestService.selectIotPestHashMap(null);
+        Map<String, IotWarnindicator> iotWarnindicatorMap = new HashMap<>();
+        for (IotWarnindicator item : childrenList) {
+            item.setDevBid(iotWarnindicator.getDevBid());
+            item.setDevtypeBid(iotWarnindicator.getDevtypeBid());
+            item.setDevCode(iotWarnindicator.getDevCode());
+
+            String pestId = item.getWiCode();
+            IotPest iotPest = iotPestHashMap.get(pestId);
+            if (iotPest == null) {
+                log.error("【设备预警】虫害:配置ID: {}, 虫情ID: {} 不存在", iotWarnindicator.getWcBid(), pestId);
+                continue;
+            }
+            iotWarnindicatorMap.put(iotPest.getPestBid(), item);
+        }
+        String datePre = getDateBefore();
+        String startTime = datePre + " 00:00:00";
+        String endTime = datePre + " 23:59:59";
+        List<WarnPestDetailStatDto> warnPestDetailStatDtoList = iotWarnBussinessService.statPestDetailByDevBid(iotDevice, startTime, endTime);
+
+        for (WarnPestDetailStatDto item : warnPestDetailStatDtoList) {
+            String pestBusid = item.getPestBusid();
+            int totalPestCount = item.getTotalPestCount();
+            IotWarnindicator iotWarnindicatorItem = iotWarnindicatorMap.get(pestBusid); //根据虫情业务id获取虫情配置
+            if (iotWarnindicatorItem == null) {
+                continue;
+            }
+            WarnStatusDto warnStatusDto = buildWarnStatusDto(totalPestCount, iotWarnindicatorItem);
+            if(warnStatusDto != null && warnStatusDto.isWarn()){
+                warnStatusDtoList.add(warnStatusDto);
+            }
+        }
+        return warnStatusDtoList;
     }
 
     /**
@@ -164,7 +209,7 @@ public class WarnPestService {
      * @param pestByCount
      * @param designatePest
      */
-    private void handlerAllWarnLog(String devBid, IotWarnconfig iotWarnconfig, WarnStatusDto pestByType, WarnStatusDto pestByCount, WarnStatusDto designatePest) {
+    private void handlerAllWarnLog(String devBid, IotWarnconfig iotWarnconfig, WarnStatusDto pestByType, WarnStatusDto pestByCount, List<WarnStatusDto> warnStatusDtoList) {
         if (StringUtils.isEmpty(devBid)) {
             log.error("【设备预警】病虫害:设备id 不可为空. 设备ID: {}, 配置ID: {}", devBid, iotWarnconfig.getWcBid());
             throw new BizException(ErrorCode.FAILURE.getCode(), "病虫害:设备id 不可为空");
@@ -172,18 +217,23 @@ public class WarnPestService {
         log.info("开始处理设备ID为 {}, 配置ID为 {} 的预警信息", devBid, iotWarnconfig.getWcBid());
         log.info("开始处理设备ID为 {}, 配置ID为 {} 的预警信息:pestByType: {}", iotWarnconfig.getWcBid(), devBid, pestByType);
         log.info("开始处理设备ID为 {}, 配置ID为 {} 的预警信息:pestByCount: {}", iotWarnconfig.getWcBid(), devBid, pestByCount);
-        log.info("开始处理设备ID为 {}, 配置ID为 {} 的预警信息:designatePest: {}", devBid, iotWarnconfig.getWcBid(), designatePest);
+        log.info("开始处理设备ID为 {}, 配置ID为 {} 的预警信息:designatePest: {}", devBid, iotWarnconfig.getWcBid(), warnStatusDtoList);
 
         String pestByTypeMessage = null;
         String pestByCountMessage = null;
         String designatePestMessage = null;
         boolean isWarn = false;
+        String devType = null;
+        String devCode = null;
 
         // 构建消息
         if (pestByType != null && pestByType.isWarn()) {
+            devType = pestByType.getDevType();
+            devCode = pestByType.getDevCode();
+
             pestByTypeMessage = WarnMessageBuilderUtil.buildWarningMessage(
-                    pestByType.getDevType(),
-                    pestByType.getDevCode(),
+                    null,
+                    null,
                     pestByType.getName(),
                     pestByType.getValue(),
                     pestByType.getUnit(),
@@ -195,9 +245,12 @@ public class WarnPestService {
         }
 
         if (pestByCount != null && pestByCount.isWarn()) {
+            devType = pestByCount.getDevType();
+            devCode = pestByCount.getDevCode();
+
             pestByCountMessage = WarnMessageBuilderUtil.buildWarningMessage(
-                    pestByCount.getDevType(),
-                    pestByCount.getDevCode(),
+                    null,
+                    null,
                     pestByCount.getName(),
                     pestByCount.getValue(),
                     pestByCount.getUnit(),
@@ -207,32 +260,48 @@ public class WarnPestService {
             log.info("根据数量生成警告消息: {}. 设备ID: {}, 配置ID: {}", pestByCountMessage, devBid, iotWarnconfig.getWcBid());
             isWarn = true;
         }
+        StringBuilder messageBuilder = new StringBuilder();
+        if(warnStatusDtoList != null && !warnStatusDtoList.isEmpty()) {
+            messageBuilder.append("指定虫害: \n");
+            for(WarnStatusDto warnStatusDto : warnStatusDtoList) {
+                devType = warnStatusDto.getDevType();
+                devCode = warnStatusDto.getDevCode();
 
-        if (designatePest != null && designatePest.isWarn()) {
-            designatePestMessage = WarnMessageBuilderUtil.buildWarningMessage(
-                    designatePest.getDevType(),
-                    designatePest.getDevCode(),
-                    designatePest.getName(),
-                    designatePest.getValue(),
-                    designatePest.getUnit(),
-                    designatePest.getOpt(),
-                    designatePest.getIndicatorValue()
-            );
+                String message = WarnMessageBuilderUtil.buildWarningMessage(
+                        null,
+                        null,
+                        warnStatusDto.getName(),
+                        warnStatusDto.getValue(),
+                        warnStatusDto.getUnit(),
+                        warnStatusDto.getOpt(),
+                        warnStatusDto.getIndicatorValue()
+                );
+                messageBuilder.append(message).append("\n");
+            }
+        }
+        if(messageBuilder.length() > 0) {
+            designatePestMessage = messageBuilder.toString();
             log.info("针对指定病虫害生成警告消息: {}. 设备ID: {}, 配置ID: {}", designatePestMessage, devBid, iotWarnconfig.getWcBid());
             isWarn = true;
         }
 
         StringBuffer buffer = new StringBuffer();
+        if(isWarn){
+            buffer.append(devType).append("设备(").append(devCode).append("), 触发告警,告警原因: \n");
+        }
         if (pestByTypeMessage != null) {
             buffer.append(pestByTypeMessage);
+            buffer.append("\n");
         }
         if (pestByCountMessage != null) {
             buffer.append(pestByCountMessage);
+            buffer.append("\n");
         }
         if (designatePestMessage != null) {
             buffer.append(designatePestMessage);
         }
 
+
         // 构建发送消息的参数
         WarnResult warnResult = new WarnResult();
         warnResult.setMessageId(warnResult.getUUId()); // 确保WarnResult中有getUUId方法