Forráskód Böngészése

预警规则匹配逻辑——添加可复制规则逻辑

zhaoxian 4 éve
szülő
commit
0430af3233

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

@@ -16,5 +16,9 @@ public interface RuleAccountThresholdMapper extends BaseMapper<RuleAccountThresh
 
     List<RuleAccountThreshold> selectByAccountId(Long accountId);
 
-    List<RuleAccountThreshold> selectBatchNoByAccountId(Long accountId);
+    List<Long> selectGroupIdsByAccountId(Long accountId);
+
+    List<Long> selectbatchNosByGroupIdId(Long accountId, Long groupId);
+
+    List<RuleAccountThreshold> selectCopyThresholdByAccountId(Long accountId, Long groupId, Long batchNo);
 }

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

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

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

@@ -117,101 +117,157 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             log.warn("查询匹配数据失败");
             return;
         }
+        boolean isCopy = false;
         Map<Long, Object> map = (Map<Long, Object>) ruleGroupMap.get(templates.getTemplateId());
         //媒体类型 1:头条2:快手
         Integer type = (Integer) map.get("type");
         //常规规则+阈值
         List<RuleGroup> notCopyGroups = (List<RuleGroup>) map.get("notCopy");
-        JSONObject notCopythreshold = getThreshold(templates.getAccountId(), false);
+        JSONObject notCopythreshold = getThreshold(templates.getAccountId(), isCopy);
         //可复制规则+阈值
         List<RuleGroup> isCopyGroups = (List<RuleGroup>) map.get("isCopy");
+        JSONObject isCopythreshold = new JSONObject();
         if (!Check.isNull(isCopyGroups)) {
-            JSONObject isCopythreshold = getThreshold(templates.getAccountId(), true);
+            isCopy = true;
+            isCopythreshold = getThreshold(templates.getAccountId(), isCopy);
         }
         log.info("已获取账户({})的数据,开始匹配规则", templates.getAccountId());
         try {
             if (!Check.isNull(notCopyGroups)) {
-                checkRuleGroups(notCopyGroups, notCopythreshold, indicators, matchData, type);
+                checkRuleGroups(notCopyGroups, notCopythreshold, indicators, matchData, type, false);
             }
+            if (isCopy && !Check.isNull(isCopythreshold)) {
+                checkRuleGroups(isCopyGroups, isCopythreshold, indicators, matchData, type, isCopy);
+            }
+
         } catch (Exception e) {
             log.error("匹配规则逻辑异常", e);
         }
     }
 
