浏览代码

预警规则匹配逻辑——集合数据判断条件调试

zhaoxian 4 年之前
父节点
当前提交
85f4648e37

+ 33 - 12
module-alarm/src/main/java/cn/com/ctop/alarm/modules/constant/MatchLogic.java

@@ -29,11 +29,16 @@ public class MatchLogic {
      * @throws
      * @throws
      * @author ZHAOXA
      * @author ZHAOXA
      */
      */
-    public static boolean matchCondition(String type, String condition, String threshold, String value) {
+    public static boolean matchCondition(String type, String condition, String threshold, String value, JSONObject obj) {
         try {
         try {
-            if (Check.isNull(threshold) || Check.isNull(value)) {
+            if (Check.isNull(threshold)) {
                 return false;
                 return false;
             }
             }
+            if (Check.isNull(value)) {
+                obj.put("isNull", true);
+                return true;
+            }
+            obj.put("isNull", false);
             if ("number".equals(type)) {
             if ("number".equals(type)) {
                 BigDecimal bigThr = new BigDecimal(threshold);
                 BigDecimal bigThr = new BigDecimal(threshold);
                 BigDecimal bigVal = new BigDecimal(value);
                 BigDecimal bigVal = new BigDecimal(value);
@@ -69,28 +74,37 @@ public class MatchLogic {
                         break;
                         break;
                 }
                 }
             } else {
             } else {
-                List<String> thresholdArr = strToList(threshold);
-                List<String> values = strToList(value);
-                if ("contain".equals(condition)) {
-                    for (String t : thresholdArr) {
+                if (value.contains("[") && threshold.contains("[")) {
+                    List<String> values = strToList(value);
+                    if ("contain".equals(condition)) {
+                        for (String v : values) {
+                            if (!threshold.contains(v)) {
+                                return true;
+                            }
+                        }
+                    } else {
                         for (String v : values) {
                         for (String v : values) {
-                            if (v.contains(t)) {
+                            if (threshold.contains(v)) {
                                 return true;
                                 return true;
                             }
                             }
                         }
                         }
                     }
                     }
                 } else {
                 } else {
-                    for (String t : thresholdArr) {
-                        for (String v : values) {
-                            if (!v.contains(t)) {
+                    List<String> thresholdArr = strToList(threshold);
+                    if ("contain".equals(condition)) {
+                        for (String t : thresholdArr) {
+                            if (t.contains(value)) {
                                 return true;
                                 return true;
                             }
                             }
                         }
                         }
+                    } else {
+                        return !threshold.contains(value);
                     }
                     }
                 }
                 }
+
             }
             }
         } catch (Exception e) {
         } catch (Exception e) {
-            log.error("条件匹配异常", e);
+            log.error("条件匹配异常,入参为:type={},condition={},threshold={},value={},", type, condition, threshold, value, e);
         }
         }
         return false;
         return false;
     }
     }
