|
@@ -9,10 +9,14 @@ import com.citu.module.menduner.reward.convert.PointRuleConfigConvert;
|
|
|
import com.citu.module.menduner.reward.core.EasyRulesEngine;
|
|
|
import com.citu.module.menduner.reward.core.PointRule;
|
|
|
import com.citu.module.menduner.reward.core.RuleMatch;
|
|
|
+import com.citu.module.menduner.reward.dal.dataobject.config.Condition;
|
|
|
import com.citu.module.menduner.reward.dal.dataobject.config.PointRuleConfigDO;
|
|
|
+import com.citu.module.menduner.reward.enums.MathOperationEnum;
|
|
|
+import com.citu.module.menduner.reward.enums.record.PointBizTypeEnum;
|
|
|
import com.citu.module.menduner.reward.rule.DynamicPointRule;
|
|
|
import com.citu.module.menduner.reward.rule.DynamicRuleAction;
|
|
|
import com.citu.module.menduner.reward.service.config.PointRuleConfigService;
|
|
|
+import com.citu.module.menduner.reward.service.record.PointRecordService;
|
|
|
import com.citu.module.menduner.system.api.url.MendunerSystemUrlApi;
|
|
|
import com.citu.module.menduner.system.api.url.UrlInfoRespVO;
|
|
|
import org.jeasy.rules.api.Facts;
|
|
@@ -44,6 +48,9 @@ public class EventTrackServiceImpl implements EventTrackService {
|
|
|
@Resource
|
|
|
private EventRecordService eventRecordService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private PointRecordService pointRecordService;
|
|
|
+
|
|
|
@Resource
|
|
|
private MendunerSystemUrlApi mendunerSystemUrlApi;
|
|
|
|
|
@@ -86,89 +93,131 @@ public class EventTrackServiceImpl implements EventTrackService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public EventTrackPointRespVO click(Long id) {
|
|
|
+ public List<TreeRespVO<List<TreeRespVO<List<UrlInfoRespVO>>>>> getUrlList() {
|
|
|
+ List<UrlInfoRespVO> list = mendunerSystemUrlApi.list().getCheckedData();
|
|
|
+ return convertUrlInfoListToTree(list);
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public EventTrackPointRespVO click(String url) {
|
|
|
+ // 初始化响应对象
|
|
|
EventTrackPointRespVO respVO = new EventTrackPointRespVO();
|
|
|
|
|
|
- // 用户信息
|
|
|
+ // 获取当前登录用户信息
|
|
|
LoginUser loginUser = LoginUserContext.get2();
|
|
|
if (null == loginUser) {
|
|
|
+ // 如果用户未登录,返回空
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- PointRuleConfigDO config = ruleConfigService.get(id);
|
|
|
+ // 根据URL获取积分规则配置信息
|
|
|
+ PointRuleConfigDO config = ruleConfigService.getByUrl(url);
|
|
|
if (null == config) {
|
|
|
- // 不存在
|
|
|
+ // 如果配置信息不存在,返回空
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- // 解析出当前规则的参数
|
|
|
- List<String> triggerKeyList = EasyRulesEngine.extractUniqueKeys(config.getTriggerRule());
|
|
|
+ // 获取触发参数映射
|
|
|
+ Map<String, Object> triggerMap =
|
|
|
+ getRuleContextMap(loginUser.getId(), config.getUrl(), config.getTriggerRule());
|
|
|
|
|
|
- // 读取对应数据
|
|
|
- // 赋值到 context
|
|
|
- Map<String, Object> triggerMap = new LinkedHashMap<>();
|
|
|
- for (String key : triggerKeyList) {
|
|
|
- if (triggerMap.containsKey(key)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- triggerMap.put(key, eventRecordService.getEventCount(
|
|
|
+ // 创建积分规则对象
|
|
|
+ PointRule pointRule = createPointRule(config, triggerMap, null);
|
|
|
+
|
|
|
+ // 设置响应对象的基本信息
|
|
|
+ respVO.setTitle(pointRule.getRuleName());
|
|
|
+ respVO.setOperation(pointRule.getOperation());
|
|
|
+ respVO.setType(config.getType());
|
|
|
+ respVO.setPoint(pointRule.getPoint());
|
|
|
+
|
|
|
+ // 执行规则匹配和响应处理
|
|
|
+ DynamicRuleAction<PointRule> action = (t) -> {
|
|
|
+ pointRecordService.createPointRecord(
|
|
|
loginUser.getId(),
|
|
|
- config.getUrl(),
|
|
|
- key)
|
|
|
- );
|
|
|
+ url,
|
|
|
+ MathOperationEnum.ADD,
|
|
|
+ t.getPoint(),
|
|
|
+ PointBizTypeEnum.EVENT,
|
|
|
+ String.valueOf(config.getId()));
|
|
|
+ respVO.match();
|
|
|
+ };
|
|
|
+
|
|
|
+ match(pointRule, action);
|
|
|
+
|
|
|
+ // 返回响应对象
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取规则参数映射
|
|
|
+ *
|
|
|
+ * @param userId 用户id
|
|
|
+ * @param condition 配置
|
|
|
+ * @param url url
|
|
|
+ * @return Map<String, Object>
|
|
|
+ **/
|
|
|
+ private Map<String, Object> getRuleContextMap(Long userId, String url, Condition condition) {
|
|
|
+ Map<String, Object> map = new LinkedHashMap<>();
|
|
|
+ List<String> keyList = EasyRulesEngine.extractUniqueKeys(condition);
|
|
|
+
|
|
|
+ for (String key : keyList) {
|
|
|
+ if (!map.containsKey(key)) {
|
|
|
+ map.put(key, eventRecordService.getEventCount(userId, url, key));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 创建积分规则对象
|
|
|
+ *
|
|
|
+ * @param config 配置
|
|
|
+ * @param triggerMap 触发键列表
|
|
|
+ * @param constraintMap 限制键列表
|
|
|
+ * @return PointRule
|
|
|
+ **/
|
|
|
+ private PointRule createPointRule(PointRuleConfigDO config,
|
|
|
+ Map<String, Object> triggerMap,
|
|
|
+ Map<String, Object> constraintMap) {
|
|
|
PointRule pointRule = new PointRule();
|
|
|
- // 基本信息
|
|
|
pointRule.setRuleId(String.valueOf(config.getId()));
|
|
|
pointRule.setRuleName(config.getUrl());
|
|
|
pointRule.setDescription(config.getTitle());
|
|
|
pointRule.setWhenExpression(RULE_WHEN);
|
|
|
pointRule.setThenExpression(RULE_THEN);
|
|
|
-
|
|
|
- // 构造参数
|
|
|
pointRule.setTriggerRule(EasyRulesEngine.buildWhenExpression(config.getTriggerRule()));
|
|
|
pointRule.setConstraintRule(EasyRulesEngine.buildWhenExpression(config.getConstraintRule()));
|
|
|
pointRule.setTriggerContext(triggerMap);
|
|
|
- pointRule.setConstraintContext(null);
|
|
|
+ pointRule.setConstraintContext(constraintMap);
|
|
|
pointRule.setOperation(config.getOperation());
|
|
|
pointRule.setPoint(config.getPoint());
|
|
|
|
|
|
+ return pointRule;
|
|
|
+ }
|
|
|
|
|
|
- // 解析器的上下文
|
|
|
+ /**
|
|
|
+ * 执行匹配
|
|
|
+ *
|
|
|
+ * @param pointRule 积分规则对象
|
|
|
+ * @return void
|
|
|
+ **/
|
|
|
+ private void match(PointRule pointRule,
|
|
|
+ DynamicRuleAction<PointRule> action) {
|
|
|
+ // 创建规则解析器的上下文
|
|
|
Facts context = new Facts();
|
|
|
context.put(RULE, new DynamicPointRule(pointRule));
|
|
|
context.put(RULE_CONFIG, pointRule);
|
|
|
|
|
|
-
|
|
|
- respVO.setTitle(pointRule.getRuleName());
|
|
|
- respVO.setOperation(pointRule.getOperation());
|
|
|
- respVO.setType(config.getType());
|
|
|
- respVO.setPoint(pointRule.getPoint());
|
|
|
-
|
|
|
- DynamicRuleAction<PointRule> action = (t) -> {
|
|
|
- // 匹配成功后的逻辑
|
|
|
- System.out.println("加分:" + t.getPoint());
|
|
|
- respVO.match();
|
|
|
- };
|
|
|
context.put(RULE_ACTION, action);
|
|
|
|
|
|
-
|
|
|
- // 调用
|
|
|
+ // 调用规则引擎进行匹配
|
|
|
EasyRulesEngine.match(RuleMatch.builder()
|
|
|
.facts(context)
|
|
|
.ruleBase(pointRule)
|
|
|
.build());
|
|
|
-
|
|
|
- return respVO;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<TreeRespVO<List<TreeRespVO<List<UrlInfoRespVO>>>>> getUrlList() {
|
|
|
- List<UrlInfoRespVO> list = mendunerSystemUrlApi.list().getCheckedData();
|
|
|
- return convertUrlInfoListToTree(list);
|
|
|
}
|
|
|
|
|
|
}
|