This commit is contained in:
liufumingren 2026-08-18 09:20:39 +08:00
parent 55b6255421
commit 916aa4444c
2 changed files with 60 additions and 33 deletions

View File

@ -449,9 +449,13 @@ async function loadContainers() {
} }
} }
/** 库位关键词解析缓存:相同关键词只查询一次 */
const spaceResolveCache = new Map<string, Promise<{ id?: number; displayName?: string }>>()
function resetBatchState() { function resetBatchState() {
importFileName.value = '' importFileName.value = ''
parsedRows.value = [] parsedRows.value = []
spaceResolveCache.clear()
} }
function normalizeSpaceId(value: unknown): number | undefined { function normalizeSpaceId(value: unknown): number | undefined {
@ -554,7 +558,12 @@ async function resolveRowSpaceBeforeSubmit(
): Promise<string | null> { ): Promise<string | null> {
const keyword = row.space_keyword?.trim() const keyword = row.space_keyword?.trim()
if (!row.space_id && keyword) { if (!row.space_id && keyword) {
const resolved = await resolveSpaceByKeyword(keyword) let pending = spaceResolveCache.get(keyword)
if (!pending) {
pending = resolveSpaceByKeyword(keyword)
spaceResolveCache.set(keyword, pending)
}
const resolved = await pending
if (!resolved.id) { if (!resolved.id) {
const label = row.rowIndex ? `第 ${row.rowIndex} 行` : '当前行' const label = row.rowIndex ? `第 ${row.rowIndex} 行` : '当前行'
return `${label}库位未匹配: ${keyword}` return `${label}库位未匹配: ${keyword}`
@ -687,13 +696,14 @@ async function handleSubmit() {
return return
} }
for (const row of parsedRows.value) { const spaceErrors = await Promise.all(
const spaceError = await resolveRowSpaceBeforeSubmit(row) parsedRows.value.map((row) => resolveRowSpaceBeforeSubmit(row))
)
const spaceError = spaceErrors.find((error) => error) || null
if (spaceError) { if (spaceError) {
ElMessage.warning(spaceError) ElMessage.warning(spaceError)
return return
} }
}
const res = await addItem({ const res = await addItem({
entry_order_id: entryOrderId, entry_order_id: entryOrderId,

View File

@ -110,6 +110,7 @@ export async function resolveSpaceByKeyword(
} }
} }
try {
const res = (await indexApi.searchSpace({ keyword: text, page: 1, pageSize: 20 })) as unknown as Record< const res = (await indexApi.searchSpace({ keyword: text, page: 1, pageSize: 20 })) as unknown as Record<
string, string,
unknown unknown
@ -132,16 +133,32 @@ export async function resolveSpaceByKeyword(
id: Number.isFinite(id) && id > 0 ? id : undefined, id: Number.isFinite(id) && id > 0 ? id : undefined,
displayName: getSpaceDisplayName(item), displayName: getSpaceDisplayName(item),
} }
} catch (error) {
console.error('查询库位失败,兜底为默认收货区:', text, error)
return { id: 1, displayName: '默认收货区' }
}
} }
/** 批量解析多行库位:并发查询,相同关键词只查询一次 */
export async function resolveParsedRowSpaces(rows: ParsedDirectInboundImportRow[]): Promise<void> { export async function resolveParsedRowSpaces(rows: ParsedDirectInboundImportRow[]): Promise<void> {
for (const row of rows) { const pendingByKeyword = new Map<string, Promise<{ id?: number; displayName?: string }>>()
await Promise.all(
rows.map(async (row) => {
const keyword = toTrimmedString(row.space_keyword) const keyword = toTrimmedString(row.space_keyword)
if (!keyword) continue if (!keyword) return
const resolved = await resolveSpaceByKeyword(keyword)
let pending = pendingByKeyword.get(keyword)
if (!pending) {
pending = resolveSpaceByKeyword(keyword)
pendingByKeyword.set(keyword, pending)
}
const resolved = await pending
if (resolved.id) row.space_id = resolved.id if (resolved.id) row.space_id = resolved.id
if (resolved.displayName) row.space_keyword = resolved.displayName if (resolved.displayName) row.space_keyword = resolved.displayName
} })
)
} }
function buildEntryItemIndex(entryItems: DirectInboundImportRow[]) { function buildEntryItemIndex(entryItems: DirectInboundImportRow[]) {