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
@@ -0,0 +1,54 @@
-- 138_fulfillment_current_attempt.sql
--
-- 履约尝试指针:
-- 1. 每个订单项单元显式记录尝试序号;
-- 2. 同一订单项单元只允许一个当前尝试;
-- 3. 新尝试创建时自动把旧尝试标为非当前,历史仍完整保留。
ALTER TABLE fulfillment_tasks
ADD COLUMN IF NOT EXISTS attempt_no INTEGER NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS is_current BOOLEAN NOT NULL DEFAULT TRUE;
WITH ranked AS (
SELECT
id,
order_item_id,
unit_index,
ROW_NUMBER() OVER (
PARTITION BY order_item_id, unit_index
ORDER BY id
)::int AS attempt_no,
ROW_NUMBER() OVER (
PARTITION BY order_item_id, unit_index
ORDER BY id DESC
) AS current_rank
FROM fulfillment_tasks
),
duplicate_units AS (
SELECT order_item_id, unit_index
FROM fulfillment_tasks
GROUP BY order_item_id, unit_index
HAVING COUNT(*) > 1
)
UPDATE fulfillment_tasks task
SET attempt_no = ranked.attempt_no,
is_current = ranked.current_rank = 1
FROM ranked
JOIN duplicate_units duplicate
ON duplicate.order_item_id = ranked.order_item_id
AND duplicate.unit_index = ranked.unit_index
WHERE task.id = ranked.id
;
CREATE UNIQUE INDEX IF NOT EXISTS uq_fulfillment_tasks_current_unit
ON fulfillment_tasks (order_item_id, unit_index)
WHERE is_current = TRUE;
CREATE INDEX IF NOT EXISTS idx_fulfillment_tasks_attempt_history
ON fulfillment_tasks (order_item_id, unit_index, attempt_no DESC, id DESC);
COMMENT ON COLUMN fulfillment_tasks.attempt_no IS
'同一订单项单元的履约尝试序号,从 1 开始递增';
COMMENT ON COLUMN fulfillment_tasks.is_current IS
'是否为该订单项单元当前尝试;历史尝试保留但不再标记为当前';
@@ -0,0 +1,30 @@
-- 139_fulfillment_automation_cutoff.sql
--
-- 历史履约数据已完成人工处置,不再参与自动判定、自动补载体或自动重试。
-- 从本迁移执行时间起,只自动处理之后新建的订单。
INSERT INTO app_config_entries (
config_key,
config_json,
source_file_path,
migrated_from_file,
created_at,
updated_at
) VALUES (
'fulfillment_automation_cutoff',
jsonb_build_object(
'cutoff',
to_char(
NOW() AT TIME ZONE 'Asia/Shanghai',
'YYYY-MM-DD"T"HH24:MI:SS.MS+08:00'
)
),
'',
FALSE,
NOW(),
NOW()
)
ON CONFLICT (config_key) DO NOTHING;
COMMENT ON TABLE app_config_entries IS
'运行时配置中心,保存从 JSON 文件迁移来的平台和系统配置;fulfillment_automation_cutoff 是履约自动化生效边界';