fix(fulfillment): 修复自动发货任务上下文并发覆盖

This commit is contained in:
yml2213
2026-11-03 13:14:07 +08:00
parent b3fb330ad9
commit b842d00fc3
4 changed files with 132 additions and 19 deletions
@@ -0,0 +1,40 @@
-- 127_repair_affiliate_dash_task_context.sql
--
-- 修复电子凭证绑定并发覆盖任务上下文的问题:
-- affiliate_dash 建单事件已经记录了 orderNo/clientOrderNo,但任务 context_json
-- 随后被旧 TaskRow 整段覆盖为空。回调按这两个键查任务时会 404。
-- 只补缺失键,不覆盖现有 affiliateDash 其它字段。
WITH latest_created AS (
SELECT DISTINCT ON (te.task_id)
te.task_id,
NULLIF(BTRIM(te.payload_json->>'orderNo'), '') AS order_no,
NULLIF(BTRIM(te.payload_json->>'clientOrderNo'), '') AS client_order_no
FROM task_events te
WHERE te.event_type = 'affiliate_dash_order_created'
ORDER BY te.task_id, te.created_at DESC, te.id DESC
)
UPDATE fulfillment_tasks ft
SET
context_json = COALESCE(ft.context_json, '{}'::jsonb) || jsonb_build_object(
'affiliateDash',
COALESCE(ft.context_json->'affiliateDash', '{}'::jsonb) || jsonb_build_object(
'orderNo',
latest_created.order_no,
'clientOrderNo',
COALESCE(
latest_created.client_order_no,
NULLIF(ft.context_json #>> '{affiliateDash,clientOrderNo}', ''),
ft.platform_order_id
)
)
),
updated_at = NOW()
FROM latest_created
WHERE ft.id = latest_created.task_id
AND ft.executor_key = 'affiliate_dash'
AND latest_created.order_no IS NOT NULL
AND (
COALESCE(ft.context_json #>> '{affiliateDash,orderNo}', '') = ''
OR COALESCE(ft.context_json #>> '{affiliateDash,clientOrderNo}', '') = ''
);