|
@@ -1,8 +1,18 @@
|
|
|
package cn.com.ctop.alarm.modules.service.impl;
|
|
|
|
|
|
import cn.com.ctop.alarm.modules.constant.MatchLogic;
|
|
|
-import cn.com.ctop.alarm.modules.entity.*;
|
|
|
-import cn.com.ctop.alarm.modules.mapper.*;
|
|
|
+import cn.com.ctop.alarm.modules.entity.RuleAccountTemplate;
|
|
|
+import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
|
|
|
+import cn.com.ctop.alarm.modules.entity.RuleBase;
|
|
|
+import cn.com.ctop.alarm.modules.entity.RuleGroup;
|
|
|
+import cn.com.ctop.alarm.modules.entity.RuleIndicator;
|
|
|
+import cn.com.ctop.alarm.modules.entity.RuleTemplate;
|
|
|
+import cn.com.ctop.alarm.modules.mapper.RuleAccountTemplateMapper;
|
|
|
+import cn.com.ctop.alarm.modules.mapper.RuleAccountThresholdMapper;
|
|
|
+import cn.com.ctop.alarm.modules.mapper.RuleBaseMapper;
|
|
|
+import cn.com.ctop.alarm.modules.mapper.RuleGroupMapper;
|
|
|
+import cn.com.ctop.alarm.modules.mapper.RuleIndicatorMapper;
|
|
|
+import cn.com.ctop.alarm.modules.mapper.RuleTemplateMapper;
|
|
|
import cn.com.ctop.alarm.modules.service.IRuleAccountTemplateService;
|
|
|
import cn.com.ctop.alarm.modules.service.IRuleGroupService;
|
|
|
import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
@@ -109,82 +119,86 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
return;
|
|
|
}
|
|
|
log.info("已获取账户({})的数据,开始匹配规则", accountId);
|
|
|
- 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());
|
|
|
+ try {
|
|
|
+ for (RuleGroup ruleGroup : ruleGroups) {
|
|
|
+ List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
|
|
|
+ if (Check.isNull(ruleBaseList)) {
|
|
|
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);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //匹配组合规则
|
|
|
- } 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 {
|
|
|
+ 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);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -233,12 +247,14 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
Long planId = obj.getLong("planId");
|
|
|
Long accountId = obj.getLong("accountId");
|
|
|
JSONObject user = getUserByAccountId(accountId);
|
|
|
- boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
|
|
|
+ //TODO 关停操作
|
|
|
+ boolean isPause = false;
|
|
|
+ /* "PAUSE".equals(ruleGroup.getOperate());
|
|
|
if (isPause) {
|
|
|
if (shutDown(accountId, planId, creativeId, user.getString("id"))) {
|
|
|
isPause = true;
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
String msg = MatchLogic.getMsg(ruleGroup, user, accountId, planId, creativeId, isPause);
|
|
|
String sendType = ruleGroup.getSendType();
|
|
|
if ("SMS".equals(sendType)) {
|
|
@@ -252,11 +268,19 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据accountId获取人员对象
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return com.alibaba.fastjson.JSONObject
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
private JSONObject getUserByAccountId(Long accountId) {
|
|
|
JSONObject obj = userObj.getJSONObject(accountId.toString());
|
|
|
if (Check.isNull(obj)) {
|
|
|
JSONObject user = sysUserMapper.selectUserByAccount(accountId);
|
|
|
- obj.put(accountId.toString(), user);
|
|
|
+ userObj.put(accountId.toString(), user);
|
|
|
return user;
|
|
|
}
|
|
|
return obj;
|
|
@@ -290,22 +314,31 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
if (Check.isNull(thresholdList)) {
|
|
|
return null;
|
|
|
}
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
- for (RuleAccountThreshold threshold : thresholdList) {
|
|
|
- obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshoId());
|
|
|
+ JSONObject obj = null;
|
|
|
+ try {
|
|
|
+ obj = new JSONObject();
|
|
|
+ for (RuleAccountThreshold threshold : thresholdList) {
|
|
|
+ obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshoId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取账户阈值数据集失败", e);
|
|
|
}
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
//整理指标数据
|
|
|
public JSONObject getRuleIndicator() {
|
|
|
- List<RuleIndicator> indicators = ruleIndicatorMapper.selectByMap(null);
|
|
|
- if (Check.isNull(indicators)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
JSONObject obj = new JSONObject();
|
|
|
- for (RuleIndicator indicator : indicators) {
|
|
|
- obj.put(indicator.getCode(), indicators);
|
|
|
+ try {
|
|
|
+ List<RuleIndicator> indicators = ruleIndicatorMapper.selectByMap(null);
|
|
|
+ if (Check.isNull(indicators)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (RuleIndicator indicator : indicators) {
|
|
|
+ obj.put(indicator.getCode(), indicator);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取指标数据集异常", e);
|
|
|
}
|
|
|
return obj;
|
|
|
}
|
|
@@ -320,20 +353,25 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
private Map<Long, List<RuleGroup>> getRuleGroupMap() {
|
|
|
- Map<Long, List<RuleGroup>> map = new HashMap<>();
|
|
|
- List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
|
|
|
- ruleTemplates.forEach(tem -> {
|
|
|
- QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
|
|
|
- groupWrapper.eq("template_id", tem.getId());
|
|
|
- List<RuleGroup> ruleGroups = ruleGroupMapper.selectList(groupWrapper);
|
|
|
- for (RuleGroup group : ruleGroups) {
|
|
|
- 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);
|
|
|
- });
|
|
|
+ try {
|
|
|
+ Map<Long, List<RuleGroup>> map = new HashMap<>();
|
|
|
+ List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
|
|
|
+ ruleTemplates.forEach(tem -> {
|
|
|
+ QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
|
|
|
+ groupWrapper.eq("template_id", tem.getId());
|
|
|
+ List<RuleGroup> ruleGroups = ruleGroupMapper.selectList(groupWrapper);
|
|
|
+ for (RuleGroup group : ruleGroups) {
|
|
|
+ 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);
|
|
|
+ });
|
|
|
+ return map;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取模板规则组集合异常", e);
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -347,7 +385,12 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
private JSONObject getRuleData(Long accountId) {
|
|
|
- return ruleDataAccountService.getRuleDataByAccountId(accountId);
|
|
|
+ try {
|
|
|
+ return ruleDataAccountService.getRuleDataByAccountId(accountId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取匹配数据集异常", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|