|
@@ -6,6 +6,7 @@ import cn.com.ctop.alarm.modules.mapper.*;
|
|
|
import cn.com.ctop.alarm.modules.service.IRuleAccountTemplateService;
|
|
|
import cn.com.ctop.alarm.modules.service.IRuleGroupService;
|
|
|
import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
+import cn.com.ctop.common.module.mapper.SysUserMapper;
|
|
|
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
|
|
|
import cn.com.ctop.common.module.service.IRuleDataAccountService;
|
|
|
import cn.com.ctop.common.module.service.ISendMessageService;
|
|
@@ -20,7 +21,6 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -49,6 +49,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
@Autowired
|
|
|
private RuleTemplateMapper ruleTemplateMapper;
|
|
|
@Autowired
|
|
|
+ private SysUserMapper sysUserMapper;
|
|
|
+ @Autowired
|
|
|
private IRuleGroupService ruleGroupService;
|
|
|
@Autowired
|
|
|
private IRuleAccountTemplateService ruleAccountTemplateService;
|
|
@@ -60,6 +62,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
private ICtopOauthTokenService oauthTokenService;
|
|
|
@Autowired
|
|
|
private IRuleDataAccountService ruleDataAccountService;
|
|
|
+ private JSONObject userObj = null;
|
|
|
|
|
|
@Override
|
|
|
public void checkRules() {
|
|
@@ -98,14 +101,19 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
private void matchAlarmRules(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject indicators, Long accountId) {
|
|
|
+ userObj = new JSONObject();
|
|
|
//查询匹配数据
|
|
|
JSONObject matchData = getRuleData(accountId);
|
|
|
if (Check.isNull(matchData)) {
|
|
|
log.warn("查询匹配数据失败");
|
|
|
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) {
|
|
@@ -116,6 +124,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
String threshold = thresholdObj.getString(ruleBase.getId().toString());
|
|
|
//阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
|
|
|
if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
|
|
|
+ log.warn("阈值为空/不限,该规则({})不执行", ruleBase.getId());
|
|
|
continue;
|
|
|
}
|
|
|
//维度数据
|
|
@@ -125,64 +134,93 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
JSONObject obj = dimensionData.getJSONObject(i);
|
|
|
String value = obj.getString(ruleBase.getIndicatorCode());
|
|
|
if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
|
|
|
- sendMsg(ruleGroup);
|
|
|
+ sendMsg(ruleGroup, obj);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//匹配组合规则
|
|
|
} else {
|
|
|
- // 是否为and全部匹配
|
|
|
- boolean isAllTrue = "and".equals(ruleGroup.getRuleRelationship());
|
|
|
boolean flag = false;
|
|
|
- List<JSONObject> targetDatas = new ArrayList<>();
|
|
|
- List<JSONObject> planDatas = new ArrayList<>();
|
|
|
- List<JSONObject> creativeDatas = new ArrayList<>();
|
|
|
- List<JSONObject> lostCostDatas = new ArrayList<>();
|
|
|
+ //符合规则的数据集
|
|
|
+ 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;
|
|
|
}
|
|
|
- //and关系,全部匹配规则
|
|
|
- if (isAllTrue) {
|
|
|
- JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
|
|
|
- if ("account".equals(ruleBase.getRuleDimension())) {
|
|
|
- //and关系时如果账户级不匹配则跳出本规则。
|
|
|
- String value = dimensionData.getJSONObject(0).getString(ruleBase.getIndicatorCode());
|
|
|
- if (!MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
|
|
|
- break;
|
|
|
- }
|
|
|
+ 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 {
|
|
|
- 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)) {
|
|
|
-// okData.add(obj);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- //or关系,任一阈值匹配,则跳出循环
|
|
|
-// flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
|
|
|
- if (flag) {
|
|
|
+ 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);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
- //执行发送
|
|
|
- if (flag) {
|
|
|
- sendMsg(ruleGroup);
|
|
|
+ //获取达标可发送的数据
|
|
|
+ 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));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * @param ruleDatas 达标数据集
|
|
|
+ * @param dimensionData 匹配数据
|
|
|
+ * @param ruleBase 规则
|
|
|
+ * @param type 数据类型
|
|
|
+ * @param threshold 阈值
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private JSONArray getOKData(JSONArray ruleDatas, JSONArray dimensionData, RuleBase ruleBase, String type, String threshold) {
|
|
|
+ if (ruleDatas.size() > 0) {
|
|
|
+ JSONArray okData = new JSONArray();
|
|
|
+ for (int i = 0; i < ruleDatas.size(); i++) {
|
|
|
+ JSONObject targetEntity = dimensionData.getJSONObject(i);
|
|
|
+ String value = targetEntity.getString(ruleBase.getIndicatorCode());
|
|
|
+ if (MatchLogic.matchCondition(type, ruleBase.getRuleCondition(), threshold, value)) {
|
|
|
+ okData.add(targetEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return okData;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < dimensionData.size(); i++) {
|
|
|
+ JSONObject targetEntity = dimensionData.getJSONObject(i);
|
|
|
+ String value = targetEntity.getString(ruleBase.getIndicatorCode());
|
|
|
+ if (MatchLogic.matchCondition(type, ruleBase.getRuleCondition(), threshold, value)) {
|
|
|
+ ruleDatas.add(targetEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ruleDatas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 发送消息
|
|
|
*
|
|
|
* @param
|
|
@@ -190,26 +228,38 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private void sendMsg(RuleGroup ruleGroup) {
|
|
|
+ private void sendMsg(RuleGroup ruleGroup, JSONObject obj) {
|
|
|
+ Long creativeId = obj.getLong("creativeId");
|
|
|
+ Long planId = obj.getLong("planId");
|
|
|
+ Long accountId = obj.getLong("accountId");
|
|
|
+ JSONObject user = getUserByAccountId(accountId);
|
|
|
boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
|
|
|
if (isPause) {
|
|
|
- //关停操作 TODO
|
|
|
-// shutDown();
|
|
|
+ if (shutDown(accountId, planId, creativeId, user.getString("id"))) {
|
|
|
+ isPause = true;
|
|
|
+ }
|
|
|
}
|
|
|
- //TODO
|
|
|
- String msg = MatchLogic.getMsg(null, null, null, null);
|
|
|
+ String msg = MatchLogic.getMsg(ruleGroup, user, accountId, planId, creativeId, isPause);
|
|
|
String sendType = ruleGroup.getSendType();
|
|
|
if ("SMS".equals(sendType)) {
|
|
|
|
|
|
- } else if ("WeChat".equals(sendType)) {
|
|
|
- sendMessageService.sendMessage("", msg);
|
|
|
} else if ("EMAIL".equals(sendType)) {
|
|
|
|
|
|
} else if ("TEL".equals(sendType)) {
|
|
|
|
|
|
} else {
|
|
|
+ sendMessageService.sendMessage(user.getString("id"), msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ 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);
|
|
|
+ return user;
|
|
|
}
|
|
|
+ return obj;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -220,16 +270,16 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private boolean shutDown(AlarmEventRule rule, String level) {
|
|
|
- CtopOauthToken token = oauthTokenService.getTokenByAccountId(rule.getAccountId());
|
|
|
+ private boolean shutDown(Long accountId, Long planId, Long creativeId, String userId) {
|
|
|
+ CtopOauthToken token = oauthTokenService.getTokenByAccountId(accountId);
|
|
|
Map<String, Object> updateMap = new HashMap<>();
|
|
|
if (!Check.isNull(token)) {
|
|
|
- if ("CAMPAIGN".equals(level)) {
|
|
|
- updateMap = kuaiShouUpdateService.updateCampaignStatus(token.getAccessToken(), rule.getAccountId(), rule.getCampaignId(), NoEn.NO2.valueInt(), rule.getUserId());
|
|
|
- log.info("---------规则关停快手计划:{}", rule.getCampaignId());
|
|
|
- } else if ("UNIT".equals(level)) {
|
|
|
- updateMap = kuaiShouUpdateService.updateUnitStatus(token.getAccessToken(), rule.getAccountId(), rule.getUnitId(), NoEn.NO2.valueInt(), rule.getUserId());
|
|
|
- log.info("---------规则关停快手组:{}", rule.getUnitId());
|
|
|
+ if (!Check.isNull(creativeId)) {
|
|
|
+ updateMap = kuaiShouUpdateService.updateCreativeStatus(token.getAccessToken(), accountId, creativeId, NoEn.NO2.valueInt(), userId);
|
|
|
+ log.info("---------规则关停创意:{}", creativeId);
|
|
|
+ } else if (!Check.isNull(planId) && Check.isNull(creativeId)) {
|
|
|
+ updateMap = kuaiShouUpdateService.updateCampaignStatus(token.getAccessToken(), accountId, planId, NoEn.NO2.valueInt(), userId);
|
|
|
+ log.info("---------规则关停计划:{}", planId);
|
|
|
}
|
|
|
}
|
|
|
return (boolean) updateMap.get("success");
|
|
@@ -273,24 +323,23 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
Map<Long, List<RuleGroup>> map = new HashMap<>();
|
|
|
List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
|
|
|
ruleTemplates.forEach(tem -> {
|
|
|
- List<String> groupIds = MatchLogic.strToList(tem.getGroupIds());
|
|
|
- if (!Check.isNull(groupIds)) {
|
|
|
- List<RuleGroup> ruleGroups = ruleGroupMapper.selectBatchIds(groupIds);
|
|
|
- for (RuleGroup group : ruleGroups) {
|
|
|
- List<String> ruleIds = MatchLogic.strToList(group.getRuleIds());
|
|
|
- if (!Check.isNull(ruleIds)) {
|
|
|
- group.setRuleBaseList(ruleBaseMapper.selectBatchIds(ruleIds));
|
|
|
- }
|
|
|
- }
|
|
|
- map.put(tem.getId(), ruleGroups);
|
|
|
+ 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 null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * TODO 获取指标数据
|
|
|
+ * 获取指标数据
|
|
|
*
|
|
|
* @param
|
|
|
* @return java.util.List<org.json.JSONObject>
|