diff --git a/apps/backend/src/db/migrations/116_voucher_internal_sku_backfill.sql b/apps/backend/src/db/migrations/116_voucher_internal_sku_backfill.sql new file mode 100644 index 00000000..0a014b25 --- /dev/null +++ b/apps/backend/src/db/migrations/116_voucher_internal_sku_backfill.sql @@ -0,0 +1,23 @@ +-- 116_voucher_internal_sku_backfill.sql +-- +-- 为已存在的三方卡券商品补齐渠道能力和内部中文 SKU 规则: +-- 卡券商品可能是自动创建后才补的迁移,旧商品没有 capability; +-- 补齐后同内部 SKU 的不同平台码会由 channel_assign 自动分流到卡券渠道。 + +INSERT INTO product_channel_capabilities (product_id, channel, priority, is_fallback, enabled, source) +SELECT p.id, 'third_party_voucher', 10, FALSE, TRUE, 'backfill' +FROM product_catalog p +WHERE p.status = 'active' AND p.canonical_sku LIKE 'tpv:%' +ON CONFLICT (product_id, channel) DO NOTHING; + +INSERT INTO sku_channel_rules ( + provider, shop_id, match_key_type, match_key, channel, priority, + source, enabled, note, created_at, updated_at +) +SELECT '', '', 'internal_sku', btrim(p.canonical_name), 'third_party_voucher', 10, + 'voucher', TRUE, '卡券商品内部SKU回填', NOW(), NOW() +FROM product_catalog p +WHERE p.status = 'active' + AND p.canonical_sku LIKE 'tpv:%' + AND btrim(p.canonical_name) <> '' +ON CONFLICT (provider, shop_id, match_key_type, match_key, channel) DO NOTHING; diff --git a/apps/backend/src/services/cost-center/product-normalize-service.ts b/apps/backend/src/services/cost-center/product-normalize-service.ts index 9403ff05..de89759c 100644 --- a/apps/backend/src/services/cost-center/product-normalize-service.ts +++ b/apps/backend/src/services/cost-center/product-normalize-service.ts @@ -182,6 +182,29 @@ async function ensureVoucherProductForItem(item: ItemRow): Promise