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 platform?: string shopId?: string } function parseObject(value: unknown): Record { return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record) : {} } /** * 下单入口读取内部 SKU 渠道决策。 * * 目前只补“自动发货”载体:若内部 SKU 能力是 affiliate_dash 且能解析出 dash sku, * 直接写 fulfillmentRoute + affiliateDash,让 delivery-task-service 建任务。 * 接单平台仍由 work_order 匹配器创建;卡券/人工无任务,由财务侧兜底。 */ export async function applyIntakeChannelDecision( item: ResolvedFulfillmentItem, _input: IntakeChannelDecisionInput, options: { preferredChannel?: FulfillmentChannel } = {}, ): Promise { const internalSku = resolveInternalSkuKey(item) if (!internalSku) return item let plan try { plan = planInternalSkuChannels(await listInternalSkuChannels(internalSku)) } catch (error) { // 单测/旧库没有 118 迁移时不影响下单主流程,生产由迁移保证。 logWarn('[intake-channel-decision]', '读取内部 SKU 渠道失败', { internalSku, error: error instanceof Error ? error.message : String(error), }) 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 nextSnapshot: Record = { ...snapshot, internalSku: String(snapshot.internalSku || internalSku), internalSkuChannel: primary.channel, internalSkuChannelSource: primary.source, } const canOverrideDefaultManual = !selected || isDefaultManualFallback const existingAffiliateSku = String(parseObject(snapshot.affiliateDash).sku || '').trim() 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') { nextSnapshot.fulfillmentRoute = { ...route, selectedExecutorKey: 'affiliate_dash', selectedRuleId: String(route.selectedRuleId || ''), reason: canOverrideDefaultManual ? '内部 SKU 渠道能力指定自动发货' : String(route.reason || '自动发货路由'), matchedRule: route.matchedRule ?? null, candidates: Array.isArray(route.candidates) ? route.candidates : [], skipped: Array.isArray(route.skipped) ? route.skipped : [], } nextSnapshot.affiliateDash = { matchMode: 'affiliate_dash_sku_mapping', productNo: internalSku, sku: dashSku, skuName: internalSku, } // 商品归一按 aff: 找商品;内部中文 SKU 固定落 spec.skuNick。 return { ...item, skuCode: dashSku, spec: { ...(item.spec && typeof item.spec === 'object' ? item.spec : {}), skuNick: internalSku, }, isConfigured: true, snapshot: nextSnapshot, } } } nextSnapshot.internalSkuChannelReason = 'missing_affiliate_dash_mapping' return { ...item, snapshot: nextSnapshot } } // 内部能力是接单/卡券,但路由只是默认人工兜底:清掉人工任务,交给工单或财务能力兜底。 if ( (desiredChannel === 'work_order' || desiredChannel === 'third_party_voucher') && isDefaultManualFallback ) { nextSnapshot.fulfillmentRoute = { ...route, selectedExecutorKey: '', 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 } }