feat(finance): 统一履约事实并改造人工账单分类

This commit is contained in:
yml2213
2026-10-23 13:35:48 +08:00
parent fb80cad695
commit 5d1ba11523
18 changed files with 764 additions and 123 deletions
@@ -0,0 +1,45 @@
-- 114_order_item_fulfillment_mode.sql
--
-- 统一履约事实:订单页、成本快照、销售事实只读 order_items.fulfillment_mode,
-- 不再各自用 provider / voucher / task 临时推断。
--
-- 说明:
-- 1. 本迁移只做 DDL;31 万行回填由 scripts/backfill-fulfillment-mode.ts 分批执行。
-- 2. product_catalog / cost_items / cost_snapshots / sales_daily_facts 的
-- carrier_type CHECK 目前都不接受 legacy_platform,必须同时放宽,
-- 否则历史平台数据写入会在运行时报错。
ALTER TABLE order_items
ADD COLUMN IF NOT EXISTS fulfillment_mode text,
ADD COLUMN IF NOT EXISTS fulfillment_decided_at timestamptz,
ADD COLUMN IF NOT EXISTS fulfillment_reason text;
ALTER TABLE order_items
ADD CONSTRAINT chk_order_items_fulfillment_mode
CHECK (
fulfillment_mode IS NULL
OR fulfillment_mode IN ('work_order', 'affiliate_dash', 'legacy_platform', 'manual')
);
CREATE INDEX IF NOT EXISTS idx_order_items_fulfillment_mode
ON order_items (fulfillment_mode, order_id);
CREATE INDEX IF NOT EXISTS idx_order_items_fulfillment_pending
ON order_items (order_id)
WHERE fulfillment_mode IS NULL;
ALTER TABLE product_catalog DROP CONSTRAINT IF EXISTS chk_product_catalog_carrier_type;
ALTER TABLE product_catalog ADD CONSTRAINT chk_product_catalog_carrier_type
CHECK (carrier_type IN ('affiliate_dash', 'work_order', 'legacy_platform', 'manual', 'shared'));
ALTER TABLE cost_items DROP CONSTRAINT IF EXISTS chk_cost_items_carrier_type;
ALTER TABLE cost_items ADD CONSTRAINT chk_cost_items_carrier_type
CHECK (carrier_type IN ('affiliate_dash', 'work_order', 'legacy_platform', 'manual', 'shared'));
ALTER TABLE cost_snapshots DROP CONSTRAINT IF EXISTS chk_cost_snapshots_carrier_type;
ALTER TABLE cost_snapshots ADD CONSTRAINT chk_cost_snapshots_carrier_type
CHECK (carrier_type IN ('affiliate_dash', 'work_order', 'legacy_platform', 'manual', 'shared'));
ALTER TABLE sales_daily_facts DROP CONSTRAINT IF EXISTS chk_sales_daily_facts_carrier;
ALTER TABLE sales_daily_facts ADD CONSTRAINT chk_sales_daily_facts_carrier
CHECK (carrier_type IN ('affiliate_dash', 'work_order', 'legacy_platform', 'manual', 'shared', 'unknown'));