默认查询的设备已经可以查询
This commit is contained in:
parent
b706f70e06
commit
17be5382bf
@ -2,16 +2,16 @@
|
||||
import type { VbenFormProps } from '#/adapter/form';
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
|
||||
import { onMounted, ref, watch } from 'vue';
|
||||
import { useRoute } from 'vue-router';
|
||||
import { ref, nextTick, watch } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { postOneNETLogInfoPage } from '#/api-client';
|
||||
import { postMetersPage, postOneNETLogInfoPage } from '#/api-client';
|
||||
|
||||
import { querySchema, tableSchema } from './schema';
|
||||
import DeviceSelect from '../deviceData/DeviceSelect.vue';
|
||||
import { querySchema, tableSchema } from './schema';
|
||||
|
||||
defineOptions({
|
||||
name: 'OneNETLog',
|
||||
@ -26,15 +26,127 @@ const deviceSelectRef = ref();
|
||||
// 添加选中的设备信息
|
||||
const selectedDeviceInfo = ref<any>(null);
|
||||
|
||||
// 存储设备信息选项的完整数据
|
||||
const deviceOptions = ref<any[]>();
|
||||
|
||||
// 获取设备信息的完整数据
|
||||
const fetchDeviceOptions = async () => {
|
||||
try {
|
||||
const { data } = await postMetersPage({
|
||||
body: {
|
||||
pageIndex: 1,
|
||||
pageSize: 1000,
|
||||
},
|
||||
});
|
||||
|
||||
if (data?.items) {
|
||||
deviceOptions.value = data.items;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备信息失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 根据设备ID获取设备信息对象
|
||||
const getDeviceInfoById = (deviceId: string) => {
|
||||
console.log('getDeviceInfoById 调用:', { deviceId, deviceOptionsLength: deviceOptions.value?.length });
|
||||
|
||||
if (!deviceId || !deviceOptions.value || deviceOptions.value.length === 0) {
|
||||
console.log('getDeviceInfoById 返回 null - 条件不满足');
|
||||
return null;
|
||||
}
|
||||
|
||||
// 查看前几个设备的数据结构
|
||||
console.log('前3构:', deviceOptions.value.slice(0, 3).map(device => ({
|
||||
id: device.id,
|
||||
meterName: device.meterName,
|
||||
meterId: device.meterId,
|
||||
focusId: device.focusId,
|
||||
systemName: device.systemName,
|
||||
keys: Object.keys(device)
|
||||
})));
|
||||
|
||||
const device = deviceOptions.value.find((device) => device.id === deviceId);
|
||||
console.log('查找结果:', device);
|
||||
|
||||
return device;
|
||||
};
|
||||
|
||||
// 格式化日期函数
|
||||
const formatDate = (date: any) => {
|
||||
if (!date) return '';
|
||||
if (typeof date === 'string') return date;
|
||||
if (date.toISOString) return date.format('YYYY-MM-DD HH:mm:ss');
|
||||
return '';
|
||||
};
|
||||
};
|
||||
// 从 DeviceSelect 组件获取设备信息
|
||||
const getDeviceInfoFromRef = async () => {
|
||||
if (
|
||||
deviceSelectRef.value &&
|
||||
typeof deviceSelectRef.value.getSelectedDevice === 'function'
|
||||
) {
|
||||
const deviceInfo = deviceSelectRef.value.getSelectedDevice();
|
||||
console.log('从 ref 获取设备信息:', deviceInfo);
|
||||
return deviceInfo;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const formOptions: VbenFormProps = {
|
||||
schema: querySchema.value,
|
||||
// 禁用表单值变化时自动提交,使用自定义处理函数
|
||||
submitOnChange: false,
|
||||
// 添加表单值变化的处理函数
|
||||
handleValuesChange: async (values, changedFields) => {
|
||||
console.log('handleValuesChange 被触发:', { values, changedFields });
|
||||
console.log('deviceSelectRef.value:', deviceSelectRef.value);
|
||||
|
||||
// 当 DeviceId 发生变化时,更新 selectedDeviceInfo
|
||||
if (changedFields.includes('DeviceId')) {
|
||||
const deviceId = values.DeviceId;
|
||||
console.log('DeviceId 变化:', deviceId);
|
||||
|
||||
if (deviceId) {
|
||||
// 尝试从 DeviceSelect 组件中获取
|
||||
if (
|
||||
deviceSelectRef.value &&
|
||||
typeof deviceSelectRef.value.getSelectedDevice === 'function'
|
||||
) {
|
||||
const device = deviceSelectRef.value.getSelectedDevice();
|
||||
if (device) {
|
||||
selectedDeviceInfo.value = device;
|
||||
console.log('handleValuesChange 更新设备信息:', device);
|
||||
}
|
||||
} else {
|
||||
console.log('deviceSelectRef 不可用或 getSelectedDevice 方法不存在');
|
||||
}
|
||||
} else {
|
||||
selectedDeviceInfo.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 当任何相关字段发生变化时,刷新表格数据
|
||||
const relevantFields = new Set([
|
||||
'DeviceId',
|
||||
'DeviceType',
|
||||
'EndCreationTime',
|
||||
'IoTDataType',
|
||||
'StartCreationTime',
|
||||
'SystemName',
|
||||
]);
|
||||
const hasRelevantChange = changedFields.some((field) =>
|
||||
relevantFields.has(field),
|
||||
);
|
||||
|
||||
if (hasRelevantChange) {
|
||||
// 使用 setTimeout 确保表单值已经完全更新
|
||||
setTimeout(async () => {
|
||||
if (gridApi) {
|
||||
gridApi.reload();
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
@ -49,7 +161,14 @@ const gridOptions: VxeGridProps<any> = {
|
||||
currentPage: 1,
|
||||
pageSize: 10,
|
||||
pageSizes: [10, 20, 100],
|
||||
layouts: ['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total'],
|
||||
layouts: [
|
||||
'PrevPage',
|
||||
'JumpNumber',
|
||||
'NextPage',
|
||||
'FullJump',
|
||||
'Sizes',
|
||||
'Total',
|
||||
],
|
||||
},
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
@ -61,27 +180,43 @@ const gridOptions: VxeGridProps<any> = {
|
||||
ajax: {
|
||||
query: async ({ page }, formValues) => {
|
||||
// 获取选中的设备信息
|
||||
let deviceId = '';
|
||||
let deviceId = formValues?.DeviceId || '';
|
||||
let systemName = '';
|
||||
let deviceType = formValues?.DeviceType || '';
|
||||
if (deviceSelectRef.value) {
|
||||
const deviceInfo = deviceSelectRef.value.getSelectedDevice();
|
||||
const deviceType = formValues?.DeviceType || '';
|
||||
|
||||
console.log('查询参数:', { deviceId, deviceType, formValues });
|
||||
|
||||
// 获取设备详细信息,根据设备类型获取正确的 id
|
||||
console.log('deviceOptions 状态:', {
|
||||
hasDeviceOptions: !!deviceOptions.value,
|
||||
deviceOptionsLength: deviceOptions.value?.length || 0,
|
||||
});
|
||||
|
||||
if (deviceId && deviceOptions.value && deviceOptions.value.length > 0) {
|
||||
const deviceInfo = getDeviceInfoById(deviceId);
|
||||
console.log('getDeviceInfoById 结果:', deviceInfo);
|
||||
if (deviceInfo) {
|
||||
console.log('找到设备信息:', deviceInfo);
|
||||
systemName = deviceInfo.systemName || '';
|
||||
// 设备类型为10时取focusId,其他情况取meterId
|
||||
// 根据设备类型获取正确的 id
|
||||
if (Number(deviceType) === 10) {
|
||||
deviceId = deviceInfo.focusId || '';
|
||||
// 集中器类型使用 focusId
|
||||
deviceId = deviceInfo.focusId || deviceId;
|
||||
} else {
|
||||
deviceId = deviceInfo.meterId || '';
|
||||
// 其他设备类型使用 meterId
|
||||
deviceId = deviceInfo.meterId || deviceId;
|
||||
}
|
||||
} else {
|
||||
console.log('未找到设备信息,使用原始 deviceId:', deviceId);
|
||||
}
|
||||
}
|
||||
// 没有deviceInfo时回退表单DeviceId
|
||||
if (!deviceId) {
|
||||
deviceId = formValues?.DeviceId || '';
|
||||
} else {
|
||||
console.log(
|
||||
'deviceOptions 未加载或为空,使用原始 deviceId:',
|
||||
deviceId,
|
||||
);
|
||||
}
|
||||
// 构建查询参数
|
||||
const queryParams: any = {
|
||||
const queryParams = {
|
||||
pageIndex: page.currentPage,
|
||||
pageSize: page.pageSize,
|
||||
SearchKeyword: formValues?.SearchKeyword || '',
|
||||
@ -137,17 +272,19 @@ watch(
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// 页面初始化时获取设备信息
|
||||
onMounted(async () => {
|
||||
await fetchDeviceOptions();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page auto-content-height>
|
||||
<Grid>
|
||||
<template #DeviceId="{ model, field }"> <DeviceSelect
|
||||
ref="deviceSelectRef"
|
||||
v-model:value="model[field]"
|
||||
:placeholder="$t('common.pleaseSelect') + $t('abp.log.deviceInfo')"
|
||||
allow-clear
|
||||
/>
|
||||
<template #DeviceId="{ model, field }">
|
||||
<DeviceSelect ref="deviceSelectRef" v-model:value="model[field]"
|
||||
:placeholder="$t('common.pleaseSelect') + $t('abp.log.deviceInfo')" allow-clear />
|
||||
</template>
|
||||
</Grid>
|
||||
</Page>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user