|
@@ -53,6 +53,12 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
private static final String SRC_ATTENDANCE = "attendance";
|
|
private static final String SRC_ATTENDANCE = "attendance";
|
|
|
private static final String SIG_APPROVED = "approved";
|
|
private static final String SIG_APPROVED = "approved";
|
|
|
|
|
|
|
|
|
|
+ /** 契约错误码(proxy 字符串契约,勿改名):越权/不存在/并发冲突/同键异载荷 */
|
|
|
|
|
+ private static final int CODE_FORBIDDEN = 403;
|
|
|
|
|
+ private static final int CODE_NOT_FOUND = 404;
|
|
|
|
|
+ private static final int CODE_VERSION_CONFLICT = 422;
|
|
|
|
|
+ private static final int CODE_PAYLOAD_CONFLICT = 4092;
|
|
|
|
|
+
|
|
|
private static final String CHAR_SET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // 去易混淆字符
|
|
private static final String CHAR_SET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // 去易混淆字符
|
|
|
private static final SecureRandom RANDOM = new SecureRandom();
|
|
private static final SecureRandom RANDOM = new SecureRandom();
|
|
|
|
|
|
|
@@ -82,7 +88,7 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
// 权限三重校验:isAdmin | 任一 active 机构 org_admin/dispatcher | 该批次主讲讲师本人
|
|
// 权限三重校验:isAdmin | 任一 active 机构 org_admin/dispatcher | 该批次主讲讲师本人
|
|
|
String noAuth = checkIssueAuth(isAdmin, userId, sessionId);
|
|
String noAuth = checkIssueAuth(isAdmin, userId, sessionId);
|
|
|
if (noAuth != null) {
|
|
if (noAuth != null) {
|
|
|
- return Result.error(noAuth);
|
|
|
|
|
|
|
+ return Result.error(CODE_FORBIDDEN, noAuth);
|
|
|
}
|
|
}
|
|
|
if (userId == null || sessionId == null || sessionId.trim().isEmpty()) {
|
|
if (userId == null || sessionId == null || sessionId.trim().isEmpty()) {
|
|
|
return Result.error("参数不完整");
|
|
return Result.error("参数不完整");
|
|
@@ -93,19 +99,28 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
return Result.error("场次ID格式不正确(应为 {批次ID}:{场次序号})");
|
|
return Result.error("场次ID格式不正确(应为 {批次ID}:{场次序号})");
|
|
|
}
|
|
}
|
|
|
if (trainCohortMapper.selectById(cohortId) == null) {
|
|
if (trainCohortMapper.selectById(cohortId) == null) {
|
|
|
- return Result.error("批次不存在");
|
|
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "批次不存在");
|
|
|
}
|
|
}
|
|
|
Date now = new Date();
|
|
Date now = new Date();
|
|
|
- // 幂等:同 session 已有 active 且未过期 → 直接返回,不重复发码
|
|
|
|
|
|
|
+ // 幂等:同 session 已有 active 且未过期 → 校验版本/载荷后复用,不重复发码
|
|
|
TrainAttendanceCode active = trainAttendanceCodeMapper.selectOne(
|
|
TrainAttendanceCode active = trainAttendanceCodeMapper.selectOne(
|
|
|
new LambdaQueryWrapper<TrainAttendanceCode>()
|
|
new LambdaQueryWrapper<TrainAttendanceCode>()
|
|
|
.eq(TrainAttendanceCode::getSessionId, sessionId)
|
|
.eq(TrainAttendanceCode::getSessionId, sessionId)
|
|
|
.eq(TrainAttendanceCode::getStatus, ST_ACTIVE)
|
|
.eq(TrainAttendanceCode::getStatus, ST_ACTIVE)
|
|
|
.last("LIMIT 1"));
|
|
.last("LIMIT 1"));
|
|
|
|
|
+ boolean replacingExpired = false;
|
|
|
if (active != null && active.getValidTo() != null && !active.getValidTo().before(now)) {
|
|
if (active != null && active.getValidTo() != null && !active.getValidTo().before(now)) {
|
|
|
|
|
+ Result<Map<String, Object>> conflict = checkReplayConflict(active, sessionHours, category, note);
|
|
|
|
|
+ if (conflict != null) {
|
|
|
|
|
+ return conflict;
|
|
|
|
|
+ }
|
|
|
return Result.success(codeVO(active));
|
|
return Result.success(codeVO(active));
|
|
|
}
|
|
}
|
|
|
- return doIssue(userId, sessionId, sessionHours, category, now);
|
|
|
|
|
|
|
+ if (active != null) {
|
|
|
|
|
+ // 过期码不可幂等复用,必须轮换旧码并签发新码
|
|
|
|
|
+ replacingExpired = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ return doIssue(userId, sessionId, sessionHours, category, now, replacingExpired);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -127,7 +142,7 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
return Result.error("批次不存在");
|
|
return Result.error("批次不存在");
|
|
|
}
|
|
}
|
|
|
Date now = new Date();
|
|
Date now = new Date();
|
|
|
- return doIssue(userId, sessionId, sessionHours, category, now);
|
|
|
|
|
|
|
+ return doIssue(userId, sessionId, sessionHours, category, now, false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -159,9 +174,58 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
return orgErr;
|
|
return orgErr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 幂等重放冲突校验:同场次(幂等键)已有未过期 active 码时,重放必须维持相同载荷(C4092),
|
|
|
|
|
+ * 且 note 携带的期望版本(version=N)必须与实体版本一致(C422)。合法返回 null。
|
|
|
|
|
+ */
|
|
|
|
|
+ private Result<Map<String, Object>> checkReplayConflict(TrainAttendanceCode active,
|
|
|
|
|
+ Integer sessionHours, String category, String note) {
|
|
|
|
|
+ Long expectedVersion = parseExpectedVersion(note);
|
|
|
|
|
+ if (expectedVersion != null) {
|
|
|
|
|
+ Long actualVersion = active.getVersion();
|
|
|
|
|
+ if (actualVersion == null || !expectedVersion.equals(actualVersion)) {
|
|
|
|
|
+ return Result.error(CODE_VERSION_CONFLICT, "签到码版本已变更,请重新发码");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ int hours = sessionHours == null || sessionHours <= 0 ? 480 : sessionHours;
|
|
|
|
|
+ String cat = category == null || category.trim().isEmpty() ? "other" : category;
|
|
|
|
|
+ int oldHours = active.getSessionHours() == null || active.getSessionHours() <= 0
|
|
|
|
|
+ ? 480 : active.getSessionHours();
|
|
|
|
|
+ String oldCat = active.getSessionCategory() == null || active.getSessionCategory().trim().isEmpty()
|
|
|
|
|
+ ? "other" : active.getSessionCategory();
|
|
|
|
|
+ if (oldHours != hours || !cat.equals(oldCat)) {
|
|
|
|
|
+ return Result.error(CODE_PAYLOAD_CONFLICT, "同场次签到码学时/分类与本次请求不一致");
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 从 note 解析期望版本号(version=N):未携带返回 null;携带但不可解析返回 -1(视为版本冲突)。 */
|
|
|
|
|
+ private Long parseExpectedVersion(String note) {
|
|
|
|
|
+ if (note == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ int idx = note.indexOf("version=");
|
|
|
|
|
+ if (idx < 0) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String tail = note.substring(idx + "version=".length()).trim();
|
|
|
|
|
+ int end = 0;
|
|
|
|
|
+ while (end < tail.length() && Character.isDigit(tail.charAt(end))) {
|
|
|
|
|
+ end++;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (end == 0) {
|
|
|
|
|
+ return -1L;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Long.valueOf(tail.substring(0, end));
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ return -1L;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/** 实际发码:轮换本 session 所有 active 旧码 + 新建 active 码 */
|
|
/** 实际发码:轮换本 session 所有 active 旧码 + 新建 active 码 */
|
|
|
private Result<Map<String, Object>> doIssue(Long userId, String sessionId, Integer sessionHours,
|
|
private Result<Map<String, Object>> doIssue(Long userId, String sessionId, Integer sessionHours,
|
|
|
- String category, Date now) {
|
|
|
|
|
|
|
+ String category, Date now, boolean replacingExpired) {
|
|
|
Long cohortId = parseCohortId(sessionId);
|
|
Long cohortId = parseCohortId(sessionId);
|
|
|
// 轮换旧码(active→rotated)
|
|
// 轮换旧码(active→rotated)
|
|
|
List<TrainAttendanceCode> olds = trainAttendanceCodeMapper.selectList(
|
|
List<TrainAttendanceCode> olds = trainAttendanceCodeMapper.selectList(
|
|
@@ -173,7 +237,13 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
o.setRotatedBy(userId);
|
|
o.setRotatedBy(userId);
|
|
|
o.setRotatedAt(now);
|
|
o.setRotatedAt(now);
|
|
|
o.setUpdatedAt(now);
|
|
o.setUpdatedAt(now);
|
|
|
- trainAttendanceCodeMapper.updateById(o);
|
|
|
|
|
|
|
+ int affected = trainAttendanceCodeMapper.updateById(o);
|
|
|
|
|
+ // 轮换未过期 active 码影响 0 行 = 并发丢失更新,必须 C422,不得静默发新码;
|
|
|
|
|
+ // 过期码轮换影响 0 行可容忍(旧码本已失效,继续签发新码)。
|
|
|
|
|
+ if (affected <= 0 && !replacingExpired
|
|
|
|
|
+ && !(o.getValidTo() != null && o.getValidTo().before(now))) {
|
|
|
|
|
+ return Result.error(CODE_VERSION_CONFLICT, "签到码轮换并发冲突,请重试");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
int hours = sessionHours == null || sessionHours <= 0 ? 480 : sessionHours; // 缺省 8h=480
|
|
int hours = sessionHours == null || sessionHours <= 0 ? 480 : sessionHours; // 缺省 8h=480
|
|
|
Date validTo = addMinutes(now, codeValidMinutes == null ? 5 : codeValidMinutes);
|
|
Date validTo = addMinutes(now, codeValidMinutes == null ? 5 : codeValidMinutes);
|
|
@@ -520,6 +590,7 @@ public class AttendanceServiceImpl implements AttendanceService {
|
|
|
c.setValidFrom(from);
|
|
c.setValidFrom(from);
|
|
|
c.setValidTo(to);
|
|
c.setValidTo(to);
|
|
|
c.setStatus(ST_ACTIVE);
|
|
c.setStatus(ST_ACTIVE);
|
|
|
|
|
+ c.setVersion(0L); // 乐观锁初始版本
|
|
|
return c;
|
|
return c;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|