feat: 添加设备标识号和货道号的排序逻辑,优化商品列表查询
This commit is contained in:
1 parent
2395ce1cc9
commit
c77276160a
1 file changed
+53
-1
@@ -327,14 +327,66 @@ const queryParams = ref({
|
||||
}) // 查询参数
|
||||
const queryFormRef = ref() // 搜索的表单Ref
|
||||
|
||||
const normalizeNumericString = (value?: number | string | null) => {
|
||||
if (value === undefined || value === null) {
|
||||
return ''
|
||||
}
|
||||
return String(value)
|
||||
}
|
||||
|
||||
const getDeviceIdValue = (spu: ProductSpuApi.Spu) => {
|
||||
if (spu.deviceId !== undefined && spu.deviceId !== null) {
|
||||
return normalizeNumericString(spu.deviceId)
|
||||
}
|
||||
const skuDevice = spu.skus?.find((sku) => sku.deviceId !== undefined && sku.deviceId !== null)
|
||||
return normalizeNumericString(skuDevice?.deviceId ?? '')
|
||||
}
|
||||
|
||||
const getCabinetChannelValue = (spu: ProductSpuApi.Spu) => {
|
||||
const channels =
|
||||
spu.skus
|
||||
?.map((sku) => sku.cabinetChannel)
|
||||
.filter((channel): channel is string => !!channel) ?? []
|
||||
if (channels.length === 0) {
|
||||
return ''
|
||||
}
|
||||
const sortedChannels = channels
|
||||
.map((channel) => normalizeNumericString(channel))
|
||||
.sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }))
|
||||
return sortedChannels[0]
|
||||
}
|
||||
|
||||
const compareByDeviceAndCabinet = (a: ProductSpuApi.Spu, b: ProductSpuApi.Spu) => {
|
||||
const deviceA = getDeviceIdValue(a)
|
||||
const deviceB = getDeviceIdValue(b)
|
||||
const deviceCompare = deviceA.localeCompare(deviceB, undefined, { numeric: true, sensitivity: 'base' })
|
||||
if (deviceCompare !== 0) {
|
||||
return deviceCompare
|
||||
}
|
||||
const cabinetA = getCabinetChannelValue(a)
|
||||
const cabinetB = getCabinetChannelValue(b)
|
||||
const cabinetCompare = cabinetA.localeCompare(cabinetB, undefined, { numeric: true, sensitivity: 'base' })
|
||||
if (cabinetCompare !== 0) {
|
||||
return cabinetCompare
|
||||
}
|
||||
return ((a.id ?? 0) as number) - ((b.id ?? 0) as number)
|
||||
}
|
||||
|
||||
/** 查询列表 */
|
||||
const getList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const data = await ProductSpuApi.getSpuPage(queryParams.value)
|
||||
list.value = data.list
|
||||
const sortedList = [...data.list].sort(compareByDeviceAndCabinet)
|
||||
list.value = sortedList
|
||||
total.value = data.total
|
||||
console.log('查询列表',data)
|
||||
const cabinetChannels = sortedList
|
||||
.flatMap((item) => item.skus ?? [])
|
||||
.map((sku) => sku.cabinetChannel)
|
||||
.filter((channel): channel is string => !!channel)
|
||||
.map((channel) => normalizeNumericString(channel))
|
||||
console.log('货道号数据:', cabinetChannels)
|
||||
// 清空选中状态(因为列表数据已更新)
|
||||
selectedRows.value = []
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user