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.LabStocktake; import com.kaidi.oa.domain.LabStocktakeLine; import com.kaidi.oa.domain.Reagent; import com.kaidi.oa.repository.LabStocktakeLineRepository; import com.kaidi.oa.repository.LabStocktakeRepository; import com.kaidi.oa.repository.ReagentRepository; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.time.Instant; import java.time.LocalDate; import java.util.List; /** * 实验室·库存盘点(试剂耗材与库存管理 §4 深化)。周期/移动端扫码盘点,自动生成盘盈盘亏报表。 * * - create 建盘点单:按 scope 把当前试剂账面库存快照成盘点明细行(盘点中); * - count 逐行录实盘数(支持扫码 byScan),自动算差异与盘盈/盘亏结果; * - finish 完成:汇总盘盈/盘亏项数,可选 apply 把账面库存调整为实盘数(应用盘点结果); * - lines 盘点明细(盘盈盘亏报表数据源)。 * * 写口默认受 default-deny(ADMIN/APPROVER) 保护。 */ @RestController @RequestMapping("/api/oa/lab-stocktakes") public class LabStocktakeController { private final LabStocktakeRepository stRepo; private final LabStocktakeLineRepository lineRepo; private final ReagentRepository reagentRepo; public LabStocktakeController(LabStocktakeRepository stRepo, LabStocktakeLineRepository lineRepo, ReagentRepository reagentRepo) { this.stRepo = stRepo; this.lineRepo = lineRepo; this.reagentRepo = reagentRepo; } @GetMapping public ApiResp> list(@RequestParam(required = false) String status) { if (status != null && !status.isBlank()) { return ApiResp.ok(stRepo.findByStatus(status)); } return ApiResp.ok(stRepo.findAll()); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(must(id)); } @GetMapping("/{id}/lines") public ApiResp> lines(@PathVariable Long id) { must(id); return ApiResp.ok(lineRepo.findByStocktakeIdOrderByIdAsc(id)); } public record CreateRequest(String scope, String operator, String stocktakeDate) { } /** 建盘点单并快照账面库存为明细行(scope=某分类 则只盘该分类,否则盘全部)。 */ @PostMapping @Transactional public ApiResp create(@RequestBody CreateRequest req) { LabStocktake st = new LabStocktake(); st.setStocktakeNo("PD-" + (stRepo.count() + 1)); st.setScope(req.scope() == null || req.scope().isBlank() ? "全部" : req.scope()); st.setOperator(req.operator()); st.setStocktakeDate(req.stocktakeDate() == null || req.stocktakeDate().isBlank() ? LocalDate.now().toString() : req.stocktakeDate()); st.setStatus("盘点中"); st.setProfitCount(0); st.setLossCount(0); st.setApplied(false); st.setCreatedAt(Instant.now()); LabStocktake saved = stRepo.save(st); List scope = ("全部".equals(saved.getScope())) ? reagentRepo.findAll() : reagentRepo.findByCategory(saved.getScope()); for (Reagent r : scope) { LabStocktakeLine ln = new LabStocktakeLine(); ln.setStocktakeId(saved.getId()); ln.setReagentId(r.getId()); ln.setReagentCode(r.getCode()); ln.setReagentName(r.getName()); ln.setBookQty(nz(r.getStockQty())); ln.setResult("未盘"); ln.setByScan(false); lineRepo.save(ln); } return ApiResp.ok(saved); } public record CountRequest(Long lineId, Long reagentId, Double countedQty, Boolean byScan) { } /** 录实盘数:按 lineId 或扫码 reagentId 定位行,写实盘数并算差异/结果。 */ @PostMapping("/{id}/count") @Transactional public ApiResp count(@PathVariable Long id, @RequestBody CountRequest req) { LabStocktake st = must(id); if (!"盘点中".equals(st.getStatus())) { throw new ApiException(409, "盘点已完成,不能再录"); } LabStocktakeLine ln = null; if (req.lineId() != null) { ln = lineRepo.findById(req.lineId()).orElse(null); } else if (req.reagentId() != null) { ln = lineRepo.findByStocktakeIdOrderByIdAsc(id).stream() .filter(x -> req.reagentId().equals(x.getReagentId())) .findFirst().orElse(null); } if (ln == null || !id.equals(ln.getStocktakeId())) { throw new NotFoundException("盘点明细行不存在"); } if (req.countedQty() == null || req.countedQty() < 0) { throw new ApiException(400, "实盘数(countedQty) 必须 >= 0"); } ln.setCountedQty(req.countedQty()); double diff = req.countedQty() - nz(ln.getBookQty()); ln.setDiff(round2(diff)); ln.setResult(diff > 1e-9 ? "盘盈" : (diff < -1e-9 ? "盘亏" : "一致")); ln.setByScan(Boolean.TRUE.equals(req.byScan())); return ApiResp.ok(lineRepo.save(ln)); } public record FinishRequest(boolean apply) { } /** 完成盘点:汇总盘盈/盘亏项数;apply=true 时把账面库存调整为实盘数。 */ @PostMapping("/{id}/finish") @Transactional public ApiResp finish(@PathVariable Long id, @RequestBody FinishRequest req) { LabStocktake st = must(id); if ("已完成".equals(st.getStatus())) { throw new ApiException(409, "盘点已完成"); } int profit = 0; int loss = 0; for (LabStocktakeLine ln : lineRepo.findByStocktakeIdOrderByIdAsc(id)) { if ("盘盈".equals(ln.getResult())) profit++; if ("盘亏".equals(ln.getResult())) loss++; if (req.apply() && ln.getCountedQty() != null && ("盘盈".equals(ln.getResult()) || "盘亏".equals(ln.getResult()))) { reagentRepo.findById(ln.getReagentId()).ifPresent(r -> { r.setStockQty(ln.getCountedQty()); reagentRepo.save(r); }); } } st.setProfitCount(profit); st.setLossCount(loss); st.setApplied(req.apply()); st.setStatus("已完成"); return ApiResp.ok(stRepo.save(st)); } // ---------- helpers ---------- private LabStocktake must(Long id) { return stRepo.findById(id) .orElseThrow(() -> new NotFoundException("stocktake not found: " + id)); } private static double nz(Double v) { return v == null ? 0.0 : v; } private static double round2(double v) { return Math.round(v * 100.0) / 100.0; } }