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.Archive; import com.kaidi.oa.domain.ArchiveVersion; import com.kaidi.oa.repository.ArchiveRepository; import com.kaidi.oa.repository.ArchiveVersionRepository; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; 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.RestController; import java.time.Instant; import java.time.LocalDate; import java.util.List; /** * 资料室·电子档案版本控制(Module 3「版本控制」缺口补深)。 * 同一档案可存在多个版本(图纸 V1.0、V2.0);当前有效版本标记「现行」, * 旧版本自动置「作废」但保留(保留历史可追溯)。 * 补审计「Archive 无 version/现行/作废字段、DocumentController 恒置 setVersion(1)」缺口。 * *

规则: *

* 写口受 default-deny(ADMIN/APPROVER) 保护。 */ @RestController @RequestMapping("/api/oa/archive-versions") public class ArchiveVersionController { private final ArchiveVersionRepository versionRepo; private final ArchiveRepository archiveRepo; public ArchiveVersionController(ArchiveVersionRepository versionRepo, ArchiveRepository archiveRepo) { this.versionRepo = versionRepo; this.archiveRepo = archiveRepo; } /** 某档案的全版本历史(降序,最新在前)。 */ @GetMapping("/by-archive/{archiveId}") public ApiResp> listByArchive(@PathVariable Long archiveId) { return ApiResp.ok(versionRepo.findByArchiveIdOrderByIdDesc(archiveId)); } /** 某档案的现行版本(唯一)。 */ @GetMapping("/current/{archiveId}") public ApiResp current(@PathVariable Long archiveId) { return ApiResp.ok(versionRepo.findByArchiveIdAndIsCurrentTrue(archiveId) .orElseThrow(() -> new NotFoundException("该档案暂无现行版本:archiveId=" + archiveId))); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(require(id)); } public record PublishRequest( Long archiveId, String versionNo, String changeNote, Long storedFileId, String fileName, String author) { } /** * 发布新版本:自动把同一档案下所有现行版本置「作废」,新版置「现行」。 * versionNo 形如 V1.0 / V2.0,若不填则自动生成(V{当前数量+1}.0)。 */ @PostMapping("/publish") @Transactional public ApiResp publish(@RequestBody PublishRequest req) { if (req.archiveId() == null) { throw new ApiException(400, "档案 id(archiveId) 不能为空"); } Archive arch = archiveRepo.findById(req.archiveId()) .orElseThrow(() -> new NotFoundException("archive not found: " + req.archiveId())); // 旧现行版本全部置「作废」 List old = versionRepo.findByArchiveIdOrderByIdDesc(req.archiveId()); for (ArchiveVersion ov : old) { if (Boolean.TRUE.equals(ov.getIsCurrent())) { ov.setIsCurrent(Boolean.FALSE); ov.setStatus("作废"); versionRepo.save(ov); } } // 自动生成版本号 String verNo = req.versionNo(); if (verNo == null || verNo.isBlank()) { long cnt = versionRepo.countByArchiveId(req.archiveId()); verNo = "V" + (cnt + 1) + ".0"; } ArchiveVersion v = new ArchiveVersion(); v.setArchiveId(arch.getId()); v.setArchiveTitle(arch.getTitle()); v.setVersionNo(verNo.trim()); v.setIsCurrent(Boolean.TRUE); v.setStatus("现行"); v.setChangeNote(req.changeNote()); v.setStoredFileId(req.storedFileId()); v.setFileName(req.fileName()); v.setAuthor(req.author()); v.setVersionDate(LocalDate.now().toString()); v.setCreatedAt(Instant.now()); return ApiResp.ok(versionRepo.save(v)); } /** 手动作废某版本(现行版→作废;其他状态版本直接设作废)。 */ @PostMapping("/{id}/deprecate") public ApiResp deprecate(@PathVariable Long id) { ArchiveVersion v = require(id); if ("作废".equals(v.getStatus())) { throw new ApiException(409, "该版本已是「作废」状态"); } v.setIsCurrent(Boolean.FALSE); v.setStatus("作废"); return ApiResp.ok(versionRepo.save(v)); } private ArchiveVersion require(Long id) { return versionRepo.findById(id) .orElseThrow(() -> new NotFoundException("archive version not found: " + id)); } }