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

告警:重复次数判断机制完善

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

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

@@ -38,7 +38,9 @@ public interface IotWarnBussinessMapper {
      */
     int insertWarnRecord(IotWarnlog iotWarnlog);
 
-    int updateIncrementReCount(@Param("devId") String devId, @Param("configId") String configId);
+    int updateIncrementReCount(@Param("devId") String devId, @Param("wcBid") String configId);
 
     int insertIncrementReCount(IotWarncount iotWarncount);
+
+    int resetReCountByDevIdAndConfigId(@Param("devId") String devId, @Param("wcBid") String configId);
 }

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

@@ -17,6 +17,7 @@ public class IotWarnBussinessService {
 
     /**
      * 查询设备+配置的重复次数
+     * 注意:如果没数据会返回Null
      *
      * @param devId
      * @param configId
@@ -24,8 +25,9 @@ public class IotWarnBussinessService {
      */
     public Long selectIotWarnCountByDevAndConfig(String devId, String configId) {
         IotWarncount iotWarncount = iotWarncountMapper.selectIotWarnCountByDevAndConfig(devId, configId);
+        // 如果为Null,说明还没触发过
         if (iotWarncount == null) {
-            return 0L;
+            return null;
         }
         return iotWarncount.getWctCount();
     }
@@ -38,10 +40,8 @@ public class IotWarnBussinessService {
      * @param configId
      * @return
      */
-    int incrementReCount(Long reCount, String devId, String configId, String tid) {
+    public int incrementReCount(Long reCount, String devId, String configId, String tid) {
         if (reCount == null) {
-            return iotWarncountMapper.updateIncrementReCount(devId, configId);
-        } else {
             //从新添加,就是从0变为1
             IotWarncount iotWarncount = new IotWarncount();
             iotWarncount.setWctBid(IdUtils.fastUUID());
@@ -51,10 +51,12 @@ public class IotWarnBussinessService {
             iotWarncount.setWctCount(1L);
             iotWarncount.setTid(tid);
             return iotWarncountMapper.insertIncrementReCount(iotWarncount);
+        } else {
+            return iotWarncountMapper.updateIncrementReCount(devId, configId);
         }
     }
 
-    int incrementReCount(Long reCount, WarnResult warnResult) {
+    public int incrementReCount(Long reCount, WarnResult warnResult) {
         return incrementReCount(reCount, warnResult.getDevId(), warnResult.getConfigId(), warnResult.getTid());
     }
 
@@ -63,16 +65,25 @@ public class IotWarnBussinessService {
      *
      * @return
      */
-    int resetReCount() {
+    public int resetReCount() {
         return iotWarncountMapper.resetReCount();
     }
 
     /**
+     * 重置指定设备和配置的重复次数
+     *
+     * @return
+     */
+    public int resetReCountByDevIdAndConfigId(String devId, String configId) {
+        return iotWarncountMapper.resetReCountByDevIdAndConfigId(devId, configId);
+    }
+
+    /**
      * 插入预警记录
      *
      * @return
      */
-    int insertWarnRecord(IotWarnlog iotWarnlog) {
+    public int insertWarnRecord(IotWarnlog iotWarnlog) {
         return iotWarncountMapper.insertWarnRecord(iotWarnlog);
     }
 

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

@@ -76,16 +76,21 @@ public class ReCountService {
     }
 
     /**
+     * 如果这次没触发,但之前有值(重复次数),需要将重复次数为0
+     *
      * @param devId
      * @param configId
      */
     public void cleanReCount(String devId, String configId) {
-        //如果之前有值,这次没触发,清除缓存,清除数据库重复次数为0
-        //1. 从缓存获取,如果为空,查数据库,并把值放到缓存
-        long cacheCount = 0;
-        //2. 缓存为0,不能是空,因为如果是空,会查数据库
-        if (cacheCount != 0) {
-            //清除缓存,清除数据库,是设置为0
+
+        Long count = iotWarnBussinessService.selectIotWarnCountByDevAndConfig(devId, configId);
+        //优化缓存情况
+        if (count == null) {
+            return;
+        }
+        if (count != 0) {
+            iotWarnBussinessService.resetReCountByDevIdAndConfigId(devId, configId);
+            return;
         }
     }
 

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

@@ -63,11 +63,14 @@
     </update>
 
     <update id="resetReCount">
-        update IotWarncount set wctCount=0;
+            update IotWarncount set wctCount=0;
     </update>
     <update id="updateIncrementReCount">
              update IotWarncount set wctCount=wctCount+1 where devBid=#{devBid} and wcBid=#{wcBid};
     </update>
+    <update id="resetReCountByDevIdAndConfigId">
+              update IotWarncount set wctCount=0 where devBid=#{devBid} and wcBid=#{wcBid};
+    </update>
 
     <select id="selectIotWarnCountByDevAndConfig"
             resultType="com.yunfeiyun.agmp.iot.common.domain.IotWarncount">