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") @Schema(description = "规格名称(模糊查询)", example = "256GB")
private String specName; private String specName;
/**
* 关键字(模糊查询商品名称、商品编码、规格名称、规格编码)
*/
@Schema(description = "关键字(模糊查询商品名称、商品编码、规格名称、规格编码)", example = "iPhone 256")
private String keywords;
} }
@@ -33,4 +33,10 @@ public class ProductSpecFilterDTO {
*/ */
@Schema(description = "规格编码", example = "IPHONE15PRO-128G") @Schema(description = "规格编码", example = "IPHONE15PRO-128G")
private String specCode; 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(); ProductFilterDTO filter = pageQueryDTO.getFilters();
if (filter != null) { if (filter != null) {
// 商品名称(模糊查询) // 关键字查询(模糊查询商品名称、商品编码、规格名称、规格编码)
if (filter.getName() != null && !filter.getName().trim().isEmpty()) { if (filter.getKeywords() != null && !filter.getKeywords().trim().isEmpty()) {
wrapper.like(ProductPO::getName, filter.getName()); String keywords = filter.getKeywords().trim();
} // 使用 OR 条件组合:商品名称 LIKE %keywords% OR 商品编码 LIKE %keywords%
// 商品编码(精准查询) // 或者通过规格关联查询:规格名称 LIKE %keywords% OR 规格编码 LIKE %keywords%
if (filter.getProductCode() != null && !filter.getProductCode().trim().isEmpty()) { wrapper.and(w -> w
wrapper.eq(ProductPO::getProductCode, filter.getProductCode()); .like(ProductPO::getName, keywords)
} .or()
.like(ProductPO::getProductCode, keywords)
// 类目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<>(); LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
specWrapper.eq(ProductSpecPO::getCompanyId, companyId); specWrapper.eq(ProductSpecPO::getCompanyId, companyId);
specWrapper.and(s -> s
// 规格编码(精准查询) .like(ProductSpecPO::getName, keywords)
if (filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) { .or()
specWrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode()); .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);
} }
// 规格名称(模糊查询) } else {
if (filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty()) { // 商品名称(模糊查询)
specWrapper.like(ProductSpecPO::getName, filter.getSpecName()); 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列表 // 类目ID(精准查询)
List<ProductSpecPO> specs = productSpecService.list(specWrapper); if (filter.getCategoryId() != null) {
if (specs.isEmpty()) { wrapper.eq(ProductPO::getCategoryId, filter.getCategoryId());
// 如果没有符合条件的规格,返回空结果
return PageResultVO.create(List.of(), 0L, pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
} }
// 提取商品ID列表 // 处理规格相关的过滤条件
List<Long> productIds = specs.stream() if ((filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) ||
.map(ProductSpecPO::getProductId) (filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty())) {
.distinct()
.collect(Collectors.toList());
// 添加商品ID条件 // 构建规格查询条件
wrapper.in(ProductPO::getId, productIds); 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);
}
} }
} }
@@ -182,14 +182,26 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
// 添加过滤条件 // 添加过滤条件
ProductSpecFilterDTO filter = pageQueryDTO.getFilters(); ProductSpecFilterDTO filter = pageQueryDTO.getFilters();
if (filter != null) { if (filter != null) {
// 必须有 productId 约束
if (filter.getProductId() != null) { if (filter.getProductId() != null) {
wrapper.eq(ProductSpecPO::getProductId, filter.getProductId()); wrapper.eq(ProductSpecPO::getProductId, filter.getProductId());
} }
if (filter.getName() != null && !filter.getName().trim().isEmpty()) {
wrapper.like(ProductSpecPO::getName, filter.getName()); // 关键字查询(模糊查询规格名称、规格编码)
} if (filter.getKeywords() != null && !filter.getKeywords().trim().isEmpty()) {
if(filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) { String keywords = filter.getKeywords().trim();
wrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode()); 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());
}
} }
} }
@@ -49,6 +49,8 @@ export namespace ProductSpecDTO {
name?: string; name?: string;
/** 规格编码 */ /** 规格编码 */
specCode?: string; specCode?: string;
/** 关键字(模糊查询规格名称、规格编码) */
keywords?: string;
} }
/** 商品规格查询DTO(用于批量查询) */ /** 商品规格查询DTO(用于批量查询) */
@@ -93,6 +93,8 @@ export namespace ProductDTO {
specCode?: string; specCode?: string;
/** 规格名称(模糊查询) */ /** 规格名称(模糊查询) */
specName?: string; specName?: string;
/** 关键字(模糊查询商品名称、商品编码、规格名称、规格编码) */
keywords?: string;
} }
/** 商品规格过滤条件 */ /** 商品规格过滤条件 */
@@ -78,19 +78,29 @@ const formData = ref<Partial<OrderDTO.Create>>({
const shopOptions = ref<{ label: string; value: number }[]>([]); const shopOptions = ref<{ label: string; value: number }[]>([]);
const orderStatusOptions = ref<{ label: string; value: number }[]>([]); const orderStatusOptions = ref<{ label: string; value: number }[]>([]);
// 商品选项数据 // 商品选项数据(支持分页)
const productOptions = ref< const productOptions = ref<
{ label: string; product: ProductVO.Info; value: number }[] { label: string; product: ProductVO.Info; value: number }[]
>([]); >([]);
const productLoading = ref(false); const productLoading = ref(false);
const productSearchKeyword = ref(''); const productSearchKeyword = ref('');
const productPage = ref(1);
const productPageSize = 50;
const productTotal = ref(0);
const productLoaded = ref(false);
let productSearchTimer: null | ReturnType<typeof setTimeout> = null;
// 规格选项数据 // 规格选项数据(支持分页)
const productSpecOptions = ref< const productSpecOptions = ref<
{ label: string; spec: ProductSpecVO.Info; value: number }[] { label: string; spec: ProductSpecVO.Info; value: number }[]
>([]); >([]);
const productSpecLoading = ref(false); const productSpecLoading = ref(false);
const productSpecSearchKeyword = ref(''); const productSpecSearchKeyword = ref('');
const specPage = ref(1);
const specPageSize = 50;
const specTotal = ref(0);
const specLoaded = ref(false);
let specSearchTimer: null | ReturnType<typeof setTimeout> = null;
// 表单验证规则 // 表单验证规则
const rules = { const rules = {
@@ -143,63 +153,131 @@ async function fetchOrderStatusOptions() {
} }
} }
// 搜索商品 // 分页加载商品列表
async function searchProducts(keyword: string) { async function fetchProducts(page = 1, append = false) {
productSearchKeyword.value = keyword; if (productLoading.value) return;
productLoading.value = true; productLoading.value = true;
try { try {
const params: PageQueryDTO<ProductDTO.Filters> = { const params: PageQueryDTO<ProductDTO.Filters> = {
page: 1, page,
pageSize: 50, pageSize: productPageSize,
filters: { filters: {
name: keyword || undefined, keywords: productSearchKeyword.value || undefined,
}, },
}; };
const data = await getProductPageApi(params); const data = await getProductPageApi(params);
productOptions.value = data.records.map((product: ProductVO.Info) => ({ productOptions.value = append
label: `${product.name}${product.productCode ? ` (${product.productCode})` : ''}`, ? [
value: product.id, ...productOptions.value,
product, ...data.records.map((product: ProductVO.Info) => ({
})); label: `${product.name}${product.productCode ? ` (${product.productCode})` : ''}`,
value: product.id,
product,
})),
]
: data.records.map((product: ProductVO.Info) => ({
label: `${product.name}${product.productCode ? ` (${product.productCode})` : ''}`,
value: product.id,
product,
}));
productTotal.value = data.total;
productPage.value = page;
productLoaded.value = true;
} catch (error) { } catch (error) {
console.error('搜索商品失败:', error); console.error('获取商品列表失败:', error);
message.error('搜索商品失败'); message.error('获取商品列表失败');
} finally { } finally {
productLoading.value = false; productLoading.value = false;
} }
} }
// 搜索规格 // 搜索商品(防抖)
async function searchProductSpecs(keyword: string) { function handleProductSearch(value: string) {
if (productSearchTimer) clearTimeout(productSearchTimer);
productSearchTimer = setTimeout(() => {
productSearchKeyword.value = value;
productPage.value = 1;
fetchProducts(1, false);
}, 300);
}
// 滚动加载更多商品
function handleProductPopupScroll(e: Event) {
const target = e.target as HTMLElement;
const { scrollTop, scrollHeight, clientHeight } = target;
if (scrollHeight - scrollTop - clientHeight < 50) {
const hasMore = productPage.value * productPageSize < productTotal.value;
if (hasMore && !productLoading.value) {
fetchProducts(productPage.value + 1, true);
}
}
}
// 分页加载规格列表
async function fetchProductSpecs(page = 1, append = false) {
if (!formData.value.productId) { if (!formData.value.productId) {
productSpecOptions.value = []; productSpecOptions.value = [];
return; return;
} }
productSpecSearchKeyword.value = keyword; if (productSpecLoading.value) return;
productSpecLoading.value = true; productSpecLoading.value = true;
try { try {
const params: PageQueryDTO<ProductSpecDTO.Filters> = { const params: PageQueryDTO<ProductSpecDTO.Filters> = {
page: 1, page,
pageSize: 50, pageSize: specPageSize,
filters: { filters: {
productId: formData.value.productId, productId: formData.value.productId,
name: keyword || undefined, keywords: productSpecSearchKeyword.value || undefined,
}, },
}; };
const data = await getProductSpecPageApi(params); const data = await getProductSpecPageApi(params);
productSpecOptions.value = data.records.map((spec: ProductSpecVO.Info) => ({ productSpecOptions.value = append
label: `${spec.name}${spec.specCode ? ` (${spec.specCode})` : ''}`, ? [
value: spec.id, ...productSpecOptions.value,
spec, ...data.records.map((spec: ProductSpecVO.Info) => ({
})); label: `${spec.name}${spec.specCode ? ` (${spec.specCode})` : ''}`,
value: spec.id,
spec,
})),
]
: data.records.map((spec: ProductSpecVO.Info) => ({
label: `${spec.name}${spec.specCode ? ` (${spec.specCode})` : ''}`,
value: spec.id,
spec,
}));
specTotal.value = data.total;
specPage.value = page;
specLoaded.value = true;
} catch (error) { } catch (error) {
console.error('搜索规格失败:', error); console.error('获取规格列表失败:', error);
message.error('搜索规格失败'); message.error('获取规格列表失败');
} finally { } finally {
productSpecLoading.value = false; productSpecLoading.value = false;
} }
} }
// 搜索规格(防抖)
function handleProductSpecSearch(value: string) {
if (specSearchTimer) clearTimeout(specSearchTimer);
specSearchTimer = setTimeout(() => {
productSpecSearchKeyword.value = value;
specPage.value = 1;
fetchProductSpecs(1, false);
}, 300);
}
// 滚动加载更多规格
function handleSpecPopupScroll(e: Event) {
const target = e.target as HTMLElement;
const { scrollTop, scrollHeight, clientHeight } = target;
if (scrollHeight - scrollTop - clientHeight < 50) {
const hasMore = specPage.value * specPageSize < specTotal.value;
if (hasMore && !productSpecLoading.value) {
fetchProductSpecs(specPage.value + 1, true);
}
}
}
// 处理商品选择 // 处理商品选择
function handleProductChange(productId: number | undefined) { function handleProductChange(productId: number | undefined) {
if (productId) { if (productId) {
@@ -208,9 +286,17 @@ function handleProductChange(productId: number | undefined) {
); );
if (selectedProduct) { if (selectedProduct) {
formData.value.linkProductName = selectedProduct.product.name; formData.value.linkProductName = selectedProduct.product.name;
// 加载该商品的规格列表
searchProductSpecs('');
} }
// 重置规格状态,重新加载
specLoaded.value = false;
specPage.value = 1;
productSpecOptions.value = [];
formData.value.productSpecId = undefined;
formData.value.linkSpecName = '';
formData.value.productCost = 0;
formData.value.shippingCost = 0;
formData.value.otherCost = 0;
fetchProductSpecs(1, false);
} else { } else {
formData.value.linkProductName = ''; formData.value.linkProductName = '';
formData.value.productSpecId = undefined; formData.value.productSpecId = undefined;
@@ -242,19 +328,6 @@ function handleProductSpecChange(specId: number | undefined) {
} }
} }
// 监听商品ID变化,加载规格列表
watch(
() => formData.value.productId,
(newProductId) => {
if (newProductId) {
searchProductSpecs('');
} else {
productSpecOptions.value = [];
formData.value.productSpecId = undefined;
}
},
);
// 打开模态框 // 打开模态框
async function open(edit: boolean, record?: OrderVO.Info) { async function open(edit: boolean, record?: OrderVO.Info) {
isEdit.value = edit; isEdit.value = edit;
@@ -308,7 +381,7 @@ async function open(edit: boolean, record?: OrderVO.Info) {
// 如果是编辑模式且有商品ID,加载商品和规格选项 // 如果是编辑模式且有商品ID,加载商品和规格选项
if (edit && record?.productId) { if (edit && record?.productId) {
await searchProducts(record.linkProductName || ''); await fetchProducts(1, false);
// 确保选中的商品在选项中 // 确保选中的商品在选项中
if (!productOptions.value.some((opt) => opt.value === record.productId)) { if (!productOptions.value.some((opt) => opt.value === record.productId)) {
productOptions.value.push({ productOptions.value.push({
@@ -321,7 +394,7 @@ async function open(edit: boolean, record?: OrderVO.Info) {
}); });
} }
if (record.productSpecId) { if (record.productSpecId) {
await searchProductSpecs(record.linkSpecName || ''); await fetchProductSpecs(1, false);
// 确保选中的规格在选项中 // 确保选中的规格在选项中
if ( if (
!productSpecOptions.value.some( !productSpecOptions.value.some(
@@ -473,7 +546,9 @@ defineExpose({
:filter-option="false" :filter-option="false"
:loading="productLoading" :loading="productLoading"
:options="productOptions" :options="productOptions"
@search="searchProducts" @search="handleProductSearch"
@focus="() => { if (!productLoaded) fetchProducts(1, false); }"
@popup-scroll="handleProductPopupScroll"
@change="(value) => handleProductChange(value as number)" @change="(value) => handleProductChange(value as number)"
> >
<template #notFoundContent> <template #notFoundContent>
@@ -497,7 +572,9 @@ defineExpose({
:loading="productSpecLoading" :loading="productSpecLoading"
:disabled="!formData.productId" :disabled="!formData.productId"
:options="productSpecOptions" :options="productSpecOptions"
@search="searchProductSpecs" @search="handleProductSpecSearch"
@focus="() => { if (!specLoaded && formData.productId) fetchProductSpecs(1, false); }"
@popup-scroll="handleSpecPopupScroll"
@change="(value) => handleProductSpecChange(value as number)" @change="(value) => handleProductSpecChange(value as number)"
> >
<template #notFoundContent> <template #notFoundContent>