|
|
@@ -1,8 +1,10 @@
|
|
|
package com.etotem.cfc.service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.entity.DimensionDailyLimit;
|
|
|
import com.etotem.cfc.entity.EnergyDimension;
|
|
|
import com.etotem.cfc.entity.EnergyRule;
|
|
|
+import com.etotem.cfc.mapper.DimensionDailyLimitMapper;
|
|
|
import com.etotem.cfc.mapper.EnergyDimensionMapper;
|
|
|
import com.etotem.cfc.mapper.EnergyRuleMapper;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
@@ -11,8 +13,11 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -23,8 +28,13 @@ public class EnergyRuleEngine {
|
|
|
private EnergyDimensionMapper energyDimensionMapper;
|
|
|
@Resource
|
|
|
private EnergyService energyService;
|
|
|
+ @Resource
|
|
|
+ private DimensionDailyLimitMapper dimensionDailyLimitMapper;
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
+ /** 写路径幂等(SC-P-87 边界):同一 (memberId, sourceType, sourceId) 只写一次 */
|
|
|
+ private final Set<String> processedSources = ConcurrentHashMap.newKeySet();
|
|
|
+
|
|
|
public List<EnergyRule> findActiveRules(String eventType) {
|
|
|
return energyRuleMapper.selectList(
|
|
|
new LambdaQueryWrapper<EnergyRule>()
|
|
|
@@ -34,35 +44,89 @@ public class EnergyRuleEngine {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- public void evaluate(String eventType, Long sourceId, Long memberId, Map<String, Object> context) {
|
|
|
+ /**
|
|
|
+ * 实时触发入口(能力键 energy.ruleEngine.apply):事件 → 规则匹配 → 归因维度+数值 → 写 EnergyLog/EnergyBalance。
|
|
|
+ *
|
|
|
+ * @return 可观测结果:awarded(维度→amount)、capped/cappedDimension/message(日上限触顶)、
|
|
|
+ * rejected/rejectReason/rejectedDimension(扣减拒绝)、skipped(重复触发)
|
|
|
+ */
|
|
|
+ public Map<String, Object> apply(String eventType, Long sourceId, Long memberId, Map<String, Object> context) {
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("sourceType", eventType);
|
|
|
+ result.put("sourceId", sourceId);
|
|
|
+ result.put("memberId", memberId);
|
|
|
+ Map<String, Integer> awarded = new LinkedHashMap<>();
|
|
|
+ result.put("awarded", awarded);
|
|
|
+
|
|
|
List<EnergyRule> rules = findActiveRules(eventType);
|
|
|
- if (rules.isEmpty()) return;
|
|
|
+ if (rules == null || rules.isEmpty()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ String dedupKey = buildDedupKey(memberId, eventType, sourceId);
|
|
|
+ if (processedSources.contains(dedupKey)) {
|
|
|
+ result.put("skipped", "duplicate");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean wrote = false;
|
|
|
+
|
|
|
for (EnergyRule rule : rules) {
|
|
|
- try {
|
|
|
- if (!matchesConditions(rule, context)) continue;
|
|
|
- int amount = calculateAmount(rule, context);
|
|
|
- if (amount == 0) continue;
|
|
|
- Long dimId = resolveDimensionId(rule);
|
|
|
- if ("increase".equals(rule.getAction())) {
|
|
|
- // SC-P-90:写入前经 daily limit 守卫校验(与 energy.ruleEngine.apply 同链)
|
|
|
- if (!guardDailyLimit(memberId, dimId, amount)) {
|
|
|
- log.info("每日上限已达,规则跳过发放: ruleId={}, memberId={}", rule.getId(), memberId);
|
|
|
- continue;
|
|
|
+ if (!matchesConditions(rule, context)) continue;
|
|
|
+ int amount = calculateAmount(rule, context);
|
|
|
+ Long dimId = resolveDimensionId(rule);
|
|
|
+
|
|
|
+ if ("increase".equals(rule.getAction())) {
|
|
|
+ // 日上限(配置驱动,SC-P-90/ED-4)
|
|
|
+ if (dimId != null && isDailyLimitCapped(memberId, dimId, rule.getDimensionCode(), amount)) {
|
|
|
+ result.put("capped", true);
|
|
|
+ result.put("cappedDimension", rule.getDimensionCode());
|
|
|
+ result.put("message", "已达今日上限");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, Integer> awardedMap;
|
|
|
+ if (dimId != null) {
|
|
|
+ // 规则声明归因维度 → awardEnergy 按规则维度落账(而非回落默认 action)
|
|
|
+ energyService.setRuleTargetDimension(dimId);
|
|
|
+ try {
|
|
|
+ awardedMap = energyService.awardEnergy(memberId, eventType, sourceId, amount, rule.getDescription(), null);
|
|
|
+ } finally {
|
|
|
+ energyService.clearRuleTargetDimension();
|
|
|
}
|
|
|
- energyService.awardEnergy(memberId, eventType, sourceId, amount, rule.getDescription(), null);
|
|
|
- } else if ("decrease".equals(rule.getAction())) {
|
|
|
- energyService.deductEnergy(memberId, dimId, Math.abs(amount), rule.getDescription());
|
|
|
+ } else {
|
|
|
+ awardedMap = energyService.awardEnergy(memberId, eventType, sourceId, amount, rule.getDescription(), null);
|
|
|
+ }
|
|
|
+ if (awardedMap != null && !awardedMap.isEmpty()) {
|
|
|
+ awarded.putAll(awardedMap);
|
|
|
+ wrote = true;
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("规则执行失败: ruleId={}, error={}", rule.getId(), e.getMessage());
|
|
|
+ } else if ("decrease".equals(rule.getAction())) {
|
|
|
+ if (dimId == null) {
|
|
|
+ throw new IllegalStateException("规则归因维度解析失败(配置异常): ruleId=" + rule.getId());
|
|
|
+ }
|
|
|
+ int balance = energyService.deductEnergy(memberId, dimId, Math.abs(amount), rule.getDescription());
|
|
|
+ if (balance < 0) {
|
|
|
+ throw new IllegalStateException("能量不足,扣减被拒绝: ruleId=" + rule.getId());
|
|
|
+ }
|
|
|
+ wrote = true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (wrote) {
|
|
|
+ processedSources.add(dedupKey);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 兼容旧调用:委托 apply,忽略返回值(保持 void 签名供存量调用方) */
|
|
|
+ public void evaluate(String eventType, Long sourceId, Long memberId, Map<String, Object> context) {
|
|
|
+ apply(eventType, sourceId, memberId, context);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 每日上限守卫钩子(SC-P-90:ruleEngine 写入前经守卫校验,S-cfc-304 同链)。
|
|
|
*
|
|
|
- * <p>委托 {@link EnergyService#isDailyLimitExceeded}(公共守卫入口)判断该成员该维度当日
|
|
|
+ * <p>委托 {@link EnergyService#isDailyLimitExceeded}(服务侧公共守卫入口)判断该成员该维度当日
|
|
|
* 累计是否已达上限;返回 true=允许发放,false=超限应跳过。</p>
|
|
|
*
|
|
|
* @param memberId 家庭成员ID
|
|
|
@@ -94,8 +158,10 @@ public class EnergyRuleEngine {
|
|
|
}
|
|
|
|
|
|
private int calculateAmount(EnergyRule rule, Map<String, Object> context) {
|
|
|
- if (rule.getAmount() != null && rule.getAmount() != 0) return rule.getAmount();
|
|
|
- return 0;
|
|
|
+ if (rule.getAmount() == null || rule.getAmount() <= 0) {
|
|
|
+ throw new IllegalArgumentException("规则金额缺失或非正数(配置异常): ruleId=" + rule.getId());
|
|
|
+ }
|
|
|
+ return rule.getAmount();
|
|
|
}
|
|
|
|
|
|
private Long resolveDimensionId(EnergyRule rule) {
|
|
|
@@ -105,6 +171,38 @@ public class EnergyRuleEngine {
|
|
|
return dim != null ? dim.getId() : null;
|
|
|
}
|
|
|
|
|
|
+ /** 幂等键:memberId:sourceType:sourceId */
|
|
|
+ private String buildDedupKey(Long memberId, String sourceType, Long sourceId) {
|
|
|
+ return memberId + ":" + sourceType + ":" + sourceId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日上限是否已达(配置驱动:energy_daily_limit,S-cfc-391 写路径)。
|
|
|
+ * 若未配置则 false;上限 ≤0 视为已达(按 0 处理,SC-P-90-E-4)。
|
|
|
+ */
|
|
|
+ private boolean isDailyLimitCapped(Long memberId, Long dimId, String dimensionCode, int amount) {
|
|
|
+ if (dimensionDailyLimitMapper == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (dimensionCode == null || dimensionCode.isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ DimensionDailyLimit config = dimensionDailyLimitMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<DimensionDailyLimit>()
|
|
|
+ .eq(DimensionDailyLimit::getDimensionCode, dimensionCode)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (config == null || config.getDailyLimit() == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int limit = config.getDailyLimit();
|
|
|
+ if (limit <= 0) {
|
|
|
+ // 上限 0/负 → 按 0 处理,不再累加(SC-P-90-E-4)
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ int todayEarned = energyService.getTodayDimensionEarned(memberId, dimId);
|
|
|
+ return todayEarned + amount > limit;
|
|
|
+ }
|
|
|
+
|
|
|
public List<EnergyDimension> getAllDimensions() {
|
|
|
return energyDimensionMapper.selectList(
|
|
|
new LambdaQueryWrapper<EnergyDimension>().eq(EnergyDimension::getStatus, 1).orderByAsc(EnergyDimension::getSortOrder));
|