refactor(mat): 优化物料出入库服务事务管理和代码规范
- 在 MatMaterialOutServiceImpl 中添加 @Transactional 注解确保数据一致性 - 修复 MatPriceHistoryController 中的服务注入变量名错误 - 统一控制器中的服务调用方法,提升代码可读性 - 优化 MatPurchaseInDetailServiceImpl 的事务配置和方法实现 - 规范化代码格式和命名约定,提高代码质量
This commit is contained in:
@@ -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<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)
|
||||
@PostMapping("/export")
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ public class MatPriceHistoryController extends BaseController {
|
||||
@GetMapping("/{historyId}")
|
||||
public R<MatPriceHistoryVo> 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<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()
|
||||
@PutMapping()
|
||||
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}")
|
||||
public R<Void> remove(@NotEmpty(message = "主键不能为空")
|
||||
@PathVariable Long[] historyIds) {
|
||||
return toAjax(iMatMatPriceHistoryService.deleteWithValidByIds(Arrays.asList(historyIds), true));
|
||||
return toAjax(iMatPriceHistoryService.deleteWithValidByIds(Arrays.asList(historyIds), true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Long> ids, Boolean isValid) {
|
||||
// 获取待删除的出库记录信息,以便后续恢复库存
|
||||
List<MatMaterialOutVo> 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);
|
||||
}
|
||||
|
||||
@@ -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<Long> ids, Boolean isValid) {
|
||||
// 获取待删除的入库记录信息,以便后续还原库存和价格历史
|
||||
List<MatPurchaseInDetailVo> 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user