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.StdApplication; import com.kaidi.oa.repository.StdApplicationRepository; 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.LocalDate; import java.time.Year; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 标准申请辅助(知识产权部,需求功能9 — 补深 LOGIC_GAP)。 * * 补深三项审计缺口: * (1) 模板化字段:预置标准名称/类型/起草单位/计划编号/技术内容/起草人/归口单位,附件含草案+编制说明; * (2) 进度阶段状态机:提交 → 受理 → 审查 → 报批 → 发布,流转时回填本阶段开始日并设默认预计耗时; * (3) 各阶段预计耗时自动计算 + 逾期提醒:按内置默认/自定义阶段耗时算预计完成日,alerts 端点列出 * 逾期或临近的在办标准申请。 * * 写口默认受保护(新控制器默认受 AuthInterceptor 保护),无需进财务前缀。 */ @RestController @RequestMapping("/api/oa/std-applications") public class StdApplicationController { private final StdApplicationRepository repo; public StdApplicationController(StdApplicationRepository repo) { this.repo = repo; } private static final List STAGES = List.of("提交", "受理", "审查", "报批", "发布"); /** 各阶段默认预计耗时(天)——内置标准制修订经验值,可在流转时覆盖。 */ private static final Map DEFAULT_DAYS = Map.of( "提交", 10, "受理", 20, "审查", 60, "报批", 30, "发布", 15); // ---------- 台账 CRUD ---------- @GetMapping public ApiResp> list(@RequestParam(required = false) String stage, @RequestParam(required = false) String stdType) { if (stage != null && !stage.isBlank()) { return ApiResp.ok(repo.findByStage(stage)); } if (stdType != null && !stdType.isBlank()) { return ApiResp.ok(repo.findByStdType(stdType)); } return ApiResp.ok(repo.findAll()); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(find(id)); } public record StdRequest(String stdName, String stdType, String draftingUnit, String planNo, String techContent, String drafters, String supervisor, String attachments, Long rdProjectId, String owner, String remark) { } @PostMapping @Transactional public ApiResp create(@RequestBody StdRequest req) { if (req.stdName() == null || req.stdName().isBlank()) { throw new ApiException(400, "标准名称不能为空"); } StdApplication s = new StdApplication(); s.setCode(nextCode()); apply(s, req); s.setStage("提交"); s.setStageStartDate(LocalDate.now().toString()); s.setStageExpectedDays(DEFAULT_DAYS.get("提交")); s.setCreatedAt(java.time.Instant.now()); s.setUpdatedAt(java.time.Instant.now()); return ApiResp.ok(repo.save(s)); } @PatchMapping("/{id}") @Transactional public ApiResp update(@PathVariable Long id, @RequestBody StdRequest req) { StdApplication s = find(id); apply(s, req); s.setUpdatedAt(java.time.Instant.now()); return ApiResp.ok(repo.save(s)); } @DeleteMapping("/{id}") @Transactional public ApiResp delete(@PathVariable Long id) { StdApplication s = find(id); if (!"提交".equals(s.getStage())) { throw new ApiException(409, "已进入受理及以后阶段的标准申请不可删除"); } repo.deleteById(id); return ApiResp.ok(null); } // ---------- 阶段流转(状态机 + 预计耗时) ---------- public record AdvanceRequest(Integer expectedDays, String note) { } /** 进入下一阶段(提交→受理→审查→报批→发布),回填本阶段开始日并设预计耗时(默认/自定义)。 */ @PostMapping("/{id}/advance") @Transactional public ApiResp advance(@PathVariable Long id, @RequestBody AdvanceRequest req) { StdApplication s = find(id); int idx = STAGES.indexOf(s.getStage()); if (idx < 0 || idx >= STAGES.size() - 1) { throw new ApiException(409, "当前阶段「" + s.getStage() + "」已是终态,无法继续推进"); } String next = STAGES.get(idx + 1); s.setStage(next); s.setStageStartDate(LocalDate.now().toString()); s.setStageExpectedDays(req.expectedDays() != null && req.expectedDays() > 0 ? req.expectedDays() : DEFAULT_DAYS.getOrDefault(next, 30)); s.setUpdatedAt(java.time.Instant.now()); return ApiResp.ok(repo.save(s)); } // ---------- 各阶段预计完成日 + 逾期提醒 ---------- public record StagePlan(String stage, int expectedDays, String startDate, String expectedDoneDate, long daysElapsed, long daysRemaining, String level) { } /** 某标准申请的当前阶段进度计算:已耗天数 / 剩余天数 / 预计完成日 / 提醒级别。 */ @GetMapping("/{id}/progress") public ApiResp progress(@PathVariable Long id) { StdApplication s = find(id); return ApiResp.ok(planOf(s)); } public record StdAlert(Long id, String code, String stdName, String stage, String expectedDoneDate, long daysRemaining, String level, String owner) { } /** * 在办标准申请的阶段逾期/临近提醒:对非「发布」终态、阶段预计完成日落在窗口内的申请, * 给出 逾期 / 紧急(<=3天) / 临近(<=7天) / 提醒(<=days) 分级。 */ @GetMapping("/alerts") public ApiResp> alerts(@RequestParam(required = false, defaultValue = "15") int days) { List out = new ArrayList<>(); for (StdApplication s : repo.findAll()) { if ("发布".equals(s.getStage())) { continue; } StagePlan plan = planOf(s); if (plan.expectedDoneDate() == null) { continue; } if (plan.daysRemaining() > days) { continue; } out.add(new StdAlert(s.getId(), s.getCode(), s.getStdName(), s.getStage(), plan.expectedDoneDate(), plan.daysRemaining(), plan.level(), s.getOwner())); } out.sort((a, b) -> Long.compare(a.daysRemaining(), b.daysRemaining())); return ApiResp.ok(out); } // ---------- helpers ---------- private StagePlan planOf(StdApplication s) { int expected = s.getStageExpectedDays() == null ? DEFAULT_DAYS.getOrDefault(s.getStage(), 30) : s.getStageExpectedDays(); LocalDate start = parseDateOrNull(s.getStageStartDate()); if (start == null) { return new StagePlan(s.getStage(), expected, s.getStageStartDate(), null, 0, 0, "未开始"); } LocalDate done = start.plusDays(expected); LocalDate today = LocalDate.now(); long elapsed = ChronoUnit.DAYS.between(start, today); long remaining = ChronoUnit.DAYS.between(today, done); String level = "发布".equals(s.getStage()) ? "已完成" : remaining < 0 ? "逾期" : remaining <= 3 ? "紧急" : remaining <= 7 ? "临近" : "正常"; return new StagePlan(s.getStage(), expected, s.getStageStartDate(), done.toString(), elapsed, remaining, level); } private StdApplication find(Long id) { return repo.findById(id) .orElseThrow(() -> new NotFoundException("标准申请不存在:" + id)); } private void apply(StdApplication s, StdRequest req) { if (req.stdName() != null && !req.stdName().isBlank()) s.setStdName(req.stdName()); if (req.stdType() != null) s.setStdType(req.stdType() == null || req.stdType().isBlank() ? "团体" : req.stdType()); if (req.draftingUnit() != null) s.setDraftingUnit(req.draftingUnit()); if (req.planNo() != null) s.setPlanNo(req.planNo()); if (req.techContent() != null) s.setTechContent(req.techContent()); if (req.drafters() != null) s.setDrafters(req.drafters()); if (req.supervisor() != null) s.setSupervisor(req.supervisor()); if (req.attachments() != null) s.setAttachments(req.attachments()); if (req.rdProjectId() != null) s.setRdProjectId(req.rdProjectId()); if (req.owner() != null) s.setOwner(req.owner()); if (req.remark() != null) s.setRemark(req.remark()); if (s.getStdType() == null || s.getStdType().isBlank()) { s.setStdType("团体"); } } private String nextCode() { String prefix = "STD-" + Year.now().getValue() + "-"; long n = repo.countByCodeStartingWith(prefix) + 1; return prefix + String.format("%03d", n); } private static LocalDate parseDateOrNull(String s) { if (s == null || s.isBlank()) { return null; } try { String t = s.trim(); return LocalDate.parse(t.substring(0, Math.min(10, t.length()))); } catch (DateTimeParseException | IndexOutOfBoundsException e) { return null; } } }