feat(finance): 接单模板发单金额落规则级成本版本
此前接单平台成本版本只有 sys_work_order_reward 的全局 0 元占位,662 个模板的发单金额从未进入 cost_versions。\n\n现在为 662 个模板各生成一条规则级版本:634 条固定发单金额 + 28 条按件单价;全局 0 元版本仅作“按工单实际结算”占位。成本版本页新增“用户实付均价”和“平均实发/项”,支持按模板名称/规则键搜索,按商品筛选也能命中规则版本。\n\n历史工单快照关联到对应模板版本;模板保存时自动同步单条成本版本;删除模板会先把历史版本脱钩停用,保留快照可追溯。\n\n新增迁移 112 覆盖索引,用户实付聚合从约 1.1s 降到约 150ms。
This commit is contained in:
@@ -382,6 +382,7 @@ async function syncDirectWorkOrderRewardSnapshots(
|
||||
SELECT DISTINCT ON (wo.order_item_id)
|
||||
wo.id AS work_order_id,
|
||||
wo.order_item_id,
|
||||
wo.product_rule_id,
|
||||
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount,
|
||||
COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at, NOW()) AS accepted_at
|
||||
FROM work_orders wo
|
||||
@@ -402,7 +403,8 @@ async function syncDirectWorkOrderRewardSnapshots(
|
||||
)
|
||||
SELECT
|
||||
'order_item', a.order_item_id, oi.order_id, a.order_item_id, a.work_order_id,
|
||||
'work_order', si.cost_item_id, si.cost_version_id, oi.canonical_product_id,
|
||||
'work_order', si.cost_item_id, COALESCE(rv.id, si.cost_version_id),
|
||||
oi.canonical_product_id,
|
||||
1, a.reward_amount, a.reward_amount, 'actual', 'resolved',
|
||||
jsonb_build_object(
|
||||
'source', 'system_work_order_reward',
|
||||
@@ -414,6 +416,18 @@ async function syncDirectWorkOrderRewardSnapshots(
|
||||
FROM accepted a
|
||||
CROSS JOIN system_item si
|
||||
JOIN order_items oi ON oi.id = a.order_item_id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT v.id
|
||||
FROM cost_versions v
|
||||
WHERE v.cost_item_id = si.cost_item_id
|
||||
AND v.scope_type = 'rule'
|
||||
AND v.work_product_rule_id = a.product_rule_id
|
||||
AND v.status IN ('active', 'retired')
|
||||
AND v.effective_from <= a.accepted_at
|
||||
AND (v.effective_to IS NULL OR v.effective_to > a.accepted_at)
|
||||
ORDER BY v.effective_from DESC, v.id DESC
|
||||
LIMIT 1
|
||||
) rv ON TRUE
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM cost_snapshots cs
|
||||
WHERE cs.subject_type = 'order_item'
|
||||
@@ -508,6 +522,7 @@ async function syncCommittedWorkOrderRewardSnapshots(
|
||||
SELECT DISTINCT ON (wo.order_item_id)
|
||||
wo.id AS work_order_id,
|
||||
wo.order_item_id,
|
||||
wo.product_rule_id,
|
||||
wo.status,
|
||||
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount,
|
||||
COALESCE(wo.created_at, o.paid_at, o.created_at, NOW()) AS basis_at
|
||||
@@ -531,7 +546,8 @@ async function syncCommittedWorkOrderRewardSnapshots(
|
||||
)
|
||||
SELECT
|
||||
'order_item', l.order_item_id, oi.order_id, l.order_item_id, l.work_order_id,
|
||||
'work_order', si.cost_item_id, si.cost_version_id, oi.canonical_product_id,
|
||||
'work_order', si.cost_item_id, COALESCE(rv.id, si.cost_version_id),
|
||||
oi.canonical_product_id,
|
||||
1, l.reward_amount, l.reward_amount, 'estimated', 'resolved',
|
||||
jsonb_build_object(
|
||||
'source', 'work_order_reward_committed',
|
||||
@@ -543,6 +559,18 @@ async function syncCommittedWorkOrderRewardSnapshots(
|
||||
FROM latest l
|
||||
CROSS JOIN system_item si
|
||||
JOIN order_items oi ON oi.id = l.order_item_id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT v.id
|
||||
FROM cost_versions v
|
||||
WHERE v.cost_item_id = si.cost_item_id
|
||||
AND v.scope_type = 'rule'
|
||||
AND v.work_product_rule_id = l.product_rule_id
|
||||
AND v.status IN ('active', 'retired')
|
||||
AND v.effective_from <= l.basis_at
|
||||
AND (v.effective_to IS NULL OR v.effective_to > l.basis_at)
|
||||
ORDER BY v.effective_from DESC, v.id DESC
|
||||
LIMIT 1
|
||||
) rv ON TRUE
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM work_orders accepted
|
||||
WHERE accepted.order_item_id = l.order_item_id
|
||||
|
||||
Reference in New Issue
Block a user