|
@@ -95,6 +95,12 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
/** ruleConfig 幂等键 → 首次落库规则ID(同键同载荷重放原样回传,避免调用方拿到空 ruleId) */
|
|
/** ruleConfig 幂等键 → 首次落库规则ID(同键同载荷重放原样回传,避免调用方拿到空 ruleId) */
|
|
|
private final Map<String, Long> ruleConfigRuleIds = new ConcurrentHashMap<>();
|
|
private final Map<String, Long> ruleConfigRuleIds = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
+ /** 幂等键 → 自动评定载荷指纹(autoEval 入口的 replay/key/payload 幂等语义) */
|
|
|
|
|
+ private final Map<String, String> autoEvalFingerprints = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ /** 幂等键 → 首次评定结果(replay=true 同键同载荷直接复用,不重复落记录) */
|
|
|
|
|
+ private final Map<String, Result<Map<String, Object>>> autoEvalResults = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
|
@Resource
|
|
@Resource
|
|
@@ -118,6 +124,56 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private PromotionService self;
|
|
private PromotionService self;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 规则自动评定幂等重放入口(train.promo.autoEval)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>同键异载荷(规则/指标阈值/操作人变化)返回 C4092;replay=true 且同键同载荷时复用首次评定结果,
|
|
|
|
|
+ * 不重复落记录。评定本体交给 {@link #evaluateAndRecord(Long, Long)}(@Lazy 自注入代理,单事务),
|
|
|
|
|
+ * 未达标缺口 unmet 由本体原样透出,调用方可查。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Result<Map<String, Object>> autoEval(Long userId, Long ruleId, Boolean replay,
|
|
|
|
|
+ String key, Map<String, Object> payload) {
|
|
|
|
|
+ if (userId == null || ruleId == null) {
|
|
|
|
|
+ return Result.error("参数不完整");
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> safePayload = payload == null ? new HashMap<String, Object>() : payload;
|
|
|
|
|
+ String fingerprint = writeJson(safePayload);
|
|
|
|
|
+ if (fingerprint == null) {
|
|
|
|
|
+ fingerprint = "";
|
|
|
|
|
+ }
|
|
|
|
|
+ String fingerprintKey = (key == null || key.trim().isEmpty())
|
|
|
|
|
+ ? ("AUTOEVAL|" + userId + "|" + ruleId) : key;
|
|
|
|
|
+ String previous = autoEvalFingerprints.putIfAbsent(fingerprintKey, fingerprint);
|
|
|
|
|
+ if (previous != null && !previous.equals(fingerprint)) {
|
|
|
|
|
+ return Result.error(CODE_SAME_KEY_DIFF_PAYLOAD,
|
|
|
|
|
+ "同一自动评定幂等键 " + fingerprintKey + " 携带不同载荷(规则/指标阈值/操作人变化),禁止静默复用既有评定结果");
|
|
|
|
|
+ }
|
|
|
|
|
+ Result<Map<String, Object>> evaluated;
|
|
|
|
|
+ if (Boolean.TRUE.equals(replay) && previous != null) {
|
|
|
|
|
+ evaluated = autoEvalResults.get(fingerprintKey);
|
|
|
|
|
+ if (evaluated == null) {
|
|
|
|
|
+ return Result.error("幂等重放的评定结果不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ evaluated = self.evaluateAndRecord(userId, ruleId);
|
|
|
|
|
+ if (evaluated == null) {
|
|
|
|
|
+ return Result.error("自动评定无结果");
|
|
|
|
|
+ }
|
|
|
|
|
+ autoEvalResults.putIfAbsent(fingerprintKey, evaluated);
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> data = evaluated.getData() == null
|
|
|
|
|
+ ? new HashMap<String, Object>() : new HashMap<String, Object>(evaluated.getData());
|
|
|
|
|
+ data.put("key", fingerprintKey);
|
|
|
|
|
+ data.put("replay", Boolean.TRUE.equals(replay));
|
|
|
|
|
+ data.put("payload", safePayload);
|
|
|
|
|
+ Result<Map<String, Object>> out = new Result<Map<String, Object>>();
|
|
|
|
|
+ out.setCode(evaluated.getCode());
|
|
|
|
|
+ out.setMessage(evaluated.getMessage());
|
|
|
|
|
+ out.setData(data);
|
|
|
|
|
+ return out;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Map<String, Object>> apply(Long userId, String authLevel) {
|
|
public Result<Map<String, Object>> apply(Long userId, String authLevel) {
|
|
|
if (userId == null || authLevel == null || authLevel.trim().isEmpty()) {
|
|
if (userId == null || authLevel == null || authLevel.trim().isEmpty()) {
|