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.SupervisionLog; import com.kaidi.oa.repository.SupervisionLogRepository; 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; /** * Engineering supervision center (工程监理) supervision logs. logType is one of * 旁站 / 巡视 / 平行检验 / 监理通知 / 计量支付; handleStatus is one of 待处理 / * 处理中 / 已闭环, defaulting to 待处理 on create. */ @RestController @RequestMapping("/api/oa/supervision-logs") public class SupervisionLogController { private final SupervisionLogRepository logRepo; public SupervisionLogController(SupervisionLogRepository logRepo) { this.logRepo = logRepo; } @GetMapping public ApiResp> list(@RequestParam(required = false) String handleStatus) { List list; if (handleStatus != null && !handleStatus.isBlank()) { list = logRepo.findByHandleStatus(handleStatus); } else { list = logRepo.findAll(); } return ApiResp.ok(list); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(logRepo.findById(id) .orElseThrow(() -> new NotFoundException("supervision log not found: " + id))); } public record CreateSupervisionLogRequest( String projectName, String logType, String content, String problem, String handleStatus, String supervisor, String logDate) { } @PostMapping public ApiResp create(@RequestBody CreateSupervisionLogRequest req) { if (req.projectName() == null || req.projectName().isBlank()) { throw new ApiException(400, "projectName is required"); } SupervisionLog l = new SupervisionLog(); l.setProjectName(req.projectName()); l.setLogType(req.logType()); l.setContent(req.content()); l.setProblem(req.problem()); l.setHandleStatus(req.handleStatus() == null || req.handleStatus().isBlank() ? "待处理" : req.handleStatus()); l.setSupervisor(req.supervisor()); l.setLogDate(req.logDate()); l.setCreatedAt(Instant.now()); return ApiResp.ok(logRepo.save(l)); } /** PUT /{id} -> 编辑监理记录; 只覆盖请求体里给出的字段。 */ @PutMapping("/{id}") public ApiResp update(@PathVariable Long id, @RequestBody CreateSupervisionLogRequest req) { SupervisionLog l = logRepo.findById(id) .orElseThrow(() -> new NotFoundException("supervision log not found: " + id)); if (req.projectName() != null) { if (req.projectName().isBlank()) { throw new ApiException(400, "projectName is required"); } l.setProjectName(req.projectName()); } if (req.logType() != null) l.setLogType(req.logType()); if (req.content() != null) l.setContent(req.content()); if (req.problem() != null) l.setProblem(req.problem()); if (req.handleStatus() != null && !req.handleStatus().isBlank()) l.setHandleStatus(req.handleStatus()); if (req.supervisor() != null) l.setSupervisor(req.supervisor()); if (req.logDate() != null) l.setLogDate(req.logDate()); return ApiResp.ok(logRepo.save(l)); } /** DELETE /{id} -> 删除监理记录。 */ @DeleteMapping("/{id}") public ApiResp delete(@PathVariable Long id) { if (!logRepo.existsById(id)) { throw new NotFoundException("supervision log not found: " + id); } logRepo.deleteById(id); return ApiResp.ok(null); } }