feat: 完善履约路由、订单服务与自动核销
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
-- 129_fulfillment_retry_semantics.sql
|
||||
--
|
||||
-- 履约载体重试语义:
|
||||
-- 1. 同一订单项/单元只限制一个“活跃”任务,历史 closed/failed 任务保留;
|
||||
-- 2. 载体补建使用独立水位,避免每次重复扫描最早的 200 条;
|
||||
-- 3. 回填历史内部 SKU 的大小写/全半角/空白规范化结果,并合并规范后重复渠道。
|
||||
|
||||
-- 允许同一订单项/单元在旧任务关闭后创建新的履约尝试。
|
||||
ALTER TABLE fulfillment_tasks
|
||||
DROP CONSTRAINT IF EXISTS fulfillment_tasks_order_item_id_unit_index_key;
|
||||
|
||||
DROP INDEX IF EXISTS fulfillment_tasks_order_item_id_unit_index_key;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_fulfillment_tasks_active_unit
|
||||
ON fulfillment_tasks (order_item_id, unit_index)
|
||||
WHERE task_status NOT IN (
|
||||
'closed', 'expired', 'failed', 'manual_review', 'retry_pending', 'cancelled'
|
||||
);
|
||||
|
||||
COMMENT ON INDEX uq_fulfillment_tasks_active_unit IS
|
||||
'同一订单项/单元只允许一个活跃履约载体;关闭、失败、待重试和人工复核任务保留为历史记录';
|
||||
|
||||
ALTER TABLE work_orders
|
||||
DROP CONSTRAINT IF EXISTS idx_work_orders_order_item_unique;
|
||||
|
||||
DROP INDEX IF EXISTS idx_work_orders_order_item_unique;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_work_orders_active_order_item
|
||||
ON work_orders (order_item_id)
|
||||
WHERE order_item_id IS NOT NULL AND status <> 'cancelled';
|
||||
|
||||
COMMENT ON INDEX uq_work_orders_active_order_item IS
|
||||
'同一订单项只允许一个未取消工单;取消工单保留为历史并可重新建单';
|
||||
|
||||
INSERT INTO finance_watermarks (pipeline, last_item_id, last_success_at)
|
||||
VALUES ('carrier_reconcile', 0, NULL)
|
||||
ON CONFLICT (pipeline) DO NOTHING;
|
||||
|
||||
COMMENT ON COLUMN finance_watermarks.pipeline IS
|
||||
'同步管道:channel_assign / carrier_reconcile / carrier_dispatch / product_normalize / cost_snapshot / cost_snapshot_hot / sales_facts / ledger_post';
|
||||
|
||||
-- 规范化前先收敛跨渠道主渠道。同一规范 SKU 只能有一个 enabled 主渠道;
|
||||
-- 优先保留带载体参数的行,再按优先级、id 稳定选择。
|
||||
WITH ranked_primary AS (
|
||||
SELECT
|
||||
id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY canonicalize_internal_sku(internal_sku)
|
||||
ORDER BY
|
||||
CASE WHEN config_json = '{}'::jsonb THEN 1 ELSE 0 END,
|
||||
priority,
|
||||
id
|
||||
) AS candidate_rank
|
||||
FROM internal_sku_channels
|
||||
WHERE enabled = TRUE AND is_fallback = FALSE
|
||||
)
|
||||
UPDATE internal_sku_channels channel
|
||||
SET is_fallback = TRUE,
|
||||
priority = GREATEST(priority, 90),
|
||||
updated_at = NOW()
|
||||
FROM ranked_primary
|
||||
WHERE channel.id = ranked_primary.id
|
||||
AND ranked_primary.candidate_rank > 1;
|
||||
|
||||
-- 同一规范 SKU + 渠道只保留一行,优先保留启用、带载体参数的行。
|
||||
WITH ranked AS (
|
||||
SELECT
|
||||
internal_sku,
|
||||
channel,
|
||||
id,
|
||||
canonicalize_internal_sku(internal_sku) AS canonical_sku,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY canonicalize_internal_sku(internal_sku), channel
|
||||
ORDER BY
|
||||
enabled DESC,
|
||||
CASE WHEN config_json = '{}'::jsonb THEN 1 ELSE 0 END,
|
||||
is_fallback,
|
||||
priority,
|
||||
id
|
||||
) AS candidate_rank
|
||||
FROM internal_sku_channels
|
||||
)
|
||||
DELETE FROM internal_sku_channels channel
|
||||
USING ranked
|
||||
WHERE channel.id = ranked.id
|
||||
AND ranked.candidate_rank > 1;
|
||||
|
||||
UPDATE internal_sku_channels
|
||||
SET internal_sku = canonicalize_internal_sku(internal_sku),
|
||||
updated_at = NOW()
|
||||
WHERE internal_sku <> canonicalize_internal_sku(internal_sku);
|
||||
|
||||
UPDATE manual_fulfillment_configs
|
||||
SET sku_key = canonicalize_internal_sku(sku_key),
|
||||
updated_at = NOW()
|
||||
WHERE sku_key <> canonicalize_internal_sku(sku_key);
|
||||
|
||||
UPDATE manual_sku_registry
|
||||
SET sku_key = canonicalize_internal_sku(sku_key),
|
||||
updated_at = NOW()
|
||||
WHERE sku_key <> canonicalize_internal_sku(sku_key);
|
||||
|
||||
UPDATE internal_sku_mappings
|
||||
SET match_value = canonicalize_internal_sku(match_value),
|
||||
internal_sku = canonicalize_internal_sku(internal_sku),
|
||||
updated_at = NOW()
|
||||
WHERE match_value <> canonicalize_internal_sku(match_value)
|
||||
OR internal_sku <> canonicalize_internal_sku(internal_sku);
|
||||
@@ -0,0 +1,50 @@
|
||||
-- Persistent delayed voucher consumption jobs.
|
||||
--
|
||||
-- Business events only enqueue a job. A scheduler worker performs the external
|
||||
-- platform call later, so a restart cannot lose a delayed consumption.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS voucher_consume_jobs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
provider TEXT NOT NULL
|
||||
CHECK (provider IN ('kuaishou_industry', 'douyin_mashida')),
|
||||
channel TEXT NOT NULL
|
||||
CHECK (channel IN ('affiliate_dash', 'work_order', 'manual', 'third_party_voucher')),
|
||||
trigger_event TEXT NOT NULL,
|
||||
target_key TEXT NOT NULL,
|
||||
platform_order_id TEXT NOT NULL DEFAULT '',
|
||||
order_id BIGINT REFERENCES orders(id) ON DELETE SET NULL,
|
||||
order_item_id BIGINT REFERENCES order_items(id) ON DELETE SET NULL,
|
||||
task_id BIGINT REFERENCES fulfillment_tasks(id) ON DELETE SET NULL,
|
||||
work_order_id BIGINT REFERENCES work_orders(id) ON DELETE SET NULL,
|
||||
due_at TIMESTAMPTZ NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN ('pending', 'processing', 'succeeded', 'failed', 'cancelled')),
|
||||
attempt_count INTEGER NOT NULL DEFAULT 0,
|
||||
max_attempts INTEGER NOT NULL DEFAULT 8,
|
||||
locked_until TIMESTAMPTZ,
|
||||
last_error TEXT NOT NULL DEFAULT '',
|
||||
result_json JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
finished_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
-- Keep one active/successful target per provider. Failed jobs may be replaced
|
||||
-- by a later business event, while succeeded jobs suppress duplicate events.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_voucher_consume_jobs_active_target
|
||||
ON voucher_consume_jobs(provider, target_key)
|
||||
WHERE status IN ('pending', 'processing', 'succeeded');
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_voucher_consume_jobs_due
|
||||
ON voucher_consume_jobs(status, due_at, id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_voucher_consume_jobs_order
|
||||
ON voucher_consume_jobs(order_id, order_item_id, id DESC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_voucher_consume_jobs_updated
|
||||
ON voucher_consume_jobs(updated_at DESC, id DESC);
|
||||
|
||||
COMMENT ON TABLE voucher_consume_jobs IS
|
||||
'Delayed voucher verification jobs; events enqueue, scheduler executes';
|
||||
COMMENT ON COLUMN voucher_consume_jobs.target_key IS
|
||||
'Stable dedupe target: order-item when available, otherwise task/order';
|
||||
Reference in New Issue
Block a user