fix(fulfillment): 封堵重复载体并完善统一SKU映射
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { query, withTransaction } from '../../db/client.js'
|
||||
import { canonicalizeInternalSku, containsCjk } from './internal-sku-resolver.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
|
||||
export type InternalSkuMappingMatchType =
|
||||
| 'sku_code'
|
||||
@@ -13,12 +14,34 @@ export type InternalSkuMappingCandidate = {
|
||||
value: unknown
|
||||
}
|
||||
|
||||
const INTERNAL_SKU_MAPPING_MATCH_TYPES = new Set<InternalSkuMappingMatchType>([
|
||||
'sku_code',
|
||||
'sku_nick',
|
||||
'out_sku_sn',
|
||||
'spec',
|
||||
'sku_name',
|
||||
])
|
||||
|
||||
export type ResolvedInternalSkuMapping = {
|
||||
id: number
|
||||
internalSku: string
|
||||
source: string
|
||||
}
|
||||
|
||||
export type InternalSkuMappingRow = {
|
||||
id: number
|
||||
provider: string
|
||||
shop_id: string
|
||||
match_type: InternalSkuMappingMatchType
|
||||
match_value: string
|
||||
internal_sku: string
|
||||
priority: number
|
||||
enabled: boolean
|
||||
source: string
|
||||
note: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export function normalizeInternalSkuMappingValue(value: unknown): string {
|
||||
return canonicalizeInternalSku(value)
|
||||
}
|
||||
@@ -83,14 +106,28 @@ export async function upsertInternalSkuMapping(input: {
|
||||
priority?: number
|
||||
source?: string
|
||||
note?: string
|
||||
enabled?: boolean
|
||||
}): Promise<number> {
|
||||
const provider = String(input.provider || '').trim()
|
||||
const shopId = String(input.shopId || '').trim()
|
||||
if (!INTERNAL_SKU_MAPPING_MATCH_TYPES.has(input.matchType)) {
|
||||
throw createHttpError('映射类型无效', {
|
||||
statusCode: 400,
|
||||
errorCode: 'internal_sku_mapping_type_invalid',
|
||||
})
|
||||
}
|
||||
const matchValue = normalizeInternalSkuMappingValue(input.matchValue)
|
||||
const internalSku = canonicalizeInternalSku(input.internalSku)
|
||||
const priority = Number.isFinite(Number(input.priority)) ? Number(input.priority) : 100
|
||||
const source = String(input.source || 'manual').trim() || 'manual'
|
||||
const note = String(input.note || '').trim()
|
||||
const enabled = input.enabled !== false
|
||||
if (!matchValue || !internalSku || !containsCjk(internalSku)) {
|
||||
throw createHttpError('内部 SKU 和匹配值不能为空,且内部 SKU 必须包含中文', {
|
||||
statusCode: 400,
|
||||
errorCode: 'internal_sku_mapping_invalid',
|
||||
})
|
||||
}
|
||||
|
||||
return withTransaction(async (client) => {
|
||||
const existing = await client.query<{ id: number }>(
|
||||
@@ -98,32 +135,114 @@ export async function upsertInternalSkuMapping(input: {
|
||||
WHERE provider = $1 AND shop_id = $2 AND match_type = $3
|
||||
AND canonicalize_internal_sku(match_value) = $4
|
||||
ORDER BY id
|
||||
LIMIT 1
|
||||
FOR UPDATE`,
|
||||
[provider, shopId, input.matchType, matchValue],
|
||||
)
|
||||
if (existing.rows.length > 1) {
|
||||
throw createHttpError('同一平台/店铺/字段存在多条规范化重复映射', {
|
||||
statusCode: 409,
|
||||
errorCode: 'internal_sku_mapping_ambiguous',
|
||||
})
|
||||
}
|
||||
const existingId = Number(existing.rows[0]?.id || 0)
|
||||
const result = existingId
|
||||
? await client.query<{ id: number }>(
|
||||
`UPDATE internal_sku_mappings
|
||||
SET match_value = $2, internal_sku = $3, priority = $4,
|
||||
enabled = TRUE, source = $5, note = $6, updated_at = NOW()
|
||||
enabled = $5, source = $6, note = $7, updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING id`,
|
||||
[existingId, matchValue, internalSku, priority, source, note],
|
||||
[existingId, matchValue, internalSku, priority, enabled, source, note],
|
||||
)
|
||||
: await client.query<{ id: number }>(
|
||||
`INSERT INTO internal_sku_mappings (
|
||||
provider, shop_id, match_type, match_value, internal_sku,
|
||||
priority, enabled, source, note, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, TRUE, $7, $8, NOW(), NOW())
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW())
|
||||
RETURNING id`,
|
||||
[provider, shopId, input.matchType, matchValue, internalSku, priority, source, note],
|
||||
[
|
||||
provider,
|
||||
shopId,
|
||||
input.matchType,
|
||||
matchValue,
|
||||
internalSku,
|
||||
priority,
|
||||
enabled,
|
||||
source,
|
||||
note,
|
||||
],
|
||||
)
|
||||
return Number(result.rows[0]?.id || 0)
|
||||
})
|
||||
}
|
||||
|
||||
export async function listInternalSkuMappingsPage(input: {
|
||||
keyword?: string
|
||||
provider?: string
|
||||
shopId?: string
|
||||
matchType?: string
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}): Promise<{ items: InternalSkuMappingRow[]; total: number; page: number; pageSize: number }> {
|
||||
const page = Math.max(1, Math.trunc(Number(input.page) || 1))
|
||||
const pageSize = Math.min(200, Math.max(1, Math.trunc(Number(input.pageSize) || 30)))
|
||||
const params: unknown[] = []
|
||||
const clauses: string[] = []
|
||||
const keyword = String(input.keyword || '').trim()
|
||||
if (keyword) {
|
||||
params.push(`%${keyword}%`)
|
||||
clauses.push(
|
||||
`(match_value ILIKE $${params.length} OR internal_sku ILIKE $${params.length} OR note ILIKE $${params.length})`,
|
||||
)
|
||||
}
|
||||
if (input.provider) {
|
||||
params.push(String(input.provider).trim())
|
||||
clauses.push(`provider = $${params.length}`)
|
||||
}
|
||||
if (input.shopId) {
|
||||
params.push(String(input.shopId).trim())
|
||||
clauses.push(`shop_id = $${params.length}`)
|
||||
}
|
||||
if (input.matchType) {
|
||||
params.push(String(input.matchType).trim())
|
||||
clauses.push(`match_type = $${params.length}`)
|
||||
}
|
||||
const where = clauses.length > 0 ? `WHERE ${clauses.join(' AND ')}` : ''
|
||||
const pageParams = [...params, pageSize, (page - 1) * pageSize]
|
||||
const [countResult, rows] = await Promise.all([
|
||||
query<{ total: number }>(
|
||||
`SELECT COUNT(*)::int AS total FROM internal_sku_mappings ${where}`,
|
||||
params,
|
||||
),
|
||||
query<InternalSkuMappingRow>(
|
||||
`SELECT id, provider, shop_id, match_type, match_value, internal_sku,
|
||||
priority, enabled, source, note, updated_at
|
||||
FROM internal_sku_mappings
|
||||
${where}
|
||||
ORDER BY enabled DESC, priority ASC, id DESC
|
||||
LIMIT $${pageParams.length - 1} OFFSET $${pageParams.length}`,
|
||||
pageParams,
|
||||
),
|
||||
])
|
||||
return {
|
||||
items: rows.rows.map((row) => ({
|
||||
...row,
|
||||
id: Number(row.id),
|
||||
priority: Number(row.priority),
|
||||
})),
|
||||
total: Number(countResult.rows[0]?.total || 0),
|
||||
page,
|
||||
pageSize,
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteInternalSkuMapping(mappingId: number): Promise<boolean> {
|
||||
const id = Math.trunc(Number(mappingId) || 0)
|
||||
if (id <= 0) return false
|
||||
const result = await query(`DELETE FROM internal_sku_mappings WHERE id = $1`, [id])
|
||||
return (result.rowCount || 0) > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单项级内部 SKU 补全:
|
||||
* - 现有 skuNick/outSkuSn/skuCode/skuName 已有中文 → 不查库;
|
||||
|
||||
Reference in New Issue
Block a user