fix(admin): 订单列表默认近7天并按支付时间筛选

避免无日期过滤时展示全库,关键词搜索不受日期限制;SSE 插入裁剪并尊重筛选,新单入库优先使用平台支付时间。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-09-15 13:41:29 +08:00
co-authored by Cursor
parent b7d246e5b9
commit 40c51e1d8e
7 changed files with 279 additions and 58 deletions
+104 -11
View File
@@ -53,6 +53,7 @@ import {
} from '@/utils/admin-auth'
import { formatAdminDateTime } from '@/utils/admin-time'
import { useRealtimeEvents, type RealtimeEvent } from '@/hooks/useRealtimeEvents'
import dayjs from '@/lib/dayjs'
const SIDEBAR_COLLAPSED_KEY = 'react-admin-sidebar-collapsed'
const NOTIFICATION_SOUND_KEY = 'admin-notification-sound-enabled'
@@ -493,35 +494,82 @@ function patchAdminOrderCaches(
queryClient: ReturnType<typeof useQueryClient>,
event: RealtimeEvent,
) {
const item = event.data as { orderId?: unknown; platformOrderId?: unknown; payStatus?: unknown }
const item = event.data as {
orderId?: unknown
platformOrderId?: unknown
payStatus?: unknown
paidAt?: unknown
createdAt?: unknown
provider?: unknown
shopName?: unknown
itemTitle?: unknown
itemSummary?: unknown
}
for (const [queryKey, current] of queryClient.getQueriesData({ queryKey: ['admin-orders'] })) {
const params = isRecord(queryKey[1]) ? queryKey[1] : {}
const page = Number(params.page || 1)
const matches =
(!String(params.platformOrderId || '').trim() ||
String(item.platformOrderId || '') === String(params.platformOrderId || '').trim()) &&
const pageSize = Math.max(1, Number(params.pageSize || 20) || 20)
const keyword = String(params.keyword || '').trim()
const matchesFilters =
matchesAdminOrderKeyword(item, keyword) &&
matchesAdminOrderProvider(item, String(params.provider || '').trim()) &&
(!String(params.payStatus || '').trim() ||
String(item.payStatus || '') === String(params.payStatus || '').trim())
const shouldInsert = event.operation === 'upsert' && page === 1 && matches && !params.skuCode
const operation = event.operation === 'remove' || !matches ? 'remove' : 'upsert'
String(item.payStatus || '') === String(params.payStatus || '').trim()) &&
// 有关键词时不限日期;否则实时插入也要落在当前日期筛选内。
(keyword ? true : matchesAdminOrderDateRange(item, params))
const shouldInsert =
event.operation === 'upsert' && page === 1 && matchesFilters && !params.skuCode
const operation = event.operation === 'remove' || !matchesFilters ? 'remove' : 'upsert'
queryClient.setQueryData(
queryKey,
patchAdminOrderListEnvelope(current, { ...event, operation }, shouldInsert),
patchAdminOrderListEnvelope(current, { ...event, operation }, shouldInsert, pageSize),
)
}
}
function patchAdminOrderListEnvelope(current: unknown, event: RealtimeEvent, insert = false) {
function patchAdminOrderListEnvelope(
current: unknown,
event: RealtimeEvent,
insert = false,
pageSize = 20,
) {
if (!isRecord(current) || !isRecord(current.data) || !Array.isArray(current.data.items)) {
return current
}
const id = Number((event.data as { orderId?: unknown }).orderId || event.entityId)
const items = current.data.items as unknown[]
const nextItems =
const existed = items.some((entry) => Number((entry as { orderId?: unknown })?.orderId) === id)
let nextItems =
event.operation === 'remove'
? items.filter((entry) => Number((entry as { orderId?: unknown })?.orderId) !== id)
: upsertById(items, event.data, 'orderId', insert)
return { ...current, data: { ...current.data, items: nextItems } }
if (insert && nextItems.length > pageSize) {
nextItems = nextItems.slice(0, pageSize)
}
const pagination = isRecord(current.data.pagination) ? current.data.pagination : null
let nextPagination = pagination
if (pagination && insert && !existed) {
nextPagination = {
...pagination,
total: Number(pagination.total || 0) + 1,
}
} else if (pagination && event.operation === 'remove' && existed) {
nextPagination = {
...pagination,
total: Math.max(0, Number(pagination.total || 0) - 1),
}
}
return {
...current,
data: {
...current.data,
items: nextItems,
...(nextPagination ? { pagination: nextPagination } : {}),
},
}
}
function patchWorkOrderListEnvelope(current: unknown, event: RealtimeEvent, insert = false) {
@@ -560,6 +608,51 @@ function upsertById(items: unknown[], nextItem: unknown, idKey: string, insert =
return items.map((item, itemIndex) => (itemIndex === index ? nextItem : item))
}
function matchesAdminOrderKeyword(
item: {
platformOrderId?: unknown
shopName?: unknown
itemTitle?: unknown
itemSummary?: unknown
},
keyword: string,
) {
const normalizedKeyword = keyword.trim().toLowerCase()
if (!normalizedKeyword) return true
return [item.platformOrderId, item.shopName, item.itemTitle, item.itemSummary]
.map((value) => String(value || '').toLowerCase())
.some((value) => value.includes(normalizedKeyword))
}
function matchesAdminOrderProvider(item: { provider?: unknown }, provider: string) {
const normalized = provider.trim().toLowerCase()
if (!normalized) return true
const value = String(item.provider || '').toLowerCase()
if (normalized === 'pdd') return value === 'pdd'
if (normalized === '91' || normalized === '91kaquan' || normalized === 'open_91') {
return ['91kaquan', 'open_91', '91'].includes(value)
}
if (normalized === 'kuaishou' || normalized === 'kuaishou-industry') {
return value.startsWith('kuaishou')
}
return value === normalized
}
function matchesAdminOrderDateRange(
item: { paidAt?: unknown; createdAt?: unknown },
params: Record<string, unknown>,
) {
const dateFrom = String(params.dateFrom || '').trim().slice(0, 10)
const dateTo = String(params.dateTo || '').trim().slice(0, 10)
if (!dateFrom && !dateTo) return true
const raw = String(item.paidAt || item.createdAt || '').trim()
if (!raw) return false
const day = dayjs(raw).format('YYYY-MM-DD')
if (dateFrom && day < dateFrom) return false
if (dateTo && day > dateTo) return false
return true
}
function matchesWorkOrderKeyword(
item: { productName?: unknown; platformOrderId?: unknown },
keyword: string,