浏览代码

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

zhaoxian 4 年之前
父节点
当前提交
e609c97e4f

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

@@ -2,6 +2,7 @@ package cn.com.ctop.alarm.modules.mapper;
 
 import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -21,4 +22,7 @@ public interface RuleAccountThresholdMapper extends BaseMapper<RuleAccountThresh
     List<String> selectbatchNosByGroupIdId(Long accountId, Long groupId);
 
     List<RuleAccountThreshold> selectCopyThresholdByAccountId(Long accountId, Long groupId, String batchNo);
+    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

@@ -26,4 +26,11 @@
         and group_id = #{groupId}
         and batch_no = #{batchNo}
     </select>
+
+    <select id="getBatchNo" resultType="java.lang.String">
+    select   batch_no from ctop_rule_account_threshold
+    where  account_id = #{accountId}
+    and group_id = #{groupId}
+    group by batch_no
+    </select>
 </mapper>

+ 4 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleAccountThresholdService.java

@@ -4,6 +4,8 @@ import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import java.util.List;
+
 /**
  * @Description: 规则关联账户 阈值
  * @Author: jeecg-boot
@@ -19,4 +21,6 @@ public interface IRuleAccountThresholdService extends IService<RuleAccountThresh
      * @return
      */
     JSONObject fillThreshold(JSONObject requestJson) throws Exception;
+
+    List<String> getBatchNo(Long accountId, Long groupId);
 }

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

@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 
 /**
  * 规则模板
@@ -74,6 +75,12 @@ public class RuleAccountTemplateServiceImpl extends ServiceImpl<RuleAccountTempl
                 continue;
             }
             groupCount += 1;
+            Integer isCopy = ruleGroup.getIsCopy();
+            String batchId = null;
+            if (isCopy == 1) {
+                batchId = UUID.randomUUID().toString().replace("-", "");
+
+            }
             JSONArray ruleIds = JSONArray.parseArray(ruleGroup.getRuleIds());
             if (Check.isNull(ruleIds)) {
                 continue;
@@ -86,7 +93,11 @@ public class RuleAccountTemplateServiceImpl extends ServiceImpl<RuleAccountTempl
                 }
                 RuleAccountThreshold threshold = new RuleAccountThreshold();
                 threshold.setAccountId(accountId);
+                threshold.setGroupId(groupId);
                 threshold.setRuleId(ruleBase.getId());
+                if (isCopy == 1) {
+                    threshold.setBatchNo(batchId);
+                }
                 if (ruleBase.getVariableType() == 0) {
                     threshold.setThreshold(ruleBase.getThreshold());
                 }

+ 14 - 17
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleAccountThresholdServiceImpl.java

@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
@@ -53,12 +54,9 @@ public class RuleAccountThresholdServiceImpl extends ServiceImpl<RuleAccountThre
             if (Check.isNull(ruleDetail)) {
                 continue;
             }
-
             Long groupId = groupJson.getLong("groupId");
             Integer isCopy = groupJson.getInteger("isCopy");
-
-            String batchNo = UUID.randomUUID().toString().replace(" ", "");
-
+            String batchNo = UUID.randomUUID().toString().replace("-", "");
             for (int j = 0; j < ruleDetail.size(); j++) {
                 if (isCopy == 1) { // 规则组可复制
                     JSONArray ruleDetailArr = ruleDetail.getJSONArray(i);
@@ -106,19 +104,6 @@ public class RuleAccountThresholdServiceImpl extends ServiceImpl<RuleAccountThre
                         continue;
                     }
                     RuleAccountThreshold updateThreshold = new RuleAccountThreshold();
-                    /*if (threshold.contains("[") && threshold.contains("]")) {
-                     *//*  String replace = threshold.replace("[", "").replace("]", "");
-                    List<String> strings = Arrays.asList(replace.split(","));
-                    JSONArray array = new JSONArray();
-                    for (int k = 0; k < strings.size(); k++) {
-                        array.add(strings.get(i))
-                         JSONArray.parseArray(strings);
-                    }*//*
-
-                    updateThreshold.setThreshoId(JSONArray.parseArray(threshold).toJSONString());
-                } else {
-
-                }*/
                     updateThreshold.setThreshold(threshold);
                     QueryWrapper<RuleAccountThreshold> updateQueryWrapper = new QueryWrapper();
                     updateQueryWrapper.eq("account_id", accountId);
