feat(mat): 实现出库操作的库存自动调整功能

- 在新增配料出库时自动扣减对应物料库存
- 在删除配料出库时自动恢复对应物料库存
- 添加insertByBoWithInventoryAdjustment方法处理带库存调整的出库
- 添加deleteWithValidByIdsWithInventoryAdjustment方法处理带库存恢复的删除
- 实现reduceInventory和restoreInventory私有方法管理库存变更
- 更新控制器调用新的带库存调整的业务方法
This commit is contained in:
2026-01-30 16:20:07 +08:00
parent d9e0205da1
commit 83e8ad2a9c
3 changed files with 106 additions and 2 deletions

View File

@@ -75,7 +75,7 @@ public class MatMaterialOutController extends BaseController {
@RepeatSubmit() @RepeatSubmit()
@PostMapping() @PostMapping()
public R<Void> add(@Validated(AddGroup.class) @RequestBody MatMaterialOutBo bo) { public R<Void> add(@Validated(AddGroup.class) @RequestBody MatMaterialOutBo bo) {
return toAjax(iMatMaterialOutService.insertByBo(bo)); return toAjax(iMatMaterialOutService.insertByBoWithInventoryAdjustment(bo));
} }
/** /**
@@ -97,6 +97,6 @@ public class MatMaterialOutController extends BaseController {
@DeleteMapping("/{outIds}") @DeleteMapping("/{outIds}")
public R<Void> remove(@NotEmpty(message = "主键不能为空") public R<Void> remove(@NotEmpty(message = "主键不能为空")
@PathVariable Long[] outIds) { @PathVariable Long[] outIds) {
return toAjax(iMatMaterialOutService.deleteWithValidByIds(Arrays.asList(outIds), true)); return toAjax(iMatMaterialOutService.deleteWithValidByIdsWithInventoryAdjustment(Arrays.asList(outIds), true));
} }
} }

View File

@@ -37,6 +37,11 @@ public interface IMatMaterialOutService {
*/ */
Boolean insertByBo(MatMaterialOutBo bo); Boolean insertByBo(MatMaterialOutBo bo);
/**
* 新增配料出库并更新库存
*/
Boolean insertByBoWithInventoryAdjustment(MatMaterialOutBo bo);
/** /**
* 修改配料出库 * 修改配料出库
*/ */
@@ -46,4 +51,9 @@ public interface IMatMaterialOutService {
* 校验并批量删除配料出库信息 * 校验并批量删除配料出库信息
*/ */
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid); Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
/**
* 校验并批量删除配料出库信息,并恢复库存
*/
Boolean deleteWithValidByIdsWithInventoryAdjustment(Collection<Long> ids, Boolean isValid);
} }

View File

@@ -15,11 +15,19 @@ import com.gear.mat.domain.vo.MatMaterialOutVo;
import com.gear.mat.domain.MatMaterialOut; import com.gear.mat.domain.MatMaterialOut;
import com.gear.mat.mapper.MatMaterialOutMapper; import com.gear.mat.mapper.MatMaterialOutMapper;
import com.gear.mat.service.IMatMaterialOutService; import com.gear.mat.service.IMatMaterialOutService;
import com.gear.mat.service.IMatMaterialService;
import com.gear.mat.domain.vo.MatMaterialVo;
import com.gear.mat.domain.bo.MatMaterialBo;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Collection; import java.util.Collection;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Arrays;
/** /**
* 配料出库Service业务层处理 * 配料出库Service业务层处理
* *
@@ -31,6 +39,7 @@ import java.util.Collection;
public class MatMaterialOutServiceImpl implements IMatMaterialOutService { public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
private final MatMaterialOutMapper baseMapper; private final MatMaterialOutMapper baseMapper;
private final IMatMaterialService matMaterialService;
/** /**
* 查询配料出库 * 查询配料出库
@@ -100,6 +109,91 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
return flag; return flag;
} }
@Override
public Boolean insertByBoWithInventoryAdjustment(MatMaterialOutBo bo) {
boolean success = insertByBo(bo);
if (success && bo.getMaterialId() != null && bo.getOutNum() != null) {
// 扣减库存数量
reduceInventory(bo.getMaterialId(), bo.getOutNum());
}
return success;
}
@Override
public Boolean deleteWithValidByIdsWithInventoryAdjustment(Collection<Long> ids, Boolean isValid) {
// 获取待删除的出库记录信息,以便后续恢复库存
List<MatMaterialOutVo> recordsToDelete = new ArrayList<>();
for (Long outId : ids) {
MatMaterialOutVo record = queryById(outId);
if (record != null) {
recordsToDelete.add(record);
}
}
boolean success = deleteWithValidByIds(ids, isValid);
if (success) {
// 对于每个被删除的出库记录,恢复库存
for (MatMaterialOutVo record : recordsToDelete) {
// 恢复库存数量
restoreInventory(record.getMaterialId(), record.getOutNum());
}
}
return success;
}
/**
* 扣减库存数量
*/
private void reduceInventory(Long materialId, BigDecimal quantity) {
MatMaterialVo material = matMaterialService.queryById(materialId);
if (material != null) {
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);
if (newStock.compareTo(BigDecimal.ZERO) < 0) {
newStock = BigDecimal.ZERO; // 库存不能为负数
}
materialBo.setCurrentStock(newStock);
materialBo.setRemark(material.getRemark());
matMaterialService.updateByBo(materialBo);
}
}
/**
* 恢复库存数量(增加库存)
*/
private void restoreInventory(Long materialId, BigDecimal quantity) {
MatMaterialVo material = matMaterialService.queryById(materialId);
if (material != null) {
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().add(quantity);
materialBo.setCurrentStock(newStock);
materialBo.setRemark(material.getRemark());
matMaterialService.updateByBo(materialBo);
}
}
/** /**
* 修改配料出库 * 修改配料出库
*/ */