feat(cost-center): 统一接单模板成本并自动同步商品
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
-- 接单模板商品改为事件驱动投影:新模板保存时自动建/更新商品。
|
||||
-- 本迁移补齐现有模板商品,并统一状态、名称、分类和价格属性。
|
||||
|
||||
WITH template_projection AS (
|
||||
SELECT
|
||||
LEFT('tpl:' || BTRIM(wpr.rule_key), 64) AS canonical_sku,
|
||||
wpr.product_name AS canonical_name,
|
||||
COALESCE(wc.name, '') AS category,
|
||||
CASE
|
||||
WHEN wpr.product_name ~ '钻石|颗' THEN '颗'
|
||||
WHEN wpr.product_name ~ '枪|把' THEN '把'
|
||||
WHEN wpr.product_name ~ '套' THEN '套'
|
||||
WHEN wpr.product_name ~ '密钥|秘钥' THEN '个'
|
||||
ELSE '件'
|
||||
END AS unit_name,
|
||||
'active' AS status,
|
||||
jsonb_build_object(
|
||||
'source', 'work_product_rule',
|
||||
'ruleId', wpr.id,
|
||||
'ruleKey', wpr.rule_key,
|
||||
'categoryId', wpr.category_id,
|
||||
'templateEnabled', wpr.enabled,
|
||||
'pricingMode',
|
||||
CASE
|
||||
WHEN wpr.unit_price_fen > 0 AND wpr.reward_amount = 0 THEN 'unit'
|
||||
ELSE 'fixed'
|
||||
END,
|
||||
'rewardAmountFen', wpr.reward_amount,
|
||||
'unitPriceFen', wpr.unit_price_fen,
|
||||
'sharingEnabled', wpr.sharing_enabled,
|
||||
'sharingTotalQuantity', wpr.sharing_total_quantity,
|
||||
'sharingUnitReward', wpr.sharing_unit_reward,
|
||||
'sharingAutoFromOrder', wpr.sharing_auto_from_order
|
||||
) AS attributes_json
|
||||
FROM work_product_rules wpr
|
||||
LEFT JOIN work_categories wc ON wc.id = wpr.category_id
|
||||
)
|
||||
INSERT INTO product_catalog (
|
||||
canonical_sku, canonical_name, category, unit_name, status,
|
||||
attributes_json, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
canonical_sku, canonical_name, category, unit_name, status,
|
||||
attributes_json, NOW(), NOW()
|
||||
FROM template_projection
|
||||
ON CONFLICT (canonical_sku) DO UPDATE SET
|
||||
canonical_name = EXCLUDED.canonical_name,
|
||||
category = EXCLUDED.category,
|
||||
unit_name = EXCLUDED.unit_name,
|
||||
attributes_json = COALESCE(product_catalog.attributes_json, '{}'::jsonb) || EXCLUDED.attributes_json,
|
||||
updated_at = NOW()
|
||||
WHERE product_catalog.canonical_sku LIKE 'tpl:%';
|
||||
|
||||
COMMENT ON TABLE product_catalog IS
|
||||
'标准商品目录;tpl: 商品由接单模板事件自动投影,价格只来自成本中心';
|
||||
Reference in New Issue
Block a user