@@ -136,4 +121,16 @@ public class RuleAccountThresholdServiceImpl extends ServiceImpl<RuleAccountThre
         returnJson.put("updateCount", updateCount);
         return returnJson;
     }
+
+    /**
+     * 获取批次号列表
+     *
+     * @param accountId
+     * @param groupId
+     * @return
+     */
+    @Override
+    public List<String> getBatchNo(Long accountId, Long groupId) {
+        return ruleAccountThresholdMapper.getBatchNo(accountId, groupId);
+    }
 }

+ 120 - 51
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleTemplateServiceImpl.java

@@ -12,6 +12,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 /**
  * 规则模板
  *
@@ -67,9 +69,7 @@ public class RuleTemplateServiceImpl extends ServiceImpl<RuleTemplateMapper, Rul
             RuleTemplate template = new RuleTemplate();
             template.setMediaType(mediaType);
             template.setTemplateName(templateName);
-
             JSONArray groupIds = new JSONArray();
-
             JSONArray ruleList = requestJson.getJSONArray("ruleList"); // 模板规则详情
             if (Check.isNull(ruleList)) {
                 throw new Exception("模板具体规则不能为空");
@@ -266,8 +266,6 @@ public class RuleTemplateServiceImpl extends ServiceImpl<RuleTemplateMapper, Rul
         if (Check.isNull(groupIds)) {
             throw new Exception("未获取规则集");
         }
-
-
         JSONArray ruleList = new JSONArray();
         for (int i = 0; i < groupIds.size(); i++) {
             Long groupId = groupIds.getLong(i);
@@ -285,68 +283,139 @@ public class RuleTemplateServiceImpl extends ServiceImpl<RuleTemplateMapper, Rul
             groupJson.put("sendType", group.getSendType());
             groupJson.put("isCopy", group.getIsCopy());
             groupJson.put("isRequired", group.getIsRequired());
-            JSONArray ruleIds = JSONArray.parseArray(group.getRuleIds());
-            if (Check.isNull(ruleIds)) {
-                continue;
-            }
             JSONArray ruleDetail = new JSONArray();
-            for (int j = 0; j < ruleIds.size(); j++) {
-                Long ruleId = ruleIds.getLong(j);
-                RuleBase ruleBase = ruleBaseService.getById(ruleId);
-                if (Check.isNull(ruleBase)) {
+            if (group.getIsCopy() == 0) { // 规则不可复制
+                JSONArray ruleIds = JSONArray.parseArray(group.getRuleIds());
+                if (Check.isNull(ruleIds)) {
                     continue;
                 }
-                JSONObject ruleJson = new JSONObject();
-                ruleJson.put("ruleId", ruleBase.getId());
-                ruleJson.put("ruleName", ruleBase.getRuleName());
-                ruleJson.put("judgeFormat", ruleBase.getJudgeFormat());
-                ruleJson.put("indicatorCode", ruleBase.getIndicatorCode());
-                ruleJson.put("ruleCondition", ruleBase.getRuleCondition());
-                QueryWrapper<RuleAccountThreshold> accountThresholdQueryWrapper = new QueryWrapper<>();
-                accountThresholdQueryWrapper.eq("account_id", accountId);
-                accountThresholdQueryWrapper.eq("rule_id", ruleId);
-                RuleAccountThreshold threshold = accountThresholdService.getOne(accountThresholdQueryWrapper);
-                if (!Check.isNull(threshold)) {
-                    String value = threshold.getThreshold();
-                    if (!Check.isNull(value)) {
-                        if (value.contains("[") && value.contains("]")) {
-                            ruleJson.put("threshold", JSONArray.parseArray(value));
+                for (int j = 0; j < ruleIds.size(); j++) {
+                    Long ruleId = ruleIds.getLong(j);
+                    RuleBase ruleBase = ruleBaseService.getById(ruleId); // 查询基础规则信息
+                    if (Check.isNull(ruleBase)) {
+                        continue;
+                    }
+                    JSONObject ruleJson = new JSONObject();
+                    ruleJson.put("ruleId", ruleBase.getId());
+                    ruleJson.put("ruleName", ruleBase.getRuleName());
+                    ruleJson.put("judgeFormat", ruleBase.getJudgeFormat());
+                    ruleJson.put("indicatorCode", ruleBase.getIndicatorCode());
+                    ruleJson.put("ruleCondition", ruleBase.getRuleCondition());
+                    QueryWrapper<RuleAccountThreshold> accountThresholdQueryWrapper = new QueryWrapper<>();
+                    accountThresholdQueryWrapper.eq("account_id", accountId);
+                    accountThresholdQueryWrapper.eq("rule_id", ruleId);
+                    accountThresholdQueryWrapper.eq("group_id", groupId);
+                    RuleAccountThreshold threshold = accountThresholdService.getOne(accountThresholdQueryWrapper); // 查询基础规则绑定的阈值
+                    if (!Check.isNull(threshold)) {
+                        String value = threshold.getThreshold();
+                        if (!Check.isNull(value)) {
+                            if (value.contains("[") && value.contains("]")) {
+                                ruleJson.put("threshold", JSONArray.parseArray(value));
+                            } else {
+                                ruleJson.put("threshold", value);
+                            }
                         } else {
-                            ruleJson.put("threshold", value);
+                            ruleJson.put("threshold", null);
                         }
                     } else {
                         ruleJson.put("threshold", null);
                     }
-                    // ruleJson.put("threshold", threshold.getThreshoId());
-                } else {
-                    ruleJson.put("threshold", null);
+
+                    ruleJson.put("ruleDimension", ruleBase.getRuleDimension());
+                    ruleJson.put("variableType", ruleBase.getVariableType());
+                    ruleJson.put("isUnlimited", ruleBase.getIsUnlimited());
+                    /**
+                     * 查询指标对应信息
+                     */
+                    QueryWrapper<RuleIndicator> indicatorQueryWrapper = new QueryWrapper<>();
+                    indicatorQueryWrapper.eq("code", ruleBase.getIndicatorCode());
+                    indicatorQueryWrapper.eq("dimension", ruleBase.getRuleDimension());
+                    indicatorQueryWrapper.last("limit 1");
+                    RuleIndicator ruleIndicator = ruleIndicatorService.getOne(indicatorQueryWrapper);  // 查询指标的详情信息
+                    if (!Check.isNull(ruleIndicator)) {
+                        ruleJson.put("dataType", ruleIndicator.getDataType());
+                        ruleJson.put("dictId", ruleIndicator.getDictId());
+                        ruleJson.put("dataUnit", ruleIndicator.getDataUnit());
+                        ruleJson.put("name", ruleIndicator.getName());
+                        ruleJson.put("modelType", ruleIndicator.getModelType());
+                    }
+
+                    ruleDetail.add(ruleJson);
                 }
