refactor(mat): 优化物料出入库服务事务管理和代码规范

- 在 MatMaterialOutServiceImpl 中添加 @Transactional 注解确保数据一致性
- 修复 MatPriceHistoryController 中的服务注入变量名错误
- 统一控制器中的服务调用方法,提升代码可读性
- 优化 MatPurchaseInDetailServiceImpl 的事务配置和方法实现
- 规范化代码格式和命名约定,提高代码质量
This commit is contained in:
2026-01-30 17:02:20 +08:00
parent 609e7707db
commit f64db505d9
3 changed files with 31 additions and 25 deletions

View File

@@ -35,14 +35,14 @@ import com.gear.common.core.page.TableDataInfo;
@RequestMapping("/mat/matPriceHistory") @RequestMapping("/mat/matPriceHistory")
public class MatPriceHistoryController extends BaseController { public class MatPriceHistoryController extends BaseController {
private final IMatPriceHistoryService iMatMatPriceHistoryService; private final IMatPriceHistoryService iMatPriceHistoryService;
/** /**
* 查询配料价格/均价变动历史列表 * 查询配料价格/均价变动历史列表
*/ */
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo<MatPriceHistoryVo> list(MatPriceHistoryBo bo, PageQuery pageQuery) { public TableDataInfo<MatPriceHistoryVo> list(MatPriceHistoryBo bo, PageQuery pageQuery) {
return iMatMatPriceHistoryService.queryPageList(bo, pageQuery); return iMatPriceHistoryService.queryPageList(bo, pageQuery);
} }
/** /**
@@ -51,7 +51,7 @@ public class MatPriceHistoryController extends BaseController {
@Log(title = "配料价格/均价变动历史", businessType = BusinessType.EXPORT) @Log(title = "配料价格/均价变动历史", businessType = BusinessType.EXPORT)
@PostMapping("/export") @PostMapping("/export")
public void export(MatPriceHistoryBo bo, HttpServletResponse response) { public void export(MatPriceHistoryBo bo, HttpServletResponse response) {
List<MatPriceHistoryVo> list = iMatMatPriceHistoryService.queryList(bo); List<MatPriceHistoryVo> list = iMatPriceHistoryService.queryList(bo);
ExcelUtil.exportExcel(list, "配料价格/均价变动历史", MatPriceHistoryVo.class, response); ExcelUtil.exportExcel(list, "配料价格/均价变动历史", MatPriceHistoryVo.class, response);
} }
@@ -63,7 +63,7 @@ public class MatPriceHistoryController extends BaseController {
@GetMapping("/{historyId}") @GetMapping("/{historyId}")
public R<MatPriceHistoryVo> getInfo(@NotNull(message = "主键不能为空") public R<MatPriceHistoryVo> getInfo(@NotNull(message = "主键不能为空")
@PathVariable Long historyId) { @PathVariable Long historyId) {
return R.ok(iMatMatPriceHistoryService.queryById(historyId)); return R.ok(iMatPriceHistoryService.queryById(historyId));
} }
/** /**
@@ -73,7 +73,7 @@ public class MatPriceHistoryController extends BaseController {
@RepeatSubmit() @RepeatSubmit()
@PostMapping() @PostMapping()
public R<Void> add(@Validated(AddGroup.class) @RequestBody MatPriceHistoryBo bo) { public R<Void> add(@Validated(AddGroup.class) @RequestBody MatPriceHistoryBo bo) {
return toAjax(iMatMatPriceHistoryService.insertByBo(bo)); return toAjax(iMatPriceHistoryService.insertByBo(bo));
} }
/** /**
@@ -83,7 +83,7 @@ public class MatPriceHistoryController extends BaseController {
@RepeatSubmit() @RepeatSubmit()
@PutMapping() @PutMapping()
public R<Void> edit(@Validated(EditGroup.class) @RequestBody MatPriceHistoryBo bo) { public R<Void> edit(@Validated(EditGroup.class) @RequestBody MatPriceHistoryBo bo) {
return toAjax(iMatMatPriceHistoryService.updateByBo(bo)); return toAjax(iMatPriceHistoryService.updateByBo(bo));
} }
/** /**
@@ -95,6 +95,6 @@ public class MatPriceHistoryController extends BaseController {
@DeleteMapping("/{historyIds}") @DeleteMapping("/{historyIds}")
public R<Void> remove(@NotEmpty(message = "主键不能为空") public R<Void> remove(@NotEmpty(message = "主键不能为空")
@PathVariable Long[] historyIds) { @PathVariable Long[] historyIds) {
return toAjax(iMatMatPriceHistoryService.deleteWithValidByIds(Arrays.asList(historyIds), true)); return toAjax(iMatPriceHistoryService.deleteWithValidByIds(Arrays.asList(historyIds), true));
} }
} }

View File

@@ -18,6 +18,7 @@ import com.gear.mat.service.IMatMaterialOutService;
import com.gear.mat.service.IMatMaterialService; import com.gear.mat.service.IMatMaterialService;
import com.gear.mat.domain.vo.MatMaterialVo; import com.gear.mat.domain.vo.MatMaterialVo;
import com.gear.mat.domain.bo.MatMaterialBo; import com.gear.mat.domain.bo.MatMaterialBo;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -108,20 +109,22 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
} }
return flag; return flag;
} }
@Override @Override
@Transactional
public Boolean insertByBoWithInventoryAdjustment(MatMaterialOutBo bo) { public Boolean insertByBoWithInventoryAdjustment(MatMaterialOutBo bo) {
boolean success = insertByBo(bo); boolean success = insertByBo(bo);
if (success && bo.getMaterialId() != null && bo.getOutNum() != null) { if (success && bo.getMaterialId() != null && bo.getOutNum() != null) {
// 扣减库存数量 // 扣减库存数量
reduceInventory(bo.getMaterialId(), bo.getOutNum()); reduceInventory(bo.getMaterialId(), bo.getOutNum());
} }
return success; return success;
} }
@Override @Override
@Transactional
public Boolean deleteWithValidByIdsWithInventoryAdjustment(Collection<Long> ids, Boolean isValid) { public Boolean deleteWithValidByIdsWithInventoryAdjustment(Collection<Long> ids, Boolean isValid) {
// 获取待删除的出库记录信息,以便后续恢复库存 // 获取待删除的出库记录信息,以便后续恢复库存
List<MatMaterialOutVo> recordsToDelete = new ArrayList<>(); List<MatMaterialOutVo> recordsToDelete = new ArrayList<>();
@@ -131,9 +134,9 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
recordsToDelete.add(record); recordsToDelete.add(record);
} }
} }
boolean success = deleteWithValidByIds(ids, isValid); boolean success = deleteWithValidByIds(ids, isValid);
if (success) { if (success) {
// 对于每个被删除的出库记录,恢复库存 // 对于每个被删除的出库记录,恢复库存
for (MatMaterialOutVo record : recordsToDelete) { for (MatMaterialOutVo record : recordsToDelete) {
@@ -141,10 +144,10 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
restoreInventory(record.getMaterialId(), record.getOutNum()); restoreInventory(record.getMaterialId(), record.getOutNum());
} }
} }
return success; return success;
} }
/** /**
* 扣减库存数量 * 扣减库存数量
*/ */
@@ -158,19 +161,19 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
materialBo.setModel(material.getModel()); materialBo.setModel(material.getModel());
materialBo.setFactory(material.getFactory()); materialBo.setFactory(material.getFactory());
materialBo.setUnit(material.getUnit()); materialBo.setUnit(material.getUnit());
// 计算扣减后的库存数量 // 计算扣减后的库存数量
BigDecimal newStock = material.getCurrentStock().subtract(quantity); BigDecimal newStock = material.getCurrentStock().subtract(quantity);
if (newStock.compareTo(BigDecimal.ZERO) < 0) { if (newStock.compareTo(BigDecimal.ZERO) < 0) {
newStock = BigDecimal.ZERO; // 库存不能为负数 newStock = BigDecimal.ZERO; // 库存不能为负数
} }
materialBo.setCurrentStock(newStock); materialBo.setCurrentStock(newStock);
materialBo.setRemark(material.getRemark()); materialBo.setRemark(material.getRemark());
matMaterialService.updateByBo(materialBo); matMaterialService.updateByBo(materialBo);
} }
} }
/** /**
* 恢复库存数量(增加库存) * 恢复库存数量(增加库存)
*/ */
@@ -184,11 +187,11 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService {
materialBo.setModel(material.getModel()); materialBo.setModel(material.getModel());
materialBo.setFactory(material.getFactory()); materialBo.setFactory(material.getFactory());
materialBo.setUnit(material.getUnit()); materialBo.setUnit(material.getUnit());
// 计算恢复后的库存数量(加上本次出库的数量) // 计算恢复后的库存数量(加上本次出库的数量)
BigDecimal newStock = material.getCurrentStock().add(quantity); BigDecimal newStock = material.getCurrentStock().add(quantity);
materialBo.setCurrentStock(newStock); materialBo.setCurrentStock(newStock);
materialBo.setRemark(material.getRemark()); materialBo.setRemark(material.getRemark());
matMaterialService.updateByBo(materialBo); matMaterialService.updateByBo(materialBo);
} }

