|
|
@@ -50,6 +50,12 @@ public class CreditServiceImpl implements CreditService {
|
|
|
private static final String CAT_THEORY = "theory";
|
|
|
private static final String CAT_PRACTICE = "practice";
|
|
|
|
|
|
+ /** train.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;
|
|
|
+ private static final Integer C_FORBIDDEN = 403;
|
|
|
+
|
|
|
@Resource
|
|
|
private TrainCreditAccountMapper trainCreditAccountMapper;
|
|
|
@Resource
|
|
|
@@ -203,10 +209,18 @@ public class CreditServiceImpl implements CreditService {
|
|
|
}
|
|
|
TrainCreditLedger l = trainCreditLedgerMapper.selectById(ledgerId);
|
|
|
if (l == null || (l.getDeleted() != null && l.getDeleted() == 1)) {
|
|
|
- return Result.error("学时记录不存在");
|
|
|
+ return Result.error(C_NOT_FOUND, "学时记录不存在");
|
|
|
}
|
|
|
if (!userId.equals(l.getUserId())) {
|
|
|
- return Result.error("无权操作他人学时记录");
|
|
|
+ return Result.error(C_FORBIDDEN, "无权操作他人学时记录");
|
|
|
+ }
|
|
|
+ // 乐观锁:reason 携带 version=N 时须与实体版本一致(不可解析视为过期版本 → C422)
|
|
|
+ Long expectedVersion = parseExpectedVersion(reason);
|
|
|
+ if (expectedVersion != null) {
|
|
|
+ Long actualVersion = l.getVersion();
|
|
|
+ if (actualVersion == null || !expectedVersion.equals(actualVersion)) {
|
|
|
+ return Result.error(C_VERSION_CONFLICT, "学时记录版本已变更,请重试");
|
|
|
+ }
|
|
|
}
|
|
|
// 仅 pending 可提异议(已生效走申诉外流程)
|
|
|
if (!ST_PENDING.equals(l.getStatus())) {
|
|
|
@@ -215,7 +229,11 @@ public class CreditServiceImpl implements CreditService {
|
|
|
l.setStatus(ST_OBJECTED);
|
|
|
l.setRemark(reason);
|
|
|
l.setUpdatedAt(new Date());
|
|
|
- trainCreditLedgerMapper.updateById(l);
|
|
|
+ int affected = trainCreditLedgerMapper.updateById(l);
|
|
|
+ if (affected <= 0) {
|
|
|
+ // 并发丢失更新:影响 0 行必须 C422,不得静默成功
|
|
|
+ return Result.error(C_VERSION_CONFLICT, "学时记录已被更新,请重试");
|
|
|
+ }
|
|
|
// 回写 attendance(source_id=attendance.id)→ objected,扣减账户 pending
|
|
|
if (SRC_ATTENDANCE.equals(l.getSourceType())) {
|
|
|
TrainAttendance att = trainAttendanceMapper.selectById(parseLong(l.getSourceId()));
|
|
|
@@ -261,6 +279,33 @@ public class CreditServiceImpl implements CreditService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 从 reason 解析期望版本号(version=N):未携带返回 null;携带但不可解析返回 -1(视为版本冲突)。
|
|
|
+ * 与 AttendanceServiceImpl.checkReplayConflict 同口径,供异议提交通道复用。
|
|
|
+ */
|
|
|
+ private Long parseExpectedVersion(String reason) {
|
|
|
+ if (reason == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ int idx = reason.indexOf("version=");
|
|
|
+ if (idx < 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String tail = reason.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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public boolean ledgerPending(Long userId, String sourceType, String sourceId, String category,
|
|
|
@@ -658,7 +703,7 @@ public class CreditServiceImpl implements CreditService {
|
|
|
public Result<Boolean> disposeObjected(Boolean isAdmin, Long operatorId, Long ledgerId, Boolean approved, String reason) {
|
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
if (noAuth != null) {
|
|
|
- return Result.error(noAuth);
|
|
|
+ return Result.error(C_FORBIDDEN, noAuth);
|
|
|
}
|
|
|
if (ledgerId == null) {
|
|
|
return Result.error("id 不能为空");
|
|
|
@@ -668,16 +713,23 @@ public class CreditServiceImpl implements CreditService {
|
|
|
}
|
|
|
TrainCreditLedger l = trainCreditLedgerMapper.selectById(ledgerId);
|
|
|
if (l == null || (l.getDeleted() != null && l.getDeleted() == 1)) {
|
|
|
- return Result.error("学时记录不存在");
|
|
|
+ return Result.error(C_NOT_FOUND, "学时记录不存在");
|
|
|
+ }
|
|
|
+ // 终态载荷一致性:同一幂等键(ledgerId)重放须维持原处置载荷,否则 C4092
|
|
|
+ // effective ⇔ approved=true(驳回异议恢复生效);rejected ⇔ approved=false(否决撤销)
|
|
|
+ if (ST_EFFECTIVE.equals(l.getStatus()) || ST_REJECTED.equals(l.getStatus())) {
|
|
|
+ boolean terminalApproved = ST_EFFECTIVE.equals(l.getStatus());
|
|
|
+ if (terminalApproved != Boolean.TRUE.equals(approved)) {
|
|
|
+ return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "同一学时记录已终态处置,载荷不一致");
|
|
|
+ }
|
|
|
+ // 载荷一致:幂等成功,不重复触发撤销/恢复
|
|
|
+ return Result.success(true);
|
|
|
+ }
|
|
|
+ if (!ST_OBJECTED.equals(l.getStatus())) {
|
|
|
+ return Result.error("当前状态不允许该处置");
|
|
|
}
|
|
|
if (Boolean.TRUE.equals(approved)) {
|
|
|
- // 驳回异议 → 恢复生效(幂等:已 effective 直接成功)
|
|
|
- if (ST_EFFECTIVE.equals(l.getStatus())) {
|
|
|
- return Result.success(true);
|
|
|
- }
|
|
|
- if (!ST_OBJECTED.equals(l.getStatus())) {
|
|
|
- return Result.error("当前状态不允许该处置");
|
|
|
- }
|
|
|
+ // 驳回异议 → 恢复生效
|
|
|
if (reason != null && !reason.trim().isEmpty()) {
|
|
|
l.setRemark(reason.trim());
|
|
|
l.setOperatorId(operatorId);
|
|
|
@@ -688,12 +740,6 @@ public class CreditServiceImpl implements CreditService {
|
|
|
return Result.success(true);
|
|
|
}
|
|
|
// 否决撤销 → objected→rejected(终态),账户不增加(pending 已在 object 时扣减,沿用既有回滚路径)
|
|
|
- if (ST_REJECTED.equals(l.getStatus())) {
|
|
|
- return Result.success(true);
|
|
|
- }
|
|
|
- if (!ST_OBJECTED.equals(l.getStatus())) {
|
|
|
- return Result.error("当前状态不允许该处置");
|
|
|
- }
|
|
|
self.applyRollBack(l);
|
|
|
return Result.success(true);
|
|
|
}
|