fix(admin): 恢复接单模板计价配置并统一成本价格链路

This commit is contained in:
yml2213
2026-11-02 17:57:49 +08:00
parent d871b93fde
commit a7bac9c1d8
27 changed files with 304 additions and 83 deletions
@@ -0,0 +1,54 @@
-- 接单模板显式保存计价方式,金额字段只由商品与成本中心写投影。
-- 历史数据按旧运行口径回填:拼单自动按件优先识别,其次识别“单价>0 且固定金额=0”。
ALTER TABLE work_product_rules
ADD COLUMN IF NOT EXISTS pricing_mode TEXT;
UPDATE work_product_rules
SET pricing_mode = CASE
WHEN sharing_enabled = TRUE
AND sharing_auto_from_order = TRUE
AND unit_price_fen > 0 THEN 'unit'
WHEN unit_price_fen > 0 AND reward_amount = 0 THEN 'unit'
ELSE 'fixed'
END
WHERE pricing_mode IS NULL
OR pricing_mode NOT IN ('fixed', 'unit');
ALTER TABLE work_product_rules
ALTER COLUMN pricing_mode SET DEFAULT 'fixed',
ALTER COLUMN pricing_mode SET NOT NULL;
-- 自动拼单只能依赖每件金额;固定金额模板不能保留“随订单自动算拼单”的旧标志。
UPDATE work_product_rules
SET sharing_auto_from_order = FALSE
WHERE pricing_mode = 'fixed'
AND sharing_auto_from_order = TRUE;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'chk_work_product_rules_pricing_mode'
AND conrelid = 'work_product_rules'::regclass
) THEN
ALTER TABLE work_product_rules
ADD CONSTRAINT chk_work_product_rules_pricing_mode
CHECK (pricing_mode IN ('fixed', 'unit'));
END IF;
END $$;
COMMENT ON COLUMN work_product_rules.pricing_mode IS
'接单计价方式:fixed 固定整单金额 / unit 按 SKU 数量乘每件金额';
-- 同步模板商品属性中的计价方式,避免继续从两个金额字段反推。
UPDATE product_catalog p
SET attributes_json = COALESCE(p.attributes_json, '{}'::jsonb) || jsonb_build_object(
'pricingMode', wpr.pricing_mode,
'rewardAmountFen', wpr.reward_amount,
'unitPriceFen', wpr.unit_price_fen
),
updated_at = NOW()
FROM work_product_rules wpr
WHERE p.canonical_sku = LEFT('tpl:' || BTRIM(wpr.rule_key), 64);