fix(finance): 支持生产快照升级并修复成本回填

修复生产 101 到 112 的迁移兼容问题;接单商品回填改为分批执行;修正未验收工单成本快照的 order_id/order_item_id 映射;更新本地生产快照数据基线。
This commit is contained in:
yml2213
2026-10-25 11:10:33 +08:00
parent 790de676c6
commit 7824170d3c
9 changed files with 156 additions and 118 deletions
@@ -246,9 +246,6 @@ CREATE INDEX IF NOT EXISTS idx_cost_exceptions_status_created
ON cost_exceptions (status, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_cost_exceptions_item
ON cost_exceptions (order_item_id, status);
-- 按履约方式筛异常时走半连接,这条索引让 order_items 侧只扫目标履约方式的行。
CREATE INDEX IF NOT EXISTS idx_order_items_fulfillment_id
ON order_items (fulfillment_mode, id);
DO $$
BEGIN
@@ -40,6 +40,10 @@ CREATE INDEX IF NOT EXISTS idx_order_items_fulfillment_pending
ON order_items (order_id)
WHERE fulfillment_mode IS NULL;
-- 按履约方式筛异常时走半连接,这条索引让 order_items 侧只扫目标履约方式的行。
CREATE INDEX IF NOT EXISTS idx_order_items_fulfillment_id
ON order_items (fulfillment_mode, id);
COMMENT ON COLUMN order_items.fulfillment_mode IS
'履约方式:work_order 接单平台 / affiliate_dash 自动发货 / legacy_platform 历史平台(已下线)/ manual 人工;唯一真源,报表与后台只读此字段';
COMMENT ON COLUMN order_items.fulfillment_decided_at IS
@@ -22,13 +22,18 @@
-- 关联备用金支出单(fund_expense_id 为空)的,保留原主体 id 不动,避免误删成本;
-- 有 fund_expense_id 的改挂到备用金支出单。
UPDATE cost_snapshots cs
SET subject_id = mer.fund_expense_id
FROM manual_expense_records mer
WHERE cs.subject_type = 'manual_case'
AND mer.fund_expense_id IS NOT NULL
AND cs.subject_id = mer.id
AND cs.subject_id <> mer.fund_expense_id;
DO $$
BEGIN
IF to_regclass('public.manual_expense_records') IS NOT NULL THEN
UPDATE cost_snapshots cs
SET subject_id = mer.fund_expense_id
FROM manual_expense_records mer
WHERE cs.subject_type = 'manual_case'
AND mer.fund_expense_id IS NOT NULL
AND cs.subject_id = mer.id
AND cs.subject_id <> mer.fund_expense_id;
END IF;
END $$;
-- 若同一备用金支出单已存在有效快照,保留先写的,把重复的作废(superseded_at)。
UPDATE cost_snapshots dup
@@ -14,6 +14,8 @@ WHERE ci.id = v.cost_item_id
AND v.status = 'active';
-- 若人工成本项还没有生效版本,补一条 0 元全局占位,保证快照能挂到版本上。
-- 104 已写入一条 1970 年的 draft 占位;这里必须用新的 effective_from,
-- 否则会与 draft 行撞 uq_cost_versions_scope(唯一键不区分状态)。
INSERT INTO cost_versions (
cost_item_id, scope_type, provider, shop_id, canonical_product_id,
work_product_rule_id, amount_fen, rate_bps, effective_from, source,
@@ -21,7 +23,7 @@ INSERT INTO cost_versions (
)
SELECT
ci.id, 'global', '', '', NULL, NULL, 0, 0,
'1970-01-01T00:00:00+08:00', 'manual', 'active',
NOW(), 'manual', 'active',
'系统占位:人工成本按人工 SKU 配置的消耗积分结算', NOW(), NOW()
FROM cost_items ci
WHERE ci.code = 'sys_manual_service'