|
@@ -13,12 +13,22 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
@Tag(name = "管理端-班级与分组", description = "班级管理、分组管理、学员管理")
|
|
@Tag(name = "管理端-班级与分组", description = "班级管理、分组管理、学员管理")
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping("/api/admin")
|
|
@RequestMapping("/api/admin")
|
|
|
public class AdminClassController {
|
|
public class AdminClassController {
|
|
|
|
|
|
|
|
|
|
+ /** train.klass.assign 契约错误码(proxy 字符串契约常量,勿改名) */
|
|
|
|
|
+ private static final int CODE_PAYLOAD_CONFLICT = 4092; // C4092 同键异载荷
|
|
|
|
|
+ private static final int CODE_OPTIMISTIC_LOCK = 422; // C422 并发更新冲突
|
|
|
|
|
+ private static final int CODE_NOT_FOUND = 404; // C404 目标不存在
|
|
|
|
|
+ private static final int CODE_FORBIDDEN = 403; // C403 越权
|
|
|
|
|
+
|
|
|
|
|
+ /** 分班幂等回执:幂等键(uid:classId) → 载荷指纹;重放命中即复用,不重复写名单/报名单。 */
|
|
|
|
|
+ private static final Map<String, String> KLASS_ASSIGN_RECEIPTS = new ConcurrentHashMap<String, String>();
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private TrainClassMapper trainClassMapper;
|
|
private TrainClassMapper trainClassMapper;
|
|
|
@Resource
|
|
@Resource
|
|
@@ -244,19 +254,43 @@ public class AdminClassController {
|
|
|
if (body.get("classId") != null && !body.get("classId").toString().trim().isEmpty()) {
|
|
if (body.get("classId") != null && !body.get("classId").toString().trim().isEmpty()) {
|
|
|
newClassId = Long.valueOf(body.get("classId").toString());
|
|
newClassId = Long.valueOf(body.get("classId").toString());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // C403:非管理员携带他人 operatorId 代学员分班 → 越权拒绝
|
|
|
|
|
+ Object operatorObj = body.get("operatorId");
|
|
|
|
|
+ if (operatorObj != null && !boolValue(body.get("isAdmin"))) {
|
|
|
|
|
+ Long operatorId = Long.valueOf(operatorObj.toString());
|
|
|
|
|
+ if (!operatorId.equals(uid)) {
|
|
|
|
|
+ return Result.error(CODE_FORBIDDEN, "非管理员不得代他人分班");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 幂等键 = uid:classId;载荷指纹承载分班要素,供 replay/payloadHash 契约校验
|
|
|
|
|
+ String idemKey = uid + ":" + (newClassId == null ? "none" : newClassId);
|
|
|
|
|
+ String payload = assignPayload(uid, newClassId);
|
|
|
|
|
+ Object payloadObj = body.get("payloadHash");
|
|
|
|
|
+ if (payloadObj != null && !payload.equals(payloadObj.toString())) {
|
|
|
|
|
+ // C4092:同幂等键异载荷,禁止静默复用首次分班结果
|
|
|
|
|
+ return Result.error(CODE_PAYLOAD_CONFLICT, "同一幂等键载荷不一致,禁止复用首次分班结果");
|
|
|
|
|
+ }
|
|
|
|
|
+ String prev = KLASS_ASSIGN_RECEIPTS.get(idemKey);
|
|
|
|
|
+ if (boolValue(body.get("replay")) && prev != null) {
|
|
|
|
|
+ // 同键同载荷重放:复用首次结果,不再写学员表与报名单
|
|
|
|
|
+ return Result.success(true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
TrainUser user = trainUserMapper.selectById(uid);
|
|
TrainUser user = trainUserMapper.selectById(uid);
|
|
|
if (user == null) {
|
|
if (user == null) {
|
|
|
- return Result.error("学员不存在");
|
|
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "学员不存在");
|
|
|
}
|
|
}
|
|
|
if (newClassId != null) {
|
|
if (newClassId != null) {
|
|
|
TrainClass clazz = trainClassMapper.selectById(newClassId);
|
|
TrainClass clazz = trainClassMapper.selectById(newClassId);
|
|
|
if (clazz == null || !"active".equals(clazz.getStatus())) {
|
|
if (clazz == null || !"active".equals(clazz.getStatus())) {
|
|
|
- return Result.error("目标班次不存在或不可用");
|
|
|
|
|
|
|
+ return Result.error(CODE_NOT_FOUND, "目标班次不存在或不可用");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
user.setClassId(newClassId);
|
|
user.setClassId(newClassId);
|
|
|
user.setUpdatedAt(new Date());
|
|
user.setUpdatedAt(new Date());
|
|
|
- trainUserMapper.updateById(user);
|
|
|
|
|
|
|
+ int affected = trainUserMapper.updateById(user);
|
|
|
// 同步最新一条 confirmed 报名单(如有)的 classId,保持数据一致
|
|
// 同步最新一条 confirmed 报名单(如有)的 classId,保持数据一致
|
|
|
TrainEnrollment latestEn = trainEnrollmentMapper.selectOne(
|
|
TrainEnrollment latestEn = trainEnrollmentMapper.selectOne(
|
|
|
new LambdaQueryWrapper<TrainEnrollment>()
|
|
new LambdaQueryWrapper<TrainEnrollment>()
|
|
@@ -269,9 +303,24 @@ public class AdminClassController {
|
|
|
latestEn.setUpdatedAt(new Date());
|
|
latestEn.setUpdatedAt(new Date());
|
|
|
trainEnrollmentMapper.updateById(latestEn);
|
|
trainEnrollmentMapper.updateById(latestEn);
|
|
|
}
|
|
}
|
|
|
|
|
+ // 无已确认报名单时,学员表更新行数是名单写入的唯一凭据;影响 0 行视为并发冲突 C422
|
|
|
|
|
+ if (latestEn == null && affected <= 0) {
|
|
|
|
|
+ return Result.error(CODE_OPTIMISTIC_LOCK, "分班名单更新冲突(并发修改),请重试");
|
|
|
|
|
+ }
|
|
|
|
|
+ KLASS_ASSIGN_RECEIPTS.put(idemKey, payload);
|
|
|
return Result.success(true);
|
|
return Result.success(true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** Boolean / 字符串布尔统一解析(契约字段 replay、isAdmin)。 */
|
|
|
|
|
+ private static boolean boolValue(Object value) {
|
|
|
|
|
+ return Boolean.TRUE.equals(value) || (value != null && Boolean.parseBoolean(value.toString()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 分班载荷指纹:同幂等键下分班要素变化即视为异载荷。 */
|
|
|
|
|
+ private static String assignPayload(Long uid, Long classId) {
|
|
|
|
|
+ return "uid=" + uid + ";classId=" + (classId == null ? "none" : classId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// ============ 分组成员管理 ============
|
|
// ============ 分组成员管理 ============
|
|
|
@Operation(summary = "将学员移入/调入分组(自动移出当前所在组)")
|
|
@Operation(summary = "将学员移入/调入分组(自动移出当前所在组)")
|
|
|
@PostMapping("/group/member/add")
|
|
@PostMapping("/group/member/add")
|