View File

@@ -21,6 +21,7 @@ import com.gear.mat.domain.vo.MatMaterialVo;
import com.gear.mat.domain.vo.MatPriceHistoryVo; import com.gear.mat.domain.vo.MatPriceHistoryVo;
import com.gear.mat.domain.bo.MatMaterialBo; import com.gear.mat.domain.bo.MatMaterialBo;
import com.gear.mat.domain.bo.MatPriceHistoryBo; import com.gear.mat.domain.bo.MatPriceHistoryBo;
import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
@@ -142,21 +143,23 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi
} }
@Override @Override
@Transactional
public Boolean insertByBoWithInventoryAndPriceHistory(MatPurchaseInDetailBo bo) { public Boolean insertByBoWithInventoryAndPriceHistory(MatPurchaseInDetailBo bo) {
boolean success = insertByBo(bo); boolean success = insertByBo(bo);
if (success && bo.getMaterialId() != null && bo.getInNum() != null && bo.getInPrice() != null) { if (success && bo.getMaterialId() != null && bo.getInNum() != null && bo.getInPrice() != null) {
// 更新库存数量 // 更新库存数量
updateInventory(bo.getMaterialId(), bo.getInNum()); updateInventory(bo.getMaterialId(), bo.getInNum());
// 插入价格历史记录 // 插入价格历史记录
insertPriceHistory(bo.getMaterialId(), bo.getInPrice(), bo.getInNum(), bo.getDetailId()); insertPriceHistory(bo.getMaterialId(), bo.getInPrice(), bo.getInNum(), bo.getDetailId());
} }
return success; return success;
} }
@Override @Override
@Transactional
public Boolean deleteWithValidByIdsWithInventoryAndPriceHistory(Collection<Long> ids, Boolean isValid) { public Boolean deleteWithValidByIdsWithInventoryAndPriceHistory(Collection<Long> ids, Boolean isValid) {
// 获取待删除的入库记录信息,以便后续还原库存和价格历史 // 获取待删除的入库记录信息,以便后续还原库存和价格历史
List<MatPurchaseInDetailVo> recordsToDelete = new ArrayList<>(); List<MatPurchaseInDetailVo> recordsToDelete = new ArrayList<>();
@@ -209,7 +212,7 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi
if (material != null) { if (material != null) {
// 计算新的平均价格 // 计算新的平均价格
BigDecimal avgPrice = calculateAveragePrice(materialId, price, quantity); BigDecimal avgPrice = calculateAveragePrice(materialId, price, quantity);
// 创建价格历史记录 // 创建价格历史记录
MatPriceHistoryBo priceHistoryBo = new MatPriceHistoryBo(); MatPriceHistoryBo priceHistoryBo = new MatPriceHistoryBo();
priceHistoryBo.setMaterialId(materialId); priceHistoryBo.setMaterialId(materialId);
@@ -217,7 +220,7 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi
priceHistoryBo.setAvgPrice(avgPrice); priceHistoryBo.setAvgPrice(avgPrice);
priceHistoryBo.setQuantity(quantity); priceHistoryBo.setQuantity(quantity);
priceHistoryBo.setPurchaseInDetailId(detailId); // 关联入库明细ID priceHistoryBo.setPurchaseInDetailId(detailId); // 关联入库明细ID
matPriceHistoryService.insertByBo(priceHistoryBo); matPriceHistoryService.insertByBo(priceHistoryBo);
} }
} }