@@ -98,7 +112,11 @@ public class MatchLogic {
     /**
     /**
      * 整理发送的消息
      * 整理发送的消息
      */
      */
-    public static String getMsg(RuleGroup ruleGroup, JSONObject user, Long accountId, Long planId, Long creativeId, boolean isPause, String accountName) {
+    public static String getMsg(RuleGroup ruleGroup, JSONObject user, JSONObject obj, boolean isPause, String accountName) {
+        Long creativeId = obj.getLong("creativeId");
+        Long planId = obj.getLong("planId");
+        Long accountId = obj.getLong("accountId");
+        boolean isNull = obj.getBoolean("isNull");
         StringBuffer header = new StringBuffer();
         StringBuffer header = new StringBuffer();
         header.append(ruleGroup.getGroupName()).append("预警").append("<br/>").
         header.append(ruleGroup.getGroupName()).append("预警").append("<br/>").
                 append("预警账户:").append(accountId).append(",授权名称为:").append(accountName).append("<br/>");
                 append("预警账户:").append(accountId).append(",授权名称为:").append(accountName).append("<br/>");
@@ -109,6 +127,9 @@ public class MatchLogic {
         } else if (!Check.isNull(creativeId)) {
         } else if (!Check.isNull(creativeId)) {
             header.append("预警创意:").append(creativeId).append("<br/>");
             header.append("预警创意:").append(creativeId).append("<br/>");
         }
         }
+        if (isNull) {
+            header.append("该规则未匹配到投放数据").append("<br/>");
+        }
         if (isPause) {
         if (isPause) {
             header.append("现已被关停,请您及时查看并调整!");
             header.append("现已被关停,请您及时查看并调整!");
         } else {
         } else {

+ 5 - 4
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleAccountThresholdMapper.xml

@@ -5,23 +5,23 @@
     <select id="selectByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
     <select id="selectByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
        SELECT * FROM ctop_rule_account_threshold
        SELECT * FROM ctop_rule_account_threshold
        WHERE account_id = #{accountId}
        WHERE account_id = #{accountId}
-       AND batch_no is null
+       AND  batch_no =''
     </select>
     </select>
     <select id="selectGroupIdsByAccountId" resultType="java.lang.Long">
     <select id="selectGroupIdsByAccountId" resultType="java.lang.Long">
        SELECT group_id FROM `ctop_rule_account_threshold`
        SELECT group_id FROM `ctop_rule_account_threshold`
-        WHERE batch_no is not null
+        WHERE  batch_no !=''
         and  account_id = #{accountId}
         and  account_id = #{accountId}
         group by group_id
         group by group_id
     </select>
     </select>
     <select id="selectbatchNosByGroupIdId" resultType="java.lang.String">
     <select id="selectbatchNosByGroupIdId" resultType="java.lang.String">
        SELECT batch_no FROM `ctop_rule_account_threshold`
        SELECT batch_no FROM `ctop_rule_account_threshold`
-        WHERE batch_no is not null
+        WHERE  batch_no !=''
         and account_id = #{accountId}
         and account_id = #{accountId}
         and group_id = #{groupId}
         and group_id = #{groupId}
         GROUP BY batch_no
         GROUP BY batch_no
     </select>
     </select>
     <select id="selectCopyThresholdByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
     <select id="selectCopyThresholdByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
-       SELECT * FROM `ctop_rule_account_threshold`
+       SELECT * FROM ctop_rule_account_threshold
         WHERE account_id = #{accountId}
         WHERE account_id = #{accountId}
         and group_id = #{groupId}
         and group_id = #{groupId}
         and batch_no = #{batchNo}
         and batch_no = #{batchNo}
@@ -31,6 +31,7 @@
     select   batch_no from ctop_rule_account_threshold
     select   batch_no from ctop_rule_account_threshold
     where  account_id = #{accountId}
     where  account_id = #{accountId}
     and group_id = #{groupId}
     and group_id = #{groupId}
+    and batch_no !=''
     group by batch_no
     group by batch_no
     </select>
     </select>
 </mapper>
 </mapper>

+ 11 - 7
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleGroupServiceImpl.java

@@ -213,7 +213,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                         for (int i = 0; i < dimensionData.size(); i++) {
                         for (int i = 0; i < dimensionData.size(); i++) {
                             JSONObject obj = dimensionData.getJSONObject(i);
                             JSONObject obj = dimensionData.getJSONObject(i);
                             String value = obj.getString(ruleBase.getIndicatorCode());
                             String value = obj.getString(ruleBase.getIndicatorCode());
-                            if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
+                            if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value, obj)) {
                                 sendMsg(ruleGroup, obj, type, accountName);
                                 sendMsg(ruleGroup, obj, type, accountName);
                             }
                             }
                         }
                         }
@@ -232,6 +232,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @param indicators   指标数据
      * @param indicators   指标数据
      * @param matchData    匹配数据
      * @param matchData    匹配数据
      * @param type         媒体类型 1:头条2:快手
      * @param type         媒体类型 1:头条2:快手
+     * @param accountName  账户名称
      * @return void
      * @return void
      * @throws
      * @throws
      * @author ZHAOXA
      * @author ZHAOXA
@@ -260,7 +261,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     return;
                     return;
                 }
                 }
                 JSONObject accountEntity = dimensionData.getJSONObject(0);
                 JSONObject accountEntity = dimensionData.getJSONObject(0);
