fix(fulfillment): 优先用平台字段映射中文SKU
This commit is contained in:
@@ -49,7 +49,7 @@ export function normalizeInternalSkuMappingValue(value: unknown): string {
|
||||
/**
|
||||
* 按平台字段候选值查内部中文 SKU 映射。
|
||||
*
|
||||
* 优先级:精确 provider/shop > 全局;同范围按 priority 升序、id 升序。
|
||||
* 优先级:精确 provider/shop > 全局;同范围按候选字段顺序、priority、id 升序。
|
||||
* 返回 null 表示没有映射,调用方回退 manual_sku_key 生成列。
|
||||
*/
|
||||
export async function resolveInternalSkuMapping(input: {
|
||||
@@ -60,9 +60,10 @@ export async function resolveInternalSkuMapping(input: {
|
||||
const provider = String(input.provider || '').trim()
|
||||
const shopId = String(input.shopId || '').trim()
|
||||
const pairs = input.candidates
|
||||
.map((candidate) => ({
|
||||
.map((candidate, candidatePriority) => ({
|
||||
matchType: candidate.matchType,
|
||||
matchValue: normalizeInternalSkuMappingValue(candidate.value),
|
||||
candidatePriority,
|
||||
}))
|
||||
.filter((candidate) => candidate.matchValue !== '')
|
||||
if (pairs.length === 0) return null
|
||||
@@ -71,7 +72,8 @@ export async function resolveInternalSkuMapping(input: {
|
||||
`
|
||||
SELECT m.id, m.internal_sku, m.source
|
||||
FROM internal_sku_mappings m
|
||||
JOIN unnest($3::text[], $4::text[]) AS c(match_type, match_value)
|
||||
JOIN unnest($3::text[], $4::text[], $5::int[])
|
||||
AS c(match_type, match_value, candidate_priority)
|
||||
ON m.match_type = c.match_type
|
||||
AND canonicalize_internal_sku(m.match_value) = c.match_value
|
||||
WHERE m.enabled = TRUE
|
||||
@@ -82,11 +84,18 @@ export async function resolveInternalSkuMapping(input: {
|
||||
WHEN m.provider = $1 THEN 1
|
||||
WHEN m.shop_id = $2 THEN 2
|
||||
ELSE 3 END,
|
||||
c.candidate_priority ASC,
|
||||
m.priority ASC,
|
||||
m.id ASC
|
||||
LIMIT 1
|
||||
`,
|
||||
[provider, shopId, pairs.map((pair) => pair.matchType), pairs.map((pair) => pair.matchValue)],
|
||||
[
|
||||
provider,
|
||||
shopId,
|
||||
pairs.map((pair) => pair.matchType),
|
||||
pairs.map((pair) => pair.matchValue),
|
||||
pairs.map((pair) => pair.candidatePriority),
|
||||
],
|
||||
)
|
||||
const row = result.rows[0]
|
||||
if (!row) return null
|
||||
@@ -245,8 +254,9 @@ export async function deleteInternalSkuMapping(mappingId: number): Promise<boole
|
||||
|
||||
/**
|
||||
* 订单项级内部 SKU 补全:
|
||||
* - 现有 skuNick/outSkuSn/skuCode/skuName 已有中文 → 不查库;
|
||||
* - 没有中文时 → 查 internal_sku_mappings,把任何平台码映射成中文内部 SKU。
|
||||
* - 明确的 skuNick/outSkuSn/skuCode 已有中文 → 不查库;
|
||||
* - 否则先查 internal_sku_mappings,避免中文长标题抢占内部 SKU;
|
||||
* - 没有映射时才由调用方回退中文标题。
|
||||
*/
|
||||
export async function resolveInternalSkuForOrderItem(input: {
|
||||
provider?: string | null
|
||||
@@ -256,7 +266,7 @@ export async function resolveInternalSkuForOrderItem(input: {
|
||||
skuName?: unknown
|
||||
}): Promise<{ internalSku: string; source: string; mappingId: number } | null> {
|
||||
const spec = input.spec && typeof input.spec === 'object' ? input.spec : {}
|
||||
const directCandidates = [spec.skuNick, spec.outSkuSn, input.skuCode, input.skuName]
|
||||
const directCandidates = [spec.skuNick, spec.outSkuSn, input.skuCode]
|
||||
.map(normalizeInternalSkuMappingValue)
|
||||
.filter(Boolean)
|
||||
if (directCandidates.some(containsCjk)) return null
|
||||
|
||||
Reference in New Issue
Block a user