|
@@ -89,6 +89,12 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
/** 幂等键 → 讲师发章载荷指纹(grantLecturerBadge 入口的 replay/key/payload 幂等语义) */
|
|
/** 幂等键 → 讲师发章载荷指纹(grantLecturerBadge 入口的 replay/key/payload 幂等语义) */
|
|
|
private final Map<String, String> lecturerGrantFingerprints = new ConcurrentHashMap<>();
|
|
private final Map<String, String> lecturerGrantFingerprints = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
+ /** ruleConfig 幂等键 → 保存载荷指纹(同键异载荷 C4092;进程内缓存,重启后以规则档案为准) */
|
|
|
|
|
+ private final Map<String, String> ruleConfigFingerprints = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ /** ruleConfig 幂等键 → 首次落库规则ID(同键同载荷重放原样回传,避免调用方拿到空 ruleId) */
|
|
|
|
|
+ private final Map<String, Long> ruleConfigRuleIds = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
|
@Resource
|
|
@Resource
|
|
@@ -346,7 +352,7 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
public Result<Map<String, Object>> saveRule(Boolean isAdmin, Map<String, Object> body) {
|
|
public Result<Map<String, Object>> saveRule(Boolean isAdmin, Map<String, Object> body) {
|
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
if (noAuth != null) {
|
|
if (noAuth != null) {
|
|
|
- return Result.error(noAuth);
|
|
|
|
|
|
|
+ return Result.error(CODE_FORBIDDEN, noAuth);
|
|
|
}
|
|
}
|
|
|
if (body == null) {
|
|
if (body == null) {
|
|
|
return Result.error("参数不完整");
|
|
return Result.error("参数不完整");
|
|
@@ -363,6 +369,26 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
if (conds == null || conds.isEmpty()) {
|
|
if (conds == null || conds.isEmpty()) {
|
|
|
return Result.error("达标条件不能为空");
|
|
return Result.error("达标条件不能为空");
|
|
|
}
|
|
}
|
|
|
|
|
+ Object en = body.get("enabled");
|
|
|
|
|
+ int enabledFlag = (en == null) ? 1 : (parseBool(en) ? 1 : 0); // 缺省启用
|
|
|
|
|
+ // train.promo.ruleConfig 幂等:ruleKey 为幂等键。同键同载荷重放不二次落库并在 data 中显式回传
|
|
|
|
|
+ // replay=true;同键异载荷(阈值/条件变化)返回 C4092,禁止静默新建另一条规则。
|
|
|
|
|
+ String ruleKey = str(body.get("ruleKey"));
|
|
|
|
|
+ String fingerprint = ruleConfigFingerprint(ruleKey, name, authLevel, enabledFlag, conds);
|
|
|
|
|
+ if (ruleKey != null && !ruleKey.isEmpty()) {
|
|
|
|
|
+ String previous = ruleConfigFingerprints.get(ruleKey);
|
|
|
|
|
+ if (previous != null) {
|
|
|
|
|
+ if (!previous.equals(fingerprint)) {
|
|
|
|
|
+ return Result.error(CODE_SAME_KEY_DIFF_PAYLOAD,
|
|
|
|
|
+ "同一规则幂等键 " + ruleKey + " 携带不同配置载荷(阈值/条件),禁止静默新建规则");
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> replayData = new HashMap<>();
|
|
|
|
|
+ replayData.put("ruleId", ruleConfigRuleIds.get(ruleKey));
|
|
|
|
|
+ replayData.put("authLevel", authLevel);
|
|
|
|
|
+ replayData.put("replay", Boolean.TRUE);
|
|
|
|
|
+ return Result.success(replayData);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
TrainPromotionRule rule = new TrainPromotionRule();
|
|
TrainPromotionRule rule = new TrainPromotionRule();
|
|
|
rule.setName(name);
|
|
rule.setName(name);
|
|
|
rule.setAuthLevel(authLevel);
|
|
rule.setAuthLevel(authLevel);
|
|
@@ -374,15 +400,21 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
rule.setOperator(str(only.get("op")));
|
|
rule.setOperator(str(only.get("op")));
|
|
|
rule.setValue(toLong(only.get("value")) == null ? null : toLong(only.get("value")).intValue());
|
|
rule.setValue(toLong(only.get("value")) == null ? null : toLong(only.get("value")).intValue());
|
|
|
}
|
|
}
|
|
|
- Object en = body.get("enabled");
|
|
|
|
|
- int enabledFlag = (en == null) ? 1 : (parseBool(en) ? 1 : 0); // 缺省启用
|
|
|
|
|
rule.setEnabled(enabledFlag);
|
|
rule.setEnabled(enabledFlag);
|
|
|
rule.setVersion(1);
|
|
rule.setVersion(1);
|
|
|
rule.setDeleted(0);
|
|
rule.setDeleted(0);
|
|
|
trainPromotionRuleMapper.insert(rule);
|
|
trainPromotionRuleMapper.insert(rule);
|
|
|
|
|
+ if (ruleKey != null && !ruleKey.isEmpty()) {
|
|
|
|
|
+ ruleConfigFingerprints.putIfAbsent(ruleKey, fingerprint);
|
|
|
|
|
+ // 主键由数据库生成;Mock/未回填场景允许为 null(HashMap 可容纳,重放时原样回传)
|
|
|
|
|
+ if (rule.getId() != null) {
|
|
|
|
|
+ ruleConfigRuleIds.putIfAbsent(ruleKey, rule.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("ruleId", rule.getId());
|
|
data.put("ruleId", rule.getId());
|
|
|
data.put("authLevel", authLevel);
|
|
data.put("authLevel", authLevel);
|
|
|
|
|
+ data.put("replay", Boolean.FALSE);
|
|
|
return Result.success(data);
|
|
return Result.success(data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -417,18 +449,22 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
public Result<Map<String, Object>> ruleEnable(Boolean isAdmin, Long ruleId, Boolean enabled) {
|
|
public Result<Map<String, Object>> ruleEnable(Boolean isAdmin, Long ruleId, Boolean enabled) {
|
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
if (noAuth != null) {
|
|
if (noAuth != null) {
|
|
|
- return Result.error(noAuth);
|
|
|
|
|
|
|
+ return Result.error(CODE_FORBIDDEN, noAuth);
|
|
|
}
|
|
}
|
|
|
if (ruleId == null || enabled == null) {
|
|
if (ruleId == null || enabled == null) {
|
|
|
return Result.error("参数不完整");
|
|
return Result.error("参数不完整");
|
|
|
}
|
|
}
|
|
|
TrainPromotionRule rule = trainPromotionRuleMapper.selectById(ruleId);
|
|
TrainPromotionRule rule = trainPromotionRuleMapper.selectById(ruleId);
|
|
|
- if (rule == null) {
|
|
|
|
|
- return Result.error("规则不存在");
|
|
|
|
|
|
|
+ if (rule == null || (rule.getDeleted() != null && rule.getDeleted() == 1)) {
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "规则不存在");
|
|
|
}
|
|
}
|
|
|
rule.setEnabled(Boolean.TRUE.equals(enabled) ? 1 : 0);
|
|
rule.setEnabled(Boolean.TRUE.equals(enabled) ? 1 : 0);
|
|
|
rule.setUpdatedAt(new Date());
|
|
rule.setUpdatedAt(new Date());
|
|
|
- trainPromotionRuleMapper.updateById(rule);
|
|
|
|
|
|
|
+ int rows = trainPromotionRuleMapper.updateById(rule);
|
|
|
|
|
+ if (rows <= 0) {
|
|
|
|
|
+ // 并发丢失更新 / 版本不匹配:禁止静默按 200 成功
|
|
|
|
|
+ return Result.error(CODE_VERSION_CONFLICT, "并发冲突:规则已被其他操作更新");
|
|
|
|
|
+ }
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("ruleId", ruleId);
|
|
data.put("ruleId", ruleId);
|
|
|
data.put("enabled", rule.getEnabled());
|
|
data.put("enabled", rule.getEnabled());
|
|
@@ -757,6 +793,23 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
return str(rule.getId()) + "|" + str(rule.getAuthLevel()) + "|" + str(rule.getConditions());
|
|
return str(rule.getId()) + "|" + str(rule.getAuthLevel()) + "|" + str(rule.getConditions());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ruleConfig 保存载荷指纹:ruleKey 之外的配置内容(名称/等级/启用/达标条件)按固定顺序拼接。
|
|
|
|
|
+ * replay 标志不参与比对——重放只是调用方式,不改变规则配置本身。
|
|
|
|
|
+ */
|
|
|
|
|
+ private String ruleConfigFingerprint(String ruleKey, String name, String authLevel,
|
|
|
|
|
+ int enabledFlag, List<Map<String, Object>> conds) {
|
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
|
+ sb.append(str(ruleKey)).append('|').append(str(name)).append('|')
|
|
|
|
|
+ .append(str(authLevel)).append('|').append(enabledFlag).append('|');
|
|
|
|
|
+ for (Map<String, Object> c : conds) {
|
|
|
|
|
+ sb.append(str(c.get("metric"))).append(':')
|
|
|
|
|
+ .append(str(c.get("op"))).append(':')
|
|
|
|
|
+ .append(str(c.get("value"))).append(';');
|
|
|
|
|
+ }
|
|
|
|
|
+ return sb.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private String normLevel(String lv) {
|
|
private String normLevel(String lv) {
|
|
|
if (lv == null || lv.trim().isEmpty()) {
|
|
if (lv == null || lv.trim().isEmpty()) {
|
|
|
return LV_NONE;
|
|
return LV_NONE;
|