修复库存分布问题

This commit is contained in:
2025-10-31 14:37:19 +08:00
parent 456367a63f
commit 3e964b4d23
3 changed files with 48 additions and 62 deletions

View File

@@ -73,6 +73,11 @@ public class WmsStockVo {
@ExcelProperty(value = "仓库/库区名称")
private String warehouseName;
/**
* 状态0=在库1=在途2=已出库)
*/
private Integer status;
/**
* 在途量
*/

View File

@@ -8,10 +8,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.common.utils.StringUtils;
import com.klp.domain.bo.WmsPurchasePlanDetailBo;
import com.klp.domain.vo.WmsPurchasePlanDetailVo;
import com.klp.domain.vo.WmsRawMaterialVo;
import com.klp.service.IWmsPurchasePlanDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.klp.domain.bo.WmsStockBo;
@@ -37,7 +33,6 @@ public class WmsStockServiceImpl implements IWmsStockService {
private final WmsStockMapper baseMapper;
private final WmsWarehouseMapper warehouseMapper;
private final IWmsPurchasePlanDetailService purchasePlanDetailService;
/**
@@ -55,40 +50,9 @@ public class WmsStockServiceImpl implements IWmsStockService {
public TableDataInfo<WmsStockVo> queryPageList(WmsStockBo bo, PageQuery pageQuery) {
QueryWrapper<WmsStock> lqw = buildQueryWrapperPlus(bo);
Page<WmsStockVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
fillDemandInfo(result.getRecords());
return TableDataInfo.build(result);
}
/**
* 填充原材料在途信息
*/
private void fillDemandInfo(List<WmsStockVo> rawMaterialList) {
if (rawMaterialList == null || rawMaterialList.isEmpty()) {
return;
}
// 为每个原材料填充信息
for (WmsStockVo vo : rawMaterialList) {
Long rawMaterialId = vo.getItemId();
// 查询在途量(采购计划明细)
BigDecimal onTheWay = getOnTheWayQuantity(rawMaterialId);
vo.setOnTheWay(onTheWay);
}
}
/**
* 获取在途量
*/
private BigDecimal getOnTheWayQuantity(Long rawMaterialId) {
WmsPurchasePlanDetailBo bo = new WmsPurchasePlanDetailBo();
bo.setRawMaterialId(rawMaterialId);
List<WmsPurchasePlanDetailVo> list = purchasePlanDetailService.queryList(bo);
return list.stream()
.filter(item -> item.getStatus() != null && item.getStatus() == 1) // 在途状态
.map(WmsPurchasePlanDetailVo::getQuantity)
.filter(qty -> qty != null)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
/**
* 查询库存:原材料/产品与库区/库位的存放关系列表
*/
@@ -98,7 +62,6 @@ public class WmsStockServiceImpl implements IWmsStockService {
return baseMapper.selectVoList(lqw);
}
private LambdaQueryWrapper<WmsStock> buildQueryWrapper(WmsStockBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<WmsStock> lqw = new LambdaQueryWrapper<>();
lqw.eq(StringUtils.isNotBlank(bo.getItemType()), WmsStock::getItemType, bo.getItemType());
lqw.eq(bo.getItemId() != null, WmsStock::getItemId, bo.getItemId());
@@ -107,23 +70,43 @@ public class WmsStockServiceImpl implements IWmsStockService {
}
private QueryWrapper<WmsStock> buildQueryWrapperPlus(WmsStockBo bo) { // 注意:这里改用 QueryWrapper 而非 LambdaQueryWrapper
Map<String, Object> params = bo.getParams();
QueryWrapper<WmsStock> qw = Wrappers.query(); // 使用普通 QueryWrapper
qw.eq("s.del_flag", 0); // 手动添加逻辑删除条件
// 使用 select 添加聚合字段
qw.select(
"mc.warehouse_id AS warehouseId",
"mc.item_type AS itemType",
"mc.item_id AS itemId",
"w.warehouse_name AS warehouseName",
"CASE WHEN mc.item_type = 'product' THEN p.product_name WHEN mc.item_type = 'raw_material' THEN r.raw_material_name ELSE NULL END AS itemName",
"CASE WHEN mc.item_type = 'product' THEN p.product_code WHEN mc.item_type = 'raw_material' THEN r.raw_material_code ELSE NULL END AS itemCode",
"COUNT(*) AS totalQuantity",
"SUM(CASE WHEN mc.status = 1 THEN 1 ELSE 0 END) AS onTheWay"
);
// 处理仓库ID查询支持递归查询子节点
if (bo.getWarehouseId() != null) {
List<Long> warehouseIds = getWarehouseIdsWithChildren(bo.getWarehouseId());
if (warehouseIds.size() == 1) {
qw.eq("s.warehouse_id", warehouseIds.get(0));
qw.eq("mc.warehouse_id", warehouseIds.get(0));
} else {
qw.in("s.warehouse_id", warehouseIds);
qw.in("mc.warehouse_id", warehouseIds);
}
}
qw.eq(StringUtils.isNotBlank(bo.getItemType()), "s.item_type", bo.getItemType());
qw.eq(bo.getItemId() != null, "s.item_id", bo.getItemId());
qw.eq(StringUtils.isNotBlank(bo.getBatchNo()), "s.batch_no", bo.getBatchNo());
qw.eq(StringUtils.isNotBlank(bo.getItemType()), "mc.item_type", bo.getItemType());
qw.eq(bo.getItemId() != null, "mc.item_id", bo.getItemId());
qw.like(StringUtils.isNotBlank(bo.getBatchNo()), "mc.enter_coil_no", bo.getBatchNo());
// 使用 groupBy 进行分组
qw.groupBy(
"mc.warehouse_id",
"mc.item_type",
"mc.item_id",
"w.warehouse_name",
"CASE WHEN mc.item_type = 'product' THEN p.product_name WHEN mc.item_type = 'raw_material' THEN r.raw_material_name ELSE NULL END",
"CASE WHEN mc.item_type = 'product' THEN p.product_code WHEN mc.item_type = 'raw_material' THEN r.raw_material_code ELSE NULL END"
);
return qw;
}