|
|
@@ -50,7 +50,7 @@ public class CreditServiceImpl implements CreditService {
|
|
|
private static final String CAT_THEORY = "theory";
|
|
|
private static final String CAT_PRACTICE = "practice";
|
|
|
|
|
|
- /** train.attend.activate / train.hours.ingest 共用契约错误码(对齐契约级单测,勿改名) */
|
|
|
+ /** 契约错误码(对齐契约级单测,勿改名):hours.account / hours.ingest / attend.activate 共用 */
|
|
|
private static final Integer C_SAME_KEY_DIFF_PAYLOAD = 4092;
|
|
|
private static final Integer C_VERSION_CONFLICT = 422;
|
|
|
private static final Integer C_NOT_FOUND = 404;
|
|
|
@@ -84,7 +84,7 @@ public class CreditServiceImpl implements CreditService {
|
|
|
@Value("${app.credit.effective-delay-hours:72}")
|
|
|
private Integer effectiveDelayHours;
|
|
|
|
|
|
- @Override
|
|
|
+ /** 内部取或建账户(2 参形态):供 mine/台账路径复用;契约入口见下方 5 参 Result 形态。 */
|
|
|
public TrainCreditAccount ensureAccount(Long userId, Integer year) {
|
|
|
int curYear = year == null ? Year.now().getValue() : year;
|
|
|
TrainCreditAccount acc = trainCreditAccountMapper.selectOne(
|
|
|
@@ -126,6 +126,55 @@ public class CreditServiceImpl implements CreditService {
|
|
|
return acc;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Result<TrainCreditAccount> ensureAccount(Long userId, Integer year, Boolean replay,
|
|
|
+ String idempotencyKey, Object payload) {
|
|
|
+ int curYear = year == null ? Year.now().getValue() : year;
|
|
|
+ String key = idempotencyKey != null ? idempotencyKey : (userId + ":" + curYear);
|
|
|
+ TrainCreditAccount existing = trainCreditAccountMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<TrainCreditAccount>()
|
|
|
+ .eq(TrainCreditAccount::getUserId, userId)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ // 同 (user_id, year) 幂等键却携带不同载荷 → C4092,禁止静默复用旧账户
|
|
|
+ if (existing != null && payload != null && !payloadMatches(existing, payload)) {
|
|
|
+ return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "学时账户幂等键 " + key + " 载荷不一致");
|
|
|
+ }
|
|
|
+ // 请求携带过期 version → C422(@Version 乐观锁),不得按成功返回
|
|
|
+ if (existing != null && payload instanceof Map) {
|
|
|
+ Object wantVersion = ((Map<?, ?>) payload).get("version");
|
|
|
+ if (wantVersion instanceof Number && existing.getVersion() != null
|
|
|
+ && ((Number) wantVersion).longValue() != existing.getVersion().longValue()) {
|
|
|
+ return Result.error(C_VERSION_CONFLICT, "学时账户版本已变更,请重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 幂等重放:原样返回既有账户,保证多来源汇总唯一;并发首建冲突由 2 参形态回查兜底
|
|
|
+ if (Boolean.TRUE.equals(replay) && existing != null) {
|
|
|
+ return Result.success(existing);
|
|
|
+ }
|
|
|
+ return Result.success(ensureAccount(userId, year));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** payload 分列与既有账户一致才允许复用;非 Map 载荷不参与一致性判定。 */
|
|
|
+ private boolean payloadMatches(TrainCreditAccount acc, Object payload) {
|
|
|
+ if (!(payload instanceof Map)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Map<?, ?> m = (Map<?, ?>) payload;
|
|
|
+ return matches(m.get("totalHours"), acc.getTotalHours())
|
|
|
+ && matches(m.get("theoryHours"), acc.getTheoryHours())
|
|
|
+ && matches(m.get("practiceHours"), acc.getPracticeHours());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean matches(Object want, Integer actual) {
|
|
|
+ if (want == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (!(want instanceof Number)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return actual != null && ((Number) want).intValue() == actual.intValue();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Result<Map<String, Object>> mine(Long userId, Integer year) {
|
|
|
int curYear = year == null ? Year.now().getValue() : year;
|
|
|
@@ -616,7 +665,7 @@ public class CreditServiceImpl implements CreditService {
|
|
|
public Result<Map<String, Object>> accountList(Boolean isAdmin, Integer year, String keyword, Integer page, Integer size) {
|
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
if (noAuth != null) {
|
|
|
- return Result.error(noAuth);
|
|
|
+ return Result.error(C_FORBIDDEN, noAuth);
|
|
|
}
|
|
|
int p = page == null || page < 1 ? 1 : page;
|
|
|
int s = size == null || size < 1 ? 20 : Math.min(size, 100);
|