feat: 优化销售统计详情数据获取逻辑,支持分页和数据合并
This commit is contained in:
1 parent
ccd74a3df6
commit
2395ce1cc9
2 files changed
+85
-38
No files matched your search
@@ -1,31 +1,29 @@
|
||||
import request from '@/config/axios'
|
||||
|
||||
// request.get 返回的是 res.data,所以这里直接定义 data 的结构
|
||||
export interface GetStatisticsDetailRes {
|
||||
code: number;
|
||||
data: {
|
||||
list: {
|
||||
id: number;
|
||||
userId: number;
|
||||
orderId: number;
|
||||
spuId: number;
|
||||
spuName: string;
|
||||
skuId: number;
|
||||
deviceId: string;
|
||||
barCode: string;
|
||||
cabinetChannel: string;
|
||||
picUrl: string;
|
||||
count: number;
|
||||
price: number;
|
||||
discountPrice: number;
|
||||
payPrice: number;
|
||||
orderPartPrice: number;
|
||||
orderDividePrice: number;
|
||||
afterSaleStatus: number;
|
||||
no: string;
|
||||
createTime: string; // 修改为正确的类型
|
||||
}[];
|
||||
total: number;
|
||||
};
|
||||
list: {
|
||||
id: number;
|
||||
userId: number;
|
||||
orderId: number;
|
||||
spuId: number;
|
||||
spuName: string;
|
||||
skuId: number;
|
||||
deviceId: string;
|
||||
barCode: string;
|
||||
cabinetChannel: string;
|
||||
picUrl: string;
|
||||
count: number;
|
||||
price: number;
|
||||
discountPrice: number;
|
||||
payPrice: number;
|
||||
orderPartPrice: number;
|
||||
orderDividePrice: number;
|
||||
afterSaleStatus: number;
|
||||
no: string;
|
||||
createTime: string;
|
||||
}[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
// 查询参数接口
|
||||
|
||||
@@ -168,22 +168,59 @@ const queryParams = reactive<StatisticsDetailPageReqVO>({
|
||||
const getAllData = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
// 先获取第一页数据,了解总数
|
||||
const firstPageData = await SalesStatisticsDetailsApi.getStatisticsDetailPage({
|
||||
...queryParams,
|
||||
pageNo: 1,
|
||||
pageSize: 1000
|
||||
})
|
||||
|
||||
if (firstPageData && firstPageData.list) {
|
||||
const sortedData = firstPageData.list.sort((a: any, b: any) => {
|
||||
console.log('API 返回的完整数据:', firstPageData)
|
||||
|
||||
const responseData = firstPageData as any
|
||||
if (responseData && responseData.list) {
|
||||
let allItems = [...responseData.list]
|
||||
const totalCount = responseData.total || 0
|
||||
const pageSize = 10
|
||||
|
||||
// 如果总数超过第一页的数量,需要继续获取剩余数据
|
||||
if (totalCount > pageSize) {
|
||||
const totalPages = Math.ceil(totalCount / pageSize)
|
||||
console.log(`总数据 ${totalCount} 条,需要获取 ${totalPages} 页`)
|
||||
|
||||
// 并发获取剩余页面的数据
|
||||
const promises: Promise<any>[] = []
|
||||
for (let page = 2; page <= totalPages; page++) {
|
||||
promises.push(
|
||||
SalesStatisticsDetailsApi.getStatisticsDetailPage({
|
||||
...queryParams,
|
||||
pageNo: page,
|
||||
pageSize: pageSize
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const remainingPages = await Promise.all(promises)
|
||||
remainingPages.forEach((pageData: any) => {
|
||||
if (pageData && pageData.list) {
|
||||
allItems = allItems.concat(pageData.list)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 打印 createTime 参数
|
||||
console.log('createTime 数据:', allItems.map((item: any) => item.createTime))
|
||||
console.log(`实际获取数据条数: ${allItems.length},总数据: ${totalCount}`)
|
||||
|
||||
const sortedData = allItems.sort((a: any, b: any) => {
|
||||
return new Date(b.createTime).getTime() - new Date(a.createTime).getTime()
|
||||
})
|
||||
|
||||
allData.value = sortedData
|
||||
total.value = firstPageData.total || 0
|
||||
total.value = totalCount
|
||||
updateCurrentPageData()
|
||||
|
||||
console.log("销售订单", firstPageData)
|
||||
} else {
|
||||
console.warn('数据格式异常,未找到 list 数据', firstPageData)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取数据失败:', error)
|
||||
@@ -194,10 +231,18 @@ const getAllData = async () => {
|
||||
}
|
||||
|
||||
const updateCurrentPageData = () => {
|
||||
const startIndex = (queryParams.pageNo - 1) * queryParams.pageSize
|
||||
const endIndex = startIndex + queryParams.pageSize
|
||||
const pageNo = queryParams.pageNo ?? 1
|
||||
const pageSize = queryParams.pageSize ?? 10
|
||||
const startIndex = (pageNo - 1) * pageSize
|
||||
const endIndex = startIndex + pageSize
|
||||
|
||||
if (queryParams.no || queryParams.deviceId || queryParams.barCode || queryParams.createTime) {
|
||||
// 检查是否有搜索条件
|
||||
const hasSearchCondition = (queryParams.no && queryParams.no.trim()) ||
|
||||
(queryParams.deviceId && queryParams.deviceId.trim()) ||
|
||||
(queryParams.barCode && queryParams.barCode.trim()) ||
|
||||
(queryParams.createTime && Array.isArray(queryParams.createTime) && queryParams.createTime.length === 2)
|
||||
|
||||
if (hasSearchCondition) {
|
||||
handleQuery()
|
||||
} else {
|
||||
list.value = allData.value.slice(startIndex, endIndex)
|
||||
@@ -220,13 +265,13 @@ const handleQuery = () => {
|
||||
|
||||
if (queryParams.deviceId && queryParams.deviceId.trim()) {
|
||||
filteredData = filteredData.filter((item: any) =>
|
||||
item.deviceId && String(item.deviceId).toLowerCase().includes(queryParams.deviceId.toLowerCase())
|
||||
item.deviceId && String(item.deviceId).toLowerCase().includes(queryParams.deviceId!.toLowerCase())
|
||||
)
|
||||
}
|
||||
|
||||
if (queryParams.barCode && queryParams.barCode.trim()) {
|
||||
filteredData = filteredData.filter((item: any) =>
|
||||
item.barCode && String(item.barCode).toLowerCase().includes(queryParams.barCode.toLowerCase())
|
||||
item.barCode && String(item.barCode).toLowerCase().includes(queryParams.barCode!.toLowerCase())
|
||||
)
|
||||
}
|
||||
|
||||
@@ -244,8 +289,10 @@ const handleQuery = () => {
|
||||
})
|
||||
|
||||
total.value = sortedData.length
|
||||
const startIndex = (queryParams.pageNo - 1) * queryParams.pageSize
|
||||
const endIndex = startIndex + queryParams.pageSize
|
||||
const pageNo = queryParams.pageNo ?? 1
|
||||
const pageSize = queryParams.pageSize ?? 10
|
||||
const startIndex = (pageNo - 1) * pageSize
|
||||
const endIndex = startIndex + pageSize
|
||||
list.value = sortedData.slice(startIndex, endIndex)
|
||||
}
|
||||
|
||||
@@ -280,7 +327,9 @@ const handleExport = async () => {
|
||||
|
||||
const formatTime = (timestamp: number | string) => {
|
||||
if (!timestamp) return '-'
|
||||
const date = new Date(Number(timestamp))
|
||||
// 如果是字符串格式的日期,直接使用;如果是数字时间戳,需要转换
|
||||
const date = typeof timestamp === 'string' ? new Date(timestamp) : new Date(Number(timestamp))
|
||||
if (isNaN(date.getTime())) return '-'
|
||||
return date.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
|
||||
Reference in new issue
Block a user