|
@@ -66,7 +66,8 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
private static final String O_CONFIRMED = "confirmed";
|
|
private static final String O_CONFIRMED = "confirmed";
|
|
|
private static final String BADGE_PREFIX = "lec_t";
|
|
private static final String BADGE_PREFIX = "lec_t";
|
|
|
|
|
|
|
|
- /** train.promo.history 合规举证契约错误码 */
|
|
|
|
|
|
|
+ /** train.promo.history / train.badge.lecturer 契约错误码 */
|
|
|
|
|
+ private static final int CODE_OK = 200;
|
|
|
private static final int CODE_FORBIDDEN = 403;
|
|
private static final int CODE_FORBIDDEN = 403;
|
|
|
private static final int CODE_NOT_FOUND = 404;
|
|
private static final int CODE_NOT_FOUND = 404;
|
|
|
private static final int CODE_VERSION_CONFLICT = 422;
|
|
private static final int CODE_VERSION_CONFLICT = 422;
|
|
@@ -82,6 +83,12 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
/** 幂等键 → 导出载荷指纹(同键异载荷 C4092;进程内缓存,重启后以档案为准) */
|
|
/** 幂等键 → 导出载荷指纹(同键异载荷 C4092;进程内缓存,重启后以档案为准) */
|
|
|
private final Map<String, String> historyExportFingerprints = new ConcurrentHashMap<>();
|
|
private final Map<String, String> historyExportFingerprints = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
+ /** 用户|讲师档位键 → 晋升载荷指纹(同档位键异载荷 C4092;进程内缓存,重启后以履历为准) */
|
|
|
|
|
+ private final Map<String, String> lecturerBadgeFingerprints = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ /** 幂等键 → 讲师发章载荷指纹(grantLecturerBadge 入口的 replay/key/payload 幂等语义) */
|
|
|
|
|
+ private final Map<String, String> lecturerGrantFingerprints = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
|
@Resource
|
|
@Resource
|
|
@@ -138,7 +145,17 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
}
|
|
}
|
|
|
TrainPromotionRule rule = trainPromotionRuleMapper.selectById(ruleId);
|
|
TrainPromotionRule rule = trainPromotionRuleMapper.selectById(ruleId);
|
|
|
if (rule == null || (rule.getDeleted() != null && rule.getDeleted() == 1)) {
|
|
if (rule == null || (rule.getDeleted() != null && rule.getDeleted() == 1)) {
|
|
|
- return Result.error("规则不存在");
|
|
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "晋升规则不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 档位唯一:认证等级 → lec_t1~t4 目录键;同键异载荷 C4092 必须先于「已达标」短路判定,
|
|
|
|
|
+ // 否则重放会静默按 200 复用既有档位(契约要求让调用方感知冲突)。
|
|
|
|
|
+ String badgeKey = lecturerBadgeKey(rule.getAuthLevel());
|
|
|
|
|
+ String fingerprint = lecturerFingerprint(rule);
|
|
|
|
|
+ String fingerprintKey = userId + "|" + badgeKey;
|
|
|
|
|
+ String previousFingerprint = lecturerBadgeFingerprints.get(fingerprintKey);
|
|
|
|
|
+ if (previousFingerprint != null && !previousFingerprint.equals(fingerprint)) {
|
|
|
|
|
+ return Result.error(CODE_SAME_KEY_DIFF_PAYLOAD,
|
|
|
|
|
+ "同一讲师档位键 " + badgeKey + " 携带不同晋升载荷(规则/指标阈值),禁止静默复用既有档位");
|
|
|
}
|
|
}
|
|
|
// 并发幂等:目标等级 ≤ 当前等级 → 不重复晋升(不落记录)
|
|
// 并发幂等:目标等级 ≤ 当前等级 → 不重复晋升(不落记录)
|
|
|
TrainPractitionerProfile p = selectProfileByUserId(userId);
|
|
TrainPractitionerProfile p = selectProfileByUserId(userId);
|
|
@@ -189,8 +206,11 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
pp.setUpdatedAt(now);
|
|
pp.setUpdatedAt(now);
|
|
|
trainPractitionerProfileMapper.updateById(pp);
|
|
trainPractitionerProfileMapper.updateById(pp);
|
|
|
}
|
|
}
|
|
|
- badgeService.grantBadge(userId, BADGE_PREFIX + rule.getAuthLevel(), 0L, "讲师认证晋升 " + rule.getAuthLevel());
|
|
|
|
|
|
|
+ // 留痕:首次真实晋升时登记档位→载荷指纹,后续同键异载荷重放返回 C4092
|
|
|
|
|
+ lecturerBadgeFingerprints.putIfAbsent(fingerprintKey, fingerprint);
|
|
|
|
|
+ badgeService.grantBadge(userId, badgeKey, 0L, "讲师认证晋升 " + rule.getAuthLevel());
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
|
|
+ data.put("badgeKey", badgeKey);
|
|
|
data.put("eligible", true);
|
|
data.put("eligible", true);
|
|
|
data.put("promoted", true);
|
|
data.put("promoted", true);
|
|
|
data.put("authLevel", rule.getAuthLevel());
|
|
data.put("authLevel", rule.getAuthLevel());
|
|
@@ -213,6 +233,58 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 讲师徽章授予(train.badge.lecturer 幂等重放入口)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>replay=true 表示幂等重放;key 为幂等键;payload 为规则/指标阈值/操作人等载荷。
|
|
|
|
|
+ * 同一幂等键异载荷返回 C4092(4092);档位不存在返回 C404(404)。
|
|
|
|
|
+ * 真实晋升评定仍由 {@link #evaluateAndRecord(Long, Long)} 在同事务内完成(档位键与留痕口径共用)。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Result<Map<String, Object>> grantLecturerBadge(Long userId, String authLevel,
|
|
|
|
|
+ Boolean replay, String key,
|
|
|
|
|
+ Map<String, Object> payload) {
|
|
|
|
|
+ if (userId == null || authLevel == null || authLevel.trim().isEmpty()) {
|
|
|
|
|
+ return Result.error("参数不完整");
|
|
|
|
|
+ }
|
|
|
|
|
+ String lv = authLevel.trim().toUpperCase();
|
|
|
|
|
+ if (levelIndex(lv) < 1) {
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "讲师档位不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ String badgeKey = lecturerBadgeKey(lv);
|
|
|
|
|
+ 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())
|
|
|
|
|
+ ? (userId + "|" + badgeKey) : key;
|
|
|
|
|
+ String previous = lecturerGrantFingerprints.putIfAbsent(fingerprintKey, fingerprint);
|
|
|
|
|
+ if (previous != null && !previous.equals(fingerprint)) {
|
|
|
|
|
+ return Result.error(CODE_SAME_KEY_DIFF_PAYLOAD,
|
|
|
|
|
+ "同一讲师档位幂等键 " + fingerprintKey + " 携带不同载荷,禁止静默复用既有档位");
|
|
|
|
|
+ }
|
|
|
|
|
+ TrainPromotionRule rule = enabledRule(lv);
|
|
|
|
|
+ if (rule == null) {
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "未配置该等级的晋升规则");
|
|
|
|
|
+ }
|
|
|
|
|
+ Result<Map<String, Object>> evaluated = self.evaluateAndRecord(userId, rule.getId());
|
|
|
|
|
+ if (evaluated == null) {
|
|
|
|
|
+ return Result.error("晋升评定无结果");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Integer.valueOf(CODE_OK).equals(evaluated.getCode())) {
|
|
|
|
|
+ return Result.error(evaluated.getCode(),
|
|
|
|
|
+ evaluated.getMessage() == null ? "晋升评定失败" : evaluated.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> data = evaluated.getData() == null
|
|
|
|
|
+ ? new HashMap<String, Object>() : new HashMap<String, Object>(evaluated.getData());
|
|
|
|
|
+ data.put("badgeKey", badgeKey);
|
|
|
|
|
+ data.put("key", fingerprintKey);
|
|
|
|
|
+ data.put("replay", Boolean.TRUE.equals(replay));
|
|
|
|
|
+ data.put("payload", safePayload);
|
|
|
|
|
+ return Result.success(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Map<String, Object>> unlockCheck(Long userId, Map<String, Object> body) {
|
|
public Result<Map<String, Object>> unlockCheck(Long userId, Map<String, Object> body) {
|
|
|
if (userId == null) {
|
|
if (userId == null) {
|
|
@@ -434,7 +506,7 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
public Result<Map<String, Object>> scanAndGrant(Boolean isAdmin) {
|
|
public Result<Map<String, Object>> scanAndGrant(Boolean isAdmin) {
|
|
|
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);
|
|
|
}
|
|
}
|
|
|
int processed = 0;
|
|
int processed = 0;
|
|
|
int granted = 0;
|
|
int granted = 0;
|
|
@@ -671,6 +743,20 @@ public class PromotionServiceImpl implements PromotionService {
|
|
|
return idx < 0 ? -1 : idx;
|
|
return idx < 0 ? -1 : idx;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 认证等级 → 徽章目录键 lec_t1~lec_t4。
|
|
|
|
|
+ * 修复旧实现 {@code BADGE_PREFIX + authLevel} 产出 {@code lec_tT2}(T 重复)的串键缺陷:
|
|
|
|
|
+ * levelIndex 的序号即目录档位序号,保证键唯一且落在目录内。
|
|
|
|
|
+ */
|
|
|
|
|
+ private String lecturerBadgeKey(String authLevel) {
|
|
|
|
|
+ return BADGE_PREFIX + levelIndex(normLevel(authLevel));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 晋升载荷指纹:规则 + 档位 + 达标条件(同键异载荷 C4092 的比对口径)。 */
|
|
|
|
|
+ private String lecturerFingerprint(TrainPromotionRule rule) {
|
|
|
|
|
+ return str(rule.getId()) + "|" + str(rule.getAuthLevel()) + "|" + str(rule.getConditions());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
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;
|