-    private void checkRuleGroups(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, Integer type) {
+    private void checkRuleGroups(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, Integer type, boolean isCopy) {
         for (RuleGroup ruleGroup : ruleGroups) {
             List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
             if (Check.isNull(ruleBaseList)) {
                 continue;
             }
-            boolean isBase = "base".equals(ruleGroup.getRuleType());
-            //匹配单规则
-            if (isBase) {
-                RuleBase ruleBase = ruleBaseList.get(0);
-                //指标对象
-                JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
-                //指标阈值
-                String threshold = thresholdObj.getString(ruleBase.getId().toString());
-                //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
-                if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
-                    log.warn("阈值为空/不限,该规则({})不执行", ruleBase.getId());
-                    continue;
-                }
-                //维度数据
-                JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
-                if (!Check.isNull(dimensionData)) {
-                    for (int i = 0; i < dimensionData.size(); i++) {
-                        JSONObject obj = dimensionData.getJSONObject(i);
-                        String value = obj.getString(ruleBase.getIndicatorCode());
-                        if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
-                            sendMsg(ruleGroup, obj);
+            //复制逻辑
+            if (isCopy) {
+                JSONObject batchNo = thresholdObj.getJSONObject(ruleGroup.getId().toString());
+                for (Map.Entry<String, Object> entry : batchNo.entrySet()) {
+                    Map<String, String> thresholdMap = (Map<String, String>) entry.getValue();
+                    //符合规则的数据集
+                    JSONArray targetDatas = new JSONArray();
+                    JSONArray planDatas = new JSONArray();
+                    JSONArray creativeDatas = new JSONArray();
+                    JSONArray accountDatas = new JSONArray();
+                    for (RuleBase ruleBase : ruleBaseList) {
+                        //指标对象
+                        JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
+                        //阈值类型
+                        String dataType = indicator.getString("dataType");
+                        //指标阈值
+                        String threshold = thresholdMap.get(ruleBase.getId());
+                        //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
+                        if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                            continue;
+                        }
+                        JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
+                        //账户维度数据
+                        if ("account".equals(ruleBase.getRuleDimension())) {
+                            JSONObject accountEntity = dimensionData.getJSONObject(0);
+                            if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()))) {
+                                accountDatas.add(accountEntity);
+                            } else {
+                                log.warn("判断规则组({}),账户规则不符合", ruleGroup.getId());
+                                break;
+                            }
+                        } else if ("plan".equals(ruleBase.getRuleDimension())) {
+                            planDatas = getOKData(planDatas, dimensionData, ruleBase, dataType, threshold);
+                        } else if ("creative".equals(ruleBase.getRuleDimension())) {
+                            creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, dataType, threshold);
+                        } else if ("target".equals(ruleBase.getRuleDimension())) {
+                            targetDatas = getOKData(targetDatas, dimensionData, ruleBase, dataType, threshold);
+                        }
+                    }
+                    //获取达标可发送的数据
+                    JSONArray sendData = MatchLogic.getSendData(accountDatas, planDatas, creativeDatas, targetDatas);
+                    if (!Check.isNull(sendData)) {
+                        //执行发送
+                        for (int i = 0; i < sendData.size(); i++) {
+                            sendMsg(ruleGroup, sendData.getJSONObject(i));
                         }
                     }
                 }
-                //匹配组合规则
             } else {
-                boolean flag = false;
-                //符合规则的数据集
-                JSONArray targetDatas = new JSONArray();
-                JSONArray planDatas = new JSONArray();
-                JSONArray creativeDatas = new JSONArray();
-                JSONArray accountDatas = new JSONArray();
-                for (RuleBase ruleBase : ruleBaseList) {
+                boolean isBase = "base".equals(ruleGroup.getRuleType());
+                //匹配单规则
+                if (isBase) {
+                    RuleBase ruleBase = ruleBaseList.get(0);
                     //指标对象
                     JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
-                    //阈值类型
-                    String dataType = indicator.getString("dataType");
                     //指标阈值
                     String threshold = thresholdObj.getString(ruleBase.getId().toString());
                     //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
                     if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                        log.warn("阈值为空/不限,该规则({})不执行", ruleBase.getId());
                         continue;
                     }
+                    //维度数据
                     JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
-                    //账户维度数据
-                    if ("account".equals(ruleBase.getRuleDimension())) {
-                        JSONObject accountEntity = dimensionData.getJSONObject(0);
-                        if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()))) {
-                            accountDatas.add(accountEntity);
-                        } else {
-                            log.warn("判断规则组({}),账户规则不符合", ruleGroup.getId());
-                            break;
+                    if (!Check.isNull(dimensionData)) {
+                        for (int i = 0; i < dimensionData.size(); i++) {
+                            JSONObject obj = dimensionData.getJSONObject(i);
+                            String value = obj.getString(ruleBase.getIndicatorCode());
+                            if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
+                                sendMsg(ruleGroup, obj);
+                            }
                         }
-                    } else if ("plan".equals(ruleBase.getRuleDimension())) {
-                        planDatas = getOKData(planDatas, dimensionData, ruleBase, dataType, threshold);
-                    } else if ("creative".equals(ruleBase.getRuleDimension())) {
-                        creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, dataType, threshold);
-                    } else if ("target".equals(ruleBase.getRuleDimension())) {
-                        targetDatas = getOKData(targetDatas, dimensionData, ruleBase, dataType, threshold);
                     }
