fix(finance): 收敛内部 SKU 履约路由与载体状态

统一内部 SKU 与渠道主/兜底解析,禁止并列主渠道。

运营指定渠道改为先创建真实载体,失败保持明确 pending。

历史缺载体、多载体冲突和自动发货兜底统一由管道修复。

迁移人工配置与旧 sku_code 规则,补充严格预检和集成测试。
This commit is contained in:
yml2213
2026-11-02 15:02:49 +08:00
parent 5fe33b0b46
commit 2f87445ed9
33 changed files with 2199 additions and 446 deletions
@@ -1,10 +1,9 @@
import {
listInternalSkuChannels,
type InternalSkuChannelRow,
} from './internal-sku-channel-service.js'
import { listInternalSkuChannels, planInternalSkuChannels } from './internal-sku-channel-service.js'
import type { ResolvedFulfillmentItem } from './product-resolution-service.js'
import { matchAffiliateDashSkuByInternalSku } from './product-resolution-service.js'
import { logWarn } from '../../utils/logger.js'
import type { FulfillmentChannel } from './channel-types.js'
import { resolveInternalSkuKey } from './internal-sku-resolver.js'
type IntakeChannelDecisionInput = {
provider?: string
@@ -18,14 +17,6 @@ function parseObject(value: unknown): Record<string, unknown> {
: {}
}
function resolveInternalSku(item: ResolvedFulfillmentItem): string {
const snapshot = parseObject(item.snapshot)
const spec = parseObject(item.spec)
return String(
snapshot.internalSku || spec.skuNick || spec.outSkuSn || item.skuCode || item.skuName || '',
).trim()
}
/**
* 下单入口读取内部 SKU 渠道决策。
*
@@ -36,13 +27,14 @@ function resolveInternalSku(item: ResolvedFulfillmentItem): string {
export async function applyIntakeChannelDecision(
item: ResolvedFulfillmentItem,
_input: IntakeChannelDecisionInput,
options: { preferredChannel?: FulfillmentChannel } = {},
): Promise<ResolvedFulfillmentItem> {
const internalSku = resolveInternalSku(item)
const internalSku = resolveInternalSkuKey(item)
if (!internalSku) return item
let channels: InternalSkuChannelRow[] = []
let plan
try {
channels = await listInternalSkuChannels(internalSku)
plan = planInternalSkuChannels(await listInternalSkuChannels(internalSku))
} catch (error) {
// 单测/旧库没有 118 迁移时不影响下单主流程,生产由迁移保证。
logWarn('[intake-channel-decision]', '读取内部 SKU 渠道失败', {
@@ -51,14 +43,32 @@ export async function applyIntakeChannelDecision(
})
return item
}
if (channels.length === 0) return item
if (plan.conflicts.length > 0) {
return {
...item,
snapshot: {
...parseObject(item.snapshot),
internalSku,
internalSkuChannelReason: 'routing_conflict',
internalSkuConflictChannels: plan.conflicts.map((channel) => channel.channel),
},
}
}
const preferred = options.preferredChannel
? plan.attempts.find((candidate) => candidate.channel === options.preferredChannel) ||
(options.preferredChannel === 'manual' || options.preferredChannel === 'third_party_voucher'
? null
: undefined)
: undefined
const primary = preferred === null ? null : preferred || plan.primary
if (!primary) return item
const snapshot = parseObject(item.snapshot)
const route = parseObject(snapshot.fulfillmentRoute)
const selected = String(route.selectedExecutorKey || '').trim()
// 路由配置未命中时会默认落 manual_dispatch;这不是运营显式规则,不能覆盖内部 SKU 能力。
const isDefaultManualFallback = selected === 'manual_dispatch' && !route.matchedRule
const primary = channels[0]!
const nextSnapshot: Record<string, unknown> = {
...snapshot,
internalSku: String(snapshot.internalSku || internalSku),
@@ -69,7 +79,8 @@ export async function applyIntakeChannelDecision(
const canOverrideDefaultManual = !selected || isDefaultManualFallback
const existingAffiliateSku = String(parseObject(snapshot.affiliateDash).sku || '').trim()
if (primary.channel === 'affiliate_dash' || selected === 'affiliate_dash') {
const desiredChannel = options.preferredChannel || primary.channel
if (desiredChannel === 'affiliate_dash' || selected === 'affiliate_dash') {
const dashSku = existingAffiliateSku || matchAffiliateDashSkuByInternalSku(internalSku)
if (dashSku) {
if (canOverrideDefaultManual || selected === 'affiliate_dash') {
@@ -109,17 +120,27 @@ export async function applyIntakeChannelDecision(
// 内部能力是接单/卡券,但路由只是默认人工兜底:清掉人工任务,交给工单或财务能力兜底。
if (
(primary.channel === 'work_order' || primary.channel === 'third_party_voucher') &&
(desiredChannel === 'work_order' || desiredChannel === 'third_party_voucher') &&
isDefaultManualFallback
) {
nextSnapshot.fulfillmentRoute = {
...route,
selectedExecutorKey: '',
reason: `内部 SKU 渠道能力为 ${primary.channel},不使用默认人工兜底`,
reason: `内部 SKU 渠道能力为 ${desiredChannel},不使用默认人工兜底`,
matchedRule: null,
}
return { ...item, isConfigured: false, snapshot: nextSnapshot }
}
if (desiredChannel === 'manual' && isDefaultManualFallback) {
nextSnapshot.fulfillmentRoute = {
...route,
selectedExecutorKey: 'manual_dispatch',
reason: '内部 SKU 渠道能力指定人工客服',
matchedRule: null,
}
return { ...item, isConfigured: true, snapshot: nextSnapshot }
}
return { ...item, snapshot: nextSnapshot }
}