|
@@ -17,6 +17,7 @@ import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 证据包导出实现(M4)。
|
|
* 证据包导出实现(M4)。
|
|
@@ -35,6 +36,12 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
private static final String ST_FAILED = "failed";
|
|
private static final String ST_FAILED = "failed";
|
|
|
private static final String ST_DONE = "done";
|
|
private static final String ST_DONE = "done";
|
|
|
|
|
|
|
|
|
|
+ /** 契约错误码(对齐契约级单测,勿改名) */
|
|
|
|
|
+ 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 TrainEvidencePackMapper trainEvidencePackMapper;
|
|
private TrainEvidencePackMapper trainEvidencePackMapper;
|
|
|
@Resource
|
|
@Resource
|
|
@@ -44,6 +51,12 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private RoleGuard roleGuard;
|
|
private RoleGuard roleGuard;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 导出请求幂等登记(进程内,按 userId+key 命名空间)。
|
|
|
|
|
+ * 异步导出请求在生成完成前需要可重放:同键同载荷复用原任务,同键异载荷 C4092。
|
|
|
|
|
+ */
|
|
|
|
|
+ private final ConcurrentHashMap<String, IdemRecord> idemCache = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Map<String, Object>> export(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
public Result<Map<String, Object>> export(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
|
if (body == null || body.get("type") == null) {
|
|
if (body == null || body.get("type") == null) {
|
|
@@ -53,8 +66,12 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
if (!TYPE_TRAINING.equals(type) && !TYPE_ORDER.equals(type)) {
|
|
if (!TYPE_TRAINING.equals(type) && !TYPE_ORDER.equals(type)) {
|
|
|
return Result.error("type 仅支持 training/order");
|
|
return Result.error("type 仅支持 training/order");
|
|
|
}
|
|
}
|
|
|
- // 归属解析
|
|
|
|
|
- Long orgId = resolveOrgId(userId, isAdmin, body);
|
|
|
|
|
|
|
+ // 归属解析:显式指定他机构而非成员 → C403,禁止静默降级为个人范围
|
|
|
|
|
+ ScopeResolution resolution = resolveScope(userId, isAdmin, body);
|
|
|
|
|
+ if (resolution.denied()) {
|
|
|
|
|
+ return Result.error(C_FORBIDDEN, resolution.reason);
|
|
|
|
|
+ }
|
|
|
|
|
+ Long orgId = resolution.orgId;
|
|
|
Map<String, Object> scope = new HashMap<>();
|
|
Map<String, Object> scope = new HashMap<>();
|
|
|
scope.put("type", type);
|
|
scope.put("type", type);
|
|
|
if (orgId != null) {
|
|
if (orgId != null) {
|
|
@@ -68,6 +85,24 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
if (body.get("orderId") != null) {
|
|
if (body.get("orderId") != null) {
|
|
|
scope.put("orderId", body.get("orderId"));
|
|
scope.put("orderId", body.get("orderId"));
|
|
|
}
|
|
}
|
|
|
|
|
+ String payload = payloadFingerprint(scope);
|
|
|
|
|
+
|
|
|
|
|
+ // 幂等契约(body.key / body.replay 为 proxy 字符串契约,勿改名):
|
|
|
|
|
+ // 同键同载荷重放 → 复用原任务;同键异载荷 → C4092,禁止静默新建第二条任务
|
|
|
|
|
+ String key = str(body.get("key"));
|
|
|
|
|
+ boolean keyed = key != null && !key.trim().isEmpty();
|
|
|
|
|
+ String idemKey = keyed ? (userId + ":" + key.trim()) : null;
|
|
|
|
|
+ if (keyed) {
|
|
|
|
|
+ IdemRecord existing = idemCache.get(idemKey);
|
|
|
|
|
+ if (existing != null) {
|
|
|
|
|
+ if (!payload.equals(existing.payload)) {
|
|
|
|
|
+ return Result.error(C_SAME_KEY_DIFF_PAYLOAD,
|
|
|
|
|
+ "幂等键 " + key + " 载荷不一致,禁止新建导出任务");
|
|
|
|
|
+ }
|
|
|
|
|
+ return Result.success(existing.data());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
TrainEvidencePack pack = new TrainEvidencePack();
|
|
TrainEvidencePack pack = new TrainEvidencePack();
|
|
|
pack.setTaskNo(genTaskNo());
|
|
pack.setTaskNo(genTaskNo());
|
|
|
pack.setOrgUserId(userId);
|
|
pack.setOrgUserId(userId);
|
|
@@ -76,6 +111,7 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
pack.setScopeJson(scope.toString());
|
|
pack.setScopeJson(scope.toString());
|
|
|
pack.setStatus(ST_PENDING);
|
|
pack.setStatus(ST_PENDING);
|
|
|
pack.setProgress(0);
|
|
pack.setProgress(0);
|
|
|
|
|
+ pack.setVersion(0L);
|
|
|
pack.setCreatedAt(new Date());
|
|
pack.setCreatedAt(new Date());
|
|
|
pack.setUpdatedAt(new Date());
|
|
pack.setUpdatedAt(new Date());
|
|
|
try {
|
|
try {
|
|
@@ -84,12 +120,15 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
log.error("证据包任务创建失败: err={}", e.getMessage());
|
|
log.error("证据包任务创建失败: err={}", e.getMessage());
|
|
|
return Result.error("导出任务创建失败,请重试");
|
|
return Result.error("导出任务创建失败,请重试");
|
|
|
}
|
|
}
|
|
|
- // 异步生成(EvidenceExportTask @Async;失败由任务内兜底置 failed,不阻塞接口)
|
|
|
|
|
- evidenceExportTask.processExport(pack.getId());
|
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("taskNo", pack.getTaskNo());
|
|
data.put("taskNo", pack.getTaskNo());
|
|
|
data.put("id", pack.getId());
|
|
data.put("id", pack.getId());
|
|
|
data.put("status", ST_PENDING);
|
|
data.put("status", ST_PENDING);
|
|
|
|
|
+ if (keyed) {
|
|
|
|
|
+ idemCache.putIfAbsent(idemKey, new IdemRecord(payload, data));
|
|
|
|
|
+ }
|
|
|
|
|
+ // 异步生成(EvidenceExportTask @Async;失败由任务内兜底置 failed,不阻塞接口)
|
|
|
|
|
+ evidenceExportTask.processExport(pack.getId());
|
|
|
return Result.success(data);
|
|
return Result.success(data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -104,12 +143,12 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
.eq(TrainEvidencePack::getTaskNo, taskNo)
|
|
.eq(TrainEvidencePack::getTaskNo, taskNo)
|
|
|
.last("LIMIT 1"));
|
|
.last("LIMIT 1"));
|
|
|
if (pack == null) {
|
|
if (pack == null) {
|
|
|
- return Result.error("导出任务不存在");
|
|
|
|
|
|
|
+ return Result.error(C_NOT_FOUND, "导出任务不存在");
|
|
|
}
|
|
}
|
|
|
// mustOwn:非 admin 只能查自己的任务
|
|
// mustOwn:非 admin 只能查自己的任务
|
|
|
if (!Boolean.TRUE.equals(isAdmin) && userId != null && pack.getOrgUserId() != null
|
|
if (!Boolean.TRUE.equals(isAdmin) && userId != null && pack.getOrgUserId() != null
|
|
|
&& !userId.equals(pack.getOrgUserId())) {
|
|
&& !userId.equals(pack.getOrgUserId())) {
|
|
|
- return Result.error("无权查看该导出任务");
|
|
|
|
|
|
|
+ return Result.error(C_FORBIDDEN, "无权查看该导出任务");
|
|
|
}
|
|
}
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("taskNo", pack.getTaskNo());
|
|
data.put("taskNo", pack.getTaskNo());
|
|
@@ -127,7 +166,7 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Map<String, Object>> adminList(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
public Result<Map<String, Object>> adminList(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
|
if (!Boolean.TRUE.equals(isAdmin)) {
|
|
if (!Boolean.TRUE.equals(isAdmin)) {
|
|
|
- return Result.error("需要管理员权限");
|
|
|
|
|
|
|
+ return Result.error(C_FORBIDDEN, "需要管理员权限");
|
|
|
}
|
|
}
|
|
|
int page = body == null ? 1 : toInt(body.get("page"), 1);
|
|
int page = body == null ? 1 : toInt(body.get("page"), 1);
|
|
|
int size = body == null ? 20 : toInt(body.get("size"), 20);
|
|
int size = body == null ? 20 : toInt(body.get("size"), 20);
|
|
@@ -169,7 +208,7 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Boolean> retry(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
public Result<Boolean> retry(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
|
if (!Boolean.TRUE.equals(isAdmin)) {
|
|
if (!Boolean.TRUE.equals(isAdmin)) {
|
|
|
- return Result.error("需要管理员权限");
|
|
|
|
|
|
|
+ return Result.error(C_FORBIDDEN, "需要管理员权限");
|
|
|
}
|
|
}
|
|
|
if (body == null || body.get("taskNo") == null) {
|
|
if (body == null || body.get("taskNo") == null) {
|
|
|
return Result.error("参数不完整:缺少 taskNo");
|
|
return Result.error("参数不完整:缺少 taskNo");
|
|
@@ -180,7 +219,7 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
.eq(TrainEvidencePack::getTaskNo, taskNo)
|
|
.eq(TrainEvidencePack::getTaskNo, taskNo)
|
|
|
.last("LIMIT 1"));
|
|
.last("LIMIT 1"));
|
|
|
if (pack == null) {
|
|
if (pack == null) {
|
|
|
- return Result.error("导出任务不存在");
|
|
|
|
|
|
|
+ return Result.error(C_NOT_FOUND, "导出任务不存在");
|
|
|
}
|
|
}
|
|
|
if (!ST_FAILED.equals(pack.getStatus()) && !ST_DONE.equals(pack.getStatus())) {
|
|
if (!ST_FAILED.equals(pack.getStatus()) && !ST_DONE.equals(pack.getStatus())) {
|
|
|
return Result.error("当前状态不可重跑(仅 failed/done 可重新导出)");
|
|
return Result.error("当前状态不可重跑(仅 failed/done 可重新导出)");
|
|
@@ -191,33 +230,38 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
pack.setProgress(0);
|
|
pack.setProgress(0);
|
|
|
pack.setError(null);
|
|
pack.setError(null);
|
|
|
pack.setFileUrl(null);
|
|
pack.setFileUrl(null);
|
|
|
|
|
+ pack.setVersion(pack.getVersion() == null ? 1L : pack.getVersion() + 1L);
|
|
|
pack.setUpdatedAt(new Date());
|
|
pack.setUpdatedAt(new Date());
|
|
|
- trainEvidencePackMapper.updateById(pack);
|
|
|
|
|
|
|
+ int affected = trainEvidencePackMapper.updateById(pack);
|
|
|
|
|
+ if (affected <= 0) {
|
|
|
|
|
+ // 乐观锁冲突/并发丢失更新:影响 0 行,必须让调用方感知 C422,不得静默成功
|
|
|
|
|
+ return Result.error(C_VERSION_CONFLICT, "导出任务已被其他请求更新,请重试");
|
|
|
|
|
+ }
|
|
|
evidenceExportTask.processExport(pack.getId());
|
|
evidenceExportTask.processExport(pack.getId());
|
|
|
return Result.success(true);
|
|
return Result.success(true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ==================== 内部 ====================
|
|
// ==================== 内部 ====================
|
|
|
|
|
|
|
|
- private Long resolveOrgId(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
|
|
|
|
|
+ private ScopeResolution resolveScope(Long userId, Boolean isAdmin, Map<String, Object> body) {
|
|
|
Object orgIdObj = body.get("orgId");
|
|
Object orgIdObj = body.get("orgId");
|
|
|
if (orgIdObj != null) {
|
|
if (orgIdObj != null) {
|
|
|
Long orgId = toLong(orgIdObj);
|
|
Long orgId = toLong(orgIdObj);
|
|
|
if (orgId == null) {
|
|
if (orgId == null) {
|
|
|
- return null;
|
|
|
|
|
|
|
+ return ScopeResolution.allow(null); // 非法 orgId:退化为个人范围(兼容既有行为)
|
|
|
}
|
|
}
|
|
|
if (Boolean.TRUE.equals(isAdmin)) {
|
|
if (Boolean.TRUE.equals(isAdmin)) {
|
|
|
- return orgId; // admin 可导出任意机构
|
|
|
|
|
|
|
+ return ScopeResolution.allow(orgId); // admin 可导出任意机构
|
|
|
}
|
|
}
|
|
|
- // org 用户须为该机构成员(任意角色)
|
|
|
|
|
|
|
+ // 显式导出他机构证据必须校验成员权;越权返回 C403,绝不静默降级为个人范围
|
|
|
String noAuth = roleGuard.requireOrgMemberAt(userId, orgId);
|
|
String noAuth = roleGuard.requireOrgMemberAt(userId, orgId);
|
|
|
if (noAuth != null) {
|
|
if (noAuth != null) {
|
|
|
- return null;
|
|
|
|
|
|
|
+ return ScopeResolution.deny(noAuth);
|
|
|
}
|
|
}
|
|
|
- return orgId;
|
|
|
|
|
|
|
+ return ScopeResolution.allow(orgId);
|
|
|
}
|
|
}
|
|
|
if (Boolean.TRUE.equals(isAdmin)) {
|
|
if (Boolean.TRUE.equals(isAdmin)) {
|
|
|
- return null;
|
|
|
|
|
|
|
+ return ScopeResolution.allow(null);
|
|
|
}
|
|
}
|
|
|
// 默认取发起人首个 active 机构
|
|
// 默认取发起人首个 active 机构
|
|
|
List<TrainOrgMember> members = trainOrgMemberMapper.selectList(
|
|
List<TrainOrgMember> members = trainOrgMemberMapper.selectList(
|
|
@@ -227,9 +271,54 @@ public class EvidenceServiceImpl implements EvidenceService {
|
|
|
.eq(TrainOrgMember::getDeleted, 0)
|
|
.eq(TrainOrgMember::getDeleted, 0)
|
|
|
.orderByAsc(TrainOrgMember::getId));
|
|
.orderByAsc(TrainOrgMember::getId));
|
|
|
if (members != null && !members.isEmpty()) {
|
|
if (members != null && !members.isEmpty()) {
|
|
|
- return members.get(0).getOrgId();
|
|
|
|
|
|
|
+ return ScopeResolution.allow(members.get(0).getOrgId());
|
|
|
|
|
+ }
|
|
|
|
|
+ return ScopeResolution.allow(null); // 非机构用户 → 个人范围
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 稳定载荷指纹:HashMap.toString() 顺序不保证,按固定字段拼接供 C4092 判定。 */
|
|
|
|
|
+ private String payloadFingerprint(Map<String, Object> scope) {
|
|
|
|
|
+ return str(scope.get("type")) + "|" + str(scope.get("orgId")) + "|"
|
|
|
|
|
+ + str(scope.get("scope")) + "|" + str(scope.get("cohortId")) + "|"
|
|
|
|
|
+ + str(scope.get("orderId"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 归属解析结果:orgId 或越权原因(二选一)。 */
|
|
|
|
|
+ private static final class ScopeResolution {
|
|
|
|
|
+ private final Long orgId;
|
|
|
|
|
+ private final String reason;
|
|
|
|
|
+
|
|
|
|
|
+ private ScopeResolution(Long orgId, String reason) {
|
|
|
|
|
+ this.orgId = orgId;
|
|
|
|
|
+ this.reason = reason;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static ScopeResolution allow(Long orgId) {
|
|
|
|
|
+ return new ScopeResolution(orgId, null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static ScopeResolution deny(String reason) {
|
|
|
|
|
+ return new ScopeResolution(null, reason);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ boolean denied() {
|
|
|
|
|
+ return reason != null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 幂等登记:记录首次请求的载荷指纹与返回体,供 replay 原样复用。 */
|
|
|
|
|
+ private static final class IdemRecord {
|
|
|
|
|
+ private final String payload;
|
|
|
|
|
+ private final Map<String, Object> data;
|
|
|
|
|
+
|
|
|
|
|
+ IdemRecord(String payload, Map<String, Object> data) {
|
|
|
|
|
+ this.payload = payload;
|
|
|
|
|
+ this.data = new HashMap<>(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> data() {
|
|
|
|
|
+ return new HashMap<>(data);
|
|
|
}
|
|
}
|
|
|
- return null; // 非机构用户 → 个人范围
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private String genTaskNo() {
|
|
private String genTaskNo() {
|