匹配日志支持结果状态关键词与日期筛选及服务端分页

This commit is contained in:
yml2213
2026-08-23 10:30:47 +08:00
parent b4918769f1
commit fdf83a6745
4 changed files with 160 additions and 19 deletions
@@ -231,16 +231,65 @@ export async function createWorkProductMatchLog(input: {
return result.rows[0] || null
}
export async function listWorkProductMatchLogs(limit = 100): Promise<WorkProductMatchLogRow[]> {
/** 匹配日志分页查询:表本身全量保留,可按结果状态、关键词(商品/SKU/店铺)与日期范围筛选。 */
export async function listWorkProductMatchLogs({
page = 1,
pageSize = 20,
matchStatus = '',
keyword = '',
createdFrom = '',
createdTo = '',
}: {
page?: number
pageSize?: number
matchStatus?: string
keyword?: string
createdFrom?: string
createdTo?: string
} = {}): Promise<{ items: WorkProductMatchLogRow[]; total: number }> {
const conditions: string[] = []
const params: unknown[] = []
const status = String(matchStatus || '').trim()
if (status) {
params.push(status)
conditions.push(`wpml.match_status = $${params.length}`)
}
const normalizedKeyword = String(keyword || '').trim()
if (normalizedKeyword) {
params.push(`%${normalizedKeyword}%`)
conditions.push(
`(wpml.item_title ILIKE $${params.length} OR wpml.rel_item_id ILIKE $${params.length} OR wpml.rel_sku_id ILIKE $${params.length} OR wpml.sku_nick ILIKE $${params.length} OR wpml.seller_id ILIKE $${params.length})`,
)
}
if (createdFrom) {
params.push(createdFrom)
conditions.push(
`wpml.created_at >= ($${params.length}::date)::timestamp AT TIME ZONE 'Asia/Shanghai'`,
)
}
if (createdTo) {
params.push(createdTo)
conditions.push(
`wpml.created_at < (($${params.length}::date + 1)::timestamp AT TIME ZONE 'Asia/Shanghai')`,
)
}
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
const totalResult = await query<{ total: number }>(
`SELECT COUNT(*)::int AS total FROM work_product_match_logs wpml ${whereClause}`,
params,
)
params.push(pageSize, (page - 1) * pageSize)
const result = await query<WorkProductMatchLogRow>(
`
SELECT wpml.*, wpr.rule_key, wpr.product_name
FROM work_product_match_logs wpml
LEFT JOIN work_product_rules wpr ON wpr.id = wpml.rule_id
${whereClause}
ORDER BY wpml.created_at DESC, wpml.id DESC
LIMIT $1
LIMIT $${params.length - 1} OFFSET $${params.length}
`,
[Math.min(500, Math.max(1, Math.floor(Number(limit) || 100)))],
params,
)
return result.rows
return { items: result.rows, total: Number(totalResult.rows[0]?.total || 0) }
}