fix(finance): 卡券商品补内部 SKU 规则,待分类自动分流

- 自动创建 tpv: 商品时补渠道能力和 internal_sku 规则
- 迁移 116 为已存在的卡券商品回填内部 SKU 规则
- 同内部中文 SKU 的待分类订单自动进入第三方卡券
This commit is contained in:
yml2213
2026-11-02 13:36:37 +08:00
parent 03566aee2b
commit 8da669b1fd
2 changed files with 46 additions and 0 deletions
@@ -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;
@@ -182,6 +182,29 @@ async function ensureVoucherProductForItem(item: ItemRow): Promise<number | null
[item.id, productId],
)
await query(
`
INSERT INTO product_channel_capabilities (product_id, channel, priority, is_fallback, enabled, source)
VALUES ($1, 'third_party_voucher', 10, FALSE, TRUE, 'auto')
ON CONFLICT (product_id, channel) DO NOTHING
`,
[productId],
)
// 同一内部中文 SKU 跨平台码统一分流到卡券渠道。
await query(
`
INSERT INTO sku_channel_rules (
provider, shop_id, match_key_type, match_key, channel, priority,
source, enabled, note, created_at, updated_at
)
VALUES ('', '', 'internal_sku', $1, 'third_party_voucher', 10, 'voucher', TRUE,
'卡券商品自动建商品后生成', NOW(), NOW())
ON CONFLICT (provider, shop_id, match_key_type, match_key, channel) DO NOTHING
`,
[internalSku],
)
const skuCode = String(item.sku_code || '').trim()
if (skuCode) {
await query(