feat(finance): 统一内部 SKU 驱动的履约与财务分流
- 新增 internal_sku_mappings / internal_sku_channels,平台字段统一映射到内部中文 SKU - 下单入口按内部 SKU 渠道决策修正默认人工路由,自动发货补全 dash sku - 财务 channel_assign 读取统一渠道表;卡券/人工能力兜底,自动发货/接单要求真实载体 - carrier reconciliation 自动补自动发货任务和接单工单 - pending 落明确原因:无内部 SKU / 无渠道能力 / 无履约载体 - 合并、重判、旧 sku_code 规则清理、部署后 preflight 核对脚本 - 待分类队列按内部 SKU 聚合并展示原因
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import {
|
||||
listInternalSkuChannels,
|
||||
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'
|
||||
|
||||
type IntakeChannelDecisionInput = {
|
||||
provider?: string
|
||||
platform?: string
|
||||
shopId?: string
|
||||
}
|
||||
|
||||
function parseObject(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === 'object' && !Array.isArray(value)
|
||||
? (value as 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 渠道决策。
|
||||
*
|
||||
* 目前只补“自动发货”载体:若内部 SKU 能力是 affiliate_dash 且能解析出 dash sku,
|
||||
* 直接写 fulfillmentRoute + affiliateDash,让 delivery-task-service 建任务。
|
||||
* 接单平台仍由 work_order 匹配器创建;卡券/人工无任务,由财务侧兜底。
|
||||
*/
|
||||
export async function applyIntakeChannelDecision(
|
||||
item: ResolvedFulfillmentItem,
|
||||
_input: IntakeChannelDecisionInput,
|
||||
): Promise<ResolvedFulfillmentItem> {
|
||||
const internalSku = resolveInternalSku(item)
|
||||
if (!internalSku) return item
|
||||
|
||||
let channels: InternalSkuChannelRow[] = []
|
||||
try {
|
||||
channels = await listInternalSkuChannels(internalSku)
|
||||
} catch (error) {
|
||||
// 单测/旧库没有 118 迁移时不影响下单主流程,生产由迁移保证。
|
||||
logWarn('[intake-channel-decision]', '读取内部 SKU 渠道失败', {
|
||||
internalSku,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
})
|
||||
return item
|
||||
}
|
||||
if (channels.length === 0) 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),
|
||||
internalSkuChannel: primary.channel,
|
||||
internalSkuChannelSource: primary.source,
|
||||
}
|
||||
|
||||
const canOverrideDefaultManual = !selected || isDefaultManualFallback
|
||||
const existingAffiliateSku = String(parseObject(snapshot.affiliateDash).sku || '').trim()
|
||||
|
||||
if (primary.channel === '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:<dashSku> 找商品;内部中文 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 (
|
||||
(primary.channel === 'work_order' || primary.channel === 'third_party_voucher') &&
|
||||
isDefaultManualFallback
|
||||
) {
|
||||
nextSnapshot.fulfillmentRoute = {
|
||||
...route,
|
||||
selectedExecutorKey: '',
|
||||
reason: `内部 SKU 渠道能力为 ${primary.channel},不使用默认人工兜底`,
|
||||
matchedRule: null,
|
||||
}
|
||||
return { ...item, isConfigured: false, snapshot: nextSnapshot }
|
||||
}
|
||||
|
||||
return { ...item, snapshot: nextSnapshot }
|
||||
}
|
||||
Reference in New Issue
Block a user