|
@@ -5,17 +5,22 @@ import cn.com.ctop.alarm.modules.entity.*;
|
|
|
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;
|
|
|
import cn.com.ctop.common.module.utils.Check;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xxl.job.core.enums.NoEn;
|
|
|
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;
|
|
@@ -44,11 +49,20 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
@Autowired
|
|
|
private RuleTemplateMapper ruleTemplateMapper;
|
|
|
@Autowired
|
|
|
+ private SysUserMapper sysUserMapper;
|
|
|
+ @Autowired
|
|
|
private IRuleGroupService ruleGroupService;
|
|
|
@Autowired
|
|
|
private IRuleAccountTemplateService ruleAccountTemplateService;
|
|
|
@Autowired
|
|
|
private ISendMessageService sendMessageService;
|
|
|
+ @Autowired
|
|
|
+ private IKuaiShouUpdateService kuaiShouUpdateService;
|
|
|
+ @Autowired
|
|
|
+ private ICtopOauthTokenService oauthTokenService;
|
|
|
+ @Autowired
|
|
|
+ private IRuleDataAccountService ruleDataAccountService;
|
|
|
+ private JSONObject userObj = null;
|
|
|
|
|
|
@Override
|
|
|
public void checkRules() {
|
|
@@ -58,12 +72,6 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
log.warn("查询账户绑定过的规则模板失败");
|
|
|
return;
|
|
|
}
|
|
|
- //查询匹配数据
|
|
|
- List<JSONObject> dataList = getData();
|
|
|
- if (Check.isNull(dataList)) {
|
|
|
- log.warn("查询匹配数据失败");
|
|
|
- return;
|
|
|
- }
|
|
|
//获取规则集
|
|
|
Map<Long, List<RuleGroup>> ruleGroupMap = getRuleGroupMap();
|
|
|
if (Check.isNull(ruleGroupMap)) {
|
|
@@ -77,12 +85,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
return;
|
|
|
}
|
|
|
for (RuleAccountTemplate templates : ruleAccountTemplates) {
|
|
|
- for (JSONObject matchData : dataList) {
|
|
|
- if (templates.getAccountId() == matchData.getLong("accountId")) {
|
|
|
- List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
|
|
|
- matchAlarmRules(ruleGroups, getThreshold(ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId())), matchData, indicators);
|
|
|
- }
|
|
|
- }
|
|
|
+ List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
|
|
|
+ matchAlarmRules(ruleGroups, getThreshold(ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId())), indicators, templates.getAccountId());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -91,60 +95,194 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
*
|
|
|
* @param ruleGroups 规则集
|
|
|
* @param thresholdObj 规则id阈值
|
|
|
- * @param matchData 匹配数据
|
|
|
* @param indicators 指标码
|
|
|
* @return void
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private void matchAlarmRules(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject matchData, JSONObject indicators) {
|
|
|
+ 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) {
|
|
|
- boolean isAllTrue = "and".equals(ruleGroup.getRuleRelationship());
|
|
|
- boolean flag = true;
|
|
|
List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
|
|
|
- for (RuleBase ruleBase : ruleBaseList) {
|
|
|
- //指标阈值
|
|
|
- String threshold = thresholdObj.getString(ruleBase.getId().toString());
|
|
|
- String value = matchData.getString(ruleBase.getIndicatorCode());
|
|
|
+ if (Check.isNull(ruleBaseList)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean isBase = "base".equals(ruleGroup.getRuleType());
|
|
|
+ //匹配单规则
|
|
|
+ if (isBase) {
|
|
|
+ RuleBase ruleBase = ruleBaseList.get(0);
|
|
|
//指标对象
|
|
|
JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
|
|
|
- //and关系,全部匹配规则
|
|
|
- if (isAllTrue) {
|
|
|
- if (flag) {
|
|
|
- flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
|
|
|
+ //指标阈值
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //匹配组合规则
|
|
|
+ } 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;
|
|
|
}
|
|
|
- } else {
|
|
|
- //or关系,任一阈值匹配,则跳出循环
|
|
|
- flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
|
|
|
- if (flag) {
|
|
|
- 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 {
|
|
|
+ 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));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //执行发送
|
|
|
- if (flag) {
|
|
|
- boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
|
|
|
- if (isPause) {
|
|
|
- //关停操作 TODO
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * @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);
|
|
|
}
|
|
|
- //TODO
|
|
|
- String msg = MatchLogic.getMsg(null, null, null, null);
|
|
|
- String sendType = ruleGroup.getSendType();
|
|
|
- if ("SMS".equals(sendType)) {
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
- } else if ("WeChat".equals(sendType)) {
|
|
|
- sendMessageService.sendMessage("", msg);
|
|
|
- } else if ("EMAIL".equals(sendType)) {
|
|
|
+ /**
|
|
|
+ * 发送消息
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ 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) {
|
|
|
+ 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)) {
|
|
|
|
|
|
- } else if ("TEL".equals(sendType)) {
|
|
|
+ } else if ("EMAIL".equals(sendType)) {
|
|
|
|
|
|
- } else {
|
|
|
+ } 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关停操作
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return boolean
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ 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 (!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");
|
|
|
}
|
|
|
|
|
|
//整理阈值数据
|
|
@@ -185,51 +323,31 @@ 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 = strToList(tem.getGroupIds());
|
|
|
- if (!Check.isNull(groupIds)) {
|
|
|
- List<RuleGroup> ruleGroups = ruleGroupMapper.selectBatchIds(groupIds);
|
|
|
- for (RuleGroup group : ruleGroups) {
|
|
|
- List<String> ruleIds = 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;
|
|
|
}
|
|
|
|
|
|
- //拆分逗号拼接数据
|
|
|
- private List<String> strToList(String strings) {
|
|
|
- List<String> ids = null;
|
|
|
- try {
|
|
|
- if (Check.isNull(strings)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- JSONObject job = new JSONObject();
|
|
|
- job.put("list", strings);
|
|
|
- ids = new ArrayList<>();
|
|
|
- JSONArray idList = job.getJSONArray("list");
|
|
|
- for (Object id : idList) {
|
|
|
- ids.add(String.valueOf(id));
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("ids格式转换异常", e);
|
|
|
- }
|
|
|
- return ids;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
- * TODO 获取指标数据
|
|
|
+ * 获取指标数据
|
|
|
*
|
|
|
* @param
|
|
|
* @return java.util.List<org.json.JSONObject>
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private List<JSONObject> getData() {
|
|
|
- return null;
|
|
|
+ private JSONObject getRuleData(Long accountId) {
|
|
|
+ return ruleDataAccountService.getRuleDataByAccountId(accountId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -277,6 +395,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
ruleBase.setThreshold(ruleDetailJson.getString("threshold"));
|
|
|
ruleBase.setVariableType(ruleDetailJson.getInteger("variableType"));
|
|
|
ruleBase.setRuleDimension(ruleDetailJson.getString("ruleDimension"));
|
|
|
+ ruleBase.setJudgeFormat(ruleDetailJson.getInteger("judgeFormat"));
|
|
|
+ ruleBase.setIsUnlimited(ruleDetailJson.getInteger("isUnlimited"));
|
|
|
int insert = ruleBaseMapper.insert(ruleBase); // 添加基础规则
|
|
|
if (insert > 0) {
|
|
|
if (!Check.isNull(accountList)) {
|
|
@@ -306,6 +426,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
|
|
|
ruleGroup.setRuleRelationship(groupJson.getString("ruleRelationship"));
|
|
|
ruleGroup.setGroupName(groupJson.getString("groupName"));
|
|
|
ruleGroup.setRemark(groupJson.getString("remark"));
|
|
|
+ ruleGroup.setIsCopy(groupJson.getInteger("isCopy"));
|
|
|
+ ruleGroup.setIsRequired(groupJson.getInteger("isRequired"));
|
|
|
boolean save = ruleGroupService.save(ruleGroup); // 新增规则组
|
|
|
if (save) {
|
|
|
|