mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
feat: 订单管理商品规格选择器支持keywords模糊搜索及分页滚动
后端改动: - ProductFilterDTO: 新增keywords字段支持模糊搜索商品名称/编码/规格名称/编码 - ProductSpecFilterDTO: 新增keywords字段支持模糊搜索规格名称/编码 - ProductServiceImpl: keywords查询时同时搜索商品和关联规格 - ProductSpecServiceImpl: 修复keywords查询时必须携带productId约束,避免跨商品查询 前端改动: - 订单表单商品/规格选择器使用keywords字段搜索 - 支持无限滚动分页加载 - 商品搜索: 模糊匹配名称和编码 - 规格搜索: 仅限当前商品,模糊匹配名称和编码
This commit is contained in:
+6
@@ -38,4 +38,10 @@ public class ProductFilterDTO {
|
||||
*/
|
||||
@Schema(description = "规格名称(模糊查询)", example = "256GB")
|
||||
private String specName;
|
||||
|
||||
/**
|
||||
* 关键字(模糊查询商品名称、商品编码、规格名称、规格编码)
|
||||
*/
|
||||
@Schema(description = "关键字(模糊查询商品名称、商品编码、规格名称、规格编码)", example = "iPhone 256")
|
||||
private String keywords;
|
||||
}
|
||||
|
||||
+6
@@ -33,4 +33,10 @@ public class ProductSpecFilterDTO {
|
||||
*/
|
||||
@Schema(description = "规格编码", example = "IPHONE15PRO-128G")
|
||||
private String specCode;
|
||||
|
||||
/**
|
||||
* 关键字(模糊查询规格名称、规格编码)
|
||||
*/
|
||||
@Schema(description = "关键字(模糊查询规格名称、规格编码)", example = "256GB")
|
||||
private String keywords;
|
||||
}
|
||||
|
||||
+68
-41
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-5
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,8 @@ export namespace ProductSpecDTO {
|
||||
name?: string;
|
||||
/** 规格编码 */
|
||||
specCode?: string;
|
||||
/** 关键字(模糊查询规格名称、规格编码) */
|
||||
keywords?: string;
|
||||
}
|
||||
|
||||
/** 商品规格查询DTO(用于批量查询) */
|
||||
|
||||
@@ -93,6 +93,8 @@ export namespace ProductDTO {
|
||||
specCode?: string;
|
||||
/** 规格名称(模糊查询) */
|
||||
specName?: string;
|
||||
/** 关键字(模糊查询商品名称、商品编码、规格名称、规格编码) */
|
||||
keywords?: string;
|
||||
}
|
||||
|
||||
/** 商品规格过滤条件 */
|
||||
|
||||
+124
-47
@@ -78,19 +78,29 @@ const formData = ref<Partial<OrderDTO.Create>>({
|
||||
const shopOptions = ref<{ label: string; value: number }[]>([]);
|
||||
const orderStatusOptions = ref<{ label: string; value: number }[]>([]);
|
||||
|
||||
// 商品选项数据
|
||||
// 商品选项数据(支持分页)
|
||||
const productOptions = ref<
|
||||
{ label: string; product: ProductVO.Info; value: number }[]
|
||||
>([]);
|
||||
const productLoading = ref(false);
|
||||
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<
|
||||
{ label: string; spec: ProductSpecVO.Info; value: number }[]
|
||||
>([]);
|
||||
const productSpecLoading = ref(false);
|
||||
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 = {
|
||||
@@ -143,63 +153,131 @@ async function fetchOrderStatusOptions() {
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索商品
|
||||
async function searchProducts(keyword: string) {
|
||||
productSearchKeyword.value = keyword;
|
||||
// 分页加载商品列表
|
||||
async function fetchProducts(page = 1, append = false) {
|
||||
if (productLoading.value) return;
|
||||
productLoading.value = true;
|
||||
try {
|
||||
const params: PageQueryDTO<ProductDTO.Filters> = {
|
||||
page: 1,
|
||||
pageSize: 50,
|
||||
page,
|
||||
pageSize: productPageSize,
|
||||
filters: {
|
||||
name: keyword || undefined,
|
||||
keywords: productSearchKeyword.value || undefined,
|
||||
},
|
||||
};
|
||||
const data = await getProductPageApi(params);
|
||||
productOptions.value = data.records.map((product: ProductVO.Info) => ({
|
||||
label: `${product.name}${product.productCode ? ` (${product.productCode})` : ''}`,
|
||||
value: product.id,
|
||||
product,
|
||||
}));
|
||||
productOptions.value = append
|
||||
? [
|
||||
...productOptions.value,
|
||||
...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) {
|
||||
console.error('搜索商品失败:', error);
|
||||
message.error('搜索商品失败');
|
||||
console.error('获取商品列表失败:', error);
|
||||
message.error('获取商品列表失败');
|
||||
} finally {
|
||||
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) {
|
||||
productSpecOptions.value = [];
|
||||
return;
|
||||
}
|
||||
productSpecSearchKeyword.value = keyword;
|
||||
if (productSpecLoading.value) return;
|
||||
productSpecLoading.value = true;
|
||||
try {
|
||||
const params: PageQueryDTO<ProductSpecDTO.Filters> = {
|
||||
page: 1,
|
||||
pageSize: 50,
|
||||
page,
|
||||
pageSize: specPageSize,
|
||||
filters: {
|
||||
productId: formData.value.productId,
|
||||
name: keyword || undefined,
|
||||
keywords: productSpecSearchKeyword.value || undefined,
|
||||
},
|
||||
};
|
||||
const data = await getProductSpecPageApi(params);
|
||||
productSpecOptions.value = data.records.map((spec: ProductSpecVO.Info) => ({
|
||||
label: `${spec.name}${spec.specCode ? ` (${spec.specCode})` : ''}`,
|
||||
value: spec.id,
|
||||
spec,
|
||||
}));
|
||||
productSpecOptions.value = append
|
||||
? [
|
||||
...productSpecOptions.value,
|
||||
...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) {
|
||||
console.error('搜索规格失败:', error);
|
||||
message.error('搜索规格失败');
|
||||
console.error('获取规格列表失败:', error);
|
||||
message.error('获取规格列表失败');
|
||||
} finally {
|
||||
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) {
|
||||
if (productId) {
|
||||
@@ -208,9 +286,17 @@ function handleProductChange(productId: number | undefined) {
|
||||
);
|
||||
if (selectedProduct) {
|
||||
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 {
|
||||
formData.value.linkProductName = '';
|
||||
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) {
|
||||
isEdit.value = edit;
|
||||
@@ -308,7 +381,7 @@ async function open(edit: boolean, record?: OrderVO.Info) {
|
||||
|
||||
// 如果是编辑模式且有商品ID,加载商品和规格选项
|
||||
if (edit && record?.productId) {
|
||||
await searchProducts(record.linkProductName || '');
|
||||
await fetchProducts(1, false);
|
||||
// 确保选中的商品在选项中
|
||||
if (!productOptions.value.some((opt) => opt.value === record.productId)) {
|
||||
productOptions.value.push({
|
||||
@@ -321,7 +394,7 @@ async function open(edit: boolean, record?: OrderVO.Info) {
|
||||
});
|
||||
}
|
||||
if (record.productSpecId) {
|
||||
await searchProductSpecs(record.linkSpecName || '');
|
||||
await fetchProductSpecs(1, false);
|
||||
// 确保选中的规格在选项中
|
||||
if (
|
||||
!productSpecOptions.value.some(
|
||||
@@ -473,7 +546,9 @@ defineExpose({
|
||||
:filter-option="false"
|
||||
:loading="productLoading"
|
||||
:options="productOptions"
|
||||
@search="searchProducts"
|
||||
@search="handleProductSearch"
|
||||
@focus="() => { if (!productLoaded) fetchProducts(1, false); }"
|
||||
@popup-scroll="handleProductPopupScroll"
|
||||
@change="(value) => handleProductChange(value as number)"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
@@ -497,7 +572,9 @@ defineExpose({
|
||||
:loading="productSpecLoading"
|
||||
:disabled="!formData.productId"
|
||||
:options="productSpecOptions"
|
||||
@search="searchProductSpecs"
|
||||
@search="handleProductSpecSearch"
|
||||
@focus="() => { if (!specLoaded && formData.productId) fetchProductSpecs(1, false); }"
|
||||
@popup-scroll="handleSpecPopupScroll"
|
||||
@change="(value) => handleProductSpecChange(value as number)"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
|
||||
Reference in New Issue
Block a user