优化查询

This commit is contained in:
ChenYi 2025-07-11 17:41:23 +08:00
parent 460121fdab
commit 12d28955bf
2 changed files with 100 additions and 62 deletions

View File

@ -8,11 +8,38 @@ import { useRoute } from 'vue-router';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { postTreeModelDeviceDataInfoPage } from '#/api-client';
import { postTreeModelDeviceDataInfoPage, postMetersPage } from '#/api-client';
import { generateDynamicColumns } from './dynamicColumns';
import { querySchema } from './schema';
//
const deviceOptions = ref<any[]>([]);
//
const fetchDeviceOptions = async () => {
try {
const { data } = await postMetersPage({
body: {
pageIndex: 1,
pageSize: 1000,
},
});
if (data?.items) {
deviceOptions.value = data.items;
console.log('设备信息选项:', deviceOptions.value);
}
} catch (error) {
console.error('获取设备信息失败:', error);
}
};
// ID
const getDeviceInfoById = (deviceId: string) => {
return deviceOptions.value.find(device => device.id === deviceId);
};
defineOptions({
name: 'DeviceData',
});
@ -50,24 +77,32 @@ const initDefaultColumns = () => {
//
initDefaultColumns();
//
fetchDeviceOptions();
const formOptions: VbenFormProps = {
schema: querySchema.value,
initialValues: {
FocusAddress: FocusAddress as string,
DeviceType: DeviceType ? Number(DeviceType) : undefined,
DeviceId: DeviceId as string,
},
// 使
submitOnChange: false,
//
handleValuesChange: async (values, changedFields) => {
//
if (changedFields.includes('SystemName')) {
console.log('SystemName changed, values:', values);
console.log('Changed fields:', changedFields);
//
const relevantFields = ['SystemName', 'DeviceType', 'IoTDataType', 'DeviceId', 'FocusAddress'];
const hasRelevantChange = changedFields.some(field => relevantFields.includes(field));
if (hasRelevantChange) {
console.log('表单字段变化:', { values, changedFields });
console.log('相关字段变化:', changedFields.filter(field => relevantFields.includes(field)));
// 使 setTimeout
setTimeout(async () => {
const latestValues = await gridApi.formApi.getValues();
console.log('Latest values after timeout:', latestValues);
console.log('最新表单值:', latestValues);
gridApi.reload(latestValues);
}, 0);
}
@ -110,12 +145,58 @@ const gridOptions: VxeGridProps<any> = {
ajax: {
query: async ({ page }, formValues) => {
console.log('=== API调用开始 ===');
// DeviceTypeIoTDataType
const deviceTypeValue = formValues.DeviceType || DeviceType;
const deviceTypeNumber = deviceTypeValue ? Number(deviceTypeValue) : undefined;
const ioTDataTypeValue = formValues.IoTDataType;
const ioTDataTypeNumber = ioTDataTypeValue ? Number(ioTDataTypeValue) : undefined;
// DeviceId(10)使focusId
let finalDeviceId = formValues.DeviceId || DeviceId;
if (deviceTypeNumber === 10 && formValues.DeviceId) {
const deviceInfo = getDeviceInfoById(formValues.DeviceId);
if (deviceInfo && deviceInfo.focusId) {
finalDeviceId = deviceInfo.focusId;
console.log('设备类型为集中器使用focusId:', {
originalDeviceId: formValues.DeviceId,
focusId: deviceInfo.focusId,
deviceInfo: deviceInfo,
});
}
}
console.log('请求参数:', {
page,
formValues,
DeviceType,
DeviceId,
FocusAddress,
routeParams: { DeviceType, DeviceId, FocusAddress },
typeConversions: {
deviceType: {
originalValue: deviceTypeValue,
convertedValue: deviceTypeNumber,
type: typeof deviceTypeNumber,
},
ioTDataType: {
originalValue: ioTDataTypeValue,
convertedValue: ioTDataTypeNumber,
type: typeof ioTDataTypeNumber,
},
deviceId: {
originalValue: formValues.DeviceId || DeviceId,
finalValue: finalDeviceId,
isFocusId: deviceTypeNumber === 10 && formValues.DeviceId && getDeviceInfoById(formValues.DeviceId)?.focusId,
},
},
finalParams: {
...formValues,
pageIndex: page.currentPage,
pageSize: page.pageSize,
DeviceType: deviceTypeNumber,
DeviceId: finalDeviceId,
FocusAddress: formValues.FocusAddress || FocusAddress,
SystemName: formValues.SystemName,
IoTDataType: ioTDataTypeNumber,
},
});
try {
@ -124,9 +205,13 @@ const gridOptions: VxeGridProps<any> = {
...formValues,
pageIndex: page.currentPage,
pageSize: page.pageSize,
DeviceType,
DeviceId,
FocusAddress,
// 使使
DeviceType: deviceTypeNumber,
DeviceId: finalDeviceId,
FocusAddress: formValues.FocusAddress || FocusAddress,
//
SystemName: formValues.SystemName,
IoTDataType: ioTDataTypeNumber,
},
});

View File

@ -2,11 +2,7 @@ import type { VxeGridProps } from '#/adapter/vxe-table';
import { computed } from 'vue';
import {
getSelectResultList,
postFocusesPage,
postMetersPage,
} from '#/api-client';
import { getSelectResultList, postMetersPage } from '#/api-client';
import { $t } from '#/locales';
export const querySchema = computed(() => [
@ -109,49 +105,6 @@ export const querySchema = computed(() => [
},
},
},
{
component: 'ApiSelect',
fieldName: 'FocusAddress',
label: $t('abp.focus.focusAddress'),
componentProps: {
api: postFocusesPage,
params: {
body: {
pageIndex: 1,
pageSize: 1000, // 获取足够多的数据用于下拉选择
},
},
labelField: 'name',
valueField: 'focusAddress',
optionsPropName: 'options',
immediate: true,
showSearch: true,
allowClear: true,
placeholder: $t('common.pleaseSelect') + $t('abp.focus.focusAddress'),
filterOption: false, // 禁用本地过滤,使用服务端搜索
optionFilterProp: 'label', // 根据 label 进行过滤
afterFetch: (res: any) => {
// 确保返回的是数组格式
if (Array.isArray(res)) {
return res;
}
// 如果是包装在 items 中的,提取出来
if (res && Array.isArray(res.items)) {
return res.items;
}
// 如果是包装在 data 中的,提取出来
if (res && Array.isArray(res.data)) {
return res.data;
}
// 如果是包装在 data.items 中的,提取出来
if (res && res.data && Array.isArray(res.data.items)) {
return res.data.items;
}
// 如果都不是,返回空数组
return [];
},
},
},
{
component: 'ApiSelect',
fieldName: 'DeviceId',
@ -164,8 +117,8 @@ export const querySchema = computed(() => [
pageSize: 1000, // 获取足够多的数据用于下拉选择
},
},
labelField: 'name',
valueField: 'focusAddress',
labelField: 'meterName',
valueField: 'id', // 使用id作为值这样可以获取完整的对象
optionsPropName: 'options',
immediate: true,
showSearch: true,