package com.kaidi.oa.web; import com.kaidi.oa.common.ApiException; import com.kaidi.oa.common.ApiResp; import com.kaidi.oa.common.NotFoundException; import com.kaidi.oa.domain.RdTestPlan; import com.kaidi.oa.repository.RdTestPlanRepository; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.time.Instant; import java.util.List; /** * 创新研发中心 / 产品开发部 · 结构化测试计划(需求模块 5 «测试计划与执行»)。 * *

补审计缺口"缺结构化测试计划实体":提供按功能/性能/可靠性/安规/环境分类编制测试计划、 * 在线记录测试数据、上传测试报告的完整端点。状态机:草稿 → 执行中 → 完成 / 失败。 * 缺陷数 defectCount 回填后可关联到问题管理(issueRef)。 * *

写口默认受 AuthInterceptor default-deny(ADMIN/APPROVER) 保护。 */ @RestController @RequestMapping("/api/oa/rd-test-plans") public class RdTestPlanController { private static final List VALID_CATEGORIES = List.of("功能", "性能", "可靠性", "安规", "环境", "其他"); private static final List VALID_STATUSES = List.of("草稿", "执行中", "完成", "失败"); private final RdTestPlanRepository repo; public RdTestPlanController(RdTestPlanRepository repo) { this.repo = repo; } @GetMapping public ApiResp> list( @RequestParam(required = false) Long devProjectId, @RequestParam(required = false) Long prototypeOrderId, @RequestParam(required = false) String testCategory, @RequestParam(required = false) String status) { if (devProjectId != null && testCategory != null && !testCategory.isBlank()) { return ApiResp.ok(repo.findByDevProjectIdAndTestCategory(devProjectId, testCategory)); } if (devProjectId != null && status != null && !status.isBlank()) { return ApiResp.ok(repo.findByDevProjectIdAndStatus(devProjectId, status)); } if (devProjectId != null) { return ApiResp.ok(repo.findByDevProjectId(devProjectId)); } if (prototypeOrderId != null) { return ApiResp.ok(repo.findByPrototypeOrderId(prototypeOrderId)); } if (status != null && !status.isBlank()) { return ApiResp.ok(repo.findByStatus(status)); } if (testCategory != null && !testCategory.isBlank()) { return ApiResp.ok(repo.findByTestCategory(testCategory)); } return ApiResp.ok(repo.findAll()); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(find(id)); } public record TestPlanRequest( Long devProjectId, Long prototypeOrderId, String planName, String testCategory, String testGoal, String testMethod, String passCriteria, String planStartDate, String planEndDate, String actualStartDate, String actualEndDate, String tester, String status, String testData, String reportRef, Integer defectCount, String issueRef, String testRemark, String createdBy) { } @PostMapping public ApiResp create(@RequestBody TestPlanRequest req) { if (req.planName() == null || req.planName().isBlank()) { throw new ApiException(400, "测试计划名称(planName) 不能为空"); } if (req.devProjectId() == null) { throw new ApiException(400, "所属研发立项(devProjectId) 不能为空"); } if (req.testCategory() != null && !VALID_CATEGORIES.contains(req.testCategory())) { throw new ApiException(400, "testCategory 须为: " + String.join("/", VALID_CATEGORIES)); } RdTestPlan p = new RdTestPlan(); apply(p, req); p.setStatus(req.status() == null || req.status().isBlank() ? "草稿" : req.status()); p.setCreatedAt(Instant.now()); p.setUpdatedAt(Instant.now()); return ApiResp.ok(repo.save(p)); } @PatchMapping("/{id}") @Transactional public ApiResp update(@PathVariable Long id, @RequestBody TestPlanRequest req) { RdTestPlan p = find(id); if ("完成".equals(p.getStatus()) || "失败".equals(p.getStatus())) { if (!"草稿".equals(req.status()) && !"执行中".equals(req.status())) { // 允许回退到草稿/执行中修改数据;否则只更新 testData/reportRef/issueRef if (req.testData() != null) p.setTestData(req.testData()); if (req.reportRef() != null) p.setReportRef(req.reportRef()); if (req.issueRef() != null) p.setIssueRef(req.issueRef()); if (req.defectCount() != null) p.setDefectCount(req.defectCount()); p.setUpdatedAt(Instant.now()); return ApiResp.ok(repo.save(p)); } } apply(p, req); p.setUpdatedAt(Instant.now()); return ApiResp.ok(repo.save(p)); } // ---------- 状态机 ---------- /** 草稿 → 执行中(开始执行,回填实际开始日期)。 */ @PostMapping("/{id}/start") @Transactional public ApiResp start(@PathVariable Long id) { RdTestPlan p = find(id); if (!"草稿".equals(p.getStatus())) { throw new ApiException(409, "仅草稿状态的测试计划可开始执行,当前为「" + p.getStatus() + "」"); } p.setStatus("执行中"); if (p.getActualStartDate() == null || p.getActualStartDate().isBlank()) { p.setActualStartDate(java.time.LocalDate.now().toString()); } p.setUpdatedAt(Instant.now()); return ApiResp.ok(repo.save(p)); } /** 执行中 → 完成(测试通过)。 */ @PostMapping("/{id}/complete") @Transactional public ApiResp complete(@PathVariable Long id, @RequestBody(required = false) CompleteRequest req) { RdTestPlan p = find(id); if (!"执行中".equals(p.getStatus())) { throw new ApiException(409, "仅执行中的测试计划可标记完成,当前为「" + p.getStatus() + "」"); } p.setStatus("完成"); p.setActualEndDate(java.time.LocalDate.now().toString()); if (req != null) { if (req.testData() != null) p.setTestData(req.testData()); if (req.reportRef() != null) p.setReportRef(req.reportRef()); if (req.defectCount() != null) p.setDefectCount(req.defectCount()); if (req.issueRef() != null) p.setIssueRef(req.issueRef()); } p.setUpdatedAt(Instant.now()); return ApiResp.ok(repo.save(p)); } /** 执行中 → 失败(测试不通过,缺陷关联问题管理)。 */ @PostMapping("/{id}/fail") @Transactional public ApiResp fail(@PathVariable Long id, @RequestBody(required = false) CompleteRequest req) { RdTestPlan p = find(id); if (!"执行中".equals(p.getStatus())) { throw new ApiException(409, "仅执行中的测试计划可标记失败,当前为「" + p.getStatus() + "」"); } p.setStatus("失败"); p.setActualEndDate(java.time.LocalDate.now().toString()); if (req != null) { if (req.testData() != null) p.setTestData(req.testData()); if (req.reportRef() != null) p.setReportRef(req.reportRef()); if (req.defectCount() != null) p.setDefectCount(req.defectCount()); if (req.issueRef() != null) p.setIssueRef(req.issueRef()); } p.setUpdatedAt(Instant.now()); return ApiResp.ok(repo.save(p)); } public record CompleteRequest(String testData, String reportRef, Integer defectCount, String issueRef) { } @DeleteMapping("/{id}") public ApiResp delete(@PathVariable Long id) { RdTestPlan p = find(id); if ("执行中".equals(p.getStatus())) { throw new ApiException(409, "执行中的测试计划不能直接删除,请先标记完成或失败"); } repo.deleteById(id); return ApiResp.ok(null); } // ---------- helpers ---------- private RdTestPlan find(Long id) { return repo.findById(id) .orElseThrow(() -> new NotFoundException("测试计划不存在:" + id)); } private void apply(RdTestPlan p, TestPlanRequest req) { if (req.devProjectId() != null) p.setDevProjectId(req.devProjectId()); if (req.prototypeOrderId() != null) p.setPrototypeOrderId(req.prototypeOrderId()); if (req.planName() != null && !req.planName().isBlank()) p.setPlanName(req.planName()); if (req.testCategory() != null) { if (!req.testCategory().isBlank() && !VALID_CATEGORIES.contains(req.testCategory())) { throw new ApiException(400, "testCategory 须为: " + String.join("/", VALID_CATEGORIES)); } p.setTestCategory(req.testCategory()); } if (req.testGoal() != null) p.setTestGoal(req.testGoal()); if (req.testMethod() != null) p.setTestMethod(req.testMethod()); if (req.passCriteria() != null) p.setPassCriteria(req.passCriteria()); if (req.planStartDate() != null) p.setPlanStartDate(req.planStartDate()); if (req.planEndDate() != null) p.setPlanEndDate(req.planEndDate()); if (req.actualStartDate() != null) p.setActualStartDate(req.actualStartDate()); if (req.actualEndDate() != null) p.setActualEndDate(req.actualEndDate()); if (req.tester() != null) p.setTester(req.tester()); if (req.status() != null && !req.status().isBlank()) { if (!VALID_STATUSES.contains(req.status())) { throw new ApiException(400, "status 须为: " + String.join("/", VALID_STATUSES)); } p.setStatus(req.status()); } if (req.testData() != null) p.setTestData(req.testData()); if (req.reportRef() != null) p.setReportRef(req.reportRef()); if (req.defectCount() != null) p.setDefectCount(req.defectCount()); if (req.issueRef() != null) p.setIssueRef(req.issueRef()); if (req.testRemark() != null) p.setTestRemark(req.testRemark()); if (req.createdBy() != null) p.setCreatedBy(req.createdBy()); } }