Browse Source

feat(backend): 证据导出运维端点、资质证书管理与工单派单来源面出

- AdminEvidenceController:/api/admin/evidence/list 分页、/retry 重跑
  (retry 复位 pending 后复用 EvidenceExportTask.processExport 同一 @Async 入口,仅 failed/done 可重跑)
- 资质证书:/api/practitioner/license/add|list(本人)、
  /api/admin/practitioner/license/list|verify(admin);uk 冲突返回「该资质已存在」
- ServiceOrderService.toVO 面出 source(org_created/self_accept),供机构派单标签
liaoxg 8 hours ago
parent
commit
8db601cdc1

+ 26 - 0
train-backend/src/main/java/com/train/controller/PractitionerController.java

@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -49,6 +50,31 @@ public class PractitionerController {
         return practitionerService.detail(userId, isAdmin, targetUserId);
     }
 
+    /**
+     * 新增资质证书(本人)
+     * body: {licenseType, licenseNo, issuer, issuedAt, expireAt?, fileId?, fileUrl?}
+     * 出参: {licenseId}
+     */
+    @Operation(summary = "新增资质证书(本人)")
+    @PostMapping("/license/add")
+    public Result<Long> licenseAdd(
+            @RequestAttribute("userId") Long userId,
+            @RequestBody(required = false) Map<String, Object> body) {
+        return practitionerService.addLicense(userId, body);
+    }
+
+    /**
+     * 我的资质证书列表(本人,含 verify_status)
+     * body: {}
+     */
+    @Operation(summary = "我的资质证书列表")
+    @PostMapping("/license/list")
+    public Result<List<Map<String, Object>>> licenseList(
+            @RequestAttribute("userId") Long userId,
+            @RequestBody(required = false) Map<String, Object> body) {
+        return practitionerService.listLicenses(userId);
+    }
+
     private Long toLong(Object v) {
         if (v == null) {
             return null;

+ 58 - 0
train-backend/src/main/java/com/train/controller/admin/AdminEvidenceController.java

@@ -0,0 +1,58 @@
+package com.train.controller.admin;
+
+import com.train.common.Result;
+import com.train.service.EvidenceService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestAttribute;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+/**
+ * 管理端-证据包导出运维 Controller(FR-009 运维端点)。
+ *
+ * 权限:仅 admin(Service 层 RoleGuard/管理员校验)。
+ * - list  :证据包任务分页(type/status 可选过滤,created_at 倒序)
+ * - retry :对 failed/done 任务重新导出(复用 EvidenceExportTask 同一 @Async 触发入口)
+ */
+@Tag(name = "管理端-证据包", description = "证据包导出运维视角(FR-009)")
+@RestController
+@RequestMapping("/api/admin/evidence")
+public class AdminEvidenceController {
+
+    @Resource
+    private EvidenceService evidenceService;
+
+    /**
+     * 证据包任务分页(admin 视角)
+     * body: {type?, status?, page=1, size=20}
+     * 出参: {total, page, size, list:[TrainEvidencePack]}
+     */
+    @Operation(summary = "证据包任务列表(分页)")
+    @PostMapping("/list")
+    public Result<Map<String, Object>> list(
+            @RequestAttribute(value = "userId", required = false) Long userId,
+            @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
+            @RequestBody(required = false) Map<String, Object> body) {
+        return evidenceService.adminList(userId, isAdmin, body);
+    }
+
+    /**
+     * 重跑证据导出(admin 视角)
+     * body: {taskNo}
+     * 出参: Boolean(true 已重新触发异步导出)
+     */
+    @Operation(summary = "证据包任务重跑(failed/done 可重跑)")
+    @PostMapping("/retry")
+    public Result<Boolean> retry(
+            @RequestAttribute(value = "userId", required = false) Long userId,
+            @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
+            @RequestBody(required = false) Map<String, Object> body) {
+        return evidenceService.retry(userId, isAdmin, body);
+    }
+}

+ 28 - 0
train-backend/src/main/java/com/train/controller/admin/AdminPractitionerController.java

@@ -51,6 +51,34 @@ public class AdminPractitionerController {
         return practitionerService.approveProfile(adminId, isAdmin, targetUserId);
     }
 
+    /**
+     * 资质证书分页(admin 视角)
+     * body: {userId?, verifyStatus?, page=1, size=20}
+     * 出参: {total, page, size, list:[TrainPractitionerLicense]}
+     */
+    @Operation(summary = "资质证书列表(分页)")
+    @PostMapping("/license/list")
+    public Result<Map<String, Object>> licenseList(
+            @RequestAttribute(value = "userId", required = false) Long userId,
+            @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
+            @RequestBody(required = false) Map<String, Object> body) {
+        return practitionerService.adminLicenseList(userId, isAdmin, body);
+    }
+
+    /**
+     * 资质证书核验(admin 视角)
+     * body: {licenseId, approved(boolean), rejectReason?}
+     * approved=true → verified;false → rejected。置 verified_by/verified_at/remark。
+     */
+    @Operation(summary = "资质证书核验")
+    @PostMapping("/license/verify")
+    public Result<Boolean> licenseVerify(
+            @RequestAttribute(value = "adminId", required = false) Long adminId,
+            @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
+            @RequestBody(required = false) Map<String, Object> body) {
+        return practitionerService.verifyLicense(adminId, isAdmin, body);
+    }
+
     private Long toLong(Object v) {
         if (v == null) {
             return null;

+ 14 - 0
train-backend/src/main/java/com/train/service/EvidenceService.java

@@ -15,4 +15,18 @@ public interface EvidenceService {
     Result<Map<String, Object>> export(Long userId, Boolean isAdmin, Map<String, Object> body);
 
     Result<Map<String, Object>> status(Long userId, Boolean isAdmin, Map<String, Object> body);
+
+    /**
+     * 管理端-证据包分页(仅 admin)。
+     * body: {type?, status?, page=1, size=20},created_at 倒序。
+     */
+    Result<Map<String, Object>> adminList(Long userId, Boolean isAdmin, Map<String, Object> body);
+
+    /**
+     * 管理端-重跑证据导出(仅 admin)。
+     * 仅当 status=failed 或 done 允许重跑;复用 EvidenceExportTask.processExport 同一 @Async 触发入口,
+     * 先复位 pending 再触发(processExport 以 pending 为处理门槛)。
+     * body: {taskNo}
+     */
+    Result<Boolean> retry(Long userId, Boolean isAdmin, Map<String, Object> body);
 }

+ 30 - 0
train-backend/src/main/java/com/train/service/PractitionerService.java

@@ -3,6 +3,7 @@ package com.train.service;
 import com.train.common.Result;
 import com.train.entity.TrainPractitionerProfile;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -47,4 +48,33 @@ public interface PractitionerService {
      * @param targetUserId 被审批档案 owner=train_user.id
      */
     Result<Map<String, Object>> approveProfile(Long operatorId, Boolean isAdmin, Long targetUserId);
+
+    /**
+     * 新增资质证书(用户端,仅本人)。
+     * body: {licenseType, licenseNo, issuer, issuedAt, expireAt?, fileId?, fileUrl?}
+     * verify_status=pending;唯一键 uk(user_id,license_type,license_no) 重复返回「该资质已存在」。
+     * 出参: Result<Long licenseId>
+     */
+    Result<Long> addLicense(Long userId, Map<String, Object> body);
+
+    /**
+     * 我的资质证书列表(仅本人,含 verify_status)。
+     * 出参: Result<List<Map<String, Object>>>
+     */
+    Result<List<Map<String, Object>>> listLicenses(Long userId);
+
+    /**
+     * 管理端-资质证书分页(仅 admin)。
+     * body: {userId?, verifyStatus?, page=1, size=20}
+     * 出参: {total, page, size, list:[TrainPractitionerLicense]}
+     */
+    Result<Map<String, Object>> adminLicenseList(Long userId, Boolean isAdmin, Map<String, Object> body);
+
+    /**
+     * 管理端-资质证书核验(仅 admin)。
+     * body: {licenseId, approved(boolean), rejectReason?}
+     * approved=true → verified;false → rejected。置 verified_by/verified_at/remark。
+     * 出参: Result<Boolean>
+     */
+    Result<Boolean> verifyLicense(Long operatorId, Boolean isAdmin, Map<String, Object> body);
 }

+ 19 - 0
train-backend/src/main/java/com/train/service/ServiceOrderService.java

@@ -331,9 +331,28 @@ public class ServiceOrderService {
         m.put("platformFee", o.getPlatformFee() == null ? 0 : o.getPlatformFee());
         m.put("staffIncome", o.getStaffIncome() == null ? 0 : o.getStaffIncome());
         m.put("remark", o.getRemark());
+        // 派单来源(org_created 组织建单 / self_accept 自接单):前端「机构派单」标签依据
+        m.put("source", resolveSource(o));
         return m;
     }
 
+    /**
+     * 派单来源解析:按 order_id + worker_id 取最近一条派单留痕的 source。
+     * 未派单(worker_id 为空)或无论痕 → 返回 null(前端 item.source==='org_created' 判断不受影响)。
+     */
+    private String resolveSource(TrainServiceOrder o) {
+        if (o.getId() == null || o.getWorkerId() == null) {
+            return null;
+        }
+        TrainOrderDispatch d = dispatchMapper.selectOne(
+                new LambdaQueryWrapper<TrainOrderDispatch>()
+                        .eq(TrainOrderDispatch::getOrderId, o.getId())
+                        .eq(TrainOrderDispatch::getWorkerId, o.getWorkerId())
+                        .orderByDesc(TrainOrderDispatch::getId)
+                        .last("LIMIT 1"));
+        return d == null ? null : d.getSource();
+    }
+
     private List<Map<String, Object>> parseItems(String json) {
         List<Map<String, Object>> list = new ArrayList<>();
         if (!StringUtils.hasText(json)) {

+ 90 - 0
train-backend/src/main/java/com/train/service/impl/EvidenceServiceImpl.java

@@ -32,6 +32,8 @@ public class EvidenceServiceImpl implements EvidenceService {
     private static final String TYPE_TRAINING = "training";
     private static final String TYPE_ORDER = "order";
     private static final String ST_PENDING = "pending";
+    private static final String ST_FAILED = "failed";
+    private static final String ST_DONE = "done";
 
     @Resource
     private TrainEvidencePackMapper trainEvidencePackMapper;
@@ -122,6 +124,79 @@ public class EvidenceServiceImpl implements EvidenceService {
         return Result.success(data);
     }
 
+    @Override
+    public Result<Map<String, Object>> adminList(Long userId, Boolean isAdmin, Map<String, Object> body) {
+        if (!Boolean.TRUE.equals(isAdmin)) {
+            return Result.error("需要管理员权限");
+        }
+        int page = body == null ? 1 : toInt(body.get("page"), 1);
+        int size = body == null ? 20 : toInt(body.get("size"), 20);
+        if (page < 1) {
+            page = 1;
+        }
+        if (size < 1) {
+            size = 20;
+        }
+        if (size > 100) {
+            size = 100;
+        }
+        String type = body == null ? null : str(body.get("type"));
+        String status = body == null ? null : str(body.get("status"));
+
+        LambdaQueryWrapper<TrainEvidencePack> qw = new LambdaQueryWrapper<>();
+        if (type != null && !type.trim().isEmpty()) {
+            qw.eq(TrainEvidencePack::getType, type.trim());
+        }
+        if (status != null && !status.trim().isEmpty()) {
+            qw.eq(TrainEvidencePack::getStatus, status.trim());
+        }
+        Long total = trainEvidencePackMapper.selectCount(qw);
+        if (total == null) {
+            total = 0L;
+        }
+        int offset = (page - 1) * size;
+        qw.orderByDesc(TrainEvidencePack::getCreatedAt).last("LIMIT " + offset + "," + size);
+        List<TrainEvidencePack> list = trainEvidencePackMapper.selectList(qw);
+
+        Map<String, Object> result = new HashMap<>();
+        result.put("total", total);
+        result.put("page", page);
+        result.put("size", size);
+        result.put("list", list);
+        return Result.success(result);
+    }
+
+    @Override
+    public Result<Boolean> retry(Long userId, Boolean isAdmin, Map<String, Object> body) {
+        if (!Boolean.TRUE.equals(isAdmin)) {
+            return Result.error("需要管理员权限");
+        }
+        if (body == null || body.get("taskNo") == null) {
+            return Result.error("参数不完整:缺少 taskNo");
+        }
+        String taskNo = body.get("taskNo").toString().trim();
+        TrainEvidencePack pack = trainEvidencePackMapper.selectOne(
+                new LambdaQueryWrapper<TrainEvidencePack>()
+                        .eq(TrainEvidencePack::getTaskNo, taskNo)
+                        .last("LIMIT 1"));
+        if (pack == null) {
+            return Result.error("导出任务不存在");
+        }
+        if (!ST_FAILED.equals(pack.getStatus()) && !ST_DONE.equals(pack.getStatus())) {
+            return Result.error("当前状态不可重跑(仅 failed/done 可重新导出)");
+        }
+        // 复位为 pending 后走同一 @Async 触发入口(EvidenceExportTask.processExport 以 pending 为门槛,
+        // 内部 setProcessing 会推进到 processing;若直接置 processing 则该入口会跳过处理)
+        pack.setStatus(ST_PENDING);
+        pack.setProgress(0);
+        pack.setError(null);
+        pack.setFileUrl(null);
+        pack.setUpdatedAt(new Date());
+        trainEvidencePackMapper.updateById(pack);
+        evidenceExportTask.processExport(pack.getId());
+        return Result.success(true);
+    }
+
     // ==================== 内部 ====================
 
     private Long resolveOrgId(Long userId, Boolean isAdmin, Map<String, Object> body) {
@@ -172,4 +247,19 @@ public class EvidenceServiceImpl implements EvidenceService {
             return null;
         }
     }
+
+    private int toInt(Object v, int def) {
+        if (v == null) {
+            return def;
+        }
+        try {
+            return Integer.parseInt(v.toString().trim());
+        } catch (NumberFormatException e) {
+            return def;
+        }
+    }
+
+    private String str(Object v) {
+        return v == null ? null : v.toString();
+    }
 }

+ 183 - 0
train-backend/src/main/java/com/train/service/impl/PractitionerServiceImpl.java

@@ -51,6 +51,10 @@ public class PractitionerServiceImpl implements PractitionerService {
     private static final String ORG_MEMBER_STATUS_ACTIVE = "active";
     /** 资质核验状态:verified 已核(有效证书口径) */
     private static final String LICENSE_VERIFY_VERIFIED = "verified";
+    /** 资质核验状态:pending 待核 */
+    private static final String LICENSE_VERIFY_PENDING = "pending";
+    /** 资质核验状态:rejected 驳回 */
+    private static final String LICENSE_VERIFY_REJECTED = "rejected";
     /** 学时台账状态:effective 已生效 */
     private static final String LEDGER_STATUS_EFFECTIVE = "effective";
 
@@ -241,6 +245,143 @@ public class PractitionerServiceImpl implements PractitionerService {
         return Result.success(data);
     }
 
+    @Override
+    public Result<Long> addLicense(Long userId, Map<String, Object> body) {
+        if (userId == null) {
+            return Result.error("请先登录");
+        }
+        if (body == null || body.get("licenseType") == null || body.get("licenseNo") == null
+                || body.get("issuer") == null || body.get("issuedAt") == null) {
+            return Result.error("参数不完整:licenseType/licenseNo/issuer/issuedAt 必填");
+        }
+        String licenseType = str(body.get("licenseType")).trim();
+        String licenseNo = str(body.get("licenseNo")).trim();
+        if (licenseType.isEmpty() || licenseNo.isEmpty()) {
+            return Result.error("参数不完整:licenseType/licenseNo 不能为空");
+        }
+        Date issuedAt = parseDate(body.get("issuedAt"));
+        if (issuedAt == null) {
+            return Result.error("issuedAt 日期格式无效(yyyy-MM-dd)");
+        }
+        // 唯一键 uk(user_id, license_type, license_no) 预检(软删行视为仍占用,保持唯一语义)
+        TrainPractitionerLicense exist = trainPractitionerLicenseMapper.selectOne(
+                new LambdaQueryWrapper<TrainPractitionerLicense>()
+                        .eq(TrainPractitionerLicense::getUserId, userId)
+                        .eq(TrainPractitionerLicense::getLicenseType, licenseType)
+                        .eq(TrainPractitionerLicense::getLicenseNo, licenseNo)
+                        .last("LIMIT 1"));
+        if (exist != null) {
+            return Result.error("该资质已存在");
+        }
+        TrainPractitionerLicense lic = new TrainPractitionerLicense();
+        lic.setUserId(userId);
+        lic.setLicenseType(licenseType);
+        lic.setLicenseNo(licenseNo);
+        lic.setIssuer(str(body.get("issuer")));
+        lic.setIssuedAt(issuedAt);
+        lic.setExpireAt(parseDate(body.get("expireAt")));
+        lic.setFileId(toLong(body.get("fileId")));
+        lic.setFileUrl(str(body.get("fileUrl")));
+        lic.setVerifyStatus(LICENSE_VERIFY_PENDING);
+        lic.setDeleted(DELETED_NORMAL);
+        lic.setCreatedAt(new Date());
+        lic.setUpdatedAt(new Date());
+        try {
+            trainPractitionerLicenseMapper.insert(lic);
+        } catch (Exception e) {
+            // uk 并发冲突兜底
+            log.warn("资质证书插入冲突: userId={}, licenseType={}, licenseNo={}, err={}",
+                    userId, licenseType, licenseNo, e.getMessage());
+            return Result.error("该资质已存在");
+        }
+        return Result.success(lic.getId());
+    }
+
+    @Override
+    public Result<List<Map<String, Object>>> listLicenses(Long userId) {
+        if (userId == null) {
+            return Result.error("请先登录");
+        }
+        List<TrainPractitionerLicense> list = trainPractitionerLicenseMapper.selectList(
+                new LambdaQueryWrapper<TrainPractitionerLicense>()
+                        .eq(TrainPractitionerLicense::getUserId, userId)
+                        .eq(TrainPractitionerLicense::getDeleted, DELETED_NORMAL)
+                        .orderByDesc(TrainPractitionerLicense::getId));
+        List<Map<String, Object>> rows = new ArrayList<>();
+        if (list != null) {
+            for (TrainPractitionerLicense lic : list) {
+                rows.add(licenseVO(lic));
+            }
+        }
+        return Result.success(rows);
+    }
+
+    @Override
+    public Result<Map<String, Object>> adminLicenseList(Long userId, Boolean isAdmin, Map<String, Object> body) {
+        if (!Boolean.TRUE.equals(isAdmin)) {
+            return Result.error("需要管理员权限");
+        }
+        int page = body == null ? 1 : toInt(body.get("page"), 1);
+        int size = body == null ? 20 : toInt(body.get("size"), 20);
+        if (page < 1) {
+            page = 1;
+        }
+        if (size < 1) {
+            size = 20;
+        }
+        if (size > 100) {
+            size = 100;
+        }
+        Long filterUserId = body == null ? null : toLong(body.get("userId"));
+        String verifyStatus = body == null ? null : str(body.get("verifyStatus"));
+
+        LambdaQueryWrapper<TrainPractitionerLicense> qw = new LambdaQueryWrapper<>();
+        qw.eq(TrainPractitionerLicense::getDeleted, DELETED_NORMAL);
+        if (filterUserId != null) {
+            qw.eq(TrainPractitionerLicense::getUserId, filterUserId);
+        }
+        if (verifyStatus != null && !verifyStatus.trim().isEmpty()) {
+            qw.eq(TrainPractitionerLicense::getVerifyStatus, verifyStatus.trim());
+        }
+        Long total = trainPractitionerLicenseMapper.selectCount(qw);
+        if (total == null) {
+            total = 0L;
+        }
+        int offset = (page - 1) * size;
+        qw.orderByDesc(TrainPractitionerLicense::getId).last("LIMIT " + offset + "," + size);
+        List<TrainPractitionerLicense> list = trainPractitionerLicenseMapper.selectList(qw);
+
+        Map<String, Object> result = new HashMap<>();
+        result.put("total", total);
+        result.put("page", page);
+        result.put("size", size);
+        result.put("list", list);
+        return Result.success(result);
+    }
+
+    @Override
+    public Result<Boolean> verifyLicense(Long operatorId, Boolean isAdmin, Map<String, Object> body) {
+        if (!Boolean.TRUE.equals(isAdmin)) {
+            return Result.error("需要管理员权限");
+        }
+        if (body == null || body.get("licenseId") == null || body.get("approved") == null) {
+            return Result.error("参数不完整:licenseId/approved 必填");
+        }
+        Long licenseId = toLong(body.get("licenseId"));
+        boolean approved = Boolean.parseBoolean(body.get("approved").toString().trim());
+        TrainPractitionerLicense lic = trainPractitionerLicenseMapper.selectById(licenseId);
+        if (lic == null) {
+            return Result.error("资质证书不存在");
+        }
+        lic.setVerifyStatus(approved ? LICENSE_VERIFY_VERIFIED : LICENSE_VERIFY_REJECTED);
+        lic.setVerifiedBy(operatorId);
+        lic.setVerifiedAt(new Date());
+        lic.setRemark(str(body.get("rejectReason")));
+        lic.setUpdatedAt(new Date());
+        trainPractitionerLicenseMapper.updateById(lic);
+        return Result.success(true);
+    }
+
     // ---------- 内部工具 ----------
 
     private LambdaQueryWrapper<TrainPractitionerProfile> baseWrapper(String keyword, String level,
@@ -520,4 +661,46 @@ public class PractitionerServiceImpl implements PractitionerService {
             return def;
         }
     }
+
+    /** 资质证书 VO(用户端 listLicenses 用) */
+    private Map<String, Object> licenseVO(TrainPractitionerLicense lic) {
+        Map<String, Object> m = new HashMap<>();
+        m.put("id", lic.getId());
+        m.put("userId", lic.getUserId());
+        m.put("licenseType", lic.getLicenseType());
+        m.put("licenseNo", lic.getLicenseNo());
+        m.put("issuer", lic.getIssuer());
+        m.put("issuedAt", fmt(lic.getIssuedAt()));
+        m.put("expireAt", fmt(lic.getExpireAt()));
+        m.put("fileId", lic.getFileId());
+        m.put("fileUrl", lic.getFileUrl());
+        m.put("verifyStatus", lic.getVerifyStatus());
+        m.put("verifiedBy", lic.getVerifiedBy());
+        m.put("verifiedAt", fmt(lic.getVerifiedAt()));
+        m.put("remark", lic.getRemark());
+        m.put("createdAt", fmt(lic.getCreatedAt()));
+        return m;
+    }
+
+    /** 日期解析:接受 Date 或 yyyy-MM-dd / yyyy-MM-dd HH:mm 字符串;非法返回 null */
+    private Date parseDate(Object v) {
+        if (v == null) {
+            return null;
+        }
+        if (v instanceof Date) {
+            return (Date) v;
+        }
+        String s = v.toString().trim();
+        if (s.isEmpty()) {
+            return null;
+        }
+        try {
+            if (s.length() > 10) {
+                return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(s);
+            }
+            return new SimpleDateFormat("yyyy-MM-dd").parse(s);
+        } catch (Exception e) {
+            return null;
+        }
+    }
 }