From 531e7d7f372767a4eea2a771dcbe5b10b62187ce Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Mon, 15 Dec 2025 10:58:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(service):=20=E5=8F=96itemIds=E5=92=8Cs?= =?UTF-8?q?electType=E5=AF=B9=E5=BA=94=E7=9A=84itemIds=E5=8F=96=E4=BA=A4?= =?UTF-8?q?=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构 item_id 查询条件生成逻辑,支持 selectType 多字段筛选与显式 itemId 并行 - 添加 matchedItemIds 与 explicitItemIds 的交集合并逻辑 - 增强异常处理,避免筛选失败导致查询中断 - 支持逗号分隔的多 coilId 查询条件 - 保留原有单 itemId 查询兼容性 - 优化去重逻辑,确保 IN 列表不包含重复或空值 - 当最终 item_id 集合为空时,强制返回无结果查询条件 --- .../impl/WmsMaterialCoilServiceImpl.java | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/klp-wms/src/main/java/com/klp/service/impl/WmsMaterialCoilServiceImpl.java b/klp-wms/src/main/java/com/klp/service/impl/WmsMaterialCoilServiceImpl.java index 9ddc87bb..b57c7ec2 100644 --- a/klp-wms/src/main/java/com/klp/service/impl/WmsMaterialCoilServiceImpl.java +++ b/klp-wms/src/main/java/com/klp/service/impl/WmsMaterialCoilServiceImpl.java @@ -338,57 +338,67 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService { qw.eq(bo.getActualWarehouseId() != null, "mc.actual_warehouse_id", bo.getActualWarehouseId()); qw.eq(StringUtils.isNotBlank(bo.getItemType()), "mc.item_type", bo.getItemType()); - // 按 selectType + 细粒度字段筛选(若对应字段非空则拼接条件;为空则不拼) + // 组合 item_id 条件:selectType 细粒度筛选 与 显式 itemIds/itemId 的并行支持 + List matchedItemIds = null; // 来自 selectType + 多字段筛选 if (StringUtils.isNotBlank(bo.getSelectType())) { - List matchedItemIds = new ArrayList<>(); boolean hasAnyItemFilter = StringUtils.isNotBlank(bo.getItemMaterial()) || StringUtils.isNotBlank(bo.getItemManufacturer()) || StringUtils.isNotBlank(bo.getItemSurfaceTreatmentDesc()) || StringUtils.isNotBlank(bo.getItemZincLayer()) - || StringUtils.isNotBlank(bo.getItemName()) // 兼容性关键字 - || StringUtils.isNotBlank(bo.getItemSpecification()); // 添加规格筛选条件 + || StringUtils.isNotBlank(bo.getItemName()) + || StringUtils.isNotBlank(bo.getItemSpecification()); if (hasAnyItemFilter) { try { matchedItemIds = queryMatchedItemIds(bo.getSelectType(), bo); } catch (Exception e) { - // 异常时强制返回空结果,避免查询报错 - Log.error("筛选产品/原材料ID失败", e); - matchedItemIds = Collections.emptyList(); + Log.error("筛选产品/原材料ID失败", e); + matchedItemIds = Collections.emptyList(); } - - // 去重,避免生成过长的IN列表和重复ID - matchedItemIds = matchedItemIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); - if (matchedItemIds.isEmpty()) { - // 强制无结果 - qw.apply("1 = 0"); - return qw; - } else { - // 1. 先排除item_id为null的钢卷(若业务需要) - qw.isNotNull("mc.item_id"); - qw.in("mc.item_id", matchedItemIds); + if (matchedItemIds != null) { + matchedItemIds = matchedItemIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); } } } - // 修改itemId筛选逻辑,支持逗号分隔的多个ID查询 + + // 解析显式 itemIds 或单个 itemId + List explicitItemIds = null; if (StringUtils.isNotBlank(bo.getItemIds())) { String[] itemIdArray = bo.getItemIds().split(","); - List itemIdList = new ArrayList<>(); + List tmp = new ArrayList<>(); for (String itemIdStr : itemIdArray) { if (StringUtils.isNotBlank(itemIdStr)) { try { - itemIdList.add(Long.parseLong(itemIdStr.trim())); - } catch (NumberFormatException e) { - // 忽略无效的ID格式 - } + tmp.add(Long.parseLong(itemIdStr.trim())); + } catch (NumberFormatException ignore) {} } } - if (!itemIdList.isEmpty()) { - qw.in("mc.item_id", itemIdList); + if (!tmp.isEmpty()) { + explicitItemIds = tmp.stream().distinct().collect(Collectors.toList()); } } else if (bo.getItemId() != null) { - // 兼容原来的itemId单值查询 - qw.eq("mc.item_id", bo.getItemId()); + explicitItemIds = Collections.singletonList(bo.getItemId()); + } + + // 合并最终 item_id 条件:若两者都有则取交集;仅一者存在则使用那一者 + List finalItemIds = null; + if (matchedItemIds != null && explicitItemIds != null) { + // 交集 + Set set = new HashSet<>(explicitItemIds); + finalItemIds = matchedItemIds.stream().filter(set::contains).collect(Collectors.toList()); + } else if (matchedItemIds != null) { + finalItemIds = matchedItemIds; + } else if (explicitItemIds != null) { + finalItemIds = explicitItemIds; + } + + if (finalItemIds != null) { + if (finalItemIds.isEmpty()) { + qw.apply("1 = 0"); + return qw; + } + qw.isNotNull("mc.item_id"); + qw.in("mc.item_id", finalItemIds); } // 添加coilIds查询条件,支持逗号分隔的多个coilId查询 if (StringUtils.isNotBlank(bo.getCoilIds())) {