修复库存分布问题
This commit is contained in:
@@ -73,6 +73,11 @@ public class WmsStockVo {
|
||||
@ExcelProperty(value = "仓库/库区名称")
|
||||
private String warehouseName;
|
||||
|
||||
/**
|
||||
* 状态(0=在库,1=在途,2=已出库)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 在途量
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -23,31 +23,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
where item_id = #{rawMaterialId} and item_type = 'raw_material' and del_flag = 0
|
||||
</select>
|
||||
|
||||
<!-- 分页联查物品名称和编码,支持Wrapper动态条件,返回Page<WmsStockVo> -->
|
||||
<!-- 查询物料库存统计,基于物料钢卷表,支持Wrapper动态条件(包含group by),返回Page<WmsStockVo> -->
|
||||
<select id="selectVoPagePlus" resultType="com.klp.domain.vo.WmsStockVo">
|
||||
SELECT
|
||||
s.*,
|
||||
w.warehouse_name,
|
||||
mc.warehouse_id AS warehouseId,
|
||||
mc.item_type AS itemType,
|
||||
mc.item_id AS itemId,
|
||||
w.warehouse_name AS warehouseName,
|
||||
CASE
|
||||
WHEN s.item_type = 'product' THEN p.product_name
|
||||
WHEN s.item_type = 'raw_material' THEN r.raw_material_name
|
||||
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 s.item_type = 'product' THEN p.product_code
|
||||
WHEN s.item_type = 'raw_material' THEN r.raw_material_code
|
||||
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,
|
||||
(SELECT COUNT(*)
|
||||
FROM wms_material_coil mc
|
||||
WHERE mc.item_type = s.item_type
|
||||
AND mc.item_id = s.item_id
|
||||
AND mc.del_flag = 0) AS totalQuantity
|
||||
FROM wms_stock s
|
||||
LEFT JOIN wms_product p ON s.item_type = 'product' AND s.item_id = p.product_id and p.del_flag = 0
|
||||
LEFT JOIN wms_raw_material r ON s.item_type = 'raw_material' AND s.item_id = r.raw_material_id and r.del_flag = 0
|
||||
left join wms_warehouse w on s.warehouse_id = w.warehouse_id and w.del_flag = 0
|
||||
${ew.customSqlSegment}
|
||||
END AS itemCode
|
||||
${ew.customSqlSegment}
|
||||
FROM wms_material_coil mc
|
||||
LEFT JOIN wms_warehouse w ON mc.warehouse_id = w.warehouse_id AND w.del_flag = 0
|
||||
LEFT JOIN wms_product p ON mc.item_type = 'product' AND mc.item_id = p.product_id AND p.del_flag = 0
|
||||
LEFT JOIN wms_raw_material r ON mc.item_type = 'raw_material' AND mc.item_id = r.raw_material_id AND r.del_flag = 0
|
||||
WHERE mc.del_flag = 0 AND mc.data_type = 1
|
||||
</select>
|
||||
|
||||
<!-- 按仓库统计库存分布 -->
|
||||
|
||||
Reference in New Issue
Block a user