浏览代码

Merge remote-tracking branch 'origin/master_rule_engine' into master_rule_engine

# Conflicts:
#	module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/RuleAccountThresholdMapper.java
yumeng 4 年之前
父节点
当前提交
f1348127dd

+ 2 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/RuleAccountThresholdMapper.java

@@ -18,4 +18,6 @@ public interface RuleAccountThresholdMapper extends BaseMapper<RuleAccountThresh
     List<RuleAccountThreshold> selectByAccountId(Long accountId);
 
     List<String> getBatchNo(@Param("accountId") Long accountId, @Param("groupId") Long groupId);
+
+    List<RuleAccountThreshold> selectBatchNoByAccountId(Long accountId);
 }

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

@@ -5,6 +5,13 @@
     <select id="selectByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
        SELECT * FROM ctop_rule_account_threshold
        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`
+        WHERE batch_no is not null
+        and  account_id = #{accountId}
+        group by group_id,batch_no
     </select>
 
     <select id="getBatchNo" resultType="java.lang.String">

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

@@ -83,7 +83,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             return;
         }
         //获取规则集
-        Map<Long, List<RuleGroup>> ruleGroupMap = getRuleGroupMap();
+        Map<Long, Object> ruleGroupMap = getRuleGroupMap();
         if (Check.isNull(ruleGroupMap)) {
             log.warn("获取规则集失败");
             return;
@@ -95,113 +95,130 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             return;
         }
         for (RuleAccountTemplate templates : ruleAccountTemplates) {
-            List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
-            matchAlarmRules(ruleGroups, getThreshold(ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId())), indicators, templates.getAccountId());
+            matchAlarmRules(ruleGroupMap, indicators, templates);
         }
     }
 
     /**
      * 匹配预警规则
      *
-     * @param ruleGroups   规则集
-     * @param thresholdObj 规则id阈值
-     * @param indicators   指标码
+     * @param ruleGroups 规则数据
+     * @param templates  规则模板
+     * @param indicators 指标码
      * @return void
      * @throws
      * @author ZHAOXA
      */
-    private void matchAlarmRules(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject indicators, Long accountId) {
+    private void matchAlarmRules(Map<Long, Object> ruleGroupMap, JSONObject indicators, RuleAccountTemplate templates) {
         userObj = new JSONObject();
         //查询匹配数据
-        JSONObject matchData = getRuleData(accountId);
+        JSONObject matchData = getRuleData(templates.getAccountId());
         if (Check.isNull(matchData)) {
             log.warn("查询匹配数据失败");
             return;
         }
-        log.info("已获取账户({})的数据,开始匹配规则", accountId);
+        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);
+        //可复制规则+阈值
+        List<RuleGroup> isCopyGroups = (List<RuleGroup>) map.get("isCopy");
+        if (!Check.isNull(isCopyGroups)) {
+            JSONObject isCopythreshold = getThreshold(templates.getAccountId(), true);
+        }
+        log.info("已获取账户({})的数据,开始匹配规则", templates.getAccountId());
         try {
-            for (RuleGroup ruleGroup : ruleGroups) {
-                List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
-                if (Check.isNull(ruleBaseList)) {
+            if (!Check.isNull(notCopyGroups)) {
+                checkRuleGroups(notCopyGroups, notCopythreshold, indicators, matchData, type);
+            }
+        } catch (Exception e) {
+            log.error("匹配规则逻辑异常", e);
+        }
+    }
+
+    private void checkRuleGroups(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, Integer type) {
+        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;
                 }
-                boolean isBase = "base".equals(ruleGroup.getRuleType());
-                //匹配单规则
-                if (isBase) {
-                    RuleBase ruleBase = ruleBaseList.get(0);
+                //维度数据
+                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);
+                        }
+                    }
+                }
+                //匹配组合规则
+            } else {
+                boolean flag = false;
+                //符合规则的数据集
+                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)) {
-                        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 ("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);
                     }
-                    //匹配组合规则
-                } else {
-                    boolean flag = false;
-                    //符合规则的数据集
-                    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));
                     }
                 }
             }
-        } catch (Exception e) {
-            log.error("匹配规则逻辑异常", e);
         }
     }
 
+
     /**
      * @param ruleDatas     达标数据集
      * @param dimensionData 匹配数据
@@ -310,20 +327,31 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
     }
 
     //整理阈值数据
-    public JSONObject getThreshold(List<RuleAccountThreshold> thresholdList) {
-        if (Check.isNull(thresholdList)) {
-            return null;
-        }
-        JSONObject obj = null;
+    public JSONObject getThreshold(Long accountId, boolean isCopy) {
+        JSONObject obj = new JSONObject();
         try {
-            obj = new JSONObject();
-            for (RuleAccountThreshold threshold : thresholdList) {
-                obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshold());
+            if (isCopy) {
+                List<RuleAccountThreshold> copyThresholdList = ruleAccountThresholdMapper.selectBatchNoByAccountId(accountId);
+                if (Check.isNull(copyThresholdList)) {
+                    return null;
+                }
+                for (RuleAccountThreshold threshold : copyThresholdList) {
+                }
+
+            } else {
+                List<RuleAccountThreshold> thresholdList = ruleAccountThresholdMapper.selectByAccountId(accountId);
+                if (Check.isNull(thresholdList)) {
+                    return null;
+                }
+                for (RuleAccountThreshold threshold : thresholdList) {
+                    obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshold());
+                }
             }
         } catch (Exception e) {
             log.error("获取账户阈值数据集失败", e);
         }
         return obj;
+
     }
 
     //整理指标数据
@@ -352,23 +380,36 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private Map<Long, List<RuleGroup>> getRuleGroupMap() {
+    private Map<Long, Object> getRuleGroupMap() {
         try {
-            Map<Long, List<RuleGroup>> map = new HashMap<>();
+            Map<Long, Object> returnMap = new HashMap<>();
             List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
             ruleTemplates.forEach(tem -> {
+                Map<String, Object> map = new HashMap<>();
                 QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
                 groupWrapper.eq("template_id", tem.getId());
-                List<RuleGroup> ruleGroups = ruleGroupMapper.selectList(groupWrapper);
-                for (RuleGroup group : ruleGroups) {
+                groupWrapper.eq("is_copy", NoEn.NO1.valueInt());
+                List<RuleGroup> copyGroups = ruleGroupMapper.selectList(groupWrapper);
+                for (RuleGroup group : copyGroups) {
+                    QueryWrapper<RuleBase> baseWrapper = new QueryWrapper<>();
+                    baseWrapper.eq("group_id", group.getId());
+                    baseWrapper.orderByDesc("is_unlimited");
+                    group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
+                }
+                map.put("isCopy", copyGroups);
+                groupWrapper.eq("is_copy", NoEn.NO0.valueInt());
+                List<RuleGroup> notCopyGroups = ruleGroupMapper.selectList(groupWrapper);
+                for (RuleGroup group : notCopyGroups) {
                     QueryWrapper<RuleBase> baseWrapper = new QueryWrapper<>();
                     baseWrapper.eq("group_id", group.getId());
                     baseWrapper.orderByDesc("is_unlimited");
                     group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
                 }
-                map.put(tem.getId(), ruleGroups);
+                map.put("notCopy", notCopyGroups);
+                map.put("type", tem.getMediaType());
+                returnMap.put(tem.getId(), map);
             });
-            return map;
+            return returnMap;
         } catch (Exception e) {
             log.error("获取模板规则组集合异常", e);
         }