feat(fulfillment): 固化当前尝试与历史自动化边界

This commit is contained in:
yml2213
2026-11-05 20:25:48 +08:00
parent ed450fac79
commit ca214854bb
14 changed files with 340 additions and 33 deletions
+18 -10
View File
@@ -94,6 +94,18 @@ export async function listTasksByOrderIds(orderIds: number[]): Promise<TaskRow[]
export async function createTask(input: TaskCreateInput): Promise<TaskRow | null> {
return withTransaction(async (client) => {
const nextAttempt = await client.query<{ attempt_no: number }>(
`SELECT COALESCE(MAX(attempt_no), 0) + 1 AS attempt_no
FROM fulfillment_tasks
WHERE order_item_id = $1 AND unit_index = $2`,
[input.orderItemId, input.unitIndex],
)
await client.query(
`UPDATE fulfillment_tasks
SET is_current = FALSE
WHERE order_item_id = $1 AND unit_index = $2 AND is_current = TRUE`,
[input.orderItemId, input.unitIndex],
)
const taskResult = await client.query<{ id: number }>(
`
INSERT INTO fulfillment_tasks (
@@ -121,11 +133,13 @@ export async function createTask(input: TaskCreateInput): Promise<TaskRow | null
last_error,
context_json,
created_at,
updated_at
updated_at,
attempt_no,
is_current
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19,
$20, $21, $22, $23::jsonb, $24, $25
$20, $21, $22, $23::jsonb, $24, $25, $26, TRUE
)
RETURNING id
`,
@@ -155,6 +169,7 @@ export async function createTask(input: TaskCreateInput): Promise<TaskRow | null
input.contextJson || '{}',
input.createdAt,
input.updatedAt,
Number(nextAttempt.rows[0]?.attempt_no || 1),
],
)
@@ -498,14 +513,7 @@ export function buildTaskListFilters({
const params: unknown[] = []
if (currentOnly) {
filters.push(`ft.id = (
SELECT latest.id
FROM fulfillment_tasks latest
WHERE latest.order_item_id = ft.order_item_id
AND latest.unit_index = ft.unit_index
ORDER BY latest.id DESC
LIMIT 1
)`)
filters.push('ft.is_current = TRUE')
}
if (keyword) {