Files
ERP/oa-backend/src/main/java/com/kaidi/oa/web/LabStocktakeController.java
T
QiufengandClaude Opus 4.8 5e51dc3f56 SNAPSHOT W7 已部署稳定态 — 凯迪ERP+OA一体化平台 (MET 73.3%)
恢复点(restore point)。别人改崩后可 git reset --hard 回到此提交。

== 此快照内容 ==
- 后端 oa-backend: 734 控制器 / 711 实体 (Spring Boot 3.2.5 + SQLite, 端口8091)
- 前端 modern-ui/app: Vue3+Vite, 约700页 (构建产物已在 oa-backend/src/main/resources/static)
- 数据库 oa-backend/data/oa.db: 含全部演示数据 (强制入库, 6.6MB)
- 交接文档 go.md + go-code-reference/endpoints/entities/database.md
- 多代理建设脚本 .claude/wf-*.js

== 状态 ==
- 对 凯迪科技ERP_20260507.xlsx 合规 MET ~73.3% (PARTIAL 75: 34可建+6种子/bug+35外部硬天花板)
- 安全: 5轮红队+5轮复检, default-deny分级鉴权, 连续零可利用
- W3~W7 累计补完436缺口; W8末轮(40缺口)为半成品(源码树可编译但未集成)
- 运行: cd oa-backend; java -jar build/libs/oa-backend-0.1.0.jar --server.port=8091; admin/123456

== 排除(gitignore, 可再生) ==
node_modules / oa-backend/build / .jdks / *.log / Backup-ERP-* / 弃用的OFBiz核心(只保留modern-ui)
完整文件夹备份见同目录 Backup-ERP-20260615-191517/ (含上述全部, 仅缺 node_modules)

时间戳: 20260615-191517

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 19:19:15 +08:00

185 lines
7.4 KiB
Java

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<LabStocktake>> 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<LabStocktake> get(@PathVariable Long id) {
return ApiResp.ok(must(id));
}
@GetMapping("/{id}/lines")
public ApiResp<List<LabStocktakeLine>> 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<LabStocktake> 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<Reagent> 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<LabStocktakeLine> 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<LabStocktake> 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;
}
}