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,12 +696,13 @@ 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))
if (spaceError) { )
ElMessage.warning(spaceError) const spaceError = spaceErrors.find((error) => error) || null
return if (spaceError) {
} ElMessage.warning(spaceError)
return
} }
const res = await addItem({ const res = await addItem({

View File

@ -110,38 +110,55 @@ export async function resolveSpaceByKeyword(
} }
} }
const res = (await indexApi.searchSpace({ keyword: text, page: 1, pageSize: 20 })) as unknown as Record< try {
string, const res = (await indexApi.searchSpace({ keyword: text, page: 1, pageSize: 20 })) as unknown as Record<
unknown string,
> unknown
const data = (res?.data || res?.list || res) as Record<string, unknown> >
const list = Array.isArray(data?.list) ? data.list : Array.isArray(data) ? data : [] const data = (res?.data || res?.list || res) as Record<string, unknown>
if (!list.length) return {} const list = Array.isArray(data?.list) ? data.list : Array.isArray(data) ? data : []
if (!list.length) return {}
const exact = const exact =
(list as Record<string, unknown>[]).find((item) => { (list as Record<string, unknown>[]).find((item) => {
const code = toTrimmedString(item?.space_code) const code = toTrimmedString(item?.space_code)
const customName = toTrimmedString(item?.custom_name ?? item?.customName) const customName = toTrimmedString(item?.custom_name ?? item?.customName)
const name = toTrimmedString(item?.space_name ?? item?.name) const name = toTrimmedString(item?.space_name ?? item?.name)
return code === text || customName === text || name === text return code === text || customName === text || name === text
}) || list[0] }) || list[0]
const item = exact as Record<string, unknown> const item = exact as Record<string, unknown>
const id = Number(item?.id) const id = Number(item?.id)
return { return {
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 }>>()
const keyword = toTrimmedString(row.space_keyword)
if (!keyword) continue await Promise.all(
const resolved = await resolveSpaceByKeyword(keyword) rows.map(async (row) => {
if (resolved.id) row.space_id = resolved.id const keyword = toTrimmedString(row.space_keyword)
if (resolved.displayName) row.space_keyword = resolved.displayName if (!keyword) return
}
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.displayName) row.space_keyword = resolved.displayName
})
)
} }
function buildEntryItemIndex(entryItems: DirectInboundImportRow[]) { function buildEntryItemIndex(entryItems: DirectInboundImportRow[]) {