refactor(service): 取itemIds和selectType对应的itemIds取交集
- 重构 item_id 查询条件生成逻辑,支持 selectType 多字段筛选与显式 itemId 并行 - 添加 matchedItemIds 与 explicitItemIds 的交集合并逻辑 - 增强异常处理,避免筛选失败导致查询中断 - 支持逗号分隔的多 coilId 查询条件 - 保留原有单 itemId 查询兼容性 - 优化去重逻辑,确保 IN 列表不包含重复或空值 - 当最终 item_id 集合为空时,强制返回无结果查询条件
This commit is contained in:
@@ -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<Long> matchedItemIds = null; // 来自 selectType + 多字段筛选
|
||||
if (StringUtils.isNotBlank(bo.getSelectType())) {
|
||||
List<Long> 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<Long> explicitItemIds = null;
|
||||
if (StringUtils.isNotBlank(bo.getItemIds())) {
|
||||
String[] itemIdArray = bo.getItemIds().split(",");
|
||||
List<Long> itemIdList = new ArrayList<>();
|
||||
List<Long> 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<Long> finalItemIds = null;
|
||||
if (matchedItemIds != null && explicitItemIds != null) {
|
||||
// 交集
|
||||
Set<Long> 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())) {
|
||||
|
||||
Reference in New Issue
Block a user