|
@@ -47,6 +47,12 @@ public class CohortServiceImpl implements CohortService {
|
|
|
private static final String SIG_CANCELLED = "cancelled";
|
|
private static final String SIG_CANCELLED = "cancelled";
|
|
|
private static final String ENR_CONFIRMED = "confirmed";
|
|
private static final String ENR_CONFIRMED = "confirmed";
|
|
|
|
|
|
|
|
|
|
+ /** train.cohort.create 契约错误码(对齐契约级单测,勿改名) */
|
|
|
|
|
+ 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
|
|
@Resource
|
|
|
private TrainCohortMapper trainCohortMapper;
|
|
private TrainCohortMapper trainCohortMapper;
|
|
|
@Resource
|
|
@Resource
|
|
@@ -64,15 +70,47 @@ public class CohortServiceImpl implements CohortService {
|
|
|
public Result<Map<String, Object>> create(Long operatorId, Boolean isAdmin, Map<String, Object> body) {
|
|
public Result<Map<String, Object>> create(Long operatorId, Boolean isAdmin, Map<String, Object> body) {
|
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
if (noAuth != null) {
|
|
if (noAuth != null) {
|
|
|
- return Result.error(noAuth);
|
|
|
|
|
|
|
+ return Result.error(C_FORBIDDEN, noAuth);
|
|
|
}
|
|
}
|
|
|
if (body == null) {
|
|
if (body == null) {
|
|
|
return Result.error("参数不完整");
|
|
return Result.error("参数不完整");
|
|
|
}
|
|
}
|
|
|
|
|
+ String code = str(body.get("code"));
|
|
|
|
|
+ Long courseId = toLong(body.get("courseId"));
|
|
|
|
|
+ String name = str(body.get("name"));
|
|
|
|
|
+ if (courseId == null || name == null || name.trim().isEmpty()) {
|
|
|
|
|
+ return Result.error("批次名称与课程必填");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 幂等键 code(唯一批次号):命中同键须判定载荷/版本,禁止静默重复落库
|
|
|
|
|
+ if (code != null) {
|
|
|
|
|
+ TrainCohort exist = trainCohortMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<TrainCohort>()
|
|
|
|
|
+ .eq(TrainCohort::getCode, code)
|
|
|
|
|
+ .eq(TrainCohort::getDeleted, 0)
|
|
|
|
|
+ .last("LIMIT 1"));
|
|
|
|
|
+ if (exist != null) {
|
|
|
|
|
+ // 同键异载荷 → C4092
|
|
|
|
|
+ if (!samePayload(exist, courseId, name)) {
|
|
|
|
|
+ return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "批次号已存在且载荷不一致");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 过期版本 → C422(乐观锁)
|
|
|
|
|
+ Long wantVersion = toLong(body.get("version"));
|
|
|
|
|
+ Long haveVersion = exist.getVersion();
|
|
|
|
|
+ if (wantVersion != null && haveVersion != null && wantVersion < haveVersion) {
|
|
|
|
|
+ return Result.error(C_VERSION_CONFLICT, "批次版本冲突");
|
|
|
|
|
+ }
|
|
|
|
|
+ // replay=true(或同载荷重复提交)→ 回带原批次,禁止再次 insert
|
|
|
|
|
+ return Result.success(cohortVO(exist));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 课程不存在 → C404
|
|
|
|
|
+ if (trainCourseMapper.selectById(courseId) == null) {
|
|
|
|
|
+ return Result.error(C_NOT_FOUND, "课程不存在");
|
|
|
|
|
+ }
|
|
|
TrainCohort c = new TrainCohort();
|
|
TrainCohort c = new TrainCohort();
|
|
|
- c.setCourseId(toLong(body.get("courseId")));
|
|
|
|
|
- c.setName(str(body.get("name")));
|
|
|
|
|
- c.setCode(str(body.get("code")));
|
|
|
|
|
|
|
+ c.setCourseId(courseId);
|
|
|
|
|
+ c.setName(name);
|
|
|
|
|
+ c.setCode(code);
|
|
|
c.setStartAt(toDate(body.get("startAt")));
|
|
c.setStartAt(toDate(body.get("startAt")));
|
|
|
c.setEndAt(toDate(body.get("endAt")));
|
|
c.setEndAt(toDate(body.get("endAt")));
|
|
|
c.setSignupDeadline(toDate(body.get("signupDeadline")));
|
|
c.setSignupDeadline(toDate(body.get("signupDeadline")));
|
|
@@ -85,22 +123,28 @@ public class CohortServiceImpl implements CohortService {
|
|
|
c.setLecturerId(toLong(body.get("lecturerId")));
|
|
c.setLecturerId(toLong(body.get("lecturerId")));
|
|
|
c.setStatus(StateGuard.C_DRAFT);
|
|
c.setStatus(StateGuard.C_DRAFT);
|
|
|
c.setDeleted(0);
|
|
c.setDeleted(0);
|
|
|
- if (c.getCourseId() == null || c.getName() == null || c.getName().trim().isEmpty()) {
|
|
|
|
|
- return Result.error("批次名称与课程必填");
|
|
|
|
|
- }
|
|
|
|
|
- if (c.getCourseId() != null && trainCourseMapper.selectById(c.getCourseId()) == null) {
|
|
|
|
|
- return Result.error("课程不存在");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ c.setVersion(1L);
|
|
|
try {
|
|
try {
|
|
|
trainCohortMapper.insert(c);
|
|
trainCohortMapper.insert(c);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- // code 唯一冲突
|
|
|
|
|
- log.warn("批次创建唯一键冲突: code={}, err={}", c.getCode(), e.getMessage());
|
|
|
|
|
- return Result.error("批次号已存在");
|
|
|
|
|
|
|
+ // code 唯一冲突 → C4092
|
|
|
|
|
+ log.warn("批次创建唯一键冲突: code={}, err={}", code, e.getMessage());
|
|
|
|
|
+ return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "批次号已存在");
|
|
|
}
|
|
}
|
|
|
return Result.success(cohortVO(c));
|
|
return Result.success(cohortVO(c));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** 同幂等键载荷一致性:以批次号对应的 name/courseId 为准 */
|
|
|
|
|
+ private boolean samePayload(TrainCohort exist, Long courseId, String name) {
|
|
|
|
|
+ if (name != null && !name.equals(exist.getName())) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (courseId != null && exist.getCourseId() != null && !courseId.equals(exist.getCourseId())) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Map<String, Object>> publish(Long operatorId, Boolean isAdmin, Long cohortId) {
|
|
public Result<Map<String, Object>> publish(Long operatorId, Boolean isAdmin, Long cohortId) {
|
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
String noAuth = roleGuard.requireAdminRole(isAdmin);
|