From f64db505d901cbdc7e02dbcc51517a4962dc884e Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Fri, 30 Jan 2026 17:02:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(mat):=20=E4=BC=98=E5=8C=96=E7=89=A9?= =?UTF-8?q?=E6=96=99=E5=87=BA=E5=85=A5=E5=BA=93=E6=9C=8D=E5=8A=A1=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1=E7=AE=A1=E7=90=86=E5=92=8C=E4=BB=A3=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 MatMaterialOutServiceImpl 中添加 @Transactional 注解确保数据一致性 - 修复 MatPriceHistoryController 中的服务注入变量名错误 - 统一控制器中的服务调用方法,提升代码可读性 - 优化 MatPurchaseInDetailServiceImpl 的事务配置和方法实现 - 规范化代码格式和命名约定,提高代码质量 --- .../controller/MatPriceHistoryController.java | 14 ++++----- .../impl/MatMaterialOutServiceImpl.java | 29 ++++++++++--------- .../impl/MatPurchaseInDetailServiceImpl.java | 13 +++++---- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/gear-mat/src/main/java/com/gear/mat/controller/MatPriceHistoryController.java b/gear-mat/src/main/java/com/gear/mat/controller/MatPriceHistoryController.java index 354a927..5ec16f3 100644 --- a/gear-mat/src/main/java/com/gear/mat/controller/MatPriceHistoryController.java +++ b/gear-mat/src/main/java/com/gear/mat/controller/MatPriceHistoryController.java @@ -35,14 +35,14 @@ import com.gear.common.core.page.TableDataInfo; @RequestMapping("/mat/matPriceHistory") public class MatPriceHistoryController extends BaseController { - private final IMatPriceHistoryService iMatMatPriceHistoryService; + private final IMatPriceHistoryService iMatPriceHistoryService; /** * 查询配料价格/均价变动历史列表 */ @GetMapping("/list") public TableDataInfo 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) @PostMapping("/export") public void export(MatPriceHistoryBo bo, HttpServletResponse response) { - List list = iMatMatPriceHistoryService.queryList(bo); + List list = iMatPriceHistoryService.queryList(bo); ExcelUtil.exportExcel(list, "配料价格/均价变动历史", MatPriceHistoryVo.class, response); } @@ -63,7 +63,7 @@ public class MatPriceHistoryController extends BaseController { @GetMapping("/{historyId}") public R getInfo(@NotNull(message = "主键不能为空") @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() @PostMapping() public R 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() @PutMapping() public R 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}") public R remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] historyIds) { - return toAjax(iMatMatPriceHistoryService.deleteWithValidByIds(Arrays.asList(historyIds), true)); + return toAjax(iMatPriceHistoryService.deleteWithValidByIds(Arrays.asList(historyIds), true)); } } diff --git a/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialOutServiceImpl.java b/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialOutServiceImpl.java index 38a3ada..648dc16 100644 --- a/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialOutServiceImpl.java +++ b/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialOutServiceImpl.java @@ -18,6 +18,7 @@ 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 org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; @@ -108,20 +109,22 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService { } return flag; } - + @Override + @Transactional 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 + @Transactional public Boolean deleteWithValidByIdsWithInventoryAdjustment(Collection ids, Boolean isValid) { // 获取待删除的出库记录信息,以便后续恢复库存 List recordsToDelete = new ArrayList<>(); @@ -131,9 +134,9 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService { recordsToDelete.add(record); } } - + boolean success = deleteWithValidByIds(ids, isValid); - + if (success) { // 对于每个被删除的出库记录,恢复库存 for (MatMaterialOutVo record : recordsToDelete) { @@ -141,10 +144,10 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService { restoreInventory(record.getMaterialId(), record.getOutNum()); } } - + return success; } - + /** * 扣减库存数量 */ @@ -158,19 +161,19 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService { 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); } } - + /** * 恢复库存数量(增加库存) */ @@ -184,11 +187,11 @@ public class MatMaterialOutServiceImpl implements IMatMaterialOutService { 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); } diff --git a/gear-mat/src/main/java/com/gear/mat/service/impl/MatPurchaseInDetailServiceImpl.java b/gear-mat/src/main/java/com/gear/mat/service/impl/MatPurchaseInDetailServiceImpl.java index fbd91b7..b06c046 100644 --- a/gear-mat/src/main/java/com/gear/mat/service/impl/MatPurchaseInDetailServiceImpl.java +++ b/gear-mat/src/main/java/com/gear/mat/service/impl/MatPurchaseInDetailServiceImpl.java @@ -21,6 +21,7 @@ import com.gear.mat.domain.vo.MatMaterialVo; import com.gear.mat.domain.vo.MatPriceHistoryVo; import com.gear.mat.domain.bo.MatMaterialBo; import com.gear.mat.domain.bo.MatPriceHistoryBo; +import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -142,21 +143,23 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi } @Override + @Transactional public Boolean insertByBoWithInventoryAndPriceHistory(MatPurchaseInDetailBo bo) { boolean success = insertByBo(bo); - + if (success && bo.getMaterialId() != null && bo.getInNum() != null && bo.getInPrice() != null) { // 更新库存数量 updateInventory(bo.getMaterialId(), bo.getInNum()); - + // 插入价格历史记录 insertPriceHistory(bo.getMaterialId(), bo.getInPrice(), bo.getInNum(), bo.getDetailId()); } - + return success; } @Override + @Transactional public Boolean deleteWithValidByIdsWithInventoryAndPriceHistory(Collection ids, Boolean isValid) { // 获取待删除的入库记录信息,以便后续还原库存和价格历史 List recordsToDelete = new ArrayList<>(); @@ -209,7 +212,7 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi if (material != null) { // 计算新的平均价格 BigDecimal avgPrice = calculateAveragePrice(materialId, price, quantity); - + // 创建价格历史记录 MatPriceHistoryBo priceHistoryBo = new MatPriceHistoryBo(); priceHistoryBo.setMaterialId(materialId); @@ -217,7 +220,7 @@ public class MatPurchaseInDetailServiceImpl implements IMatPurchaseInDetailServi priceHistoryBo.setAvgPrice(avgPrice); priceHistoryBo.setQuantity(quantity); priceHistoryBo.setPurchaseInDetailId(detailId); // 关联入库明细ID - + matPriceHistoryService.insertByBo(priceHistoryBo); } }