135 lines
3.6 KiB
TypeScript
135 lines
3.6 KiB
TypeScript
import type { DynamicDeviceData, DynamicPageResponse } from './types';
|
||
|
||
// 示例1:处理标准IoTDB数据
|
||
export const handleStandardIoTDBData = (response: any): DynamicPageResponse<DynamicDeviceData> => {
|
||
// 确保响应格式正确
|
||
if (!response || !response.items) {
|
||
return { items: [], totalCount: 0 };
|
||
}
|
||
|
||
// 转换数据,确保所有字段都被包含
|
||
const items = response.items.map((item: any) => ({
|
||
SystemName: item.SystemName || '',
|
||
ProjectId: item.ProjectId || '',
|
||
ProjectName: item.ProjectName || '',
|
||
IoTDataType: item.IoTDataType || '',
|
||
DeviceType: item.DeviceType || '',
|
||
DeviceId: item.DeviceId || '',
|
||
Timestamps: item.Timestamps || '',
|
||
APPData: item.APPData || '',
|
||
// 包含所有其他字段
|
||
...item,
|
||
}));
|
||
|
||
return {
|
||
items,
|
||
totalCount: response.totalCount || items.length,
|
||
};
|
||
};
|
||
|
||
// 示例2:处理自定义字段数据
|
||
export const handleCustomFieldData = (response: any): DynamicPageResponse<DynamicDeviceData> => {
|
||
if (!response || !response.items) {
|
||
return { items: [], totalCount: 0 };
|
||
}
|
||
|
||
// 直接使用原始数据,允许任意字段
|
||
return {
|
||
items: response.items,
|
||
totalCount: response.totalCount || response.items.length,
|
||
};
|
||
};
|
||
|
||
// 示例3:数据验证和清理
|
||
export const validateAndCleanData = (data: any[]): DynamicDeviceData[] => {
|
||
return data.map(item => {
|
||
const cleaned: DynamicDeviceData = {};
|
||
|
||
// 遍历所有字段,进行类型转换和清理
|
||
Object.keys(item).forEach(key => {
|
||
const value = item[key];
|
||
|
||
// 根据字段名进行特殊处理
|
||
switch (key) {
|
||
case 'Timestamps':
|
||
cleaned[key] = value ? String(value) : '';
|
||
break;
|
||
case 'SystemName':
|
||
case 'ProjectName':
|
||
case 'DeviceType':
|
||
case 'IoTDataType':
|
||
cleaned[key] = value ? String(value) : '';
|
||
break;
|
||
case 'ProjectId':
|
||
case 'DeviceId':
|
||
cleaned[key] = value ? String(value) : '';
|
||
break;
|
||
case 'APPData':
|
||
// 尝试解析JSON数据
|
||
try {
|
||
if (typeof value === 'string') {
|
||
cleaned[key] = JSON.parse(value);
|
||
} else {
|
||
cleaned[key] = value;
|
||
}
|
||
} catch {
|
||
cleaned[key] = value;
|
||
}
|
||
break;
|
||
default:
|
||
// 对于未知字段,保持原值
|
||
cleaned[key] = value;
|
||
}
|
||
});
|
||
|
||
return cleaned;
|
||
});
|
||
};
|
||
|
||
// 示例4:动态字段检测
|
||
export const detectDynamicFields = (data: DynamicDeviceData[]): string[] => {
|
||
if (!data || data.length === 0) return [];
|
||
|
||
const allFields = new Set<string>();
|
||
|
||
data.forEach(item => {
|
||
Object.keys(item).forEach(key => {
|
||
allFields.add(key);
|
||
});
|
||
});
|
||
|
||
return Array.from(allFields);
|
||
};
|
||
|
||
// 示例5:字段重要性排序
|
||
export const sortFieldsByImportance = (fields: string[]): string[] => {
|
||
const importanceOrder = [
|
||
'Timestamps',
|
||
'SystemName',
|
||
'ProjectName',
|
||
'DeviceType',
|
||
'DeviceId',
|
||
'IoTDataType',
|
||
'APPData',
|
||
];
|
||
|
||
return fields.sort((a, b) => {
|
||
const aIndex = importanceOrder.indexOf(a);
|
||
const bIndex = importanceOrder.indexOf(b);
|
||
|
||
// 如果都在预定义列表中,按列表顺序排序
|
||
if (aIndex !== -1 && bIndex !== -1) {
|
||
return aIndex - bIndex;
|
||
}
|
||
|
||
// 如果只有a在预定义列表中,a排在前面
|
||
if (aIndex !== -1) return -1;
|
||
|
||
// 如果只有b在预定义列表中,b排在前面
|
||
if (bIndex !== -1) return 1;
|
||
|
||
// 都不在预定义列表中,按字母顺序排序
|
||
return a.localeCompare(b);
|
||
});
|
||
};
|