+                    //匹配组合规则
+                } else {
+                    //符合规则的数据集
+                    JSONArray targetDatas = new JSONArray();
+                    JSONArray planDatas = new JSONArray();
+                    JSONArray creativeDatas = new JSONArray();
+                    JSONArray accountDatas = new JSONArray();
+                    for (RuleBase ruleBase : ruleBaseList) {
+                        //指标对象
+                        JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
+                        //阈值类型
+                        String dataType = indicator.getString("dataType");
+                        //指标阈值
+                        String threshold = thresholdObj.getString(ruleBase.getId().toString());
+                        //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
+                        if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                            continue;
+                        }
+                        JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
+                        //账户维度数据
+                        if ("account".equals(ruleBase.getRuleDimension())) {
+                            JSONObject accountEntity = dimensionData.getJSONObject(0);
+                            if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()))) {
+                                accountDatas.add(accountEntity);
+                            } else {
+                                log.warn("判断规则组({}),账户规则不符合", ruleGroup.getId());
+                                break;
+                            }
+                        } else if ("plan".equals(ruleBase.getRuleDimension())) {
+                            planDatas = getOKData(planDatas, dimensionData, ruleBase, dataType, threshold);
+                        } else if ("creative".equals(ruleBase.getRuleDimension())) {
+                            creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, dataType, threshold);
+                        } else if ("target".equals(ruleBase.getRuleDimension())) {
+                            targetDatas = getOKData(targetDatas, dimensionData, ruleBase, dataType, threshold);
+                        }
 
-                }
-                //获取达标可发送的数据
-                JSONArray sendData = MatchLogic.getSendData(accountDatas, planDatas, creativeDatas, targetDatas);
-                if (!Check.isNull(sendData)) {
-                    //执行发送
-                    for (int i = 0; i < sendData.size(); i++) {
-                        sendMsg(ruleGroup, sendData.getJSONObject(i));
+                    }
+                    //获取达标可发送的数据
+                    JSONArray sendData = MatchLogic.getSendData(accountDatas, planDatas, creativeDatas, targetDatas);
+                    if (!Check.isNull(sendData)) {
+                        //执行发送
+                        for (int i = 0; i < sendData.size(); i++) {
+                            sendMsg(ruleGroup, sendData.getJSONObject(i));
+                        }
                     }
                 }
             }
@@ -331,13 +387,29 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         JSONObject obj = new JSONObject();
         try {
             if (isCopy) {
-                List<RuleAccountThreshold> copyThresholdList = ruleAccountThresholdMapper.selectBatchNoByAccountId(accountId);
-                if (Check.isNull(copyThresholdList)) {
+                List<Long> groupList = ruleAccountThresholdMapper.selectGroupIdsByAccountId(accountId);
+                if (Check.isNull(groupList)) {
                     return null;
                 }
-                for (RuleAccountThreshold threshold : copyThresholdList) {
+                for (Long groupId : groupList) {
+                    JSONObject groupIdObj = new JSONObject();
+                    List<Long> batchNos = ruleAccountThresholdMapper.selectbatchNosByGroupIdId(accountId, groupId);
+                    if (Check.isNull(batchNos)) {
+                        return null;
+                    }
+                    for (Long batchNo : batchNos) {
+                        JSONObject batchNoObj = new JSONObject();
+                        List<RuleAccountThreshold> thresholdList = ruleAccountThresholdMapper.selectCopyThresholdByAccountId(accountId, groupId, batchNo);
+                        if (Check.isNull(thresholdList)) {
+                            return null;
+                        }
+                        for (RuleAccountThreshold threshold : thresholdList) {
+                            batchNoObj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshold());
+                        }
+                        groupIdObj.put(batchNo.toString(), batchNoObj);
+                    }
+                    obj.put(groupId.toString(), groupIdObj);
                 }
-
             } else {
                 List<RuleAccountThreshold> thresholdList = ruleAccountThresholdMapper.selectByAccountId(accountId);
                 if (Check.isNull(thresholdList)) {