作为 xiaoqu-agent-service(MCP 服务) 的开发参考。新系统与旧系统零代码耦合, 仅照搬其行级数据权限规则与数据源结构。设计方案见 xiaoqu-franchisee/doc/agent-mcp-service-design.md
784 lines
35 KiB
Markdown
784 lines
35 KiB
Markdown
# 详细设计:出入库单据回写剩余库存数(全量版)
|
||
|
||
> **需求**:所有变更 `warehouse_stock_manage.current_inventory` / `out_of_stock` 的库存操作,在完成后把该物料在该仓库的**剩余库存数**回写到关联单据明细,便于后续查账、对库存、追溯每一张单据执行后的即时库存快照。
|
||
> **涉及仓库**:
|
||
> - `xiaoqu-publicbeanandmap`(公共模块,新增/增强回写能力)
|
||
> - `xiaoqu-franchisee`(本仓库,所有库存调用点接入回写)
|
||
> **涉及数据库**:业务库 `xiaoqu_franchisee_d`(dev/test)、`xiaoqu_franchisee`(prd)。
|
||
> **版本**:基于 2026-06-17 仓库快照梳理。
|
||
|
||
---
|
||
|
||
## 1. 名词定义
|
||
|
||
| 名词 | 说明 |
|
||
|------|------|
|
||
| **剩余库存数** | 单据完成库存加减后,`warehouse_stock_manage.current_inventory` 在该物料+该仓库下的最新值。 |
|
||
| **出库中库存数** | 单据完成 `out_of_stock` 加减后,`warehouse_stock_manage.out_of_stock` 在该物料+该仓库下的最新值。 |
|
||
| **单据明细** | 入库明细 `warehouse_in_details`、出库明细 `warehouse_out_details`。 |
|
||
| **库存变更** | 对 `warehouse_stock_manage.current_inventory` / `out_of_stock` 的加/减操作,调用 `warehouseStockManageService.updateStock(...) / updateOutOfStock(...)`。 |
|
||
|
||
---
|
||
|
||
## 2. 现状分析
|
||
|
||
### 2.1 表结构已具备字段
|
||
|
||
`warehouse_in_details` 与 `warehouse_out_details` 已存在如下字段(无需新建表/字段):
|
||
|
||
```sql
|
||
-- warehouse_in_details 已包含
|
||
current_inventory int(11) DEFAULT '0' COMMENT '当前库存(快照)',
|
||
|
||
-- warehouse_out_details 已包含
|
||
current_inventory int(11) DEFAULT '0' COMMENT '当前库存(快照)',
|
||
out_of_stock int(11) DEFAULT '0' COMMENT '出库中库存(快照)',
|
||
```
|
||
|
||
> 注:`current_inventory` 在部分历史代码里被当作"单据创建时的库存"使用;本期需求是要把它明确为"**单据完成库存更新后的剩余库存**",因此需要补全/修正写入时机。
|
||
|
||
### 2.2 库存更新方法(公共模块)
|
||
|
||
公共模块 `me.iiv.iivframework.service.s2.WarehouseStockManageService` 现有方法:
|
||
|
||
| 方法 | 作用 | 是否影响 current_inventory | 是否影响 out_of_stock | 是否需回写 |
|
||
|------|------|----------------------------|----------------------|-----------|
|
||
| `updateStock(...)` | 加减当前库存 | ✅ | ❌ | ✅ 回写 current_inventory |
|
||
| `updateOutOfStock(...)` | 加减出库中库存 | ❌ | ✅ | 视需求回写 out_of_stock |
|
||
| `updateBatteryInventory(...)` | 加减费用化电池库存 | ❌(只改 battery_inventory) | ❌ | 本期不回写 |
|
||
| `newUpdateStock(...)` | 操作 SN 设备库存,仅电池费用化时改 battery_inventory | ❌ | ❌ | 本期不回写 |
|
||
| `cancelUpdateStock(...)` | 撤销 SN 出库状态 | ❌ | ❌ | 本期不回写 |
|
||
|
||
### 2.3 全量缺口清单
|
||
|
||
经扫描两个仓库,涉及 `warehouseStockManageService` 的库存更新调用点共 **46 处**。其中:
|
||
- 影响 `current_inventory` 的 `updateStock(...)` 共 **20 处**
|
||
- 影响 `out_of_stock` 的 `updateOutOfStock(...)` 共 **14 处**
|
||
- 不影响 `current_inventory` 的 `updateBatteryInventory` / `newUpdateStock` / `cancelUpdateStock` 共 **12 处**(本期不回写)
|
||
|
||
#### `updateStock(...)` — 影响 current_inventory,**需要回写**(20 处)
|
||
|
||
**`xiaoqu-franchisee`(15 处)**
|
||
|
||
| # | 文件 | 行号 | 场景 | 当前是否回写 |
|
||
|---|------|------|------|-------------|
|
||
| 1 | `WarehouseInController.java` | 2122 | 入库作废扣库存 | ❌ |
|
||
| 2 | `WarehouseInController.java` | 2411 | 入库确认加库存 | ❌ |
|
||
| 3 | `WarehouseOutController.java` | 3081 | 出库单作废:恢复出库仓库库存 | ❌ |
|
||
| 4 | `WarehouseOutController.java` | 3172 | 出库单作废:扣减关联入库单库存 | ❌ |
|
||
| 5 | `WarehouseOutController.java` | 3911 | 出库确认扣库存 | ⚠️ 部分 |
|
||
| 6 | `WarehouseAllocationController.java` | 3649 | 调拨/划转驳回恢复出库仓库库存 | ❌ |
|
||
| 7 | `WarehouseAllocationController.java` | 3916 | 调拨/划转作废:出库仓库加库存 | ❌ |
|
||
| 8 | `WarehouseAllocationController.java` | 3917 | 调拨/划转作废:入库仓库扣库存 | ❌ |
|
||
| 9 | `WarehousePublicService.java` | 868 | 调拨/划转入库加库存 | ⚠️ key 错误 |
|
||
| 10 | `WarehousePublicService.java` | 1114 | 调拨/划转出库扣库存(SN 需汇总后回写) | ⚠️ key 错误 |
|
||
| 11 | `PublicService.java` | 1808 | 安装/工单出库扣库存 | ❌ |
|
||
| 12 | `PublicService.java` | 1900 | 安装/工单出库扣库存 | ❌ |
|
||
| 13 | `XxlJobTask.java` | 178 | 自动出库任务扣库存 | ❌ |
|
||
| 14 | `AutoOutWarehouseTask.java` | 400 | 自动出库任务扣库存 | ❌ |
|
||
| 15 | `AutoOutWarehouseTask.java` | 986 | 自动出库任务扣库存 | ❌ |
|
||
|
||
**`xiaoqu-publicbeanandmap`(5 处)**
|
||
|
||
| # | 文件 | 行号 | 场景 | 当前是否回写 |
|
||
|---|------|------|------|-------------|
|
||
| 16 | `service/s2/InstallOrderPublicService.java` | 223 | 装机出库扣库存(SN 需汇总后回写) | ❌ |
|
||
| 17 | `service/s2/Impl/WarehouseAllocationServiceImpl.java` | 261 | 调拨/划转入库加库存 | ❌ |
|
||
| 18 | `service/s2/Impl/WarehouseAllocationServiceImpl.java` | 513 | 调拨/划转出库扣库存(SN 需汇总后回写) | ❌ |
|
||
| 19 | `service/s2/Impl/PickupOrderServiceImpl.java` | 478 | 取货工单入库加库存(仅 SN 子明细) | ❌ |
|
||
| 20 | `service/s2/Impl/PickupOrderServiceImpl.java` | 907 | 取货工单入库加库存(仅 SN 子明细) | ❌ |
|
||
|
||
#### `updateOutOfStock(...)` — 影响 out_of_stock,**视需求回写**(14 处,全在 `xiaoqu-franchisee`)
|
||
|
||
| # | 文件 | 行号 | 场景 |
|
||
|---|------|------|------|
|
||
| 1 | `PublicService.java` | 1809 | 安装/工单出库扣减出库中库存 |
|
||
| 2 | `PublicService.java` | 1901 | 安装/工单出库扣减出库中库存 |
|
||
| 3 | `WarehouseOutController.java` | 1410 | 出库中库存预占 |
|
||
| 4 | `WarehouseOutController.java` | 1812 | 出库中库存预占 |
|
||
| 5 | `WarehouseOutController.java` | 2049 | 出库中库存预占 |
|
||
| 6 | `WarehouseOutController.java` | 2243 | 出库中库存预占 |
|
||
| 7 | `WarehouseOutController.java` | 2507 | 出库中库存预占 |
|
||
| 8 | `WarehouseOutController.java` | 2775 | 调拨/划转出库中库存 |
|
||
| 9 | `WarehouseOutController.java` | 2988 | 调拨/划转出库中库存 |
|
||
| 10 | `WarehouseOutController.java` | 3463 | 出库确认扣减出库中库存 |
|
||
| 11 | `WarehouseOutController.java` | 3805 | SN 重新分组恢复出库中库存 |
|
||
| 12 | `WarehouseOutController.java` | 3913 | 出库确认扣减出库中库存 |
|
||
| 13 | `WarehouseAllocationController.java` | 1296 | 调拨提交预占出库中库存 |
|
||
| 14 | `WarehouseAllocationController.java` | 3641 | 调拨驳回恢复出库中库存 |
|
||
|
||
#### `updateBatteryInventory(...)` / `newUpdateStock(...)` / `cancelUpdateStock(...)`
|
||
|
||
- 这些操作不改变 `current_inventory`,本期**不回写**。
|
||
- 如后续业务需要记录 `battery_inventory` 快照,可另行扩展。
|
||
|
||
---
|
||
|
||
## 3. 方案设计
|
||
|
||
### 3.1 核心思路
|
||
|
||
采用 **"公共模块增强 + 调用点传入单据明细"** 的方案:
|
||
|
||
1. 在 `xiaoqu-publicbeanandmap` 的 `WarehouseStockManageService` 中新增重载方法,支持传入 `WarehouseInDetails` / `WarehouseOutDetails`。
|
||
2. 方法内部完成库存更新后,立即查询最新库存,并**只更新**单据明细的 `current_inventory` / `out_of_stock` 字段。
|
||
3. `xiaoqu-franchisee` 与 `xiaoqu-publicbeanandmap` 所有调用 `updateStock(...)` 的地方,如果能拿到对应单据明细,就改调新的重载方法。
|
||
4. 对于 `updateOutOfStock(...)`,同样新增重载,用于需要记录 `out_of_stock` 快照的场景。
|
||
|
||
```
|
||
调用方
|
||
│
|
||
▼
|
||
warehouseStockManageService.updateStock(mdId, whId, count, type, detail)
|
||
│
|
||
├─ 1. 执行原子 SQL 更新 warehouse_stock_manage
|
||
│
|
||
├─ 2. SELECT 最新 current_inventory
|
||
│
|
||
└─ 3. UPDATE 单据明细 current_inventory = 最新值
|
||
```
|
||
|
||
### 3.2 为什么不改方法返回值?
|
||
|
||
- 如果直接修改 `updateStock(...)` 返回类型(`boolean` → `Integer`),所有调用点编译都会报错,风险高、回归大。
|
||
- 采用**重载 + 传入明细**的方式,原有 4 参数方法签名不变,未改造的调用点行为完全不变,可分批灰度接入。
|
||
|
||
### 3.3 公共模块新增接口
|
||
|
||
在 `xiaoqu-publicbeanandmap/src/main/java/me/iiv/iivframework/service/s2/WarehouseStockManageService.java` 中增加:
|
||
|
||
```java
|
||
/**
|
||
* 更新库存并回写到入库明细
|
||
*
|
||
* @param materielDefinitionId 物料定义 ID
|
||
* @param warehouseId 仓库 ID
|
||
* @param changeCount 变更数量
|
||
* @param operation 0=减库存,1=加库存
|
||
* @param inDetail 入库明细(非空时回写 current_inventory)
|
||
* @return 是否更新成功
|
||
*/
|
||
boolean updateStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseInDetails inDetail);
|
||
|
||
/**
|
||
* 更新库存并回写到出库明细
|
||
*
|
||
* @param materielDefinitionId 物料定义 ID
|
||
* @param warehouseId 仓库 ID
|
||
* @param changeCount 变更数量
|
||
* @param operation 0=减库存,1=加库存
|
||
* @param outDetail 出库明细(非空时回写 current_inventory)
|
||
* @return 是否更新成功
|
||
*/
|
||
boolean updateStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseOutDetails outDetail);
|
||
|
||
/**
|
||
* 更新库存并回写到出库明细(同时回写 out_of_stock)
|
||
*
|
||
* @param materielDefinitionId 物料定义 ID
|
||
* @param warehouseId 仓库 ID
|
||
* @param changeCount 变更数量
|
||
* @param operation 0=减库存,1=加库存
|
||
* @param outDetail 出库明细(非空时回写 current_inventory)
|
||
* @param writeOutOfStock 是否同时查询并回写 out_of_stock
|
||
* @return 是否更新成功
|
||
*/
|
||
boolean updateStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseOutDetails outDetail, boolean writeOutOfStock);
|
||
|
||
/**
|
||
* 更新出库中库存并回写到出库明细
|
||
*
|
||
* @param materielDefinitionId 物料定义 ID
|
||
* @param warehouseId 仓库 ID
|
||
* @param changeCount 变更数量
|
||
* @param operation 0=减出库中,1=加出库中
|
||
* @param outDetail 出库明细(非空时回写 out_of_stock)
|
||
* @return 是否更新成功
|
||
*/
|
||
boolean updateOutOfStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseOutDetails outDetail);
|
||
```
|
||
|
||
### 3.4 公共模块实现示例
|
||
|
||
在 `WarehouseStockManageServiceImpl` 中新增实现:
|
||
|
||
```java
|
||
@Autowired
|
||
private WarehouseInDetailsService warehouseInDetailsService;
|
||
@Autowired
|
||
private WarehouseOutDetailsService warehouseOutDetailsService;
|
||
|
||
@Override
|
||
public boolean updateStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseInDetails inDetail) {
|
||
boolean result = updateStock(materielDefinitionId, warehouseId, changeCount, operation);
|
||
if (result && inDetail != null && inDetail.getId() != null) {
|
||
writeBackCurrentInventoryToInDetail(materielDefinitionId, warehouseId, inDetail.getId());
|
||
}
|
||
return result;
|
||
}
|
||
|
||
@Override
|
||
public boolean updateStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseOutDetails outDetail) {
|
||
return updateStock(materielDefinitionId, warehouseId, changeCount, operation, outDetail, false);
|
||
}
|
||
|
||
@Override
|
||
public boolean updateStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseOutDetails outDetail, boolean writeOutOfStock) {
|
||
boolean result = updateStock(materielDefinitionId, warehouseId, changeCount, operation);
|
||
if (result && outDetail != null && outDetail.getId() != null) {
|
||
writeBackCurrentInventoryToOutDetail(materielDefinitionId, warehouseId, outDetail.getId(), writeOutOfStock);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
@Override
|
||
public boolean updateOutOfStock(Long materielDefinitionId, Long warehouseId, Integer changeCount, Integer operation, WarehouseOutDetails outDetail) {
|
||
boolean result = updateOutOfStock(materielDefinitionId, warehouseId, changeCount, operation);
|
||
if (result && outDetail != null && outDetail.getId() != null) {
|
||
writeBackOutOfStockToOutDetail(materielDefinitionId, warehouseId, outDetail.getId());
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 查询最新库存并回写入库明细 current_inventory
|
||
*/
|
||
private void writeBackCurrentInventoryToInDetail(Long materielDefinitionId, Long warehouseId, Long detailId) {
|
||
QueryWrapper<WarehouseStockManage> wrapper = Wrappers.query();
|
||
wrapper.eq("materiel_definition_id", materielDefinitionId);
|
||
wrapper.eq("warehouse_id", warehouseId);
|
||
wrapper.eq("soft_delete", 0);
|
||
wrapper.last(" limit 1");
|
||
WarehouseStockManage stock = getOne(wrapper);
|
||
Integer currentInventory = stock == null ? 0 : stock.getCurrentInventory();
|
||
|
||
UpdateWrapper<WarehouseInDetails> updateWrapper = Wrappers.update();
|
||
updateWrapper.eq("id", detailId);
|
||
updateWrapper.set("current_inventory", currentInventory);
|
||
warehouseInDetailsService.update(updateWrapper);
|
||
}
|
||
|
||
/**
|
||
* 查询最新库存并回写出库明细 current_inventory(可选 out_of_stock)
|
||
*/
|
||
private void writeBackCurrentInventoryToOutDetail(Long materielDefinitionId, Long warehouseId, Long detailId, boolean writeOutOfStock) {
|
||
QueryWrapper<WarehouseStockManage> wrapper = Wrappers.query();
|
||
wrapper.eq("materiel_definition_id", materielDefinitionId);
|
||
wrapper.eq("warehouse_id", warehouseId);
|
||
wrapper.eq("soft_delete", 0);
|
||
wrapper.last(" limit 1");
|
||
WarehouseStockManage stock = getOne(wrapper);
|
||
Integer currentInventory = stock == null ? 0 : stock.getCurrentInventory();
|
||
|
||
UpdateWrapper<WarehouseOutDetails> updateWrapper = Wrappers.update();
|
||
updateWrapper.eq("id", detailId);
|
||
updateWrapper.set("current_inventory", currentInventory);
|
||
if (writeOutOfStock && stock != null) {
|
||
updateWrapper.set("out_of_stock", stock.getOutOfStock());
|
||
}
|
||
warehouseOutDetailsService.update(updateWrapper);
|
||
}
|
||
|
||
/**
|
||
* 查询最新出库中库存并回写出库明细 out_of_stock
|
||
*/
|
||
private void writeBackOutOfStockToOutDetail(Long materielDefinitionId, Long warehouseId, Long detailId) {
|
||
QueryWrapper<WarehouseStockManage> wrapper = Wrappers.query();
|
||
wrapper.eq("materiel_definition_id", materielDefinitionId);
|
||
wrapper.eq("warehouse_id", warehouseId);
|
||
wrapper.eq("soft_delete", 0);
|
||
wrapper.last(" limit 1");
|
||
WarehouseStockManage stock = getOne(wrapper);
|
||
Integer outOfStock = stock == null ? 0 : stock.getOutOfStock();
|
||
|
||
UpdateWrapper<WarehouseOutDetails> updateWrapper = Wrappers.update();
|
||
updateWrapper.eq("id", detailId);
|
||
updateWrapper.set("out_of_stock", outOfStock);
|
||
warehouseOutDetailsService.update(updateWrapper);
|
||
}
|
||
```
|
||
|
||
> 使用 `UpdateWrapper` 只更新快照字段,不触碰 `modify_id` / `modify_time`,由调用方自行维护审计字段。
|
||
|
||
### 3.5 独立回写工具方法(供 SN 汇总后使用)
|
||
|
||
对于 SN 物料,汇总明细在 `updateStock(...)` 之后生成。公共模块同时暴露以下独立方法,供调用方在汇总完成后回写汇总明细:
|
||
|
||
```java
|
||
/**
|
||
* 将当前库存回写到指定入库明细
|
||
*/
|
||
public void writeBackCurrentInventoryToInDetail(Long materielDefinitionId, Long warehouseId, Long detailId);
|
||
|
||
/**
|
||
* 将当前库存回写到指定出库明细
|
||
*/
|
||
public void writeBackCurrentInventoryToOutDetail(Long materielDefinitionId, Long warehouseId, Long detailId);
|
||
|
||
/**
|
||
* 将当前库存和出库中库存回写到指定出库明细
|
||
*/
|
||
public void writeBackCurrentInventoryToOutDetail(Long materielDefinitionId, Long warehouseId, Long detailId, boolean writeOutOfStock);
|
||
|
||
/**
|
||
* 将出库中库存回写到指定出库明细
|
||
*/
|
||
public void writeBackOutOfStockToOutDetail(Long materielDefinitionId, Long warehouseId, Long detailId);
|
||
```
|
||
|
||
> 若不希望暴露私有方法,可在 `WarehousePublicService` / `WarehouseAllocationServiceImpl` / `InstallOrderPublicService` 中自行用 `UpdateWrapper` 更新。
|
||
|
||
### 3.6 并发控制
|
||
|
||
| 风险 | 控制手段 |
|
||
|------|----------|
|
||
| 同一物料同一仓库并发更新 | `updateStock()` / `updateOutOfStock()` 内部使用 SQL 原子更新,并发安全。 |
|
||
| 查询最新库存瞬间被其他请求修改 | 接受。单据回写的是"该单据完成后那一刻"的库存快照;极小时间差内的并发变动会在另一张单据里体现,不影响对账。 |
|
||
| 同一单据重复提交 | 依赖 `@AvoidRepeatSubmit` 防重;回写本身是幂等的(多次 update 结果一致)。 |
|
||
|
||
---
|
||
|
||
## 4. 全量改造点
|
||
|
||
### 4.1 `xiaoqu-publicbeanandmap`(公共模块)
|
||
|
||
| 文件 | 改动 |
|
||
|------|------|
|
||
| `service/s2/WarehouseStockManageService.java` | 新增 4 个带明细参数的重载方法 |
|
||
| `service/s2/Impl/WarehouseStockManageServiceImpl.java` | 实现重载方法;注入 `WarehouseInDetailsService` / `WarehouseOutDetailsService`;新增 3 个私有回写方法 |
|
||
|
||
### 4.2 `xiaoqu-franchisee`(本仓库)
|
||
|
||
#### 4.2.1 入库相关
|
||
|
||
| 文件 | 行号 | 改造内容 |
|
||
|------|------|----------|
|
||
| `WarehouseInController.java` | 2122 | 入库作废:传入 `warehouseInDetails` 回写 |
|
||
| `WarehouseInController.java` | 2411 | 入库确认:传入 `warehouseInDetails` 回写 |
|
||
|
||
#### 4.2.2 出库相关
|
||
|
||
| 文件 | 行号 | 改造内容 |
|
||
|------|------|----------|
|
||
| `WarehouseOutController.java` | 3081 | 出库单作废:恢复出库仓库库存,传入对应 `WarehouseOutDetails` 回写 |
|
||
| `WarehouseOutController.java` | 3172 | 出库单作废:扣减关联入库单库存,传入对应 `WarehouseInDetails` 回写 |
|
||
| `WarehouseOutController.java` | 3911 | 出库确认扣库存:传入 `warehouseOutDetails`,同时回写 `out_of_stock` |
|
||
| `WarehouseOutController.java` | 3913 | 出库确认扣出库中:传入 `warehouseOutDetails` 回写 `out_of_stock` |
|
||
|
||
#### 4.2.3 调拨 / 划转
|
||
|
||
| 文件 | 行号 | 改造内容 |
|
||
|------|------|----------|
|
||
| `WarehouseAllocationController.java` | 1296 | 调拨提交预占出库中:传入调拨商品对应出库明细回写 `out_of_stock` |
|
||
| `WarehouseAllocationController.java` | 3641 | 调拨驳回恢复出库中:传入对应出库明细回写 `out_of_stock` |
|
||
| `WarehouseAllocationController.java` | 3649 | 调拨/划转驳回恢复当前库存:传入对应出库明细回写 `current_inventory` |
|
||
| `WarehouseAllocationController.java` | 3916 | 调拨/划转作废:出库仓库加库存,回写关联入库明细 |
|
||
| `WarehouseAllocationController.java` | 3917 | 调拨/划转作废:入库仓库扣库存,回写关联出库明细 |
|
||
| `WarehousePublicService.java` | 868 | 调拨/划转入库加库存:传入 `warehouseInDetails` 回写,并修正 `sotckmaps` 的 key |
|
||
| `WarehousePublicService.java` | 1114 | 调拨/划转出库扣库存:非 SN 直接回写;SN 在汇总明细生成后统一回写,并修正 `sotckmaps` 的 key |
|
||
| `WarehouseAllocationServiceImpl.java`(公共模块) | 261 | 调拨/划转入库加库存:传入 `warehouseInDetails` 回写 |
|
||
| `WarehouseAllocationServiceImpl.java`(公共模块) | 513 | 调拨/划转出库扣库存:非 SN 直接回写;SN 在汇总明细生成后统一回写 |
|
||
|
||
#### 4.2.4 安装 / 工单 / 自动任务
|
||
|
||
| 文件 | 行号 | 改造内容 |
|
||
|------|------|----------|
|
||
| `PublicService.java` | 1808 | 出库扣库存:传入 `warehouseOutDetails` 回写 |
|
||
| `PublicService.java` | 1809 | 出库扣出库中:传入 `warehouseOutDetails` 回写 `out_of_stock` |
|
||
| `PublicService.java` | 1900 | 出库扣库存:传入 `warehouseOutDetails` 回写 |
|
||
| `PublicService.java` | 1901 | 出库扣出库中:传入 `warehouseOutDetails` 回写 `out_of_stock` |
|
||
| `InstallOrderPublicService.java`(公共模块) | 223 | 装机出库:循环内先不回写;汇总明细生成后统一查询最新库存回写 |
|
||
| `PickupOrderServiceImpl.java`(公共模块) | 478 | 取货工单入库:传入当前 `warehouseInDetails`(SN 子明细)回写 |
|
||
| `PickupOrderServiceImpl.java`(公共模块) | 907 | 取货工单入库:传入当前 `warehouseInDetails`(SN 子明细)回写 |
|
||
| `XxlJobTask.java` | 178 | 自动出库扣库存:传入 `warehouseOutDetails` 回写 |
|
||
| `AutoOutWarehouseTask.java` | 400 | 自动出库扣库存:传入 `warehouseOutDetails` 回写 |
|
||
| `AutoOutWarehouseTask.java` | 986 | 自动出库扣库存:传入 `warehouseOutDetails` 回写 |
|
||
|
||
---
|
||
|
||
## 5. 关键改造示例
|
||
|
||
### 5.1 入库确认
|
||
|
||
```java
|
||
// 改造前
|
||
warehouseStockManageService.updateStock(
|
||
warehouseInDetails.getMaterielDefinitionId(),
|
||
warehouseInDetails.getWarehouseId(),
|
||
warehouseInDetails.getInCount(),
|
||
1);
|
||
|
||
// 改造后
|
||
warehouseStockManageService.updateStock(
|
||
warehouseInDetails.getMaterielDefinitionId(),
|
||
warehouseInDetails.getWarehouseId(),
|
||
warehouseInDetails.getInCount(),
|
||
1,
|
||
warehouseInDetails);
|
||
```
|
||
|
||
### 5.2 出库确认
|
||
|
||
```java
|
||
// 改造前
|
||
if (warehouseOutDetails.getParentId() == 0) {
|
||
warehouseStockManageService.updateStock(
|
||
warehouseOutDetails.getMaterielDefinitionId(),
|
||
warehouseOutDetails.getWarehouseId(),
|
||
warehouseOutDetails.getOutCount(),
|
||
0);
|
||
if (!flag1) {
|
||
warehouseStockManageService.updateOutOfStock(
|
||
warehouseOutDetails.getMaterielDefinitionId(),
|
||
warehouseOutDetails.getWarehouseId(),
|
||
warehouseOutDetails.getOutCount(),
|
||
0);
|
||
}
|
||
}
|
||
|
||
// 改造后
|
||
if (warehouseOutDetails.getParentId() == 0) {
|
||
warehouseStockManageService.updateStock(
|
||
warehouseOutDetails.getMaterielDefinitionId(),
|
||
warehouseOutDetails.getWarehouseId(),
|
||
warehouseOutDetails.getOutCount(),
|
||
0,
|
||
warehouseOutDetails,
|
||
true); // 同时回写 out_of_stock
|
||
if (!flag1) {
|
||
warehouseStockManageService.updateOutOfStock(
|
||
warehouseOutDetails.getMaterielDefinitionId(),
|
||
warehouseOutDetails.getWarehouseId(),
|
||
warehouseOutDetails.getOutCount(),
|
||
0,
|
||
warehouseOutDetails);
|
||
}
|
||
}
|
||
```
|
||
|
||
### 5.3 调拨/划转入库(`WarehousePublicService.setWarehouseIn`)
|
||
|
||
```java
|
||
// 改造前
|
||
warehouseStockManageService.updateStock(
|
||
warehouseAllocationGoods.getMaterielDefinitionId(),
|
||
warehouseAllocation.getWarehouseInId(),
|
||
warehouseAllocationGoods.getAllocationCount(),
|
||
1);
|
||
// 同时修正创建时快照 key
|
||
warehouseInDetails.setCurrentInventory(sotckmaps.get(warehouseAllocationGoods.getMaterielDefinitionId()));
|
||
|
||
// 改造后
|
||
warehouseStockManageService.updateStock(
|
||
warehouseAllocationGoods.getMaterielDefinitionId(),
|
||
warehouseAllocation.getWarehouseInId(),
|
||
warehouseAllocationGoods.getAllocationCount(),
|
||
1,
|
||
warehouseInDetails);
|
||
```
|
||
|
||
> 注意:`sotckmaps` 的 key 从 `materielIndexId` 改为 `materielDefinitionId`。
|
||
>
|
||
> SN 物料特殊处理:`setWarehouseOut` 会先为每个 SN 生成子明细并调用 `updateStock`,循环结束后再生成 `query_flag=1` 的汇总明细。汇总明细才是列表展示和查账的入口,因此汇总明细生成后需要**再查询一次最新库存并回写**。非 SN 物料没有汇总逻辑,可直接在 `updateStock` 时回写。
|
||
|
||
### 5.4 调拨/划转作废
|
||
|
||
调拨/划转作废会同时影响两个仓库,需要找到关联的出入库明细分别回写:
|
||
|
||
```java
|
||
for (WarehouseAllocationGoods warehouseAllocationGoods : goodslist) {
|
||
if (warehouseAllocationGoods.getQueryFlag() == 1) {
|
||
// 出库仓库加库存(相当于入库)
|
||
WarehouseInDetails outWarehouseInDetail = findInDetailByAllocation(allocationOrderNo, warehouseAllocationGoods);
|
||
warehouseStockManageService.updateStock(
|
||
warehouseAllocationGoods.getMaterielDefinitionId(),
|
||
warehouseAllocation.getWarehouseOutId(),
|
||
warehouseAllocationGoods.getAllocationCount(),
|
||
1,
|
||
outWarehouseInDetail);
|
||
|
||
// 入库仓库扣库存(相当于出库)
|
||
WarehouseOutDetails inWarehouseOutDetail = findOutDetailByAllocation(allocationOrderNo, warehouseAllocationGoods);
|
||
warehouseStockManageService.updateStock(
|
||
warehouseAllocationGoods.getMaterielDefinitionId(),
|
||
warehouseAllocation.getWarehouseInId(),
|
||
warehouseAllocationGoods.getAllocationCount(),
|
||
0,
|
||
inWarehouseOutDetail,
|
||
true);
|
||
}
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 6. 数据库变更
|
||
|
||
本期**不新增表/字段**。
|
||
|
||
### 6.1 字段存在性检查
|
||
|
||
```sql
|
||
USE xiaoqu_franchisee_d;
|
||
|
||
SELECT COLUMN_NAME, COLUMN_COMMENT, COLUMN_DEFAULT, DATA_TYPE
|
||
FROM INFORMATION_SCHEMA.COLUMNS
|
||
WHERE TABLE_SCHEMA = 'xiaoqu_franchisee_d'
|
||
AND TABLE_NAME IN ('warehouse_in_details', 'warehouse_out_details')
|
||
AND COLUMN_NAME IN ('current_inventory', 'out_of_stock');
|
||
```
|
||
|
||
### 6.2 历史数据补偿
|
||
|
||
由于全量改造覆盖所有新增数据,历史数据通过一次性 Java 任务补偿。
|
||
|
||
#### 补偿任务逻辑
|
||
|
||
```java
|
||
/**
|
||
* 历史单据库存快照补偿任务
|
||
* 按单据确认时间从早到晚排序,模拟当时库存并回写 current_inventory
|
||
*/
|
||
@Component
|
||
public class WarehouseBillStockBalanceCompensateJob {
|
||
|
||
@Autowired
|
||
private WarehouseInDetailsService warehouseInDetailsService;
|
||
@Autowired
|
||
private WarehouseOutDetailsService warehouseOutDetailsService;
|
||
@Autowired
|
||
private WarehouseStockManageService warehouseStockManageService;
|
||
|
||
public void compensateIn() {
|
||
// 查询所有已完成(order_state=3)的入库单明细,按 in_confirm_time 升序
|
||
// 用 Map<String, Integer> 维护 warehouseId + "_" + materielDefinitionId -> 库存
|
||
// 每条明细:current_inventory = map.getOrDefault(key, 0) + in_count
|
||
// update warehouse_in_details set current_inventory = ? where id = ?
|
||
}
|
||
|
||
public void compensateOut() {
|
||
// 查询所有已完成(order_state=3)的出库单明细,按 out_confirm_time 升序
|
||
// 用 Map 维护库存
|
||
// 每条明细:current_inventory = map.getOrDefault(key, 0) - out_count
|
||
// update warehouse_out_details set current_inventory = ? where id = ?
|
||
}
|
||
|
||
public void compensateAllocation() {
|
||
// 调拨/划转生成的出入库单已包含在 compensateIn / compensateOut 中
|
||
// 因为它们的 order_state 也是 3
|
||
}
|
||
}
|
||
```
|
||
|
||
> 补偿原则:
|
||
> - 只补偿 `order_state = 3`(已完成)的单据。
|
||
> - 调拨/划转生成的出入库单 `allocation_order_no` 非空,按确认时间一起参与补偿。
|
||
> - 由于并发和历史修正,补偿值可能与真实历史快照有微小偏差。
|
||
> - 补偿前务必备份;补偿后抽样核对。
|
||
|
||
---
|
||
|
||
## 7. 代码改动清单汇总
|
||
|
||
### 7.1 `xiaoqu-publicbeanandmap`
|
||
|
||
| 文件 | 改动 |
|
||
|------|------|
|
||
| `service/s2/WarehouseStockManageService.java` | 新增 4 个重载方法 |
|
||
| `service/s2/Impl/WarehouseStockManageServiceImpl.java` | 实现重载方法;新增 3 个私有回写方法;注入 `WarehouseInDetailsService` / `WarehouseOutDetailsService` |
|
||
| `service/s2/Impl/WarehouseAllocationServiceImpl.java` | 2 处 updateStock 调用接入回写 |
|
||
| `service/s2/InstallOrderPublicService.java` | 1 处 updateStock 调用接入回写 |
|
||
| `service/s2/Impl/PickupOrderServiceImpl.java` | 2 处 updateStock 调用接入回写 |
|
||
|
||
### 7.2 `xiaoqu-franchisee`
|
||
|
||
| 文件 | 改动 |
|
||
|------|------|
|
||
| `controller/warehouse/WarehouseInController.java` | 2 处 updateStock 调用接入回写 |
|
||
| `controller/warehouse/WarehouseOutController.java` | 4 处 updateStock + 3 处 updateOutOfStock 接入回写 |
|
||
| `controller/warehouse/WarehouseAllocationController.java` | 5 处 updateStock/updateOutOfStock 接入回写;作废时双向回写 |
|
||
| `service/WarehousePublicService.java` | 2 处 updateStock 接入回写;修正 `sotckmaps` key |
|
||
| `service/PublicService.java` | 4 处 updateStock/updateOutOfStock 接入回写 |
|
||
| `task/XxlJobTask.java` | 1 处 updateStock 调用接入回写 |
|
||
| `task/AutoOutWarehouseTask.java` | 2 处 updateStock 调用接入回写 |
|
||
| `jobhandler/` 或 `task/` | 新增历史数据补偿任务 |
|
||
|
||
---
|
||
|
||
## 8. 接口与页面变更
|
||
|
||
本期**不新增页面/菜单/按钮**,也不新增 Controller 接口。仅改造现有接口的后端逻辑。
|
||
|
||
> 如前端需要展示"剩余库存"列,可复用已有 `WarehouseInDetailsOutput.currentInventory` / `WarehouseOutDetailsOutput.currentInventory` / `WarehouseOutDetailsOutput.outOfStock` 字段,无需后端改动。
|
||
|
||
---
|
||
|
||
## 9. 测试方案
|
||
|
||
### 9.1 单元/接口测试
|
||
|
||
| 场景 | 预期 |
|
||
|------|------|
|
||
| 入库确认前库存 100,入库 20 | 库存 120;入库明细 current_inventory = 120 |
|
||
| 入库作废前库存 120,作废 20 | 库存 100;入库明细 current_inventory = 100 |
|
||
| 出库确认前库存 100,出库 15 | 库存 85;出库明细 current_inventory = 85,out_of_stock 同步更新 |
|
||
| 调拨出库前 A 仓 100 | A 仓 70;生成的出库明细 current_inventory = 70 |
|
||
| 调拨入库前 B 仓 50 | B 仓 80;生成的入库明细 current_inventory = 80 |
|
||
| 调拨单作废 | A 仓恢复 100、B 仓扣回 50;关联出入库明细快照同步更新 |
|
||
| 自动任务出库 | 扣库存后,对应出库明细 current_inventory 回写正确 |
|
||
| 取货工单入库 | 加库存后,对应入库明细 current_inventory 回写正确 |
|
||
| 装机工单出库 | 扣库存后,对应出库明细 current_inventory 回写正确 |
|
||
| 并发两张入库单同时确认 | 两张单据 current_inventory 分别对应各自完成后的真实库存 |
|
||
|
||
### 9.2 对账校验 SQL
|
||
|
||
```sql
|
||
-- 校验最近 N 张已完成入库单明细的 current_inventory 是否与 warehouse_stock_manage 一致
|
||
SELECT d.id,
|
||
d.in_order_no,
|
||
d.material_no,
|
||
d.warehouse_id,
|
||
d.current_inventory AS detail_current_inventory,
|
||
sm.current_inventory AS stock_current_inventory
|
||
FROM warehouse_in_details d
|
||
JOIN warehouse_in w ON d.warehouse_in_id = w.id
|
||
LEFT JOIN warehouse_stock_manage sm
|
||
ON sm.materiel_definition_id = d.materiel_definition_id
|
||
AND sm.warehouse_id = d.warehouse_id
|
||
WHERE w.order_state = 3
|
||
AND d.soft_delete = 0
|
||
ORDER BY d.in_confirm_time DESC
|
||
LIMIT 100;
|
||
|
||
-- 校验最近 N 张已完成出库单明细
|
||
SELECT d.id,
|
||
d.out_order_no,
|
||
d.material_no,
|
||
d.warehouse_id,
|
||
d.current_inventory AS detail_current_inventory,
|
||
d.out_of_stock AS detail_out_of_stock,
|
||
sm.current_inventory AS stock_current_inventory,
|
||
sm.out_of_stock AS stock_out_of_stock
|
||
FROM warehouse_out_details d
|
||
JOIN warehouse_out w ON d.warehouse_out_id = w.id
|
||
LEFT JOIN warehouse_stock_manage sm
|
||
ON sm.materiel_definition_id = d.materiel_definition_id
|
||
AND sm.warehouse_id = d.warehouse_id
|
||
WHERE w.order_state = 3
|
||
AND d.soft_delete = 0
|
||
ORDER BY d.out_confirm_time DESC
|
||
LIMIT 100;
|
||
```
|
||
|
||
---
|
||
|
||
## 10. 上线步骤
|
||
|
||
1. **公共模块改动**:
|
||
```bash
|
||
cd ../xiaoqu-publicbeanandmap
|
||
mvn clean install -DskipTests
|
||
```
|
||
2. **部署本仓库**:
|
||
```bash
|
||
cd ../xiaoqu-franchisee
|
||
mvn clean package -DskipTests
|
||
```
|
||
3. **执行历史数据补偿任务**:低峰期运行一次性补偿 Job。
|
||
4. **冒烟验证**:
|
||
- 入库确认/作废
|
||
- 出库确认
|
||
- 调拨/划转审核通过
|
||
- 调拨/划转作废
|
||
- 自动任务出库
|
||
- 抽样对账 SQL 核对
|
||
5. **回滚**:如出问题,先回滚 `xiaoqu-franchisee` WAR;公共模块新增的是重载方法,不影响旧调用点,可独立回滚。
|
||
|
||
---
|
||
|
||
## 11. 风险与注意事项
|
||
|
||
| 风险 | 说明 | 缓解措施 |
|
||
|------|------|----------|
|
||
| 历史数据 `current_inventory` 语义不统一 | 部分旧数据可能是"创建时库存" | 通过补偿任务统一为"完成后库存" |
|
||
| 并发下查询最新库存存在极小时间差 | 不影响对账主诉求 | 接受;回写本身是"完成后快照" |
|
||
| 公共模块新增依赖 | `WarehouseStockManageServiceImpl` 需要注入 `WarehouseInDetailsService` / `WarehouseOutDetailsService` | 二者都在同一公共模块,无循环依赖 |
|
||
| 调拨/划转作废涉及双向回写 | 需要找到关联的入库明细和出库明细 | 通过 `allocation_order_no` + `materiel_definition_id` + `warehouse_id` + `query_flag=1` 关联 |
|
||
| 自动任务/工单等异步场景明细可能未持久化 | 若异步流程未生成明细,则无法回写 | 这些场景通常已先生成明细再更新库存;如确实缺失,回写逻辑会跳过(detail 为 null) |
|
||
| 同一明细被多次 updateStock | 汇总项 + SN 子项可能同时触发 | 调用方只传入 `parent_id=0` 的汇总明细;SN 子项不传入明细 |
|
||
| SN 物料汇总明细回写时机 | `setWarehouseOut` 汇总明细生成在 `updateStock` 之后 | 汇总完成后再统一查询最新库存回写;公共模块可提供独立回写工具方法 |
|
||
| 性能开销 | 每条明细增加一次 SELECT + 一次 UPDATE | 一期优先保证正确性;二期可改为批量回写或 MQ |
|
||
|
||
---
|
||
|
||
## 12. 后续优化建议
|
||
|
||
1. **批量回写**:单据明细较多时,可改为批量 `UPDATE CASE WHEN ... END`,减少数据库往返。
|
||
2. **公共模块返回最新库存**:让 `updateStock(...)` 直接返回更新后的 `current_inventory`,调用方无需二次查询。
|
||
3. **快照日志**:如查账诉求更复杂,可考虑新增 `warehouse_stock_log` 流水表,记录每次库存变更前后的值及关联单据号。
|
||
4. **MQ 异步回写**:如性能瓶颈明显,可将回写逻辑改为发送 MQ,由消费者异步更新明细快照。
|
||
|
||
---
|
||
|
||
## 13. 附录:快速核对脚本
|
||
|
||
```sql
|
||
-- 近 7 天已完成入库单明细快照
|
||
SELECT w.in_order_no,
|
||
d.material_no,
|
||
d.materiel_full_name,
|
||
d.warehouse_name,
|
||
d.in_count,
|
||
d.current_inventory,
|
||
d.in_confirm_time
|
||
FROM warehouse_in_details d
|
||
JOIN warehouse_in w ON d.warehouse_in_id = w.id
|
||
WHERE w.order_state = 3
|
||
AND d.in_confirm_time >= DATE_SUB(NOW(), INTERVAL 7 DAY)
|
||
ORDER BY d.in_confirm_time DESC
|
||
LIMIT 50;
|
||
|
||
-- 近 7 天已完成出库单明细快照
|
||
SELECT w.out_order_no,
|
||
d.material_no,
|
||
d.materiel_full_name,
|
||
d.warehouse_name,
|
||
d.out_count,
|
||
d.current_inventory,
|
||
d.out_of_stock,
|
||
d.out_confirm_time
|
||
FROM warehouse_out_details d
|
||
JOIN warehouse_out w ON d.warehouse_out_id = w.id
|
||
WHERE w.order_state = 3
|
||
AND d.out_confirm_time >= DATE_SUB(NOW(), INTERVAL 7 DAY)
|
||
ORDER BY d.out_confirm_time DESC
|
||
LIMIT 50;
|
||
|
||
-- 近 7 天调拨/划转生成的出入库明细快照
|
||
SELECT d.allocation_order_no,
|
||
d.material_no,
|
||
d.warehouse_name,
|
||
d.in_count AS change_count,
|
||
d.current_inventory,
|
||
d.in_confirm_time AS confirm_time,
|
||
'in' AS direction
|
||
FROM warehouse_in_details d
|
||
WHERE d.allocation_order_no IS NOT NULL
|
||
AND d.allocation_order_no != ''
|
||
AND d.in_confirm_time >= DATE_SUB(NOW(), INTERVAL 7 DAY)
|
||
UNION ALL
|
||
SELECT d.allocation_order_no,
|
||
d.material_no,
|
||
d.warehouse_name,
|
||
-d.out_count AS change_count,
|
||
d.current_inventory,
|
||
d.out_confirm_time AS confirm_time,
|
||
'out' AS direction
|
||
FROM warehouse_out_details d
|
||
WHERE d.allocation_order_no IS NOT NULL
|
||
AND d.allocation_order_no != ''
|
||
AND d.out_confirm_time >= DATE_SUB(NOW(), INTERVAL 7 DAY)
|
||
ORDER BY confirm_time DESC
|
||
LIMIT 100;
|
||
```
|