import { listInternalSkuChannels, planInternalSkuChannels, type InternalSkuChannelPlan, type InternalSkuChannelRow, } 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 } type IntakeChannelDecisionOptions = { preferredChannel?: FulfillmentChannel } function parseObject(value: unknown): Record { return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record) : {} } function emptyRoute(reason: string) { return { selectedExecutorKey: '', selectedRuleId: '', reason, matchedRule: null, candidates: [], skipped: [], } } function classification(channel: FulfillmentChannel | 'unmatched', internalSku: string) { return { channel, canonicalSku: internalSku } } function withChannelSnapshot(input: { item: ResolvedFulfillmentItem internalSku: string channel: FulfillmentChannel | 'unmatched' source?: string reason?: string route?: Record affiliateDash?: Record | null configured: boolean skuCode?: string }): ResolvedFulfillmentItem { const snapshot = parseObject(input.item.snapshot) return { ...input.item, ...(input.skuCode ? { skuCode: input.skuCode } : {}), spec: { ...(input.item.spec && typeof input.item.spec === 'object' ? input.item.spec : {}), skuNick: input.internalSku, }, isConfigured: input.configured, snapshot: { ...snapshot, internalSku: input.internalSku, ...(input.source ? { internalSkuChannelSource: input.source } : {}), ...(input.channel !== 'unmatched' ? { internalSkuChannel: input.channel } : {}), ...(input.reason ? { internalSkuChannelReason: input.reason } : {}), fulfillmentRoute: input.route || emptyRoute(input.reason || ''), affiliateDash: input.affiliateDash ?? null, classification: classification(input.channel, input.internalSku), }, } } /** * 纯函数:把内部 SKU 渠道计划应用到订单项。 * 渠道选择只允许来自 internal_sku_channels,旧路由不能抢占。 */ export function applyIntakeChannelPlan( item: ResolvedFulfillmentItem, plan: InternalSkuChannelPlan, options: IntakeChannelDecisionOptions = {}, ): ResolvedFulfillmentItem { const internalSku = resolveInternalSkuKey(item) if (!internalSku) { return withChannelSnapshot({ item, internalSku: '', channel: 'unmatched', reason: 'no_internal_sku', configured: false, }) } if (plan.conflicts.length > 0) { return withChannelSnapshot({ item, internalSku, channel: 'unmatched', reason: 'routing_conflict', configured: false, }) } const preferred = options.preferredChannel ? plan.attempts.find((candidate) => candidate.channel === options.preferredChannel) : undefined const primary = preferred || plan.primary if (!primary) { return withChannelSnapshot({ item, internalSku, channel: 'unmatched', reason: 'no_channel_capability', configured: false, }) } const channel = primary.channel const carrierParams = parseObject(primary.config_json) if (channel === 'affiliate_dash') { const dashSku = String(carrierParams.affiliateSku || '').trim() || matchAffiliateDashSkuByInternalSku(internalSku) if (!dashSku) { return withChannelSnapshot({ item, internalSku, channel, source: primary.source, reason: 'missing_affiliate_dash_mapping', configured: false, }) } return withChannelSnapshot({ item, internalSku, channel, source: primary.source, reason: '内部 SKU 渠道配置指定自动发货', configured: true, skuCode: dashSku, route: { selectedExecutorKey: 'affiliate_dash', selectedRuleId: '', reason: '内部 SKU 渠道配置指定自动发货', matchedRule: null, candidates: [], skipped: [], }, affiliateDash: { matchMode: 'affiliate_dash_sku_mapping', productNo: internalSku, sku: dashSku, skuName: internalSku, }, }) } if (channel === 'manual') { if (!Number(primary.config_json.manualConfigId || 0)) { return withChannelSnapshot({ item, internalSku, channel, source: primary.source, reason: 'manual_config_missing', configured: false, }) } return withChannelSnapshot({ item, internalSku, channel, source: primary.source, reason: '内部 SKU 渠道配置指定人工客服', configured: true, route: { selectedExecutorKey: 'manual_dispatch', selectedRuleId: '', reason: '内部 SKU 渠道配置指定人工客服', matchedRule: null, candidates: [], skipped: [], }, }) } // 接单平台由发单入口读取同一份 internal_sku_channels 创建工单; // 卡券无执行任务。两者都不能留下 affiliate/manual 路由。 return withChannelSnapshot({ item, internalSku, channel, source: primary.source, reason: channel === 'work_order' ? '内部 SKU 渠道配置指定接单平台' : '内部 SKU 渠道配置指定第三方卡券', configured: false, }) } /** 下单入口读取唯一渠道源:internal_sku_channels。 */ export async function applyIntakeChannelDecision( item: ResolvedFulfillmentItem, _input: IntakeChannelDecisionInput, options: IntakeChannelDecisionOptions = {}, ): Promise { const internalSku = resolveInternalSkuKey(item) if (!internalSku) { return withChannelSnapshot({ item, internalSku: '', channel: 'unmatched', reason: 'no_internal_sku', configured: false, }) } let channels: InternalSkuChannelRow[] try { channels = await listInternalSkuChannels(internalSku) } catch (error) { logWarn('[intake-channel-decision]', '读取内部 SKU 渠道失败', { internalSku, error: error instanceof Error ? error.message : String(error), }) return withChannelSnapshot({ item, internalSku, channel: 'unmatched', reason: 'channel_config_unavailable', configured: false, }) } return applyIntakeChannelPlan(item, planInternalSkuChannels(channels), options) }