+                groupJson.put("ruleDetail", ruleDetail);
+            } else if (group.getIsCopy() == 1) {  // 如果规则可以复制
 
-                ruleJson.put("ruleDimension", ruleBase.getRuleDimension());
-                ruleJson.put("variableType", ruleBase.getVariableType());
-                ruleJson.put("isUnlimited", ruleBase.getIsUnlimited());
-                /**
-                 * 查询指标对应信息
-                 */
-                QueryWrapper<RuleIndicator> indicatorQueryWrapper = new QueryWrapper<>();
-                indicatorQueryWrapper.eq("code", ruleBase.getIndicatorCode());
-                indicatorQueryWrapper.eq("dimension", ruleBase.getRuleDimension());
-                indicatorQueryWrapper.last("limit 1");
-                RuleIndicator ruleIndicator = ruleIndicatorService.getOne(indicatorQueryWrapper);
-                if (!Check.isNull(ruleIndicator)) {
-                    ruleJson.put("dataType", ruleIndicator.getDataType());
-                    ruleJson.put("dictId", ruleIndicator.getDictId());
-                    ruleJson.put("dataUnit", ruleIndicator.getDataUnit());
-                    ruleJson.put("name", ruleIndicator.getName());
-                    ruleJson.put("modelType", ruleIndicator.getModelType());
+                List<String> batchNoList = accountThresholdService.getBatchNo(accountId, groupId); // 查询批次号
+                if (Check.isNull(batchNoList)) {
+                    continue;
                 }
 
-                ruleDetail.add(ruleJson);
+                JSONArray ruleArr = new JSONArray();
+                for (int j = 0; j < batchNoList.size(); j++) {
+                    String batchNo = (String) batchNoList.get(j);
+                    if (Check.isNull(batchNo)) {
+                        continue;
+                    }
+
+                    QueryWrapper<RuleAccountThreshold> queryWrapper = new QueryWrapper<>();
+                    queryWrapper.eq("account_id", accountId);
+                    queryWrapper.eq("group_id", groupId);
+                    queryWrapper.eq("batch_no", batchNo);
+                    List<RuleAccountThreshold> list = accountThresholdService.list(queryWrapper); // 根据批次号查询绑定的基础规则阈值集合
+                    if (Check.isNull(list)) {
+                        continue;
+                    }
+
+                    for (RuleAccountThreshold threshold : list) {
+                        RuleBase ruleBase = ruleBaseService.getById(threshold.getRuleId()); // 根据阈值集合ruleId 查询基础规则信息
+                        if (Check.isNull(ruleBase)) {
+                            continue;
+                        }
+                        JSONObject ruleJson = new JSONObject();
+                        ruleJson.put("ruleId", ruleBase.getId());
+                        ruleJson.put("ruleName", ruleBase.getRuleName());
+                        ruleJson.put("judgeFormat", ruleBase.getJudgeFormat());
+                        ruleJson.put("indicatorCode", ruleBase.getIndicatorCode());
+                        ruleJson.put("ruleCondition", ruleBase.getRuleCondition());
+                        String value = threshold.getThreshold();
+                        if (!Check.isNull(value)) {
+                            if (value.contains("[") && value.contains("]")) {
+                                ruleJson.put("threshold", JSONArray.parseArray(value));
+                            } else {
+                                ruleJson.put("threshold", value);
+                            }
+                        } else {
+                            ruleJson.put("threshold", null);
+                        }
+
+
+                        ruleJson.put("ruleDimension", ruleBase.getRuleDimension());
+                        ruleJson.put("variableType", ruleBase.getVariableType());
+                        ruleJson.put("isUnlimited", ruleBase.getIsUnlimited());
+                        /**
+                         * 查询指标对应信息
+                         */
+                        QueryWrapper<RuleIndicator> indicatorQueryWrapper = new QueryWrapper<>();
+                        indicatorQueryWrapper.eq("code", ruleBase.getIndicatorCode());
+                        indicatorQueryWrapper.eq("dimension", ruleBase.getRuleDimension());
+                        indicatorQueryWrapper.last("limit 1");
+                        RuleIndicator ruleIndicator = ruleIndicatorService.getOne(indicatorQueryWrapper); // 查询基础规则的指标信息
+                        if (!Check.isNull(ruleIndicator)) {
+                            ruleJson.put("dataType", ruleIndicator.getDataType());
+                            ruleJson.put("dictId", ruleIndicator.getDictId());
+                            ruleJson.put("dataUnit", ruleIndicator.getDataUnit());
+                            ruleJson.put("name", ruleIndicator.getName());
+                            ruleJson.put("modelType", ruleIndicator.getModelType());
+                        }
+                        ruleArr.add(ruleJson);
+                    }
+
+                    ruleDetail.add(ruleArr);
+                }
+                groupJson.put("ruleDetail", ruleDetail);
             }
-            groupJson.put("ruleDetail", ruleDetail);
             ruleList.add(groupJson);
         }
-
         returnJson.put("ruleList", ruleList);
         return returnJson;
     }