refactor(service): 重构物料筛选逻辑以支持多条件查询
- 将产品和原材料的筛选逻辑提取到独立方法 queryMatchedItemIds - 支持对多个字段使用逗号分隔的多值查询 - 统一处理规格、材料、制造商等字段的模糊匹配 - 添加去重逻辑避免重复 ID 影响查询性能 - 简化主查询流程,提高代码可读性和维护性
This commit is contained in:
@@ -178,7 +178,6 @@ public class WmsMaterialCoilBo extends BaseEntity {
|
||||
private String itemSurfaceTreatmentDesc;
|
||||
// 锌层厚度(两表通用字段名:zinc_layer)
|
||||
private String itemZincLayer;
|
||||
|
||||
//规格
|
||||
private String itemSpecification;
|
||||
|
||||
|
||||
@@ -350,93 +350,15 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
|
||||
|
||||
if (hasAnyItemFilter) {
|
||||
try {
|
||||
if ("product".equals(bo.getSelectType())) {
|
||||
QueryWrapper<WmsProduct> pq = new QueryWrapper<>();
|
||||
pq.eq("del_flag", 0);
|
||||
if (StringUtils.isNotBlank(bo.getItemName())) {
|
||||
pq.like("product_name", bo.getItemName());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemMaterial())) {
|
||||
pq.like("material", bo.getItemMaterial());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemManufacturer())) {
|
||||
pq.like("manufacturer", bo.getItemManufacturer());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemSurfaceTreatmentDesc())) {
|
||||
pq.like("surface_treatment_desc", bo.getItemSurfaceTreatmentDesc());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemZincLayer())) {
|
||||
pq.like("zinc_layer", bo.getItemZincLayer());
|
||||
}
|
||||
// 添加规格筛选条件,支持逗号分隔的多个规格
|
||||
if (StringUtils.isNotBlank(bo.getItemSpecification())) {
|
||||
String[] specs = bo.getItemSpecification().split(",");
|
||||
if (specs.length == 1) {
|
||||
pq.like("specification", specs[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < specs.length; i++) {
|
||||
if (i == 0) {
|
||||
wrapper.like("specification", specs[i].trim());
|
||||
} else {
|
||||
wrapper.or().like("specification", specs[i].trim());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// MyBatis selectList默认返回空列表,无需判null,直接流式提取ID
|
||||
matchedItemIds = productMapper.selectList(pq).stream()
|
||||
.map(WmsProduct::getProductId)
|
||||
.filter(Objects::nonNull) // 过滤null的productId
|
||||
.collect(Collectors.toList());
|
||||
} else if ("raw_material".equals(bo.getSelectType())) {
|
||||
QueryWrapper<WmsRawMaterial> rq = new QueryWrapper<>();
|
||||
rq.eq("del_flag", 0);
|
||||
if (StringUtils.isNotBlank(bo.getItemName())) {
|
||||
rq.like("raw_material_name", bo.getItemName());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemMaterial())) {
|
||||
rq.like("material", bo.getItemMaterial());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemManufacturer())) {
|
||||
rq.like("manufacturer", bo.getItemManufacturer());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemSurfaceTreatmentDesc())) {
|
||||
rq.like("surface_treatment_desc", bo.getItemSurfaceTreatmentDesc());
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemZincLayer())) {
|
||||
rq.like("zinc_layer", bo.getItemZincLayer());
|
||||
}
|
||||
// 添加规格筛选条件,支持逗号分隔的多个规格
|
||||
if (StringUtils.isNotBlank(bo.getItemSpecification())) {
|
||||
String[] specs = bo.getItemSpecification().split(",");
|
||||
if (specs.length == 1) {
|
||||
rq.like("specification", specs[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < specs.length; i++) {
|
||||
if (i == 0) {
|
||||
wrapper.like("specification", specs[i].trim());
|
||||
} else {
|
||||
wrapper.or().like("specification", specs[i].trim());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// 流式提取rawMaterialId,过滤null
|
||||
matchedItemIds = rawMaterialMapper.selectList(rq).stream()
|
||||
.map(WmsRawMaterial::getRawMaterialId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
matchedItemIds = queryMatchedItemIds(bo.getSelectType(), bo);
|
||||
} catch (Exception e) {
|
||||
// 异常时强制返回空结果,避免查询报错
|
||||
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");
|
||||
@@ -512,6 +434,238 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
|
||||
return qw;
|
||||
}
|
||||
|
||||
public List<Long> queryMatchedItemIds(String selectType, WmsMaterialCoilBo bo) {
|
||||
List<Long> result = new ArrayList<>();
|
||||
if ("product".equals(selectType)) {
|
||||
QueryWrapper<WmsProduct> pq = new QueryWrapper<>();
|
||||
pq.eq("del_flag", 0);
|
||||
if (StringUtils.isNotBlank(bo.getItemName())) {
|
||||
String[] vals = bo.getItemName().split(",");
|
||||
if (vals.length == 1) {
|
||||
pq.like("product_name", vals[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("product_name", v);
|
||||
} else {
|
||||
wrapper.or().like("product_name", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemMaterial())) {
|
||||
String[] vals = bo.getItemMaterial().split(",");
|
||||
if (vals.length == 1) {
|
||||
pq.like("material", vals[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("material", v);
|
||||
} else {
|
||||
wrapper.or().like("material", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemManufacturer())) {
|
||||
String[] vals = bo.getItemManufacturer().split(",");
|
||||
if (vals.length == 1) {
|
||||
pq.like("manufacturer", vals[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("manufacturer", v);
|
||||
} else {
|
||||
wrapper.or().like("manufacturer", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemSurfaceTreatmentDesc())) {
|
||||
String[] vals = bo.getItemSurfaceTreatmentDesc().split(",");
|
||||
if (vals.length == 1) {
|
||||
pq.like("surface_treatment_desc", vals[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("surface_treatment_desc", v);
|
||||
} else {
|
||||
wrapper.or().like("surface_treatment_desc", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemZincLayer())) {
|
||||
String[] vals = bo.getItemZincLayer().split(",");
|
||||
if (vals.length == 1) {
|
||||
pq.like("zinc_layer", vals[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("zinc_layer", v);
|
||||
} else {
|
||||
wrapper.or().like("zinc_layer", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemSpecification())) {
|
||||
String[] specs = bo.getItemSpecification().split(",");
|
||||
if (specs.length == 1) {
|
||||
pq.like("specification", specs[0].trim());
|
||||
} else {
|
||||
pq.and(wrapper -> {
|
||||
for (int i = 0; i < specs.length; i++) {
|
||||
if (i == 0) {
|
||||
wrapper.like("specification", specs[i].trim());
|
||||
} else {
|
||||
wrapper.or().like("specification", specs[i].trim());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
result = productMapper.selectList(pq).stream()
|
||||
.map(WmsProduct::getProductId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
} else if ("raw_material".equals(selectType)) {
|
||||
QueryWrapper<WmsRawMaterial> rq = new QueryWrapper<>();
|
||||
rq.eq("del_flag", 0);
|
||||
if (StringUtils.isNotBlank(bo.getItemName())) {
|
||||
String[] vals = bo.getItemName().split(",");
|
||||
if (vals.length == 1) {
|
||||
rq.like("raw_material_name", vals[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("raw_material_name", v);
|
||||
} else {
|
||||
wrapper.or().like("raw_material_name", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemMaterial())) {
|
||||
String[] vals = bo.getItemMaterial().split(",");
|
||||
if (vals.length == 1) {
|
||||
rq.like("material", vals[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("material", v);
|
||||
} else {
|
||||
wrapper.or().like("material", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemManufacturer())) {
|
||||
String[] vals = bo.getItemManufacturer().split(",");
|
||||
if (vals.length == 1) {
|
||||
rq.like("manufacturer", vals[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("manufacturer", v);
|
||||
} else {
|
||||
wrapper.or().like("manufacturer", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemSurfaceTreatmentDesc())) {
|
||||
String[] vals = bo.getItemSurfaceTreatmentDesc().split(",");
|
||||
if (vals.length == 1) {
|
||||
rq.like("surface_treatment_desc", vals[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("surface_treatment_desc", v);
|
||||
} else {
|
||||
wrapper.or().like("surface_treatment_desc", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemZincLayer())) {
|
||||
String[] vals = bo.getItemZincLayer().split(",");
|
||||
if (vals.length == 1) {
|
||||
rq.like("zinc_layer", vals[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String v = vals[i].trim();
|
||||
if (v.isEmpty()) continue;
|
||||
if (i == 0) {
|
||||
wrapper.like("zinc_layer", v);
|
||||
} else {
|
||||
wrapper.or().like("zinc_layer", v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(bo.getItemSpecification())) {
|
||||
String[] specs = bo.getItemSpecification().split(",");
|
||||
if (specs.length == 1) {
|
||||
rq.like("specification", specs[0].trim());
|
||||
} else {
|
||||
rq.and(wrapper -> {
|
||||
for (int i = 0; i < specs.length; i++) {
|
||||
if (i == 0) {
|
||||
wrapper.like("specification", specs[i].trim());
|
||||
} else {
|
||||
wrapper.or().like("specification", specs[i].trim());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
result = rawMaterialMapper.selectList(rq).stream()
|
||||
.map(WmsRawMaterial::getRawMaterialId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询钢卷物料表列表
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user