diff --git a/src/views/mall/product/summary/SummaryForm.vue b/src/views/mall/product/summary/SummaryForm.vue deleted file mode 100644 index 1ee182d5..00000000 --- a/src/views/mall/product/summary/SummaryForm.vue +++ /dev/null @@ -1,248 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 确 定 - 取 消 - - - - diff --git a/src/views/mall/product/summary/index.vue b/src/views/mall/product/summary/index.vue index 914a016e..91052753 100644 --- a/src/views/mall/product/summary/index.vue +++ b/src/views/mall/product/summary/index.vue @@ -8,7 +8,7 @@ :inline="true" label-width="68px" > - + --> - + 搜索 重置 @@ -151,7 +151,7 @@ > 新增 - 导出 - + --> @@ -167,62 +167,78 @@ + + + + + + + + + + + + + + {{ + row.createTime ? dateFormatter(null, null, new Date(row.createTime)) : '' + }} + + + + + + - - + - + + + + {{ row.sources[0]?.type }} + + + + - - - - - - - - - - - - - + + + + + + + /> --> - + /> @@ -240,8 +256,9 @@ import { dateFormatter } from '@/utils/formatTime' import download from '@/utils/download' import { SummaryApi, SummaryVO } from '@/api/product/summary' -import SummaryForm from './SummaryForm.vue' +import * as ProductSpuApi from '@/api/mall/product/spu' import ProductTypeSelector from './ProductTypeSelector.vue' +import { onActivated, onMounted, reactive, ref } from 'vue' /** 商品汇总 列表 */ defineOptions({ name: 'Summary' }) @@ -255,81 +272,145 @@ const list = ref([]) // 列表的数据 const total = ref(0) // 列表的总页数 const showProductTypeDialog = ref(false) // 商品类型选择弹窗显示状态 -const queryParams = reactive({ +// 查询参数 +const queryParams = ref({ pageNo: 1, pageSize: 10, - /*商品编号,自增 */ - id: undefined, - - /*商品名称 */ - name: undefined, - - /*设备IMEI */ - deviceId: undefined, - - /*SKU 的条形码 */ - barCode: undefined, - - /*关键字 */ - keyword: undefined, - - /*商品简介 */ - introduction: undefined, - - /*商品详情 */ - description: undefined, - - /*商品分类编号 */ + tabType: 0, + name: '', categoryId: undefined, - - /*商品品牌编号 */ - brandId: undefined, - - /*商品封面图 */ - picUrl: undefined, - - /*商品轮播图地址数组,以逗号分隔 最多上传15张 */ - sliderPicUrls: undefined, - - /*排序字段 */ - sort: undefined, - - /*商品价格,单位使用:分 */ - price: undefined, - - /*市场价,单位使用:分 */ - marketPrice: undefined, - - /*成本价,单位: 分 */ - costPrice: undefined, - - /*创建时间 */ - createTime: [], - - /*库存 */ - stock: undefined, - - /*商品销量 */ - salesCount: undefined, + createTime: undefined, + types: 0, + barCode: undefined }) + const queryFormRef = ref() // 搜索的表单 const exportLoading = ref(false) // 导出的加载中 -/** 查询列表 */ +/** + * 获取所有商品数据(商城 + 提货柜) + */ +const fetchAllProducts = async () => { + const [mallResponse, containerResponse] = await Promise.all([ + ProductSpuApi.getSpuPage({ ...queryParams.value, types: 0 }), // 商城商品 + ProductSpuApi.getSpuPage({ ...queryParams.value, types: 1 }) // 提货柜商品 + ]) + + return [ + ...(mallResponse.list || []).map((product) => ({ ...product, source: 'mall' })), + ...(containerResponse.list || []).map((product) => ({ ...product, source: 'container' })) + ] +} + +/** + * 获取商品详细信息 + */ +const fetchProductDetails = async (products) => { + const spuIds = products.map((item) => item.id).filter(Boolean) + + if (spuIds.length === 0) return [] + + const detailResponse = await ProductSpuApi.getSpuDetailList(spuIds) + return (detailResponse || []).map((product) => { + const originalProduct = products.find((p) => p.id === product.id) + return { ...product, source: originalProduct?.source || 'unknown' } + }) +} + +/** 查询列表 - 修复分页问题 */ const getList = async () => { loading.value = true try { - const data = await SummaryApi.getSummaryPage(queryParams) - list.value = data.list - total.value = data.total + // 获取所有商品数据 + const allProducts = await fetchAllProducts() + + // 获取详细信息 + const detailedProducts = await fetchProductDetails(allProducts) + + // 按barCode分组合并商品 + const searchBarCode = queryParams.value.barCode?.trim() || null + const allGroupedProducts = groupProductsByBarCode(detailedProducts, searchBarCode) + + // 前端分页处理 + const { pageNo, pageSize } = queryParams.value + const startIndex = (pageNo - 1) * pageSize + const endIndex = startIndex + pageSize + + list.value = allGroupedProducts.slice(startIndex, endIndex) + total.value = allGroupedProducts.length + } catch (error) { + console.error('获取商品数据失败:', error) + message.error('获取商品数据失败') } finally { loading.value = false } } +/** + * 按barCode分组合并商品 - 优化版本 + */ +const groupProductsByBarCode = (products, searchBarCode = null) => { + const groupedMap = new Map() + + const addToGroup = (key, productData, skuData = null) => { + if (!groupedMap.has(key)) { + groupedMap.set(key, { + id: productData.id, + barCode: skuData?.barCode || '', + name: productData.name, + deviceId: productData.deviceId || '', + introduction: productData.introduction || '', + categoryId: productData.categoryId, + picUrl: productData.picUrl, + stock: 0, + salesCount: 0, + createTime: productData.createTime, + sources: [] + }) + } + + const grouped = groupedMap.get(key) + const stock = skuData?.stock || productData.stock || 0 + const salesCount = productData.salesCount || 0 + + grouped.stock += stock + grouped.salesCount += salesCount + grouped.sources.push({ + type: productData.source === 'mall' ? '商城商品' : '提货柜商品', + spuName: productData.name, + deviceId: productData.deviceId || '', + stock, + salesCount, + source: productData.source, + spuId: productData.id, + createTime: productData.createTime + }) + } + + products.forEach((product) => { + if (product.skus?.length > 0) { + // 处理有SKU数据的商品 + product.skus.forEach((sku) => { + if (!sku.barCode) return + + // 如果有搜索条件,只处理匹配的barCode + if (searchBarCode && sku.barCode !== searchBarCode) return + + addToGroup(sku.barCode, product, sku) + }) + } else { + // 处理没有SKU数据的商品 + const key = `spu_${product.id}` + addToGroup(key, product) + } + }) + + return Array.from(groupedMap.values()) +} + /** 搜索按钮操作 */ const handleQuery = () => { - queryParams.pageNo = 1 + queryParams.value.pageNo = 1 getList() } @@ -341,69 +422,80 @@ const resetQuery = () => { /** 处理商品类型确认选择 */ const handleProductTypeConfirm = (type: 'mall' | 'pickup') => { - // 立即关闭弹窗 showProductTypeDialog.value = false - // 根据选择的类型跳转到对应的添加页面 - if (type === 'mall') { - // 跳转到商城商品添加页面 - push({ name: 'ProductSpuAdd' }) - } else if (type === 'pickup') { - // 跳转到提货柜商品添加页面 - push({ name: 'ProductPickupAdd' }) + const routeMap = { + mall: 'ProductSpuAdd', + pickup: 'ProductPickupAdd' + } + + const routeName = routeMap[type] + if (routeName) { + push({ name: routeName }) } } /** 添加/修改操作 */ -const formRef = ref() -const openForm = (type: string, id?: number) => { - formRef.value.open(type, id) -} +// const formRef = ref() +// const openForm = (type: string, id?: number) => { +// formRef.value.open(type, id) +// } /** 删除按钮操作 */ -const handleDelete = async (id: number) => { - try { - // 删除的二次确认 - await message.delConfirm() - // 发起删除 - await SummaryApi.deleteSummary(id) - message.success(t('common.delSuccess')) - // 刷新列表 - await getList() - } catch {} -} +// const handleDelete = async (id: number) => { +// try { +// await message.delConfirm() +// await SummaryApi.deleteSummary(id) +// message.success(t('common.delSuccess')) +// await getList() +// } catch {} +// } /** 导出按钮操作 */ -const handleExport = async () => { - try { - // 导出的二次确认 - await message.exportConfirm() - // 发起导出 - exportLoading.value = true - const data = await SummaryApi.exportSummary(queryParams) - download.excel(data, '商品汇总.xls') - } catch { - } finally { - exportLoading.value = false - } -} +// const handleExport = async () => { +// try { +// await message.exportConfirm() +// exportLoading.value = true +// const data = await SummaryApi.exportSummary(queryParams.value) +// download.excel(data, '商品汇总.xls') +// message.success('导出成功') +// } catch (error) { +// console.error('导出失败:', error) +// message.error('导出失败,请检查网络连接或联系管理员') +// } finally { +// exportLoading.value = false +// } +// } -const productTypeSelectorKey = ref(0) // 添加 key 控制组件重新渲染 +const productTypeSelectorKey = ref(0) /** 页面激活时清除弹窗状态 */ -onActivated(() => { - // 确保从其他页面返回时弹窗是关闭的 +const resetPageState = () => { showProductTypeDialog.value = false - - // 强制重新渲染组件 productTypeSelectorKey.value++ - - // 重新获取数据 getList() -}) +} -/** 初始化 **/ +onActivated(resetPageState) onMounted(() => { getList() }) + +