refactor(mat): 优化物料库存查询逻辑
- 添加物料ID非空校验,避免空指针异常 - 移除按物料ID分组的复杂逻辑,简化查询流程 - 统一采购单查询条件,增加删除标记过滤 - 优化计划总数、已入库数和在途数的计算逻辑 - 移除不必要的PO到VO转换注释 - 重构代码结构,提高可读性和性能
This commit is contained in:
@@ -184,15 +184,19 @@ public class MatMaterialServiceImpl implements IMatMaterialService {
|
|||||||
// 创建结果列表
|
// 创建结果列表
|
||||||
List<MaterialInventoryVo> result = new ArrayList<>();
|
List<MaterialInventoryVo> result = new ArrayList<>();
|
||||||
|
|
||||||
// 构建采购单查询条件(直接用QueryWrapper,和Service层逻辑保持一致)
|
// 物料ID,先做非空校验
|
||||||
QueryWrapper<MatPurchase> purchaseQw = new QueryWrapper<>();
|
if (materialId == null) {
|
||||||
if (materialId != null) {
|
return result;
|
||||||
purchaseQw.eq("material_id", materialId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 构建采购单查询条件(materialId + 过滤删除标记)
|
||||||
|
QueryWrapper<MatPurchase> purchaseQw = new QueryWrapper<>();
|
||||||
|
purchaseQw.eq("material_id", materialId);
|
||||||
|
purchaseQw.eq("del_flag", 0);
|
||||||
// 直接查采购单PO
|
// 直接查采购单PO
|
||||||
List<MatPurchase> purchaseListPo = matPurchaseMapper.selectList(purchaseQw);
|
List<MatPurchase> purchaseListPo = matPurchaseMapper.selectList(purchaseQw);
|
||||||
|
|
||||||
// 将PO转换为VO,以便后续处理
|
// 将PO转换为VO
|
||||||
List<MatPurchaseVo> purchaseList = purchaseListPo.stream()
|
List<MatPurchaseVo> purchaseList = purchaseListPo.stream()
|
||||||
.map(purchase -> {
|
.map(purchase -> {
|
||||||
MatPurchaseVo vo = new MatPurchaseVo();
|
MatPurchaseVo vo = new MatPurchaseVo();
|
||||||
@@ -201,72 +205,58 @@ public class MatMaterialServiceImpl implements IMatMaterialService {
|
|||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
// 按物料ID分组
|
// 1. 查询当前物料的详细信息
|
||||||
Map<Long, List<MatPurchaseVo>> purchaseMap = purchaseList.stream()
|
MatMaterialVo material = baseMapper.selectVoById(materialId);
|
||||||
.collect(Collectors.groupingBy(MatPurchaseVo::getMaterialId));
|
// 2. 初始化物料库存VO
|
||||||
|
MaterialInventoryVo inventory = new MaterialInventoryVo();
|
||||||
|
inventory.setMaterialId(materialId);
|
||||||
|
if (material != null) {
|
||||||
|
inventory.setMaterialName(material.getMaterialName());
|
||||||
|
inventory.setSpec(material.getSpec());
|
||||||
|
inventory.setModel(material.getModel());
|
||||||
|
inventory.setUnit(material.getUnit());
|
||||||
|
inventory.setCurrentStock(material.getCurrentStock());
|
||||||
|
}
|
||||||
|
|
||||||
for (Long matId : purchaseMap.keySet()) {
|
// 3. 计算该物料的计划总数、已入库总数、在途总数
|
||||||
|
BigDecimal planTotal = BigDecimal.ZERO;
|
||||||
|
BigDecimal receivedTotal = BigDecimal.ZERO;
|
||||||
|
BigDecimal inTransitTotal = BigDecimal.ZERO;
|
||||||
|
|
||||||
// 通过采购单中的物料ID查询物料详细信息
|
for (MatPurchaseVo purchase : purchaseList) {
|
||||||
MatMaterialVo material = baseMapper.selectVoById(matId);
|
// 累加计划总数
|
||||||
|
BigDecimal planNum = Optional.ofNullable(purchase.getPlanNum()).orElse(BigDecimal.ZERO);
|
||||||
|
planTotal = planTotal.add(planNum);
|
||||||
|
|
||||||
MaterialInventoryVo inventory = new MaterialInventoryVo();
|
// 查询该采购单下的入库详情
|
||||||
inventory.setMaterialId(matId);
|
MatPurchaseInDetailBo detailBo = new MatPurchaseInDetailBo();
|
||||||
|
detailBo.setPurchaseId(purchase.getPurchaseId());
|
||||||
|
List<MatPurchaseInDetailVo> details = matPurchaseInDetailService.queryList(detailBo);
|
||||||
|
|
||||||
if (material != null) {
|
// 计算该采购单的已入库数量
|
||||||
inventory.setMaterialName(material.getMaterialName());
|
BigDecimal receivedInPurchase = details.stream()
|
||||||
inventory.setSpec(material.getSpec());
|
.map(MatPurchaseInDetailVo::getInNum)
|
||||||
inventory.setModel(material.getModel());
|
|
||||||
inventory.setUnit(material.getUnit());
|
|
||||||
inventory.setCurrentStock(material.getCurrentStock());
|
|
||||||
}
|
|
||||||
|
|
||||||
List<MatPurchaseVo> purchases = purchaseMap.get(matId);
|
|
||||||
|
|
||||||
// 计算计划总数
|
|
||||||
BigDecimal planTotal = purchases.stream()
|
|
||||||
.map(MatPurchaseVo::getPlanNum)
|
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
receivedTotal = receivedTotal.add(receivedInPurchase);
|
||||||
|
|
||||||
inventory.setPlanNum(planTotal);
|
// 计算该采购单的在途数量
|
||||||
|
if (purchase.getStatus() != null && (purchase.getStatus() == 1 || purchase.getStatus() == 4)) {
|
||||||
// 计算已入库数量和在途数量
|
BigDecimal pending = planNum.subtract(receivedInPurchase);
|
||||||
BigDecimal receivedTotal = BigDecimal.ZERO;
|
if (pending.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
BigDecimal inTransitTotal = BigDecimal.ZERO;
|
inTransitTotal = inTransitTotal.add(pending);
|
||||||
|
|
||||||
for (MatPurchaseVo purchase : purchases) {
|
|
||||||
// 查询该采购单下的入库详情
|
|
||||||
MatPurchaseInDetailBo detailBo = new MatPurchaseInDetailBo();
|
|
||||||
detailBo.setPurchaseId(purchase.getPurchaseId());
|
|
||||||
|
|
||||||
List<MatPurchaseInDetailVo> details = matPurchaseInDetailService.queryList(detailBo);
|
|
||||||
|
|
||||||
// 计算该采购单的已入库数量
|
|
||||||
BigDecimal receivedInPurchase = details.stream()
|
|
||||||
.map(MatPurchaseInDetailVo::getInNum)
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
||||||
|
|
||||||
receivedTotal = receivedTotal.add(receivedInPurchase);
|
|
||||||
|
|
||||||
// 根据采购单状态判断在途数量
|
|
||||||
// 1-待入(在途) 2-已完成(全部入库) 3-已取消(归零) 4-部分入库
|
|
||||||
if (purchase.getStatus() != null && (purchase.getStatus() == 1 || purchase.getStatus() == 4)) {
|
|
||||||
// 在途或部分入库,计算未入库数量
|
|
||||||
BigDecimal pending = purchase.getPlanNum().subtract(receivedInPurchase);
|
|
||||||
if (pending.compareTo(BigDecimal.ZERO) > 0) {
|
|
||||||
inTransitTotal = inTransitTotal.add(pending);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory.setReceivedNum(receivedTotal);
|
|
||||||
inventory.setInTransitNum(inTransitTotal);
|
|
||||||
|
|
||||||
result.add(inventory);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4. 设置库存VO的数值字段
|
||||||
|
inventory.setPlanNum(planTotal);
|
||||||
|
inventory.setReceivedNum(receivedTotal);
|
||||||
|
inventory.setInTransitNum(inTransitTotal);
|
||||||
|
|
||||||
|
// 5. 添加到结果列表
|
||||||
|
result.add(inventory);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user