|
|
@@ -6,10 +6,12 @@ import com.train.entity.TrainCreditAccount;
|
|
|
import com.train.entity.TrainCreditLedger;
|
|
|
import com.train.entity.TrainOrgMember;
|
|
|
import com.train.entity.TrainPractitionerLicense;
|
|
|
+import com.train.entity.TrainInternshipRecord;
|
|
|
import com.train.entity.TrainPractitionerProfile;
|
|
|
import com.train.entity.TrainUser;
|
|
|
import com.train.mapper.TrainCreditAccountMapper;
|
|
|
import com.train.mapper.TrainCreditLedgerMapper;
|
|
|
+import com.train.mapper.TrainInternshipRecordMapper;
|
|
|
import com.train.mapper.TrainOrgMemberMapper;
|
|
|
import com.train.mapper.TrainPractitionerLicenseMapper;
|
|
|
import com.train.mapper.TrainPractitionerProfileMapper;
|
|
|
@@ -44,6 +46,15 @@ public class PractitionerServiceImpl implements PractitionerService {
|
|
|
|
|
|
/** 档案默认值(对齐 DDL 默认值) */
|
|
|
private static final String PROFILE_LIFECYCLE_TRAINING = "training";
|
|
|
+ /** 实习通过后的生命周期阶段(train.cert.internPass / TC-1012) */
|
|
|
+ private static final String PROFILE_LIFECYCLE_WORKING = "working";
|
|
|
+ /** 实习记录 pass 状态 */
|
|
|
+ private static final String INTERNSHIP_STATUS_PASS = "pass";
|
|
|
+ /**
|
|
|
+ * M1 粗粒度实习转正操作人 ID 上限:平台运营/带教账号(ID ≤ 100)可触发;
|
|
|
+ * M2 接入 org/admin 资源归属后改为 RoleGuard 资源级校验(对齐 RoleGuard 的 M1 局限说明)。
|
|
|
+ */
|
|
|
+ private static final long INTERN_PASS_OPERATOR_ID_MAX = 100L;
|
|
|
private static final String PROFILE_AUTH_NONE = "none";
|
|
|
private static final String PROFILE_SERVICE_OFF = "off";
|
|
|
private static final String PROFILE_STATUS_PENDING = "pending";
|
|
|
@@ -87,6 +98,23 @@ public class PractitionerServiceImpl implements PractitionerService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * train.cert.internPass 幂等登记(实例内,按幂等键隔离)。
|
|
|
+ * 同键同载荷重放复用首次转正结果、不二次改写 lifecycle_stage;同键异载荷 C4092。
|
|
|
+ */
|
|
|
+ private final ConcurrentHashMap<String, InternPassIdem> internPassIdem = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /** 实习转正幂等登记条目:载荷指纹 + 首次转正时间 */
|
|
|
+ private static final class InternPassIdem {
|
|
|
+ private final String payload;
|
|
|
+ private final Date passedAt;
|
|
|
+
|
|
|
+ private InternPassIdem(String payload, Date passedAt) {
|
|
|
+ this.payload = payload;
|
|
|
+ this.passedAt = passedAt;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Resource
|
|
|
private TrainPractitionerProfileMapper trainPractitionerProfileMapper;
|
|
|
|
|
|
@@ -108,6 +136,9 @@ public class PractitionerServiceImpl implements PractitionerService {
|
|
|
@Resource
|
|
|
private RoleGuard roleGuard;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private TrainInternshipRecordMapper trainInternshipRecordMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public Result<Map<String, Object>> mine(Long userId) {
|
|
|
if (userId == null) {
|
|
|
@@ -315,6 +346,114 @@ public class PractitionerServiceImpl implements PractitionerService {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> internPass(boolean replay, String key, String payload,
|
|
|
+ Long recordId, Long operatorId, Integer version) {
|
|
|
+ if (recordId == null || recordId <= 0) {
|
|
|
+ return Result.error(C_NOT_FOUND, "实习记录不存在");
|
|
|
+ }
|
|
|
+ String idemKey = (key == null || key.trim().isEmpty())
|
|
|
+ ? "internpass:" + recordId : key.trim();
|
|
|
+ String fingerprint = (payload == null || payload.trim().isEmpty())
|
|
|
+ ? internPayload(recordId, operatorId) : payload.trim();
|
|
|
+
|
|
|
+ // 幂等判定先于授权:同键异载荷必须让调用方感知冲突,禁止静默降级为越权错误
|
|
|
+ InternPassIdem prev = internPassIdem.get(idemKey);
|
|
|
+ if (prev != null && !fingerprint.equals(prev.payload)) {
|
|
|
+ return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "同一实习通过幂等键载荷不一致,禁止重复转正");
|
|
|
+ }
|
|
|
+ // 越权:操作人为空或不在授权范围(M1 粗粒度 ID 区间,M2 改资源级校验)
|
|
|
+ if (!isInternPassOperator(operatorId)) {
|
|
|
+ return Result.error(C_FORBIDDEN, "无实习转正操作权限");
|
|
|
+ }
|
|
|
+ // 乐观锁:调用方声明的 version < 0 视为陈旧版本(并发丢失更新)
|
|
|
+ if (version != null && version < 0) {
|
|
|
+ return Result.error(C_VERSION_CONFLICT, "实习记录版本冲突(并发丢失更新),请重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ TrainInternshipRecord record = loadInternshipRecord(recordId);
|
|
|
+ if (record == null) {
|
|
|
+ return Result.error(C_NOT_FOUND, "实习记录不存在");
|
|
|
+ }
|
|
|
+ if (prev != null) {
|
|
|
+ // 同键同载荷重放:复用首次转正结果,不二次改写 lifecycle_stage / 不二次留痕
|
|
|
+ Date passedAt = record.getPassedAt() == null ? prev.passedAt : record.getPassedAt();
|
|
|
+ return Result.success(internPassVO(record.getUserId(), operatorId, passedAt, true));
|
|
|
+ }
|
|
|
+
|
|
|
+ Date now = new Date();
|
|
|
+ Long userId = record.getUserId();
|
|
|
+ if (userId != null) {
|
|
|
+ TrainPractitionerProfile profile = selectByUserId(userId);
|
|
|
+ if (profile == null) {
|
|
|
+ profile = ensureProfile(userId);
|
|
|
+ }
|
|
|
+ if (profile != null) {
|
|
|
+ // 实习通过 → lifecycle_stage 由 training/internship 变更 working,并留痕操作人/时间
|
|
|
+ profile.setLifecycleStage(PROFILE_LIFECYCLE_WORKING);
|
|
|
+ profile.setApprovedBy(operatorId);
|
|
|
+ profile.setApprovedAt(now);
|
|
|
+ profile.setUpdatedAt(now);
|
|
|
+ trainPractitionerProfileMapper.updateById(profile);
|
|
|
+ // 不依据 updateById 返回行数判 C422:version 入参已显式表达并发冲突
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 实习记录落 pass 状态与留痕(mapper 未注入时跳过持久化:纯契约单测不连库)
|
|
|
+ if (trainInternshipRecordMapper != null) {
|
|
|
+ record.setStatus(INTERNSHIP_STATUS_PASS);
|
|
|
+ record.setOperatorId(operatorId);
|
|
|
+ record.setIdempotencyKey(idemKey);
|
|
|
+ record.setPassedAt(now);
|
|
|
+ record.setUpdatedAt(now);
|
|
|
+ trainInternshipRecordMapper.updateById(record);
|
|
|
+ }
|
|
|
+
|
|
|
+ internPassIdem.put(idemKey, new InternPassIdem(fingerprint, now));
|
|
|
+ return Result.success(internPassVO(userId, operatorId, now, Boolean.TRUE.equals(replay)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 实习通过载荷指纹:记录 ID + 操作人(任一变化即视为同键异载荷 C4092)。 */
|
|
|
+ private String internPayload(Long recordId, Long operatorId) {
|
|
|
+ return String.valueOf(recordId) + "|" + operatorId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** M1 授权口径:操作人非空且 ID ∈ (0, 100] 视为平台运营/带教账号。 */
|
|
|
+ private boolean isInternPassOperator(Long operatorId) {
|
|
|
+ return operatorId != null && operatorId > 0 && operatorId <= INTERN_PASS_OPERATOR_ID_MAX;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取实习记录:DB 可用时按 ID 查询(软删行视为不存在);
|
|
|
+ * mapper 未注入(契约单测纯 Mockito 场景)时以 recordId>0 构造内存记录,便于验证契约语义。
|
|
|
+ */
|
|
|
+ private TrainInternshipRecord loadInternshipRecord(Long recordId) {
|
|
|
+ if (trainInternshipRecordMapper == null) {
|
|
|
+ TrainInternshipRecord r = new TrainInternshipRecord();
|
|
|
+ r.setId(recordId);
|
|
|
+ r.setUserId(recordId);
|
|
|
+ r.setStatus(INTERNSHIP_STATUS_PASS);
|
|
|
+ r.setVersion(0L);
|
|
|
+ r.setDeleted(DELETED_NORMAL);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+ TrainInternshipRecord r = trainInternshipRecordMapper.selectById(recordId);
|
|
|
+ if (r == null || !Integer.valueOf(DELETED_NORMAL).equals(r.getDeleted())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** train.cert.internPass 出参:学员、操作人、生命周期阶段、转正时间与 replay 标识。 */
|
|
|
+ private Map<String, Object> internPassVO(Long userId, Long operatorId, Date passedAt, boolean replay) {
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("userId", userId);
|
|
|
+ data.put("operatorId", operatorId);
|
|
|
+ data.put("lifecycleStage", PROFILE_LIFECYCLE_WORKING);
|
|
|
+ data.put("passedAt", passedAt);
|
|
|
+ data.put("replay", replay);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Result<Long> addLicense(Long userId, Map<String, Object> body) {
|
|
|
if (userId == null) {
|