feat: 订单管理商品规格选择器支持keywords模糊搜索及分页滚动

后端改动:
- ProductFilterDTO: 新增keywords字段支持模糊搜索商品名称/编码/规格名称/编码
- ProductSpecFilterDTO: 新增keywords字段支持模糊搜索规格名称/编码
- ProductServiceImpl: keywords查询时同时搜索商品和关联规格
- ProductSpecServiceImpl: 修复keywords查询时必须携带productId约束,避免跨商品查询

前端改动:
- 订单表单商品/规格选择器使用keywords字段搜索
- 支持无限滚动分页加载
- 商品搜索: 模糊匹配名称和编码
- 规格搜索: 仅限当前商品,模糊匹配名称和编码
This commit is contained in:
小蟑螂
2026-04-29 11:33:32 +08:00
parent 16039f18a0
commit d6206827d5
7 changed files with 225 additions and 93 deletions
@@ -38,4 +38,10 @@ public class ProductFilterDTO {
*/
@Schema(description = "规格名称(模糊查询)", example = "256GB")
private String specName;
/**
* 关键字(模糊查询商品名称、商品编码、规格名称、规格编码)
*/
@Schema(description = "关键字(模糊查询商品名称、商品编码、规格名称、规格编码)", example = "iPhone 256")
private String keywords;
}
@@ -33,4 +33,10 @@ public class ProductSpecFilterDTO {
*/
@Schema(description = "规格编码", example = "IPHONE15PRO-128G")
private String specCode;
/**
* 关键字(模糊查询规格名称、规格编码)
*/
@Schema(description = "关键字(模糊查询规格名称、规格编码)", example = "256GB")
private String keywords;
}
@@ -367,52 +367,79 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 添加过滤条件
ProductFilterDTO filter = pageQueryDTO.getFilters();
if (filter != null) {
// 商品名称(模糊查询)
if (filter.getName() != null && !filter.getName().trim().isEmpty()) {
wrapper.like(ProductPO::getName, filter.getName());
}
// 商品编码(精准查询)
if (filter.getProductCode() != null && !filter.getProductCode().trim().isEmpty()) {
wrapper.eq(ProductPO::getProductCode, filter.getProductCode());
}
// 类目ID(精准查询)
if (filter.getCategoryId() != null) {
wrapper.eq(ProductPO::getCategoryId, filter.getCategoryId());
}
// 处理规格相关的过滤条件
if ((filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) ||
(filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty())) {
// 构建规格查询条件
// 关键字查询(模糊查询商品名称、商品编码、规格名称、规格编码)
if (filter.getKeywords() != null && !filter.getKeywords().trim().isEmpty()) {
String keywords = filter.getKeywords().trim();
// 使用 OR 条件组合:商品名称 LIKE %keywords% OR 商品编码 LIKE %keywords%
// 或者通过规格关联查询:规格名称 LIKE %keywords% OR 规格编码 LIKE %keywords%
wrapper.and(w -> w
.like(ProductPO::getName, keywords)
.or()
.like(ProductPO::getProductCode, keywords)
);
// 同时通过规格关联查询匹配的规格
LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
specWrapper.eq(ProductSpecPO::getCompanyId, companyId);
// 规格编码(精准查询)
if (filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) {
specWrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode());
specWrapper.and(s -> s
.like(ProductSpecPO::getName, keywords)
.or()
.like(ProductSpecPO::getSpecCode, keywords)
);
List<ProductSpecPO> matchedSpecs = productSpecService.list(specWrapper);
if (!matchedSpecs.isEmpty()) {
List<Long> matchedProductIds = matchedSpecs.stream()
.map(ProductSpecPO::getProductId)
.collect(Collectors.toList());
wrapper.or().in(ProductPO::getId, matchedProductIds);
}
// 规格名称(模糊查询)
if (filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty()) {
specWrapper.like(ProductSpecPO::getName, filter.getSpecName());
} else {
// 商品名称(模糊查询)
if (filter.getName() != null && !filter.getName().trim().isEmpty()) {
wrapper.like(ProductPO::getName, filter.getName());
}
// 查询符合条件的规格,获取商品ID列表
List<ProductSpecPO> specs = productSpecService.list(specWrapper);
if (specs.isEmpty()) {
// 如果没有符合条件的规格,返回空结果
return PageResultVO.create(List.of(), 0L, pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
// 商品编码(精准查询)
if (filter.getProductCode() != null && !filter.getProductCode().trim().isEmpty()) {
wrapper.eq(ProductPO::getProductCode, filter.getProductCode());
}
// 类目ID(精准查询)
if (filter.getCategoryId() != null) {
wrapper.eq(ProductPO::getCategoryId, filter.getCategoryId());
}
// 处理规格相关的过滤条件
if ((filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) ||
(filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty())) {
// 构建规格查询条件
LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
specWrapper.eq(ProductSpecPO::getCompanyId, companyId);
// 规格编码(精准查询)
if (filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) {
specWrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode());
}
// 规格名称(模糊查询)
if (filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty()) {
specWrapper.like(ProductSpecPO::getName, filter.getSpecName());
}
// 查询符合条件的规格,获取商品ID列表
List<ProductSpecPO> specs = productSpecService.list(specWrapper);
if (specs.isEmpty()) {
// 如果没有符合条件的规格,返回空结果
return PageResultVO.create(List.of(), 0L, pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
}
// 提取商品ID列表
List<Long> productIds = specs.stream()
.map(ProductSpecPO::getProductId)
.distinct()
.collect(Collectors.toList());
// 添加商品ID条件
wrapper.in(ProductPO::getId, productIds);
}
// 提取商品ID列表
List<Long> productIds = specs.stream()
.map(ProductSpecPO::getProductId)
.distinct()
.collect(Collectors.toList());
// 添加商品ID条件
wrapper.in(ProductPO::getId, productIds);
}
}
@@ -182,14 +182,26 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
// 添加过滤条件
ProductSpecFilterDTO filter = pageQueryDTO.getFilters();
if (filter != null) {
// 必须有 productId 约束
if (filter.getProductId() != null) {
wrapper.eq(ProductSpecPO::getProductId, filter.getProductId());
}
if (filter.getName() != null && !filter.getName().trim().isEmpty()) {
wrapper.like(ProductSpecPO::getName, filter.getName());
}
if(filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) {
wrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode());
// 关键字查询(模糊查询规格名称、规格编码)
if (filter.getKeywords() != null && !filter.getKeywords().trim().isEmpty()) {
String keywords = filter.getKeywords().trim();
wrapper.and(w -> w
.like(ProductSpecPO::getName, keywords)
.or()
.like(ProductSpecPO::getSpecCode, keywords)
);
} else {
if (filter.getName() != null && !filter.getName().trim().isEmpty()) {
wrapper.like(ProductSpecPO::getName, filter.getName());
}
if(filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) {
wrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode());
}
}
}