优化订单履约分流与后台筛选
This commit is contained in:
@@ -79,6 +79,19 @@ export async function listTasksByOrderId(orderId: number | string): Promise<Task
|
||||
return result.rows
|
||||
}
|
||||
|
||||
export async function listTasksByOrderIds(orderIds: number[]): Promise<TaskRow[]> {
|
||||
const normalizedIds = orderIds.map(Number).filter((value) => value > 0)
|
||||
if (normalizedIds.length === 0) return []
|
||||
|
||||
const result = await query<TaskRow>(
|
||||
`${buildTaskSelect()}
|
||||
WHERE ft.order_id = ANY($1::bigint[])
|
||||
ORDER BY ft.order_id ASC, ft.id ASC`,
|
||||
[normalizedIds],
|
||||
)
|
||||
return result.rows
|
||||
}
|
||||
|
||||
export async function createTask(input: TaskCreateInput): Promise<TaskRow | null> {
|
||||
return withTransaction(async (client) => {
|
||||
const taskResult = await client.query<{ id: number }>(
|
||||
@@ -418,6 +431,43 @@ export async function findAffiliateDashTaskByOrder(input: {
|
||||
export async function listTasks({
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
...filters
|
||||
}: TaskListQueryInput = {}): Promise<TaskListQueryResult> {
|
||||
const offset = (page - 1) * pageSize
|
||||
const { whereClause, params } = buildTaskListFilters(filters)
|
||||
|
||||
const totalResult = await query<{ [column: string]: unknown; total: number }>(
|
||||
`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM fulfillment_tasks ft
|
||||
LEFT JOIN order_items oi ON oi.id = ft.order_item_id
|
||||
LEFT JOIN task_runtime_contexts ctx ON ctx.task_id = ft.id
|
||||
LEFT JOIN kuaishou_cloud_task_states kcts ON kcts.task_id = ft.id
|
||||
${whereClause}
|
||||
`,
|
||||
params,
|
||||
)
|
||||
|
||||
params.push(pageSize)
|
||||
params.push(offset)
|
||||
const itemsResult = await query<TaskRow>(
|
||||
`${buildTaskSelect('oi.sku_code, oi.sku_name, oi.quantity')}
|
||||
LEFT JOIN order_items oi ON oi.id = ft.order_item_id
|
||||
${whereClause}
|
||||
ORDER BY ft.id DESC
|
||||
LIMIT $${params.length - 1} OFFSET $${params.length}`,
|
||||
params,
|
||||
)
|
||||
|
||||
return {
|
||||
items: itemsResult.rows,
|
||||
total: Number(totalResult.rows[0]?.total || 0),
|
||||
}
|
||||
}
|
||||
|
||||
export function buildTaskListFilters({
|
||||
keyword = '',
|
||||
executorKey = '',
|
||||
status = '',
|
||||
platformOrderId = '',
|
||||
taskNo = '',
|
||||
@@ -425,11 +475,29 @@ export async function listTasks({
|
||||
roleId = '',
|
||||
dateFrom = '',
|
||||
dateTo = '',
|
||||
}: TaskListQueryInput = {}): Promise<TaskListQueryResult> {
|
||||
const offset = (page - 1) * pageSize
|
||||
}: TaskListQueryInput = {}): { whereClause: string; params: unknown[] } {
|
||||
const filters: string[] = []
|
||||
const params: unknown[] = []
|
||||
|
||||
if (keyword) {
|
||||
params.push(`%${keyword}%`)
|
||||
const keywordParam = `$${params.length}`
|
||||
filters.push(`(
|
||||
(ft.task_no || ' ' || ft.platform_order_id) ILIKE ${keywordParam}
|
||||
OR oi.sku_code ILIKE ${keywordParam}
|
||||
OR oi.sku_name ILIKE ${keywordParam}
|
||||
OR COALESCE(oi.item_snapshot_json->'kuaishouSendCode'->>'itemTitle', '') ILIKE ${keywordParam}
|
||||
OR COALESCE(oi.item_snapshot_json->'kuaishouSendCode'->>'skuNick', '') ILIKE ${keywordParam}
|
||||
OR COALESCE(NULLIF(kcts.role_id, ''), ctx.role_id, '') ILIKE ${keywordParam}
|
||||
OR COALESCE(NULLIF(kcts.role_name, ''), NULLIF(ctx.role_name, ''), ctx.nickname, '') ILIKE ${keywordParam}
|
||||
)`)
|
||||
}
|
||||
|
||||
if (executorKey) {
|
||||
params.push(executorKey)
|
||||
filters.push(`ft.executor_key = $${params.length}`)
|
||||
}
|
||||
|
||||
if (status) {
|
||||
params.push(status)
|
||||
filters.push(`ft.task_status = $${params.length}`)
|
||||
@@ -465,33 +533,9 @@ export async function listTasks({
|
||||
filters.push(`ft.created_at <= $${params.length}`)
|
||||
}
|
||||
|
||||
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
|
||||
const totalResult = await query<{ [column: string]: unknown; total: number }>(
|
||||
`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM fulfillment_tasks ft
|
||||
LEFT JOIN order_items oi ON oi.id = ft.order_item_id
|
||||
LEFT JOIN task_runtime_contexts ctx ON ctx.task_id = ft.id
|
||||
LEFT JOIN kuaishou_cloud_task_states kcts ON kcts.task_id = ft.id
|
||||
${whereClause}
|
||||
`,
|
||||
params,
|
||||
)
|
||||
|
||||
params.push(pageSize)
|
||||
params.push(offset)
|
||||
const itemsResult = await query<TaskRow>(
|
||||
`${buildTaskSelect('oi.sku_code, oi.sku_name, oi.quantity')}
|
||||
LEFT JOIN order_items oi ON oi.id = ft.order_item_id
|
||||
${whereClause}
|
||||
ORDER BY ft.id DESC
|
||||
LIMIT $${params.length - 1} OFFSET $${params.length}`,
|
||||
params,
|
||||
)
|
||||
|
||||
return {
|
||||
items: itemsResult.rows,
|
||||
total: Number(totalResult.rows[0]?.total || 0),
|
||||
whereClause: filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : '',
|
||||
params,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user