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.DesignChange; import com.kaidi.oa.repository.DesignChangeRepository; import org.springframework.web.bind.annotation.DeleteMapping; 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.PutMapping; 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; /** * Design research center (设计研究中心) design changes. changeType is one of * 设计变更 / 技术核定 / 工程洽商; status is one of 申请 / 审批中 / 已实施 / 已驳回, * defaulting to 申请 on create. */ @RestController @RequestMapping("/api/oa/design-changes") public class DesignChangeController { private final DesignChangeRepository changeRepo; public DesignChangeController(DesignChangeRepository changeRepo) { this.changeRepo = changeRepo; } @GetMapping public ApiResp> list(@RequestParam(required = false) String status) { List list; if (status != null && !status.isBlank()) { list = changeRepo.findByStatus(status); } else { list = changeRepo.findAll(); } return ApiResp.ok(list); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(changeRepo.findById(id) .orElseThrow(() -> new NotFoundException("design change not found: " + id))); } public record CreateDesignChangeRequest( String docName, String projectName, String reason, String changeType, String impact, String status, String applicant) { } @PostMapping public ApiResp create(@RequestBody CreateDesignChangeRequest req) { boolean noDocName = req.docName() == null || req.docName().isBlank(); boolean noProjectName = req.projectName() == null || req.projectName().isBlank(); if (noDocName && noProjectName) { throw new ApiException(400, "docName or projectName is required"); } DesignChange c = new DesignChange(); c.setDocName(req.docName()); c.setProjectName(req.projectName()); c.setReason(req.reason()); c.setChangeType(req.changeType()); c.setImpact(req.impact()); c.setStatus(req.status() == null || req.status().isBlank() ? "申请" : req.status()); c.setApplicant(req.applicant()); c.setCreatedAt(Instant.now()); return ApiResp.ok(changeRepo.save(c)); } /** PUT /{id} -> 编辑设计变更; 只覆盖请求体里给出的字段。 */ @PutMapping("/{id}") public ApiResp update(@PathVariable Long id, @RequestBody CreateDesignChangeRequest req) { DesignChange c = changeRepo.findById(id) .orElseThrow(() -> new NotFoundException("design change not found: " + id)); if (req.docName() != null) c.setDocName(req.docName()); if (req.projectName() != null) c.setProjectName(req.projectName()); if (req.reason() != null) c.setReason(req.reason()); if (req.changeType() != null) c.setChangeType(req.changeType()); if (req.impact() != null) c.setImpact(req.impact()); if (req.status() != null && !req.status().isBlank()) c.setStatus(req.status()); if (req.applicant() != null) c.setApplicant(req.applicant()); return ApiResp.ok(changeRepo.save(c)); } /** DELETE /{id} -> 删除设计变更。 */ @DeleteMapping("/{id}") public ApiResp delete(@PathVariable Long id) { if (!changeRepo.existsById(id)) { throw new NotFoundException("design change not found: " + id); } changeRepo.deleteById(id); return ApiResp.ok(null); } }