匹配日志支持结果状态关键词与日期筛选及服务端分页
This commit is contained in:
@@ -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) }
|
||||
}
|
||||
|
||||
@@ -15,7 +15,12 @@ import type { JsonObject } from '../../types/json.js'
|
||||
import type { OrderItemRow, OrderRow } from '../../types/repository/rows.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
import { normalizePage, normalizePageSize, safeParseJson } from '../admin/admin-query-utils.js'
|
||||
import {
|
||||
normalizeDateString,
|
||||
normalizePage,
|
||||
normalizePageSize,
|
||||
safeParseJson,
|
||||
} from '../admin/admin-query-utils.js'
|
||||
import {
|
||||
mapWorkProductRule,
|
||||
normalizeBoolean,
|
||||
@@ -262,8 +267,17 @@ export async function testAdminKuaishouProductMatch(payload: JsonObject = {}) {
|
||||
}
|
||||
|
||||
export async function listAdminWorkProductMatchLogs(payload: JsonObject = {}) {
|
||||
const limit = Math.min(500, normalizePositiveInteger(payload.limit, 100))
|
||||
return { items: (await listWorkProductMatchLogs(limit)).map(mapWorkProductMatchLog) }
|
||||
const page = normalizePage(payload.page)
|
||||
const pageSize = Math.min(200, normalizePageSize(payload.pageSize))
|
||||
const { items, total } = await listWorkProductMatchLogs({
|
||||
page,
|
||||
pageSize,
|
||||
matchStatus: String(payload.matchStatus || payload.status || '').trim(),
|
||||
keyword: String(payload.keyword || '').trim(),
|
||||
createdFrom: normalizeDateString(payload.createdFrom),
|
||||
createdTo: normalizeDateString(payload.createdTo),
|
||||
})
|
||||
return { items: items.map(mapWorkProductMatchLog), pagination: { page, pageSize, total } }
|
||||
}
|
||||
|
||||
function mapWorkProductRuleMapping(mapping: WorkProductRuleMappingRow) {
|
||||
|
||||
Reference in New Issue
Block a user