feat: 批量入库库位解析改用 index.searchSpaces 批量查询,失败兜底默认收货区
This commit is contained in:
parent
2bcee8db80
commit
5ef2f10139
@ -382,6 +382,21 @@ export function searchSpace(data:any){
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 按当前仓库上下文批量搜索库位(keywords 与返回 data 分组一一对应) */
|
||||
export function searchSpaces(data: any) {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'index.searchSpaces',
|
||||
v: '1.0',
|
||||
timestamp: new Date().getTime()
|
||||
},
|
||||
data: data,
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
export function recognizeLicensePlate(data:any){
|
||||
return request({
|
||||
url: '/system',
|
||||
|
||||
@ -88,6 +88,29 @@ export function getSpaceDisplayName(space: Record<string, unknown> | null | unde
|
||||
return toTrimmedString(space?.space_name ?? space?.name ?? space?.space_code)
|
||||
}
|
||||
|
||||
/** 从库位列表中精确匹配关键词(space_code / custom_name / space_name),否则取第一条 */
|
||||
export function pickSpaceFromList(
|
||||
list: Record<string, unknown>[],
|
||||
keyword: string
|
||||
): { id?: number; displayName?: string } {
|
||||
if (!list.length) return {}
|
||||
|
||||
const text = toTrimmedString(keyword)
|
||||
const exact =
|
||||
list.find((item) => {
|
||||
const code = toTrimmedString(item?.space_code)
|
||||
const customName = toTrimmedString(item?.custom_name ?? item?.customName)
|
||||
const name = toTrimmedString(item?.space_name ?? item?.name)
|
||||
return code === text || customName === text || name === text
|
||||
}) || list[0]
|
||||
|
||||
const id = Number(exact?.id)
|
||||
return {
|
||||
id: Number.isFinite(id) && id > 0 ? id : undefined,
|
||||
displayName: getSpaceDisplayName(exact),
|
||||
}
|
||||
}
|
||||
|
||||
/** 按库位条码、名称或 ID 解析库位 */
|
||||
export async function resolveSpaceByKeyword(
|
||||
keyword: string
|
||||
@ -117,48 +140,72 @@ export async function resolveSpaceByKeyword(
|
||||
>
|
||||
const data = (res?.data || res?.list || res) as Record<string, unknown>
|
||||
const list = Array.isArray(data?.list) ? data.list : Array.isArray(data) ? data : []
|
||||
if (!list.length) return {}
|
||||
|
||||
const exact =
|
||||
(list as Record<string, unknown>[]).find((item) => {
|
||||
const code = toTrimmedString(item?.space_code)
|
||||
const customName = toTrimmedString(item?.custom_name ?? item?.customName)
|
||||
const name = toTrimmedString(item?.space_name ?? item?.name)
|
||||
return code === text || customName === text || name === text
|
||||
}) || list[0]
|
||||
|
||||
const item = exact as Record<string, unknown>
|
||||
const id = Number(item?.id)
|
||||
return {
|
||||
id: Number.isFinite(id) && id > 0 ? id : undefined,
|
||||
displayName: getSpaceDisplayName(item),
|
||||
}
|
||||
return pickSpaceFromList(list as Record<string, unknown>[], text)
|
||||
} catch (error) {
|
||||
console.error('查询库位失败,兜底为默认收货区:', text, error)
|
||||
return { id: 1, displayName: '默认收货区' }
|
||||
}
|
||||
}
|
||||
|
||||
/** 批量解析多行库位:并发查询,相同关键词只查询一次 */
|
||||
/**
|
||||
* 批量解析多行库位:
|
||||
* - 非数字关键词走 index.searchSpaces 一次批量查询,结果按传入顺序返回;
|
||||
* - 纯数字 ID 关键词走精确查询(并发、去重);
|
||||
* - 查询失败兜底 id: 1 默认收货区。
|
||||
*/
|
||||
export async function resolveParsedRowSpaces(rows: ParsedDirectInboundImportRow[]): Promise<void> {
|
||||
const pendingByKeyword = new Map<string, Promise<{ id?: number; displayName?: string }>>()
|
||||
const rowsByKeyword = new Map<string, ParsedDirectInboundImportRow[]>()
|
||||
for (const row of rows) {
|
||||
const keyword = toTrimmedString(row.space_keyword)
|
||||
if (!keyword) continue
|
||||
const bucket = rowsByKeyword.get(keyword) || []
|
||||
bucket.push(row)
|
||||
rowsByKeyword.set(keyword, bucket)
|
||||
}
|
||||
if (!rowsByKeyword.size) return
|
||||
|
||||
await Promise.all(
|
||||
rows.map(async (row) => {
|
||||
const keyword = toTrimmedString(row.space_keyword)
|
||||
if (!keyword) return
|
||||
const keywords = [...rowsByKeyword.keys()]
|
||||
const resolvedByKeyword = new Map<string, { id?: number; displayName?: string }>()
|
||||
|
||||
let pending = pendingByKeyword.get(keyword)
|
||||
if (!pending) {
|
||||
pending = resolveSpaceByKeyword(keyword)
|
||||
pendingByKeyword.set(keyword, pending)
|
||||
const textKeywords = keywords.filter((keyword) => {
|
||||
const numericId = Number(keyword)
|
||||
return !(Number.isFinite(numericId) && numericId > 0)
|
||||
})
|
||||
|
||||
if (textKeywords.length) {
|
||||
try {
|
||||
const res = (await indexApi.searchSpaces({ keywords: textKeywords })) as unknown as Record<
|
||||
string,
|
||||
unknown
|
||||
>
|
||||
const data = (res?.data || res?.list || res) as Record<string, unknown>
|
||||
const groups = Array.isArray(data?.list) ? data.list : Array.isArray(data) ? data : []
|
||||
textKeywords.forEach((keyword, index) => {
|
||||
const list = Array.isArray(groups[index]) ? (groups[index] as Record<string, unknown>[]) : []
|
||||
resolvedByKeyword.set(keyword, pickSpaceFromList(list, keyword))
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('批量查询库位失败,兜底为默认收货区:', error)
|
||||
for (const keyword of textKeywords) {
|
||||
resolvedByKeyword.set(keyword, { id: 1, displayName: '默认收货区' })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const resolved = await pending
|
||||
if (resolved.id) row.space_id = resolved.id
|
||||
if (resolved.displayName) row.space_keyword = resolved.displayName
|
||||
const numericKeywords = keywords.filter((keyword) => !resolvedByKeyword.has(keyword))
|
||||
await Promise.all(
|
||||
numericKeywords.map(async (keyword) => {
|
||||
resolvedByKeyword.set(keyword, await resolveSpaceByKeyword(keyword))
|
||||
})
|
||||
)
|
||||
|
||||
for (const [keyword, resolved] of resolvedByKeyword) {
|
||||
const targetRows = rowsByKeyword.get(keyword) || []
|
||||
for (const row of targetRows) {
|
||||
if (resolved.id) row.space_id = resolved.id
|
||||
if (resolved.displayName) row.space_keyword = resolved.displayName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function buildEntryItemIndex(entryItems: DirectInboundImportRow[]) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user