fix(inventory): 修复库存扣减时缺少验证的问题

- 在reduceInventory方法中添加库存数量验证,防止扣减数量超过当前库存
- 在revertInventory方法中添加库存数量验证,防止减少数量超过当前库存
- 移除不必要的物料属性设置,简化库存更新逻辑
- 统一异常信息格式,提供更清晰的错误提示
This commit is contained in:
2026-01-30 17:17:15 +08:00
parent 316d13479d
commit 5a2b5ff2ff
2 changed files with 12 additions and 12 deletions

View File

@@ -154,13 +154,13 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
private void reduceInventory(Long materialId, BigDecimal quantity) {
MatMaterialVo material = matMaterialService.queryById(materialId);
if (material != null) {
// 验证要扣减的数量不能大于当前库存
if (quantity.compareTo(material.getCurrentStock()) > 0) {
throw new IllegalArgumentException("要扣减的数量不能大于当前库存数量物料ID" + materialId + ",当前库存:" + material.getCurrentStock() + ",请求扣减:" + quantity);
}
MatMaterialBo materialBo = new MatMaterialBo();
materialBo.setMaterialId(materialId);
materialBo.setMaterialName(material.getMaterialName());
materialBo.setSpec(material.getSpec());
materialBo.setModel(material.getModel());
materialBo.setFactory(material.getFactory());
materialBo.setUnit(material.getUnit());
// 计算扣减后的库存数量
BigDecimal newStock = material.getCurrentStock().subtract(quantity);
@@ -169,11 +169,11 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
}
materialBo.setCurrentStock(newStock);
materialBo.setRemark(material.getRemark());
matMaterialService.updateByBo(materialBo);
}
}
/**
* 恢复库存数量(增加库存)
*/

View File

@@ -275,13 +275,14 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi
private void revertInventory(Long materialId, BigDecimal quantity) {
MatMaterialVo material = matMaterialService.queryById(materialId);
if (material != null) {
// 验证要减少的数量不能大于当前库存
if (quantity.compareTo(material.getCurrentStock()) > 0) {
throw new IllegalArgumentException("要减少的数量不能大于当前库存数量物料ID" + materialId + ",当前库存:" + material.getCurrentStock() + ",请求减少:" + quantity);
}
MatMaterialBo materialBo = new MatMaterialBo();
materialBo.setMaterialId(materialId);
materialBo.setMaterialName(material.getMaterialName());
materialBo.setSpec(material.getSpec());
materialBo.setModel(material.getModel());
materialBo.setFactory(material.getFactory());
materialBo.setUnit(material.getUnit());
// 计算还原后的库存数量(减去本次入库的数量)
BigDecimal newStock = material.getCurrentStock().subtract(quantity);
@@ -290,7 +291,6 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi
}
materialBo.setCurrentStock(newStock);
materialBo.setRemark(material.getRemark());
matMaterialService.updateByBo(materialBo);
}
}