优化核销展示

This commit is contained in:
yml2213
2026-07-11 21:19:33 +08:00
parent bae8715f5d
commit 38c022e20c
5 changed files with 146 additions and 49 deletions
@@ -227,9 +227,20 @@ export async function listKuaishouIndustryVouchersByTaskId(
return result.rows
}
export type KuaishouIndustryVoucherAdminListRow = KuaishouIndustryVoucherRow & {
sku_code?: string | null
sku_name?: string | null
task_no?: string | null
}
export async function listKuaishouIndustryVouchersForAdmin(
input: KuaishouIndustryVoucherAdminListQuery = {},
): Promise<{ items: KuaishouIndustryVoucherRow[]; total: number; page: number; pageSize: number }> {
): Promise<{
items: KuaishouIndustryVoucherAdminListRow[]
total: number
page: number
pageSize: number
}> {
const page = normalizePositiveLimit(input.page, 1)
const pageSize = Math.min(normalizePositiveLimit(input.pageSize, 50), 200)
const where: string[] = []
@@ -238,50 +249,65 @@ export async function listKuaishouIndustryVouchersForAdmin(
const oid = String(input.oid || '').trim()
if (oid) {
params.push(oid)
where.push(`oid = $${params.length}`)
where.push(`v.oid = $${params.length}`)
}
const voucherCode = String(input.voucherCode || '').trim()
if (voucherCode) {
params.push(Array.from(new Set([voucherCode, voucherCode.toUpperCase()])))
where.push(`voucher_code = ANY($${params.length}::text[])`)
where.push(`v.voucher_code = ANY($${params.length}::text[])`)
}
const taskId = Number(input.taskId || 0)
if (Number.isFinite(taskId) && taskId > 0) {
params.push(Math.trunc(taskId))
where.push(`task_id = $${params.length}`)
where.push(`v.task_id = $${params.length}`)
}
const sellerId = String(input.sellerId || '').trim()
if (sellerId) {
params.push(sellerId)
where.push(`seller_id = $${params.length}`)
where.push(`v.seller_id = $${params.length}`)
}
const status = String(input.status || '').trim().toUpperCase()
if (status) {
params.push(status)
where.push(`status = $${params.length}`)
where.push(`v.status = $${params.length}`)
}
const whereSql = where.length > 0 ? `WHERE ${where.join(' AND ')}` : ''
const totalResult = await query<{ total: string | number }>(
`
SELECT COUNT(*) AS total
FROM kuaishou_industry_vouchers
FROM kuaishou_industry_vouchers v
${whereSql}
`,
params,
)
const listParams = [...params, pageSize, (page - 1) * pageSize]
const listResult = await query<KuaishouIndustryVoucherRow>(
// 关联任务/订单商品,便于运营后台展示「买的是什么」
const listResult = await query<KuaishouIndustryVoucherAdminListRow>(
`
SELECT *
FROM kuaishou_industry_vouchers
SELECT
v.*,
COALESCE(oi_task.sku_code, oi_order.sku_code, '') AS sku_code,
COALESCE(oi_task.sku_name, oi_order.sku_name, '') AS sku_name,
COALESCE(ft.task_no, '') AS task_no
FROM kuaishou_industry_vouchers v
LEFT JOIN fulfillment_tasks ft ON ft.id = v.task_id
LEFT JOIN order_items oi_task ON oi_task.id = ft.order_item_id
LEFT JOIN LATERAL (
SELECT oi.sku_code, oi.sku_name
FROM order_items oi
WHERE v.order_id IS NOT NULL
AND oi.order_id = v.order_id
ORDER BY oi.id ASC
LIMIT 1
) oi_order ON true
${whereSql}
ORDER BY updated_at DESC, id DESC
ORDER BY v.updated_at DESC, v.id DESC
LIMIT $${params.length + 1}
OFFSET $${params.length + 2}
`,
@@ -6,6 +6,7 @@ import {
listKuaishouIndustryVouchersForAdmin,
updateKuaishouIndustryVoucherByCode,
type KuaishouIndustryVoucherAdminListQuery,
type KuaishouIndustryVoucherAdminListRow,
} from '../../repositories/kuaishou-industry-voucher-repo.js'
import { getOrderById } from '../../repositories/order-repo.js'
import { getTaskById } from '../../repositories/task-repo.js'
@@ -395,19 +396,23 @@ function sanitizeOpenApiRequest(request: JsonObject | undefined): JsonObject | n
}
function mapAdminKuaishouIndustryVoucher(
voucher: KuaishouIndustryVoucherRow,
voucher: KuaishouIndustryVoucherRow | KuaishouIndustryVoucherAdminListRow,
shopNameBySellerId: Map<string, string> = buildKuaishouIndustryShopNameMap(),
) {
const sellerId = String(voucher.seller_id || '').trim()
const listRow = voucher as KuaishouIndustryVoucherAdminListRow
return {
id: voucher.id,
voucherCode: voucher.voucher_code,
oid: voucher.oid,
orderId: voucher.order_id,
taskId: voucher.task_id,
taskNo: String(listRow.task_no || '').trim(),
unitIndex: voucher.unit_index,
sellerId,
shopName: shopNameBySellerId.get(sellerId) || '',
skuCode: String(listRow.sku_code || '').trim(),
skuName: String(listRow.sku_name || '').trim(),
tokenMasked: maskSecret(voucher.token),
status: voucher.status,
validStartTime: Number(voucher.valid_start_time || 0) || 0,