diff --git a/src/views/mall/product/containergoods/index.vue b/src/views/mall/product/containergoods/index.vue index b9e06a28..8e05df53 100644 --- a/src/views/mall/product/containergoods/index.vue +++ b/src/views/mall/product/containergoods/index.vue @@ -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 {