package com.zioinfo.mro.receiving; import com.zioinfo.mro.admin.AuditService; import com.zioinfo.mro.receiving.mapper.ReceivingMapper; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.List; import java.util.Set; /** * 입고 서비스 — 순수 입고 기록 + 검사결과 등록. 재고 연동 없음. */ @Service @RequiredArgsConstructor public class ReceivingService { private static final Set INSPECT_RESULTS = Set.of("PASS", "FAIL", "PENDING"); private final ReceivingMapper mapper; private final AuditService audit; public List list(String poNo, String inspectResult) { return mapper.findAll(poNo, inspectResult); } public MroReceiving get(Long id) { MroReceiving r = mapper.findById(id); if (r == null) throw new RuntimeException("ERR-RCV-404: 입고 없음"); return r; } public MroReceiving create(MroReceiving r, String actor) { if (r.getMaterialCode() == null || r.getMaterialCode().isBlank()) throw new IllegalArgumentException("ERR-RCV-400: materialCode 필수"); if (r.getReceivingNo() == null || r.getReceivingNo().isBlank()) { r.setReceivingNo("RCV-" + System.currentTimeMillis()); } if (mapper.countByNo(r.getReceivingNo()) > 0) { throw new RuntimeException("ERR-RCV-409: 중복 입고번호"); } if (r.getLocationCode() == null) r.setLocationCode("WH-MRO"); if (r.getUnit() == null) r.setUnit("EA"); if (r.getInspectResult() == null) r.setInspectResult("PENDING"); else if (!INSPECT_RESULTS.contains(r.getInspectResult().toUpperCase())) throw new IllegalArgumentException("ERR-RCV-400: inspectResult 은 PASS/FAIL/PENDING"); r.setReceivedBy(actor); r.setReceivedAt(LocalDateTime.now()); r.setCreatedBy(actor); mapper.insert(r); audit.log(actor, "RECEIVING_CREATE", r.getReceivingNo(), "po=" + r.getPoNo()); return mapper.findById(r.getId()); } /** 검사결과 등록 — PASS/FAIL. */ public MroReceiving inspect(Long id, String inspectResult, String actor) { MroReceiving cur = get(id); String s = inspectResult == null ? "" : inspectResult.trim().toUpperCase(); if (!"PASS".equals(s) && !"FAIL".equals(s)) { throw new IllegalArgumentException("ERR-RCV-400: inspectResult 은 PASS/FAIL"); } cur.setInspectResult(s); mapper.update(cur); audit.log(actor, "RECEIVING_INSPECT", cur.getReceivingNo(), "result=" + s); return mapper.findById(id); } }