-                if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()))) {
+                if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()), accountEntity)) {
                     accountDatas.add(accountEntity);
                     accountDatas.add(accountEntity);
                 } else {
                 } else {
                     log.warn("匹配规则组({}),账户维度数据不符合直接跳过", ruleGroup.getId());
                     log.warn("匹配规则组({}),账户维度数据不符合直接跳过", ruleGroup.getId());
@@ -313,7 +314,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             for (int i = 0; i < ruleDatas.size(); i++) {
             for (int i = 0; i < ruleDatas.size(); i++) {
                 JSONObject targetEntity = dimensionData.getJSONObject(i);
                 JSONObject targetEntity = dimensionData.getJSONObject(i);
                 String value = targetEntity.getString(ruleBase.getIndicatorCode());
                 String value = targetEntity.getString(ruleBase.getIndicatorCode());
-                if (MatchLogic.matchCondition(type, ruleBase.getRuleCondition(), threshold, value)) {
+                if (MatchLogic.matchCondition(type, ruleBase.getRuleCondition(), threshold, value, targetEntity)) {
                     okData.add(targetEntity);
                     okData.add(targetEntity);
                 }
                 }
             }
             }
@@ -322,7 +323,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         for (int i = 0; i < dimensionData.size(); i++) {
         for (int i = 0; i < dimensionData.size(); i++) {
             JSONObject targetEntity = dimensionData.getJSONObject(i);
             JSONObject targetEntity = dimensionData.getJSONObject(i);
             String value = targetEntity.getString(ruleBase.getIndicatorCode());
             String value = targetEntity.getString(ruleBase.getIndicatorCode());
-            if (MatchLogic.matchCondition(type, ruleBase.getRuleCondition(), threshold, value)) {
+            if (MatchLogic.matchCondition(type, ruleBase.getRuleCondition(), threshold, value, targetEntity)) {
                 ruleDatas.add(targetEntity);
                 ruleDatas.add(targetEntity);
             }
             }
         }
         }
@@ -335,6 +336,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @param ruleGroup 达标的规则组对象
      * @param ruleGroup 达标的规则组对象
      * @param obj       达标数据对象
      * @param obj       达标数据对象
      * @param type      1:头条2:快手
      * @param type      1:头条2:快手
+     * @param isNull    查询的值为空
      * @return void
      * @return void
      * @throws
      * @throws
      * @author ZHAOXA
      * @author ZHAOXA
@@ -353,7 +355,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             }
             }
             type 1:头条2:快手
             type 1:头条2:快手
         }*/
         }*/
-        String msg = MatchLogic.getMsg(ruleGroup, user, accountId, planId, creativeId, isPause, accountName);
+        String msg = MatchLogic.getMsg(ruleGroup, user, obj, isPause, accountName);
         String sendType = ruleGroup.getSendType();
         String sendType = ruleGroup.getSendType();
         if ("SMS".equals(sendType)) {
         if ("SMS".equals(sendType)) {
 
 
@@ -495,7 +497,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         try {
         try {
             Map<Long, Object> returnMap = new HashMap<>();
             Map<Long, Object> returnMap = new HashMap<>();
             List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
             List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
-            ruleTemplates.forEach(tem -> {
+            for (RuleTemplate tem : ruleTemplates) {
                 Map<String, Object> map = new HashMap<>();
                 Map<String, Object> map = new HashMap<>();
                 QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
                 QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
                 groupWrapper.eq("template_id", tem.getId());
                 groupWrapper.eq("template_id", tem.getId());
@@ -508,6 +510,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
                     group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
                 }
                 }
                 map.put("isCopy", copyGroups);
                 map.put("isCopy", copyGroups);
+                groupWrapper = new QueryWrapper<>();
+                groupWrapper.eq("template_id", tem.getId());
                 groupWrapper.eq("is_copy", NoEn.NO0.valueInt());
                 groupWrapper.eq("is_copy", NoEn.NO0.valueInt());
                 List<RuleGroup> notCopyGroups = ruleGroupMapper.selectList(groupWrapper);
                 List<RuleGroup> notCopyGroups = ruleGroupMapper.selectList(groupWrapper);
                 for (RuleGroup group : notCopyGroups) {
                 for (RuleGroup group : notCopyGroups) {
@@ -519,7 +523,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                 map.put("notCopy", notCopyGroups);
                 map.put("notCopy", notCopyGroups);
                 map.put("type", tem.getMediaType());
                 map.put("type", tem.getMediaType());
                 returnMap.put(tem.getId(), map);
                 returnMap.put(tem.getId(), map);
-            });
+            }
             return returnMap;
             return returnMap;
         } catch (Exception e) {
         } catch (Exception e) {
             log.error("获取模板规则组集合异常", e);
             log.error("获取模板规则组集合异常", e);