Przeglądaj źródła

告警阶段提交

yf_zn 9 miesięcy temu
rodzic
commit
364ed0a5f1

+ 68 - 4
src/main/java/com/yunfeiyun/agmp/iots/warn/service/IotWarnBussinessService.java

@@ -5,15 +5,17 @@ import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
 import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
 import com.yunfeiyun.agmp.common.web.system.domain.SysConfig;
 import com.yunfeiyun.agmp.common.web.system.domain.SysConfig;
-import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
-import com.yunfeiyun.agmp.iot.common.domain.IotWarncount;
-import com.yunfeiyun.agmp.iot.common.domain.IotWarnindicator;
-import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
+import com.yunfeiyun.agmp.iot.common.domain.*;
 import com.yunfeiyun.agmp.iots.warn.mapper.IotWarnBussinessMapper;
 import com.yunfeiyun.agmp.iots.warn.mapper.IotWarnBussinessMapper;
 import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.aggregation.Aggregation;
+import org.springframework.data.mongodb.core.aggregation.AggregationResults;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
@@ -32,6 +34,9 @@ public class IotWarnBussinessService {
     @Resource
     @Resource
     private RedisCacheManager redisCacheManager;
     private RedisCacheManager redisCacheManager;
 
 
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
     public Long selectIotWarnCountByDevAndConfig(String devId, String configId) {
     public Long selectIotWarnCountByDevAndConfig(String devId, String configId) {
         log.info("查询设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
         log.info("查询设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
         Long count = redisCacheManager.getCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
         Long count = redisCacheManager.getCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
@@ -186,4 +191,63 @@ public class IotWarnBussinessService {
         log.info("查询到 {} 条预警配置信息", list.size());
         log.info("查询到 {} 条预警配置信息", list.size());
         return list;
         return list;
     }
     }
+
+    /**
+     * 统计设备的虫子种类数量
+     *
+     * @param devId     设备业务标识
+     * @param startTime 开始时间
+     * @param endTime   结束时间
+     * @return 虫子种类数量
+     */
+    public int statPestTypeCountByDevId(String devId, String startTime, String endTime) {
+        Query query = new Query();
+
+        // 添加条件:设备业务标识
+        query.addCriteria(Criteria.where("devBid").is(devId));
+
+        // 添加条件:创建时间在指定范围内
+        query.addCriteria(Criteria.where("pestrecogCreatedDate").gte(startTime).lte(endTime));
+
+        // 只查询害虫业务标识字段
+        query.fields().include("pestBusid");
+
+        // 去重查询害虫业务标识
+        List<String> pestTypes = mongoTemplate.findDistinct(query, "pestBusid", IotPestrecog.class, String.class);
+
+        // 返回去重后的害虫类型数量
+        return pestTypes.size();
+    }
+
+    /**
+     * 统计设备的虫子总数量
+     *
+     * @param devId     设备业务标识
+     * @param startTime 开始时间
+     * @param endTime   结束时间
+     * @return 虫子总数量
+     */
+    public int statPestCountByDevId(String devId, String startTime, String endTime) {
+        Query query = new Query();
+
+        // 添加条件:设备业务标识
+        query.addCriteria(Criteria.where("devBid").is(devId));
+
+        // 添加条件:创建时间在指定范围内
+        query.addCriteria(Criteria.where("pestrecogCreatedDate").gte(startTime).lte(endTime));
+
+        // 使用聚合查询统计害虫数量
+        Aggregation aggregation = Aggregation.newAggregation(
+                Aggregation.match(Criteria.where("devBid").is(devId)
+                        .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;
+    }
 }
 }

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

@@ -1,43 +1,183 @@
 package com.yunfeiyun.agmp.iots.warn.service;
 package com.yunfeiyun.agmp.iots.warn.service;
 
 
+import com.yunfeiyun.agmp.common.utils.DateUtils;
+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.warn.model.WarnResult;
+import com.yunfeiyun.agmp.iots.warn.model.WarnStatusDto;
+import com.yunfeiyun.agmp.iots.warn.util.CompareUtil;
+import com.yunfeiyun.agmp.iots.warn.util.WarnMessageBuilderUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+
 @Service
 @Service
+@Slf4j
 public class WarnPestService {
 public class WarnPestService {
     @Autowired
     @Autowired
     private IotWarnBussinessService iotWarnBussinessService;
     private IotWarnBussinessService iotWarnBussinessService;
+    @Autowired
+    private ReCountService reCountService;
 
 
     /**
     /**
      * 虫害处理入口
      * 虫害处理入口
      */
      */
     public void process() {
     public void process() {
-        processPestByType();
-        processPestByCount();
-        processPestByDesignatePest();
+        //单个配置单个设备的处理逻辑,外层需要遍历
+
+        //需要的参数,devId
+        String devId = "";
+
+        //需要的参数,iotWarnindicator的:默认全部,外加+devCode,devTypeBid
+        IotWarnindicator iotWarnindicator = new IotWarnindicator();
+
+        //需要的参数,iotWarnconfig的:全部
+        IotWarnconfig iotWarnconfig = new IotWarnconfig();
+
+        WarnStatusDto pestByType = processPestByType(iotWarnindicator);
+        WarnStatusDto pestByCount = processPestByCount(iotWarnindicator);
+        WarnStatusDto designatePest = processPestByDesignatePest(iotWarnindicator);
+        handlerAllWarnLog(devId, iotWarnconfig, pestByType, pestByCount, designatePest);
     }
     }
 
 
     /**
     /**
      * 根据虫害类型处理
      * 根据虫害类型处理
      */
      */
-    public void processPestByType() {
-        iotWarnBussinessService.selectIotWarnPestConfigInfoList("pestType");
+    public WarnStatusDto processPestByType(IotWarnindicator 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);
+        return buildWarnStatusDto(currentValue, iotWarnindicator);
     }
     }
 
 
     /**
     /**
      * 根据虫害总数处理
      * 根据虫害总数处理
      */
      */
-    public void processPestByCount() {
-        iotWarnBussinessService.selectIotWarnPestConfigInfoList("pestNum");
+    public WarnStatusDto processPestByCount(IotWarnindicator 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);
+        return buildWarnStatusDto(currentValue, iotWarnindicator);
+    }
 
 
+    /**
+     * @param currentValue
+     * @param iotWarnindicator
+     * @return
+     */
+    WarnStatusDto buildWarnStatusDto(int currentValue, IotWarnindicator iotWarnindicator) {
+        String expression = iotWarnindicator.getWiExpression();
+        String targetValue = iotWarnindicator.getWiValue();
+        String statue = iotWarnindicator.getWiStatus();
+        //开启的处理
+        if ("0".equals(statue)) {
+            EnumWarnRuleOp warnRuleOp = EnumWarnRuleOp.findEnumByCode(expression);
+            boolean tempSuccess = CompareUtil.comp(currentValue + "", expression, targetValue);
+            WarnStatusDto warnStatusDto = new WarnStatusDto();
+            warnStatusDto.setDevType(iotWarnindicator.getDevTypeBid());
+            warnStatusDto.setDevCode(iotWarnindicator.getDevCode());
+            warnStatusDto.setName(iotWarnindicator.getWiName());
+            warnStatusDto.setValue(currentValue + "");
+            warnStatusDto.setUnit(iotWarnindicator.getWiUnit());
+            warnStatusDto.setOpt(warnRuleOp.getName());
+            warnStatusDto.setIndicatorValue(targetValue);
+            warnStatusDto.setWarn(tempSuccess);
+            return warnStatusDto;
+        }
+        return null;
     }
     }
 
 
     /**
     /**
      * 根据指定虫害处理
      * 根据指定虫害处理
      */
      */
-    public void processPestByDesignatePest() {
+    public WarnStatusDto processPestByDesignatePest(IotWarnindicator iotWarnindicator) {
         //根据自己需要的父子结构单独再写一个方法
         //根据自己需要的父子结构单独再写一个方法
-        iotWarnBussinessService.selectIotWarnPestConfigInfoList("pestDetail");
+        //iotWarnBussinessService.selectIotWarnPestConfigInfoList("pestDetail");
+        return null;
+    }
+
+    /**
+     * 将三个的告警处理结果合并
+     *
+     * @param devBid
+     * @param iotWarnconfig
+     * @param pestByType
+     * @param pestByCount
+     * @param designatePest
+     */
+    void handlerAllWarnLog(String devBid, IotWarnconfig iotWarnconfig, WarnStatusDto pestByType, WarnStatusDto pestByCount, WarnStatusDto designatePest) {
+        String pestByTypeMessage = null;
+        String pestByCountMessage = null;
+        String designatePestMessage = null;
+        boolean isWarn = false;
+        //构建消息
+        if (pestByType != null && pestByType.isWarn()) {
+            pestByTypeMessage = WarnMessageBuilderUtil.buildWarningMessage(
+                    pestByType.getDevType(),
+                    pestByType.getDevCode(),
+                    pestByType.getName(),
+                    pestByType.getValue(),
+                    pestByType.getUnit(),
+                    pestByType.getOpt(),
+                    pestByType.getIndicatorValue()
+            );
+            isWarn = true;
+        }
+        //构建消息
+        if (pestByCount != null && pestByCount.isWarn()) {
+            pestByCountMessage = WarnMessageBuilderUtil.buildWarningMessage(
+                    pestByType.getDevType(),
+                    pestByType.getDevCode(),
+                    pestByType.getName(),
+                    pestByType.getValue(),
+                    pestByType.getUnit(),
+                    pestByType.getOpt(),
+
+                    pestByType.getIndicatorValue()
+            );
+            isWarn = true;
+        }
+        //构建消息
+        if (designatePest != null && designatePest.isWarn()) {
+            designatePestMessage = WarnMessageBuilderUtil.buildWarningMessage(
+                    pestByType.getDevType(),
+                    pestByType.getDevCode(),
+                    pestByType.getName(),
+                    pestByType.getValue(),
+                    pestByType.getUnit(),
+                    pestByType.getOpt(),
+                    pestByType.getIndicatorValue()
+            );
+            isWarn = true;
+        }
 
 
+        StringBuffer buffer = new StringBuffer();
+        if (pestByTypeMessage != null) {
+            buffer.append(buffer);
+        }
+        if (pestByCountMessage != null) {
+            buffer.append(pestByCountMessage);
+        }
+        if (designatePestMessage != null) {
+            buffer.append(designatePestMessage);
+        }
+        //构建发送消息的参数
+        WarnResult warnResult = new WarnResult();
+        warnResult.setMessageId(warnResult.getUUId());
+        warnResult.setDevId(devBid);
+        warnResult.setTid(iotWarnconfig.getTid());
+        warnResult.setConfigId(iotWarnconfig.getWcBid());
+        warnResult.setReportData("-");
+        warnResult.setTargetReCount(iotWarnconfig.getWcRepeatnum());
+        warnResult.setDevtypeBid(iotWarnconfig.getDevtypeBid());
+        warnResult.setConfig(iotWarnconfig);
+        warnResult.setTriggered(isWarn);
+        warnResult.setMessage(buffer.toString());
+        //发送告警
+        reCountService.handlerMessage(warnResult);
     }
     }
 }
 }

+ 2 - 2
src/main/java/com/yunfeiyun/agmp/iots/warn/service/WarnService.java

@@ -399,7 +399,7 @@ public class WarnService {
             }
             }
         }
         }
         if (warnStatusDto != null && warnStatusDto.isWarn()) {
         if (warnStatusDto != null && warnStatusDto.isWarn()) {
-            String message = WarnMessageBuilderUtil.buildQxzWarningMessage(
+            String message = WarnMessageBuilderUtil.buildWarningMessage(
                     warnStatusDto.getDevType(),
                     warnStatusDto.getDevType(),
                     warnStatusDto.getDevCode(),
                     warnStatusDto.getDevCode(),
                     warnStatusDto.getName(),
                     warnStatusDto.getName(),
@@ -450,7 +450,7 @@ public class WarnService {
                 devType = warnStatusDto.getDevType();
                 devType = warnStatusDto.getDevType();
                 dCode = warnStatusDto.getDevCode();
                 dCode = warnStatusDto.getDevCode();
             }
             }
-            String message = WarnMessageBuilderUtil.buildQxzWarningMessage(
+            String message = WarnMessageBuilderUtil.buildWarningMessage(
                     devType,
                     devType,
                     dCode,
                     dCode,
                     warnStatusDto.getName(),
                     warnStatusDto.getName(),

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iots/warn/util/WarnMessageBuilderUtil.java

@@ -16,7 +16,7 @@ public class WarnMessageBuilderUtil {
      * @param indicatorValue 设定的目标对比值,例如“10-35℃”。
      * @param indicatorValue 设定的目标对比值,例如“10-35℃”。
      * @return 格式化后的告警信息。
      * @return 格式化后的告警信息。
      */
      */
-    public static String buildQxzWarningMessage(String devType, String devCode, String name, String value, String unit, String opt, String indicatorValue) {
+    public static String buildWarningMessage(String devType, String devCode, String name, String value, String unit, String opt, String indicatorValue) {
         // 格式化字符串,将设备类型、设备唯一编码、告警参数名、当前值、单位、动作和目标对比值插入到模板中
         // 格式化字符串,将设备类型、设备唯一编码、告警参数名、当前值、单位、动作和目标对比值插入到模板中
         String tag = "";
         String tag = "";
         if (devType != null && devCode != null) {
         if (devType != null && devCode != null) {

+ 9 - 3
src/main/resources/mapper/IotWarnBusinessMapper.xml

@@ -112,9 +112,15 @@
     <select id="selectIotWarnPestConfigInfoList"
     <select id="selectIotWarnPestConfigInfoList"
             resultType="com.yunfeiyun.agmp.iot.common.domain.IotWarnindicator">
             resultType="com.yunfeiyun.agmp.iot.common.domain.IotWarnindicator">
 
 
-    SELECT * from (
-    select * from IotWarnindicator tb_wi2 where wiCode=#{code}
-    ) tb_wi  LEFT JOIN IotWarnconfig tb_config on tb_wi.wcBid=tb_config.wcBid where tb_config.wcStatus='0'
+          SELECT tb_wi.*,tb_it.devBid,tb_it.devCode,tb_it.devtypeBid from (
+                SELECT tb_2.*,iwo.devBid from (
+                    select * from IotWarnindicator  where wiCode=#{code}
+                ) tb_2
+                LEFT JOIN IotWarnconfig iwc ON tb_2.wcBid=iwc.wcBid
+                LEFT JOIN IotWarnobject iwo on tb_2.wcBid=iwo.wcBid
+        ) tb_wi  LEFT JOIN IotWarnconfig tb_config on tb_wi.wcBid=tb_config.wcBid
+        LEFT JOIN IotDevice tb_it on tb_it.devBid = tb_wi.devBid
+        where tb_config.wcStatus='0'
     </select>
     </select>
     <insert id="insertIotOfflineWarnconfig" parameterType="IotWarnconfig" useGeneratedKeys="true" keyProperty="id">
     <insert id="insertIotOfflineWarnconfig" parameterType="IotWarnconfig" useGeneratedKeys="true" keyProperty="id">
         insert into IotWarnconfig
         insert into IotWarnconfig