2025-12-18 10:45:13 +08:00
|
|
|
const DeviceField = require('./models/DeviceField');
|
|
|
|
|
const { sequelize } = require('./db');
|
|
|
|
|
|
|
|
|
|
// 默认设备字段配置
|
2026-02-04 17:10:58 +08:00
|
|
|
// 系统字段(isSystem: true)是核心字段,不可删除
|
2025-12-18 10:45:13 +08:00
|
|
|
const defaultDeviceFields = [
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'deviceId',
|
|
|
|
|
displayName: '设备ID',
|
|
|
|
|
fieldType: 'string',
|
2026-02-06 16:23:56 +08:00
|
|
|
required: false,
|
2025-12-18 10:45:13 +08:00
|
|
|
order: 1,
|
2026-02-06 16:23:56 +08:00
|
|
|
visible: false,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'name',
|
|
|
|
|
displayName: '设备名称',
|
|
|
|
|
fieldType: 'string',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 2,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'type',
|
|
|
|
|
displayName: '设备类型',
|
|
|
|
|
fieldType: 'select',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 3,
|
|
|
|
|
visible: true,
|
2026-02-04 17:10:58 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
options: [
|
|
|
|
|
{ value: 'server', label: '服务器' },
|
|
|
|
|
{ value: 'switch', label: '交换机' },
|
|
|
|
|
{ value: 'router', label: '路由器' },
|
|
|
|
|
{ value: 'storage', label: '存储设备' },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ value: 'other', label: '其他设备' },
|
|
|
|
|
],
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'model',
|
|
|
|
|
displayName: '型号',
|
|
|
|
|
fieldType: 'string',
|
2026-02-06 16:59:03 +08:00
|
|
|
required: false,
|
2025-12-18 10:45:13 +08:00
|
|
|
order: 4,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'serialNumber',
|
|
|
|
|
displayName: '序列号',
|
|
|
|
|
fieldType: 'string',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 5,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'rackId',
|
|
|
|
|
displayName: '所在机柜',
|
|
|
|
|
fieldType: 'select',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 6,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'position',
|
|
|
|
|
displayName: '位置(U)',
|
|
|
|
|
fieldType: 'number',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 7,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'height',
|
|
|
|
|
displayName: '高度(U)',
|
|
|
|
|
fieldType: 'number',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 8,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'powerConsumption',
|
|
|
|
|
displayName: '功率(W)',
|
|
|
|
|
fieldType: 'number',
|
2026-03-06 13:57:37 +08:00
|
|
|
required: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
order: 9,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'status',
|
|
|
|
|
displayName: '状态',
|
|
|
|
|
fieldType: 'select',
|
|
|
|
|
required: true,
|
|
|
|
|
order: 10,
|
|
|
|
|
visible: true,
|
2026-02-04 17:10:58 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
options: [
|
|
|
|
|
{ value: 'running', label: '运行中' },
|
|
|
|
|
{ value: 'maintenance', label: '维护中' },
|
|
|
|
|
{ value: 'offline', label: '离线' },
|
2026-03-24 14:54:39 +08:00
|
|
|
{ value: 'fault', label: '故障' },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ value: 'idle', label: '空闲' },
|
|
|
|
|
],
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'purchaseDate',
|
|
|
|
|
displayName: '购买日期',
|
|
|
|
|
fieldType: 'date',
|
2026-02-06 16:59:03 +08:00
|
|
|
required: false,
|
2025-12-18 10:45:13 +08:00
|
|
|
order: 11,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'warrantyExpiry',
|
|
|
|
|
displayName: '保修到期',
|
|
|
|
|
fieldType: 'date',
|
2026-02-06 16:59:03 +08:00
|
|
|
required: false,
|
2025-12-18 10:45:13 +08:00
|
|
|
order: 12,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: true,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'ipAddress',
|
|
|
|
|
displayName: 'IP地址',
|
|
|
|
|
fieldType: 'string',
|
|
|
|
|
required: false,
|
|
|
|
|
order: 13,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: false,
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'description',
|
|
|
|
|
displayName: '描述',
|
|
|
|
|
fieldType: 'textarea',
|
|
|
|
|
required: false,
|
|
|
|
|
order: 14,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: false,
|
2026-01-26 17:21:20 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fieldName: 'brand',
|
|
|
|
|
displayName: '品牌',
|
|
|
|
|
fieldType: 'string',
|
|
|
|
|
required: false,
|
2026-02-06 15:53:57 +08:00
|
|
|
order: 15,
|
2026-02-04 17:10:58 +08:00
|
|
|
visible: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
isSystem: false,
|
|
|
|
|
},
|
2025-12-18 10:45:13 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 初始化设备字段
|
|
|
|
|
async function initDeviceFields() {
|
|
|
|
|
try {
|
|
|
|
|
console.log('开始初始化设备字段配置...');
|
2026-02-05 12:35:50 +08:00
|
|
|
// 注意:数据库连接和表结构同步已在 server.js 中完成,这里直接初始化数据
|
|
|
|
|
|
2026-02-10 09:32:16 +08:00
|
|
|
// 批量创建默认字段(只创建不存在的字段,保留用户自定义配置)
|
2025-12-18 10:45:13 +08:00
|
|
|
for (const field of defaultDeviceFields) {
|
|
|
|
|
// 检查字段是否已存在
|
|
|
|
|
const existingField = await DeviceField.findOne({
|
2026-03-27 19:12:16 +08:00
|
|
|
where: { fieldName: field.fieldName },
|
2025-12-18 10:45:13 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-02-10 09:32:16 +08:00
|
|
|
if (!existingField) {
|
|
|
|
|
// 只创建新字段,不更新已存在的字段
|
2025-12-18 10:45:13 +08:00
|
|
|
await DeviceField.create(field);
|
|
|
|
|
console.log(`创建字段: ${field.displayName}`);
|
2026-02-10 09:32:16 +08:00
|
|
|
} else {
|
2026-03-20 17:15:14 +08:00
|
|
|
// 如果字段已存在但缺少 options,则补充 options
|
2026-04-28 10:57:17 +08:00
|
|
|
// 确保 options 是数组(数据库 JSON 类型可能返回字符串)
|
|
|
|
|
let existingOptions = existingField.options;
|
|
|
|
|
if (existingOptions && typeof existingOptions === 'string') {
|
|
|
|
|
try {
|
|
|
|
|
existingOptions = JSON.parse(existingOptions);
|
|
|
|
|
} catch {
|
|
|
|
|
existingOptions = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field.options && !existingOptions) {
|
2026-03-20 17:15:14 +08:00
|
|
|
await existingField.update({ options: field.options });
|
|
|
|
|
console.log(`更新字段 options: ${field.displayName}`);
|
2026-04-28 10:57:17 +08:00
|
|
|
} else if (field.options && Array.isArray(existingOptions)) {
|
2026-03-24 14:54:39 +08:00
|
|
|
// 如果系统字段已有 options,检查是否缺少默认选项,补充缺失的选项
|
2026-04-28 10:57:17 +08:00
|
|
|
const existingValues = existingOptions.map(o => o.value);
|
2026-03-24 14:54:39 +08:00
|
|
|
const defaultValues = field.options.map(o => o.value);
|
|
|
|
|
const missingOptions = field.options.filter(o => !existingValues.includes(o.value));
|
|
|
|
|
if (missingOptions.length > 0) {
|
2026-04-28 10:57:17 +08:00
|
|
|
const updatedOptions = [...existingOptions, ...missingOptions];
|
2026-03-24 14:54:39 +08:00
|
|
|
await existingField.update({ options: updatedOptions });
|
2026-03-27 19:12:16 +08:00
|
|
|
console.log(
|
|
|
|
|
`补充缺失的 options: ${field.displayName},新增: ${missingOptions.map(o => o.label).join(', ')}`
|
|
|
|
|
);
|
2026-03-24 14:54:39 +08:00
|
|
|
} else {
|
|
|
|
|
console.log(`跳过已存在字段: ${field.displayName}`);
|
|
|
|
|
}
|
2026-03-20 17:15:14 +08:00
|
|
|
} else {
|
|
|
|
|
console.log(`跳过已存在字段: ${field.displayName}`);
|
|
|
|
|
}
|
2025-12-18 10:45:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2025-12-18 10:45:13 +08:00
|
|
|
console.log('设备字段配置初始化完成');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('设备字段配置初始化失败:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 导出初始化函数
|
2026-03-27 19:12:16 +08:00
|
|
|
module.exports = initDeviceFields;
|