feat(fulfillment): 收敛履约状态写入口并追加状态事件
This commit is contained in:
@@ -94,6 +94,26 @@ export async function listTasksByOrderIds(orderIds: number[]): Promise<TaskRow[]
|
||||
|
||||
export async function createTask(input: TaskCreateInput): Promise<TaskRow | null> {
|
||||
return withTransaction(async (client) => {
|
||||
// 同一订单项单元的创建必须串行,否则并发调度会同时算出同一尝试号,
|
||||
// 再一起绕过“仅一个当前尝试”的唯一索引。
|
||||
await client.query(`SELECT id FROM order_items WHERE id = $1 FOR UPDATE`, [input.orderItemId])
|
||||
const active = await client.query<{ id: number }>(
|
||||
`SELECT id
|
||||
FROM fulfillment_tasks
|
||||
WHERE order_item_id = $1
|
||||
AND unit_index = $2
|
||||
AND task_status NOT IN (
|
||||
'closed', 'expired', 'failed', 'manual_review', 'retry_pending', 'cancelled'
|
||||
)
|
||||
ORDER BY is_current DESC, id DESC
|
||||
LIMIT 1`,
|
||||
[input.orderItemId, input.unitIndex],
|
||||
)
|
||||
const activeTaskId = Number(active.rows[0]?.id || 0)
|
||||
if (activeTaskId > 0) {
|
||||
return getTaskByIdWithExecutor(client.query.bind(client) as TaskQueryExecutor, activeTaskId)
|
||||
}
|
||||
|
||||
const nextAttempt = await client.query<{ attempt_no: number }>(
|
||||
`SELECT COALESCE(MAX(attempt_no), 0) + 1 AS attempt_no
|
||||
FROM fulfillment_tasks
|
||||
@@ -106,8 +126,11 @@ export async function createTask(input: TaskCreateInput): Promise<TaskRow | null
|
||||
WHERE order_item_id = $1 AND unit_index = $2 AND is_current = TRUE`,
|
||||
[input.orderItemId, input.unitIndex],
|
||||
)
|
||||
const taskResult = await client.query<{ id: number }>(
|
||||
`
|
||||
const nextAttemptNo = Number(nextAttempt.rows[0]?.attempt_no || 1)
|
||||
let taskResult: { rows: Array<{ id: number }> }
|
||||
try {
|
||||
taskResult = await client.query<{ id: number }>(
|
||||
`
|
||||
INSERT INTO fulfillment_tasks (
|
||||
order_id,
|
||||
order_item_id,
|
||||
@@ -143,35 +166,44 @@ export async function createTask(input: TaskCreateInput): Promise<TaskRow | null
|
||||
)
|
||||
RETURNING id
|
||||
`,
|
||||
[
|
||||
input.orderId,
|
||||
input.orderItemId,
|
||||
input.unitIndex,
|
||||
input.taskNo,
|
||||
input.provider,
|
||||
input.platform,
|
||||
input.shopId || '',
|
||||
input.shopName || '',
|
||||
input.platformOrderId,
|
||||
input.profileId,
|
||||
input.executorKey,
|
||||
input.taskStatus || 'pending_payment',
|
||||
input.deliveryStatus || 'pending',
|
||||
input.resultCode || '',
|
||||
input.resultMessage || '',
|
||||
input.claimToken || '',
|
||||
input.claimExpiresAt || null,
|
||||
input.automationMode || 'manual',
|
||||
Boolean(input.requiresClaim),
|
||||
input.userActionStatus || 'not_required',
|
||||
input.attemptCount || 0,
|
||||
input.lastError || '',
|
||||
input.contextJson || '{}',
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
Number(nextAttempt.rows[0]?.attempt_no || 1),
|
||||
],
|
||||
)
|
||||
[
|
||||
input.orderId,
|
||||
input.orderItemId,
|
||||
input.unitIndex,
|
||||
input.taskNo,
|
||||
input.provider,
|
||||
input.platform,
|
||||
input.shopId || '',
|
||||
input.shopName || '',
|
||||
input.platformOrderId,
|
||||
input.profileId,
|
||||
input.executorKey,
|
||||
input.taskStatus || 'pending_payment',
|
||||
input.deliveryStatus || 'pending',
|
||||
input.resultCode || '',
|
||||
input.resultMessage || '',
|
||||
input.claimToken || '',
|
||||
input.claimExpiresAt || null,
|
||||
input.automationMode || 'manual',
|
||||
Boolean(input.requiresClaim),
|
||||
input.userActionStatus || 'not_required',
|
||||
input.attemptCount || 0,
|
||||
input.lastError || '',
|
||||
input.contextJson || '{}',
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
nextAttemptNo,
|
||||
],
|
||||
)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
throw new Error(
|
||||
`履约任务创建失败 orderItemId=${input.orderItemId} ` +
|
||||
`unitIndex=${input.unitIndex} taskStatus=${input.taskStatus || 'pending_payment'} ` +
|
||||
`attemptNo=${nextAttemptNo}: ${message}`,
|
||||
{ cause: error },
|
||||
)
|
||||
}
|
||||
|
||||
const taskId = Number(taskResult.rows[0]?.id || 0)
|
||||
if (input.runtimeContext) {
|
||||
|
||||
Reference in New Issue
Block a user