fix(fulfillment): 统一渠道规则与标准商品绑定

This commit is contained in:
yml2213
2026-11-05 18:17:12 +08:00
parent 8b9b4fc4ad
commit 89a68b5734
3 changed files with 318 additions and 59 deletions
@@ -556,13 +556,13 @@ export async function assignOrderItemsAfterCarrierReady(input: {
} else {
await query(
`UPDATE order_items
SET fulfillment_channel = $3,
SET fulfillment_channel = NULL,
fulfillment_status = 'pending',
fulfillment_source = 'operator_assigned',
fulfillment_locked = TRUE,
fulfillment_pending_reason = 'no_carrier',
fulfillment_pending_detail = $2::jsonb,
canonical_product_id = COALESCE($4, canonical_product_id),
canonical_product_id = COALESCE($3, canonical_product_id),
updated_at = NOW()
WHERE id = $1 AND fulfillment_status <> 'archived'`,
[
@@ -573,7 +573,6 @@ export async function assignOrderItemsAfterCarrierReady(input: {
message: carrier.message,
carrierIssue: carrier.reason,
}),
input.channel,
input.canonicalProductId || null,
],
)
@@ -761,6 +760,14 @@ export async function switchChannelForOrderItems(input: {
switchInternalSkus.length === 0 ? 'internal_sku_required' : 'multiple_internal_skus',
})
}
const targetInternalSku = switchInternalSkus[0]!
const draftProductId = await ensureDraftProductForChannel(targetInternalSku, input.channel)
if (!draftProductId) {
throw createHttpError('渠道标准商品创建或启用失败,请先在商品配置中处理', {
statusCode: 409,
errorCode: 'channel_standard_product_required',
})
}
const items = (
await query<OrderItemRow>(`SELECT * FROM order_items WHERE id = ANY($1::bigint[])`, [ids])
@@ -909,6 +916,7 @@ export async function switchChannelForOrderItems(input: {
source: 'operator_assigned',
lock: true,
note: input.note,
canonicalProductId: draftProductId,
})
await query(
`UPDATE order_items
@@ -917,12 +925,11 @@ export async function switchChannelForOrderItems(input: {
[readyIds, input.adminUserId],
)
if (assigned.updated > 0) {
const internalSku = switchInternalSkus[0]!
const carrier = await resolveInternalSkuCarrierConfig(internalSku, input.channel)
const carrier = await resolveInternalSkuCarrierConfig(targetInternalSku, input.channel)
await setInternalSkuPrimaryChannel({
internalSku,
internalSku: targetInternalSku,
channel: input.channel,
canonicalProductId: carrier.canonicalProductId,
canonicalProductId: draftProductId,
configJson: carrier.configJson,
source: 'operator',
note: input.note,
@@ -1580,21 +1587,18 @@ export async function assignChannelByInternalSku(input: {
[internalSku, input.channel],
)
const draftProductId = await ensureDraftProductForChannel(internalSku, input.channel).catch(
(error) => {
logWarn('[channel-assign]', '自动创建渠道草稿商品失败', {
internalSku,
channel: input.channel,
error: error instanceof Error ? error.message : String(error),
})
return null
},
)
const draftProductId = await ensureDraftProductForChannel(internalSku, input.channel)
if (!draftProductId) {
throw createHttpError('渠道标准商品创建或启用失败,请先在商品配置中处理', {
statusCode: 409,
errorCode: 'channel_standard_product_required',
})
}
const carrier = await resolveInternalSkuCarrierConfig(internalSku, input.channel)
await setInternalSkuPrimaryChannel({
internalSku,
channel: input.channel,
canonicalProductId: carrier.canonicalProductId,
canonicalProductId: draftProductId,
source: 'operator',
configJson: {
...carrier.configJson,
@@ -1818,7 +1822,18 @@ async function ensureDraftProductForChannel(
$5::jsonb, $6, $6
)
ON CONFLICT (canonical_sku) DO UPDATE
SET updated_at = product_catalog.updated_at
SET status = 'active',
canonical_name = CASE
WHEN BTRIM(product_catalog.canonical_name) = '' THEN EXCLUDED.canonical_name
ELSE product_catalog.canonical_name
END,
attributes_json = (
product_catalog.attributes_json
- 'deletedAt'
- 'deletedBy'
- 'archivedAt'
) || jsonb_build_object('reactivatedAt', EXCLUDED.updated_at),
updated_at = EXCLUDED.updated_at
RETURNING id, status
`,
[
@@ -1891,7 +1906,7 @@ async function resolveInternalSkuCarrierConfig(
FROM product_catalog
WHERE status = 'active'
AND (canonical_sku LIKE $2 OR canonical_sku LIKE $3)
AND canonicalize_internal_sku(canonical_name) = $1
AND canonicalize_internal_sku(substring(canonical_sku from 5)) = $1
ORDER BY id`,
[key, `${prefixes[0]}%`, `${prefixes[1] || prefixes[0]}%`],
)
@@ -1958,19 +1973,28 @@ export async function upsertChannelRule(input: {
})
}
const priority = Math.trunc(Number(input.priority) || 100)
const draftProductId = await ensureDraftProductForChannel(matchKey, input.channel)
if (!draftProductId) {
throw createHttpError('渠道标准商品创建或启用失败,请先在商品配置中处理', {
statusCode: 409,
errorCode: 'channel_standard_product_required',
})
}
const priority =
input.channel === 'third_party_voucher' ? 10 : Math.trunc(Number(input.priority) || 100)
const carrier = await resolveInternalSkuCarrierConfig(matchKey, input.channel)
const id = await upsertInternalSkuChannel({
internalSku: matchKey,
channel: input.channel,
priority,
isFallback: priority >= 90,
isFallback: input.channel === 'third_party_voucher' ? false : priority >= 90,
enabled: input.enabled !== false,
source: source === 'operator' ? 'operator' : `rule:${source}`,
note: String(input.note || '').slice(0, 300),
adminUserId: input.adminUserId,
replacePriority: true,
canonicalProductId: carrier.canonicalProductId,
canonicalProductId: draftProductId,
configJson: carrier.configJson,
})
return { id }
@@ -2079,15 +2103,18 @@ export async function ensureVoucherRuleForSku(skuCode: string): Promise<boolean>
LIMIT 1`,
[`tpv:${key}`],
)
const productId = Number(product.rows[0]?.id || 0)
if (!productId) return false
await upsertInternalSkuChannel({
internalSku: key,
channel: 'third_party_voucher',
priority: 10,
isFallback: false,
enabled: true,
canonicalProductId: Number(product.rows[0]?.id || 0) || null,
canonicalProductId: productId,
configJson: { voucherSku: key },
source: 'voucher',
replacePriority: true,
})
return true
}
@@ -2110,7 +2137,8 @@ export async function ensureVoucherChannelRules(): Promise<{
FROM product_catalog product
WHERE product.status = 'active'
AND product.canonical_sku LIKE 'tpv:%'
AND canonicalize_internal_sku(channel.internal_sku) = canonicalize_internal_sku(product.canonical_name)
AND canonicalize_internal_sku(channel.internal_sku) =
canonicalize_internal_sku(substring(product.canonical_sku from 5))
AND channel.channel <> 'third_party_voucher'
AND channel.enabled = TRUE
AND channel.is_fallback = FALSE`,
@@ -2122,17 +2150,21 @@ export async function ensureVoucherChannelRules(): Promise<{
canonical_product_id, config_json, source, created_at, updated_at
)
SELECT
canonicalize_internal_sku(p.canonical_name), 'third_party_voucher', 10, FALSE, TRUE,
canonicalize_internal_sku(substring(p.canonical_sku from 5)),
'third_party_voucher', 10, FALSE, TRUE,
p.id, jsonb_build_object('voucherSku', substring(p.canonical_sku from 5)),
'voucher', NOW(), NOW()
FROM product_catalog p
WHERE p.status = 'active'
AND p.canonical_sku LIKE 'tpv:%'
AND BTRIM(COALESCE(p.canonical_name, '')) <> ''
AND canonicalize_internal_sku(substring(p.canonical_sku from 5)) <> ''
ON CONFLICT (internal_sku, channel) DO UPDATE SET
priority = 10,
is_fallback = FALSE,
enabled = TRUE,
canonical_product_id = COALESCE(EXCLUDED.canonical_product_id, internal_sku_channels.canonical_product_id),
canonical_product_id = EXCLUDED.canonical_product_id,
config_json = internal_sku_channels.config_json || EXCLUDED.config_json,
source = EXCLUDED.source,
updated_at = NOW()
RETURNING id
`,