修复接单模板动态匹配并移除冗余运维文档

This commit is contained in:
yml
2026-11-15 00:00:04 +08:00
parent 572c43ce0e
commit 36f52a7f68
20 changed files with 627 additions and 1330 deletions
@@ -0,0 +1,100 @@
-- 166_work_order_dynamic_template_routing.sql
--
-- 接单平台路由只声明“由接单模板匹配”,具体模板必须按真实订单匹配。
-- 旧版统一渠道配置曾把内部 SKU 名称直接写成 workRuleKey,并生成
-- tpl:<内部SKU> 草稿商品;包含匹配、SKU 数量系列和店铺绑定因此永远走不到。
--
-- 本迁移:
-- 1. 清除不存在模板对应的 workRuleKey 和错误商品关联;
-- 2. 归档统一渠道配置遗留的假 tpl 草稿商品;
-- 3. 用实际接单模板商品补齐正式履约配置;
-- 4. 增加数据库闸门,未来非空 workRuleKey 必须是真实模板 key。
WITH invalid_routes AS (
SELECT route.id
FROM sku_channel_routes route
WHERE route.channel = 'work_order'
AND BTRIM(COALESCE(route.carrier_config_json ->> 'workRuleKey', '')) <> ''
AND NOT EXISTS (
SELECT 1
FROM work_product_rules rule
WHERE rule.rule_key = BTRIM(route.carrier_config_json ->> 'workRuleKey')
)
)
UPDATE sku_channel_routes route
SET carrier_config_json = (route.carrier_config_json - 'workRuleKey')
|| '{"templateSelection":"dynamic"}'::jsonb,
canonical_product_id = NULL,
note = CASE
WHEN route.note = '' THEN '迁移 166:清除不存在的接单模板 key'
ELSE LEFT(route.note || ';迁移 166:清除不存在的接单模板 key', 300)
END,
updated_at = NOW()
FROM invalid_routes invalid
WHERE route.id = invalid.id;
UPDATE product_catalog product
SET status = 'archived',
attributes_json = COALESCE(product.attributes_json, '{}'::jsonb) || jsonb_build_object(
'archivedAt', NOW(),
'archiveReason', 'invalid_work_order_draft_product'
),
updated_at = NOW()
WHERE product.source_kind = 'work_order'
AND product.canonical_sku LIKE 'tpl:%'
AND product.attributes_json ->> 'draft' = 'true'
AND NOT EXISTS (
SELECT 1
FROM work_product_rules rule
WHERE rule.canonical_product_id = product.id
);
UPDATE fulfillments fulfillment
SET canonical_product_id = rule.canonical_product_id,
updated_at = NOW()
FROM work_orders work_order
JOIN work_product_rules rule ON rule.id = work_order.product_rule_id
WHERE work_order.order_item_id = fulfillment.order_item_id
AND work_order.status <> 'cancelled'
AND rule.canonical_product_id IS NOT NULL
AND fulfillment.canonical_product_id IS DISTINCT FROM rule.canonical_product_id;
CREATE OR REPLACE FUNCTION assert_work_order_route_template_key()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
template_key TEXT;
BEGIN
IF NEW.channel <> 'work_order' THEN
RETURN NEW;
END IF;
template_key := BTRIM(COALESCE(NEW.carrier_config_json ->> 'workRuleKey', ''));
IF template_key <> '' AND NOT EXISTS (
SELECT 1
FROM work_product_rules rule
WHERE rule.rule_key = template_key
) THEN
RAISE EXCEPTION
'work-order route % references missing template key %',
NEW.id,
template_key
USING ERRCODE = '23503',
HINT = 'Leave workRuleKey empty for dynamic template matching or bind an existing rule_key.';
END IF;
RETURN NEW;
END;
$$;
DROP TRIGGER IF EXISTS trg_sku_channel_routes_work_order_template_key
ON sku_channel_routes;
CREATE TRIGGER trg_sku_channel_routes_work_order_template_key
BEFORE INSERT OR UPDATE OF channel, carrier_config_json
ON sku_channel_routes
FOR EACH ROW
EXECUTE FUNCTION assert_work_order_route_template_key();
COMMENT ON FUNCTION assert_work_order_route_template_key() IS
'接单平台路由的非空 workRuleKey 必须指向真实模板;空值表示按订单动态匹配';