fix(finance): 重写履约路由迁移与统一渠道配置

重写 120/121 迁移,统一内部 SKU、渠道 id、config_json 和旧规则转换。

后台渠道配置改读统一渠道表,卡券与人工配置不再写旧规则。

增加安全渠道切换,并排除已关闭、失败或过期的历史任务载体。

按生产 119 基线重跑迁移、完整履约财务链路和严格预检。
This commit is contained in:
yml2213
2026-11-02 15:29:13 +08:00
parent 2f87445ed9
commit 6bf28489e0
31 changed files with 958 additions and 464 deletions
@@ -178,6 +178,7 @@ export async function backfillAdminAffiliateDashSkus() {
) AS sku_code
FROM fulfillment_tasks ft
WHERE ft.executor_key = 'affiliate_dash'
AND ft.task_status NOT IN ('closed','expired','failed','manual_review','retry_pending','cancelled')
AND ft.created_at >= NOW() - INTERVAL '30 days'
AND btrim(COALESCE(ft.context_json->>'skuCode', '')) <> ''
ORDER BY ft.order_item_id, ft.created_at DESC, ft.id DESC
@@ -202,6 +203,7 @@ export async function backfillAdminAffiliateDashSkus() {
SELECT COUNT(DISTINCT ft.order_item_id)::int
FROM fulfillment_tasks ft
WHERE ft.executor_key = 'affiliate_dash'
AND ft.task_status NOT IN ('closed','expired','failed','manual_review','retry_pending','cancelled')
AND ft.created_at >= NOW() - INTERVAL '30 days'
) AS affiliate_item_count,
(SELECT COUNT(*)::int FROM target) AS matched_count,
@@ -245,6 +247,7 @@ export async function backfillAdminAffiliateDashSkus() {
SELECT 1 FROM fulfillment_tasks ft
WHERE ft.order_item_id = oi.id
AND ft.executor_key = 'affiliate_dash'
AND ft.task_status NOT IN ('closed','expired','failed','manual_review','retry_pending','cancelled')
AND ft.created_at >= NOW() - INTERVAL '30 days'
)
`,
@@ -408,7 +408,6 @@ export async function deleteManualFulfillmentConfig(skuKey: string) {
`DELETE FROM manual_fulfillment_configs WHERE sku_key = $1 RETURNING id`,
[key],
)
await query(`DELETE FROM sku_channel_rules WHERE source = 'manual' AND match_key = $1`, [key])
await upsertInternalSkuChannel({
internalSku: key,
channel: 'manual',
@@ -722,9 +721,7 @@ function requiredRow<T>(row: T | undefined, message: string): T {
}
/**
* 人工配置 → 渠道规则(内部 SKU 精确匹配)。
*
* 幂等:按唯一键 (provider, shop, match_key_type, match_key, channel) upsert。
* 人工配置 → internal_sku_channels(内部 SKU 精确匹配)。
*/
async function syncManualChannelRule(config: {
id: number
@@ -757,29 +754,4 @@ async function syncManualChannelRule(config: {
configJson: { manualConfigId: config.id, note: String(config.note || '').slice(0, 200) },
replacePriority: true,
})
await query(
`
INSERT INTO sku_channel_rules (
provider, shop_id, match_key_type, match_key, channel, priority,
source, source_ref_id, enabled, note, created_at, updated_at
)
VALUES ('', '', 'internal_sku', $1, 'manual', $5, 'manual', $2, $3, $4, NOW(), NOW())
ON CONFLICT (provider, shop_id, match_key_type, match_key, channel)
DO UPDATE SET
enabled = EXCLUDED.enabled,
priority = EXCLUDED.priority,
source = 'manual',
source_ref_id = EXCLUDED.source_ref_id,
note = EXCLUDED.note,
updated_at = NOW()
`,
[
skuKey,
config.id,
config.enabled !== false,
String(config.note || '').slice(0, 200),
hasOtherChannel ? 90 : 10,
],
)
}
@@ -198,23 +198,10 @@ async function ensureVoucherProductForItem(item: ItemRow): Promise<number | null
priority: 10,
isFallback: false,
canonicalProductId: productId,
configJson: { voucherSku: internalSku },
source: 'auto',
}).catch(() => undefined)
// 同一内部中文 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],
)
return productId
}