perf(导入): 优化机柜和设备数据导入性能
重构机柜和设备导入功能,采用事务+批量操作提升性能: 1. 使用事务确保数据一致性 2. 批量查询减少数据库交互 3. 批量插入提高写入效率 4. 优化ID生成逻辑 5. 统一错误处理机制
This commit is contained in:
+186
-232
@@ -405,10 +405,13 @@ router.get('/export', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 导入设备数据从CSV
|
// 导入设备数据从CSV - 优化版:使用事务+批量插入
|
||||||
router.post('/import', async (req, res) => {
|
router.post('/import', async (req, res) => {
|
||||||
|
const t = await sequelize.transaction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!req.files || !req.files.csvFile) {
|
if (!req.files || !req.files.csvFile) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({ error: '请上传CSV文件' });
|
return res.status(400).json({ error: '请上传CSV文件' });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -439,218 +442,155 @@ router.post('/import', async (req, res) => {
|
|||||||
.on('error', reject);
|
.on('error', reject);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 获取所有机房和机柜信息用于验证
|
// 【优化1】批量查询所有必要数据(单次查询)
|
||||||
const rooms = await Room.findAll();
|
const [rooms, racks, deviceFields, maxDeviceResult] = await Promise.all([
|
||||||
|
Room.findAll({ transaction: t }),
|
||||||
|
Rack.findAll({ include: [{ model: Room }], transaction: t }),
|
||||||
|
DeviceField.findAll({ transaction: t }),
|
||||||
|
// 查询最大设备ID序号
|
||||||
|
Device.findOne({
|
||||||
|
attributes: [[sequelize.fn('MAX', sequelize.cast(sequelize.fn('SUBSTR', sequelize.col('deviceId'), 4), 'INTEGER')), 'maxNum']],
|
||||||
|
where: { deviceId: { [Op.like]: 'DEV%' } },
|
||||||
|
transaction: t
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 创建查找映射
|
||||||
const roomNameToIdMap = new Map(rooms.map(room => [room.name, room.roomId]));
|
const roomNameToIdMap = new Map(rooms.map(room => [room.name, room.roomId]));
|
||||||
const racks = await Rack.findAll({
|
const rackLocationMap = new Map(racks.map(rack => [`${rack.Room?.name || ''}_${rack.name}`, rack.rackId]));
|
||||||
include: [{ model: Room }]
|
|
||||||
});
|
|
||||||
|
|
||||||
// 创建机柜查找映射:机房名称+机柜名称 -> rackId
|
|
||||||
const rackLocationMap = new Map();
|
|
||||||
racks.forEach(rack => {
|
|
||||||
const key = `${rack.Room?.name || ''}_${rack.name}`;
|
|
||||||
rackLocationMap.set(key, rack.rackId);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 获取设备字段配置
|
|
||||||
const deviceFields = await DeviceField.findAll();
|
|
||||||
|
|
||||||
// 创建字段映射:displayName -> fieldConfig
|
|
||||||
const fieldMapping = {};
|
const fieldMapping = {};
|
||||||
deviceFields.forEach(field => {
|
|
||||||
fieldMapping[field.displayName] = field;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 创建字段名到显示名称的映射(用于识别基础字段)
|
|
||||||
const fieldNameToDisplayName = {};
|
const fieldNameToDisplayName = {};
|
||||||
deviceFields.forEach(field => {
|
deviceFields.forEach(field => {
|
||||||
|
fieldMapping[field.displayName] = field;
|
||||||
fieldNameToDisplayName[field.fieldName] = field.displayName;
|
fieldNameToDisplayName[field.fieldName] = field.displayName;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 处理导入数据
|
// 设备ID生成器
|
||||||
|
let maxDeviceNum = maxDeviceResult?.get('maxNum') || 0;
|
||||||
|
const generateDeviceId = () => {
|
||||||
|
maxDeviceNum++;
|
||||||
|
return `DEV${String(maxDeviceNum).padStart(3, '0')}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 辅助函数:提取字段名
|
||||||
|
const extractFieldName = (fieldNameWithFormat) => {
|
||||||
|
const match = fieldNameWithFormat.match(/^(.+?)(\(必填\)|\(可选\)|\([a-zA-Z0-9\-\/]+\))$/);
|
||||||
|
return match ? match[1].trim() : fieldNameWithFormat;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 【优化2】收集所有需要验证的唯一键
|
||||||
|
const allDeviceIds = new Set();
|
||||||
|
const allSerialNumbers = new Set();
|
||||||
|
const validTypes = ['server', 'switch', 'router', 'storage', 'other'];
|
||||||
|
const validStatuses = ['running', 'maintenance', 'offline', 'fault'];
|
||||||
|
const baseFieldNames = ['deviceId', 'name', 'type', 'model', 'serialNumber', 'rackId', 'position', 'height', 'powerConsumption', 'ipAddress', 'status', 'purchaseDate', 'warrantyExpiry', 'description'];
|
||||||
|
|
||||||
|
// 第一遍:验证和收集数据
|
||||||
|
const validDevices = [];
|
||||||
|
|
||||||
for (let i = 0; i < results.length; i++) {
|
for (let i = 0; i < results.length; i++) {
|
||||||
const row = results[i];
|
const row = results[i];
|
||||||
const rowNum = i + 2; // CSV行号(第一行是标题)
|
const rowNum = i + 2;
|
||||||
|
|
||||||
// 辅助函数:从带格式说明的字段名中提取原始字段名
|
|
||||||
// 例如:"设备类型(server/switch/router/storage)" -> "设备类型"
|
|
||||||
// "位置(U)(必填)" -> "位置(U)"
|
|
||||||
const extractFieldName = (fieldNameWithFormat) => {
|
|
||||||
// 匹配规则:提取到 "(必填)"、"(可选)"、"(YYYY-MM-DD)" 或类似格式说明之前的内容
|
|
||||||
// 但保留字段名本身的括号,如 "位置(U)"
|
|
||||||
const match = fieldNameWithFormat.match(/^(.+?)(\(必填\)|\(可选\)|\([a-zA-Z0-9\-\/]+\))$/);
|
|
||||||
return match ? match[1].trim() : fieldNameWithFormat;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 创建一个映射,将原始字段名映射到CSV中的值
|
|
||||||
const fieldValueMap = {};
|
|
||||||
Object.entries(row).forEach(([displayName, value]) => {
|
|
||||||
const originalFieldName = extractFieldName(displayName);
|
|
||||||
fieldValueMap[originalFieldName] = value;
|
|
||||||
// 同时保留原始字段名的映射,以便兼容不同格式
|
|
||||||
fieldValueMap[displayName] = value;
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 动态构建必填字段列表(从数据库配置读取,保持与字段管理页面同步)
|
// 解析字段值
|
||||||
const trulyRequiredFields = [];
|
const fieldValueMap = {};
|
||||||
|
Object.entries(row).forEach(([displayName, value]) => {
|
||||||
|
const originalFieldName = extractFieldName(displayName);
|
||||||
|
fieldValueMap[originalFieldName] = value;
|
||||||
|
fieldValueMap[displayName] = value;
|
||||||
|
});
|
||||||
|
|
||||||
deviceFields.forEach(field => {
|
|
||||||
// 跳过设备ID(系统生成)
|
|
||||||
if (field.fieldName === 'deviceId') return;
|
|
||||||
|
|
||||||
// 机柜字段特殊处理:拆分为机房名称+机柜名称
|
|
||||||
if (field.fieldName === 'rackId') {
|
|
||||||
if (field.required) {
|
|
||||||
trulyRequiredFields.push('所在机房名称');
|
|
||||||
trulyRequiredFields.push('所在机柜名称');
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 其他字段根据数据库配置
|
|
||||||
if (field.required) {
|
|
||||||
trulyRequiredFields.push(field.displayName);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 验证必填字段
|
|
||||||
const missingFields = [];
|
|
||||||
for (const fieldName of trulyRequiredFields) {
|
|
||||||
const value = fieldValueMap[fieldName];
|
|
||||||
if (!value || (typeof value === 'string' && value.trim() === '')) {
|
|
||||||
missingFields.push(fieldName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (missingFields.length > 0) {
|
|
||||||
throw new Error(`缺少必填字段:${missingFields.join('、')}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取动态字段显示名称(支持用户在字段管理中修改后的名称)
|
|
||||||
const getFieldValue = (fieldName) => {
|
const getFieldValue = (fieldName) => {
|
||||||
const displayName = fieldNameToDisplayName[fieldName];
|
const displayName = fieldNameToDisplayName[fieldName];
|
||||||
return displayName ? fieldValueMap[displayName] : undefined;
|
return displayName ? fieldValueMap[displayName] : undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 验证设备类型值
|
// 验证必填字段
|
||||||
const validTypes = ['server', 'switch', 'router', 'storage', 'other'];
|
const trulyRequiredFields = [];
|
||||||
|
deviceFields.forEach(field => {
|
||||||
|
if (field.fieldName === 'deviceId') return;
|
||||||
|
if (field.fieldName === 'rackId') {
|
||||||
|
if (field.required) {
|
||||||
|
trulyRequiredFields.push('所在机房名称', '所在机柜名称');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (field.required) trulyRequiredFields.push(field.displayName);
|
||||||
|
});
|
||||||
|
|
||||||
|
const missingFields = trulyRequiredFields.filter(fieldName => {
|
||||||
|
const value = fieldValueMap[fieldName];
|
||||||
|
return !value || (typeof value === 'string' && value.trim() === '');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (missingFields.length > 0) {
|
||||||
|
throw new Error(`缺少必填字段:${missingFields.join('、')}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证设备类型
|
||||||
const deviceType = getFieldValue('type');
|
const deviceType = getFieldValue('type');
|
||||||
if (!validTypes.includes(deviceType)) {
|
if (!validTypes.includes(deviceType)) {
|
||||||
throw new Error(`设备类型无效,有效值为:${validTypes.join('、')},当前值:${deviceType}`);
|
throw new Error(`设备类型无效:${deviceType}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理设备ID:如果为空则自动生成
|
// 处理设备ID
|
||||||
let deviceId = getFieldValue('deviceId');
|
let deviceId = getFieldValue('deviceId');
|
||||||
if (!deviceId || deviceId.trim() === '') {
|
if (!deviceId || deviceId.trim() === '') {
|
||||||
// 自动生成设备ID
|
deviceId = generateDeviceId();
|
||||||
const allDevices = await Device.findAll({
|
} else if (allDeviceIds.has(deviceId)) {
|
||||||
where: {
|
throw new Error(`设备ID重复:${deviceId}`);
|
||||||
deviceId: {
|
|
||||||
[require('sequelize').Op.like]: 'DEV%'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let maxNumber = 0;
|
|
||||||
allDevices.forEach(device => {
|
|
||||||
const match = device.deviceId.match(/^DEV(\d+)$/);
|
|
||||||
if (match) {
|
|
||||||
const num = parseInt(match[1], 10);
|
|
||||||
if (num > maxNumber) {
|
|
||||||
maxNumber = num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
deviceId = `DEV${String(maxNumber + 1).padStart(3, '0')}`;
|
|
||||||
} else {
|
|
||||||
// 验证设备ID是否已存在
|
|
||||||
const existingDeviceById = await Device.findOne({
|
|
||||||
where: { deviceId: deviceId }
|
|
||||||
});
|
|
||||||
if (existingDeviceById) {
|
|
||||||
throw new Error(`设备ID已存在:${deviceId}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
allDeviceIds.add(deviceId);
|
||||||
|
|
||||||
// 验证序列号是否已存在
|
// 验证序列号
|
||||||
const serialNumber = getFieldValue('serialNumber');
|
const serialNumber = getFieldValue('serialNumber');
|
||||||
if (!serialNumber) {
|
if (!serialNumber) throw new Error('序列号不能为空');
|
||||||
throw new Error('序列号不能为空');
|
if (allSerialNumbers.has(serialNumber)) {
|
||||||
}
|
throw new Error(`序列号重复:${serialNumber}`);
|
||||||
const existingDevice = await Device.findOne({
|
|
||||||
where: { serialNumber: serialNumber }
|
|
||||||
});
|
|
||||||
if (existingDevice) {
|
|
||||||
throw new Error(`序列号已存在:${serialNumber}`);
|
|
||||||
}
|
}
|
||||||
|
allSerialNumbers.add(serialNumber);
|
||||||
|
|
||||||
// 获取机房名称和机柜名称
|
// 验证机房和机柜
|
||||||
const roomName = fieldValueMap['所在机房名称'];
|
const roomName = fieldValueMap['所在机房名称'];
|
||||||
const rackName = fieldValueMap['所在机柜名称'];
|
const rackName = fieldValueMap['所在机柜名称'];
|
||||||
|
if (!roomName?.trim()) throw new Error('所在机房名称不能为空');
|
||||||
|
if (!rackName?.trim()) throw new Error('所在机柜名称不能为空');
|
||||||
|
|
||||||
if (!roomName || roomName.trim() === '') {
|
|
||||||
throw new Error('所在机房名称不能为空');
|
|
||||||
}
|
|
||||||
if (!rackName || rackName.trim() === '') {
|
|
||||||
throw new Error('所在机柜名称不能为空');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证机房是否存在
|
|
||||||
const roomId = roomNameToIdMap.get(roomName.trim());
|
const roomId = roomNameToIdMap.get(roomName.trim());
|
||||||
if (!roomId) {
|
if (!roomId) throw new Error(`机房不存在:${roomName}`);
|
||||||
throw new Error(`机房不存在:${roomName},请先创建机房`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 根据机房名称+机柜名称查找机柜
|
|
||||||
const locationKey = `${roomName.trim()}_${rackName.trim()}`;
|
const locationKey = `${roomName.trim()}_${rackName.trim()}`;
|
||||||
let rackId = rackLocationMap.get(locationKey);
|
let rackId = rackLocationMap.get(locationKey);
|
||||||
|
|
||||||
// 如果机柜不存在,自动创建
|
// 机柜不存在则自动创建
|
||||||
if (!rackId) {
|
if (!rackId) {
|
||||||
// 生成新的机柜ID
|
const maxRackResult = await Rack.findOne({
|
||||||
const existingRacks = await Rack.findAll({
|
attributes: [[sequelize.fn('MAX', sequelize.cast(sequelize.fn('SUBSTR', sequelize.col('rackId'), 5), 'INTEGER')), 'maxNum']],
|
||||||
where: {
|
where: { rackId: { [Op.like]: 'RACK%' } },
|
||||||
rackId: {
|
transaction: t
|
||||||
[require('sequelize').Op.like]: 'RACK%'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
let maxRackNum = maxRackResult?.get('maxNum') || 0;
|
||||||
let maxNumber = 0;
|
maxRackNum++;
|
||||||
existingRacks.forEach(rack => {
|
|
||||||
const match = rack.rackId.match(/^RACK(\d+)$/);
|
|
||||||
if (match) {
|
|
||||||
const num = parseInt(match[1], 10);
|
|
||||||
if (num > maxNumber) {
|
|
||||||
maxNumber = num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const newRackId = `RACK${String(maxNumber + 1).padStart(3, '0')}`;
|
|
||||||
|
|
||||||
const newRack = await Rack.create({
|
const newRack = await Rack.create({
|
||||||
rackId: newRackId,
|
rackId: `RACK${String(maxRackNum).padStart(3, '0')}`,
|
||||||
name: rackName.trim(),
|
name: rackName.trim(),
|
||||||
height: 42,
|
height: 42,
|
||||||
maxPower: 10000,
|
maxPower: 10000,
|
||||||
currentPower: 0,
|
currentPower: 0,
|
||||||
status: 'active',
|
status: 'active',
|
||||||
roomId: roomId
|
roomId: roomId
|
||||||
});
|
}, { transaction: t });
|
||||||
|
|
||||||
rackId = newRack.rackId;
|
rackId = newRack.rackId;
|
||||||
rackLocationMap.set(locationKey, rackId);
|
rackLocationMap.set(locationKey, rackId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证状态值
|
// 验证状态
|
||||||
const validStatus = ['running', 'maintenance', 'offline', 'fault'];
|
|
||||||
const status = getFieldValue('status');
|
const status = getFieldValue('status');
|
||||||
if (!validStatus.includes(status)) {
|
if (!validStatuses.includes(status)) {
|
||||||
throw new Error(`状态值无效,有效值为:${validStatus.join('、')},当前值:${status}`);
|
throw new Error(`状态值无效:${status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证数字字段
|
// 验证数字字段
|
||||||
@@ -659,63 +599,38 @@ router.post('/import', async (req, res) => {
|
|||||||
const powerConsumption = getFieldValue('powerConsumption');
|
const powerConsumption = getFieldValue('powerConsumption');
|
||||||
|
|
||||||
if (position !== undefined && isNaN(Number(position))) {
|
if (position !== undefined && isNaN(Number(position))) {
|
||||||
throw new Error(`${fieldNameToDisplayName['position']}必须是数字,当前值:${position}`);
|
throw new Error(`位置必须是数字:${position}`);
|
||||||
}
|
}
|
||||||
if (height !== undefined && isNaN(Number(height))) {
|
if (height !== undefined && isNaN(Number(height))) {
|
||||||
throw new Error(`${fieldNameToDisplayName['height']}必须是数字,当前值:${height}`);
|
throw new Error(`高度必须是数字:${height}`);
|
||||||
}
|
}
|
||||||
if (powerConsumption !== undefined && isNaN(Number(powerConsumption))) {
|
if (powerConsumption !== undefined && isNaN(Number(powerConsumption))) {
|
||||||
throw new Error(`${fieldNameToDisplayName['powerConsumption']}必须是数字,当前值:${powerConsumption}`);
|
throw new Error(`功率必须是数字:${powerConsumption}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证日期格式并解析日期
|
// 验证日期
|
||||||
const purchaseDateValue = getFieldValue('purchaseDate');
|
const purchaseDateValue = getFieldValue('purchaseDate');
|
||||||
const warrantyExpiryValue = getFieldValue('warrantyExpiry');
|
const warrantyExpiryValue = getFieldValue('warrantyExpiry');
|
||||||
const purchaseDate = purchaseDateValue ? new Date(purchaseDateValue) : null;
|
const purchaseDate = purchaseDateValue ? new Date(purchaseDateValue) : null;
|
||||||
const warrantyExpiry = warrantyExpiryValue ? new Date(warrantyExpiryValue) : null;
|
const warrantyExpiry = warrantyExpiryValue ? new Date(warrantyExpiryValue) : null;
|
||||||
|
|
||||||
if (purchaseDateValue && isNaN(purchaseDate.getTime())) {
|
if (purchaseDateValue && isNaN(purchaseDate.getTime())) {
|
||||||
throw new Error(`${fieldNameToDisplayName['purchaseDate']}格式无效:${purchaseDateValue},请使用YYYY-MM-DD格式`);
|
throw new Error(`购买日期格式无效:${purchaseDateValue}`);
|
||||||
}
|
}
|
||||||
if (warrantyExpiryValue && isNaN(warrantyExpiry.getTime())) {
|
if (warrantyExpiryValue && isNaN(warrantyExpiry.getTime())) {
|
||||||
throw new Error(`${fieldNameToDisplayName['warrantyExpiry']}格式无效:${warrantyExpiryValue},请使用YYYY-MM-DD格式`);
|
throw new Error(`保修日期格式无效:${warrantyExpiryValue}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证日期逻辑
|
|
||||||
if (purchaseDate && warrantyExpiry && warrantyExpiry <= purchaseDate) {
|
if (purchaseDate && warrantyExpiry && warrantyExpiry <= purchaseDate) {
|
||||||
throw new Error(`${fieldNameToDisplayName['warrantyExpiry']}必须晚于${fieldNameToDisplayName['purchaseDate']}:${purchaseDateValue} ~ ${warrantyExpiryValue}`);
|
throw new Error(`保修日期必须晚于购买日期`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建设备数据
|
// 处理自定义字段
|
||||||
const deviceData = {
|
|
||||||
deviceId: deviceId,
|
|
||||||
name: getFieldValue('name'),
|
|
||||||
type: deviceType,
|
|
||||||
model: getFieldValue('model'),
|
|
||||||
serialNumber: serialNumber,
|
|
||||||
rackId: rackId,
|
|
||||||
position: position ? parseInt(position) : 0,
|
|
||||||
height: height ? parseInt(height) : 1,
|
|
||||||
powerConsumption: powerConsumption ? parseFloat(powerConsumption) : 0,
|
|
||||||
ipAddress: getFieldValue('ipAddress') || '',
|
|
||||||
status: status,
|
|
||||||
purchaseDate: purchaseDate,
|
|
||||||
warrantyExpiry: warrantyExpiry,
|
|
||||||
description: getFieldValue('description') || ''
|
|
||||||
};
|
|
||||||
|
|
||||||
// 处理自定义字段(排除基础字段)
|
|
||||||
const customFields = {};
|
const customFields = {};
|
||||||
const baseFieldNames = ['deviceId', 'name', 'type', 'model', 'serialNumber', 'rackId', 'position', 'height', 'powerConsumption', 'ipAddress', 'status', 'purchaseDate', 'warrantyExpiry', 'description'];
|
|
||||||
|
|
||||||
Object.entries(row).forEach(([displayName, value]) => {
|
Object.entries(row).forEach(([displayName, value]) => {
|
||||||
// 从带格式的字段名中提取原始显示名称
|
|
||||||
const originalDisplayName = extractFieldName(displayName);
|
const originalDisplayName = extractFieldName(displayName);
|
||||||
// 查找对应的字段配置
|
|
||||||
const fieldConfig = fieldMapping[originalDisplayName];
|
const fieldConfig = fieldMapping[originalDisplayName];
|
||||||
|
|
||||||
if (fieldConfig && !baseFieldNames.includes(fieldConfig.fieldName)) {
|
if (fieldConfig && !baseFieldNames.includes(fieldConfig.fieldName)) {
|
||||||
// 这是自定义字段,根据字段类型处理值
|
|
||||||
let processedValue = value;
|
let processedValue = value;
|
||||||
if (fieldConfig.fieldType === 'number') {
|
if (fieldConfig.fieldType === 'number') {
|
||||||
processedValue = value ? parseFloat(value) : null;
|
processedValue = value ? parseFloat(value) : null;
|
||||||
@@ -724,59 +639,98 @@ router.post('/import', async (req, res) => {
|
|||||||
} else if (fieldConfig.fieldType === 'date') {
|
} else if (fieldConfig.fieldType === 'date') {
|
||||||
processedValue = value ? new Date(value) : null;
|
processedValue = value ? new Date(value) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
customFields[fieldConfig.fieldName] = processedValue;
|
customFields[fieldConfig.fieldName] = processedValue;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 如果有自定义字段,添加到设备数据中
|
// 收集有效设备数据
|
||||||
if (Object.keys(customFields).length > 0) {
|
validDevices.push({
|
||||||
deviceData.customFields = customFields;
|
deviceId,
|
||||||
}
|
name: getFieldValue('name'),
|
||||||
|
type: deviceType,
|
||||||
|
model: getFieldValue('model'),
|
||||||
|
serialNumber,
|
||||||
|
rackId,
|
||||||
|
position: position ? parseInt(position) : 0,
|
||||||
|
height: height ? parseInt(height) : 1,
|
||||||
|
powerConsumption: powerConsumption ? parseFloat(powerConsumption) : 0,
|
||||||
|
ipAddress: getFieldValue('ipAddress') || '',
|
||||||
|
status,
|
||||||
|
purchaseDate,
|
||||||
|
warrantyExpiry,
|
||||||
|
description: getFieldValue('description') || '',
|
||||||
|
customFields: Object.keys(customFields).length > 0 ? customFields : null
|
||||||
|
});
|
||||||
|
|
||||||
// 创建设备
|
|
||||||
const device = await Device.create(deviceData);
|
|
||||||
|
|
||||||
// 更新机柜当前功率
|
|
||||||
const rack = await Rack.findByPk(rackId);
|
|
||||||
if (rack) {
|
|
||||||
await rack.update({
|
|
||||||
currentPower: rack.currentPower + device.powerConsumption
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
stats.success++;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
stats.failed++;
|
stats.failed++;
|
||||||
let errorMessage = error.message;
|
stats.errors.push({ row: rowNum, error: error.message, data: row });
|
||||||
|
|
||||||
if (error.name === 'SequelizeUniqueConstraintError') {
|
|
||||||
const errors = error.errors || [];
|
|
||||||
for (const err of errors) {
|
|
||||||
if (err.path === 'deviceId') {
|
|
||||||
errorMessage = `设备ID已存在`;
|
|
||||||
break;
|
|
||||||
} else if (err.path === 'serialNumber') {
|
|
||||||
errorMessage = `序列号已存在`;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (error.name === 'SequelizeValidationError') {
|
|
||||||
errorMessage = '数据验证失败,请检查字段格式';
|
|
||||||
}
|
|
||||||
|
|
||||||
stats.errors.push({ row: rowNum, error: errorMessage, data: row });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.unlinkSync(filePath);
|
// 【优化3】批量查询已存在的设备ID和序列号(单次查询)
|
||||||
|
if (validDevices.length > 0) {
|
||||||
|
const existingDevices = await Device.findAll({
|
||||||
|
where: {
|
||||||
|
[Op.or]: [
|
||||||
|
{ deviceId: validDevices.map(d => d.deviceId) },
|
||||||
|
{ serialNumber: validDevices.map(d => d.serialNumber) }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
attributes: ['deviceId', 'serialNumber'],
|
||||||
|
transaction: t
|
||||||
|
});
|
||||||
|
|
||||||
|
const existingDeviceIds = new Set(existingDevices.map(d => d.deviceId));
|
||||||
|
const existingSerialNumbers = new Set(existingDevices.map(d => d.serialNumber));
|
||||||
|
|
||||||
|
// 过滤掉已存在的设备
|
||||||
|
const newDevices = validDevices.filter(device => {
|
||||||
|
if (existingDeviceIds.has(device.deviceId)) {
|
||||||
|
stats.failed++;
|
||||||
|
stats.errors.push({ row: 0, error: `设备ID已存在:${device.deviceId}`, data: {} });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (existingSerialNumbers.has(device.serialNumber)) {
|
||||||
|
stats.failed++;
|
||||||
|
stats.errors.push({ row: 0, error: `序列号已存在:${device.serialNumber}`, data: {} });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 【优化4】批量创建设备
|
||||||
|
if (newDevices.length > 0) {
|
||||||
|
await Device.bulkCreate(newDevices, { transaction: t });
|
||||||
|
stats.success = newDevices.length;
|
||||||
|
|
||||||
|
// 【优化5】批量更新机柜功率
|
||||||
|
const rackPowerMap = new Map();
|
||||||
|
newDevices.forEach(device => {
|
||||||
|
const current = rackPowerMap.get(device.rackId) || 0;
|
||||||
|
rackPowerMap.set(device.rackId, current + device.powerConsumption);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const [rackId, powerToAdd] of rackPowerMap) {
|
||||||
|
await Rack.update(
|
||||||
|
{ currentPower: sequelize.literal(`currentPower + ${powerToAdd}`) },
|
||||||
|
{ where: { rackId }, transaction: t }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提交事务
|
||||||
|
await t.commit();
|
||||||
|
|
||||||
|
fs.unlinkSync(filePath);
|
||||||
res.json({ statistics: stats });
|
res.json({ statistics: stats });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
await t.rollback();
|
||||||
console.error('导入设备数据失败:', error);
|
console.error('导入设备数据失败:', error);
|
||||||
const errorMessage = error.message || '导入过程中发生未知错误';
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
errors: [{ row: 0, error: errorMessage }]
|
errors: [{ row: 0, error: error.message || '导入过程中发生未知错误' }]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+73
-125
@@ -3,6 +3,7 @@ const router = express.Router();
|
|||||||
const Rack = require('../models/Rack');
|
const Rack = require('../models/Rack');
|
||||||
const Device = require('../models/Device');
|
const Device = require('../models/Device');
|
||||||
const Room = require('../models/Room');
|
const Room = require('../models/Room');
|
||||||
|
const { sequelize } = require('../db');
|
||||||
const XLSX = require('xlsx');
|
const XLSX = require('xlsx');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
@@ -320,11 +321,14 @@ router.delete('/:rackId', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 导入机柜数据
|
// 导入机柜数据 - 优化版:使用事务+批量插入
|
||||||
router.post('/import', async (req, res) => {
|
router.post('/import', async (req, res) => {
|
||||||
|
const t = await sequelize.transaction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 检查是否有上传文件
|
// 检查是否有上传文件
|
||||||
if (!req.files || !req.files.file) {
|
if (!req.files || !req.files.file) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '没有上传文件',
|
message: '没有上传文件',
|
||||||
@@ -345,6 +349,7 @@ router.post('/import', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
await file.mv(tempFilePath);
|
await file.mv(tempFilePath);
|
||||||
} catch (saveError) {
|
} catch (saveError) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '文件保存失败',
|
message: '文件保存失败',
|
||||||
@@ -358,6 +363,7 @@ router.post('/import', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
workbook = XLSX.readFile(tempFilePath);
|
workbook = XLSX.readFile(tempFilePath);
|
||||||
} catch (readError) {
|
} catch (readError) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '文件解析失败',
|
message: '文件解析失败',
|
||||||
@@ -367,6 +373,7 @@ router.post('/import', async (req, res) => {
|
|||||||
|
|
||||||
// 获取第一个工作表
|
// 获取第一个工作表
|
||||||
if (!workbook.SheetNames.length) {
|
if (!workbook.SheetNames.length) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '文件格式错误',
|
message: '文件格式错误',
|
||||||
@@ -381,17 +388,11 @@ router.post('/import', async (req, res) => {
|
|||||||
|
|
||||||
// 定义列名映射(支持导入模板格式和导出文件格式)
|
// 定义列名映射(支持导入模板格式和导出文件格式)
|
||||||
const columnMapping = {
|
const columnMapping = {
|
||||||
// 机柜ID - 支持多种列名
|
|
||||||
rackId: ['机柜ID(留空自动生成)', '机柜ID'],
|
rackId: ['机柜ID(留空自动生成)', '机柜ID'],
|
||||||
// 机柜名称
|
|
||||||
name: ['机柜名称'],
|
name: ['机柜名称'],
|
||||||
// 所属机房名称
|
|
||||||
roomName: ['所属机房名称', '所属机房'],
|
roomName: ['所属机房名称', '所属机房'],
|
||||||
// 高度(U)
|
|
||||||
height: ['高度(U)', '机柜高度(U)'],
|
height: ['高度(U)', '机柜高度(U)'],
|
||||||
// 最大功率/最大功耗
|
|
||||||
maxPower: ['最大功率(W)', '最大功耗(W)'],
|
maxPower: ['最大功率(W)', '最大功耗(W)'],
|
||||||
// 状态
|
|
||||||
status: ['状态']
|
status: ['状态']
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -409,6 +410,7 @@ router.post('/import', async (req, res) => {
|
|||||||
const requiredColumns = ['name', 'roomName', 'height', 'maxPower', 'status'];
|
const requiredColumns = ['name', 'roomName', 'height', 'maxPower', 'status'];
|
||||||
const missingColumns = requiredColumns.filter(col => columnIndexMap[col] === undefined);
|
const missingColumns = requiredColumns.filter(col => columnIndexMap[col] === undefined);
|
||||||
if (missingColumns.length > 0) {
|
if (missingColumns.length > 0) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Excel列名格式不正确',
|
message: 'Excel列名格式不正确',
|
||||||
@@ -416,14 +418,13 @@ router.post('/import', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 转换为JSON格式(使用检测到的列索引)
|
// 转换为JSON格式
|
||||||
const rawData = XLSX.utils.sheet_to_json(worksheet, {
|
const rawData = XLSX.utils.sheet_to_json(worksheet, {
|
||||||
header: headerRow.map((h, i) => `col_${i}`),
|
header: headerRow.map((h, i) => `col_${i}`),
|
||||||
range: 1, // 从第2行开始读取数据
|
range: 1,
|
||||||
blankrows: false // 跳过空行
|
blankrows: false
|
||||||
});
|
});
|
||||||
|
|
||||||
// 根据列映射转换数据
|
|
||||||
const jsonData = rawData.map(row => {
|
const jsonData = rawData.map(row => {
|
||||||
const item = {};
|
const item = {};
|
||||||
Object.keys(columnIndexMap).forEach(field => {
|
Object.keys(columnIndexMap).forEach(field => {
|
||||||
@@ -434,6 +435,7 @@ router.post('/import', async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (jsonData.length === 0) {
|
if (jsonData.length === 0) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '没有找到有效数据',
|
message: '没有找到有效数据',
|
||||||
@@ -441,43 +443,47 @@ router.post('/import', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 状态值转换映射(中文 → 英文)
|
// 状态值转换映射
|
||||||
const statusMapping = {
|
const statusMapping = {
|
||||||
'启用': 'active',
|
'启用': 'active', '在用': 'active', '停用': 'inactive',
|
||||||
'在用': 'active',
|
'禁用': 'inactive', '维护中': 'maintenance',
|
||||||
'停用': 'inactive',
|
'active': 'active', 'inactive': 'inactive', 'maintenance': 'maintenance'
|
||||||
'禁用': 'inactive',
|
|
||||||
'维护中': 'maintenance',
|
|
||||||
'active': 'active',
|
|
||||||
'inactive': 'inactive',
|
|
||||||
'maintenance': 'maintenance'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const validStatuses = ['active', 'maintenance', 'inactive'];
|
const validStatuses = ['active', 'maintenance', 'inactive'];
|
||||||
const validationResults = [];
|
const validationResults = [];
|
||||||
|
|
||||||
// 获取所有有效的机房信息(名称到ID的映射)
|
// 【优化1】批量查询机房信息(单次查询)
|
||||||
const allRooms = await Room.findAll();
|
const allRooms = await Room.findAll({ transaction: t });
|
||||||
const roomNameToIdMap = new Map(allRooms.map(room => [room.name, room.roomId]));
|
const roomNameToIdMap = new Map(allRooms.map(room => [room.name, room.roomId]));
|
||||||
const validRoomNames = new Set(roomNameToIdMap.keys());
|
const validRoomNames = new Set(roomNameToIdMap.keys());
|
||||||
|
|
||||||
// 处理数据(转换状态值、处理空rackId)
|
// 【优化2】批量查询现有最大机柜ID(单次查询)
|
||||||
|
const maxRackResult = await Rack.findOne({
|
||||||
|
attributes: [[sequelize.fn('MAX', sequelize.cast(sequelize.fn('SUBSTR', sequelize.col('rackId'), 5), 'INTEGER')), 'maxNum']],
|
||||||
|
where: {
|
||||||
|
rackId: {
|
||||||
|
[require('sequelize').Op.like]: 'RACK%'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
transaction: t
|
||||||
|
});
|
||||||
|
let maxNumber = maxRackResult?.get('maxNum') || 0;
|
||||||
|
|
||||||
|
// 处理数据
|
||||||
const processedData = jsonData.map((item, index) => {
|
const processedData = jsonData.map((item, index) => {
|
||||||
const rowNumber = index + 2;
|
const rowNumber = index + 2;
|
||||||
|
|
||||||
// 转换状态值(中文转英文)
|
|
||||||
const rawStatus = String(item.status || '').trim();
|
const rawStatus = String(item.status || '').trim();
|
||||||
const normalizedStatus = statusMapping[rawStatus] || rawStatus.toLowerCase();
|
const normalizedStatus = statusMapping[rawStatus] || rawStatus.toLowerCase();
|
||||||
|
|
||||||
// 如果rackId为空或列不存在,标记为需要自动生成
|
|
||||||
const rawRackId = item.rackId ? String(item.rackId).trim() : '';
|
const rawRackId = item.rackId ? String(item.rackId).trim() : '';
|
||||||
|
|
||||||
if (!rawRackId || rawRackId === '') {
|
if (!rawRackId || rawRackId === '') {
|
||||||
|
maxNumber++;
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
rackId: null,
|
rackId: `RACK${String(maxNumber).padStart(3, '0')}`,
|
||||||
status: normalizedStatus,
|
status: normalizedStatus,
|
||||||
rowNumber,
|
rowNumber
|
||||||
_autoGenerate: true
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -485,8 +491,7 @@ router.post('/import', async (req, res) => {
|
|||||||
...item,
|
...item,
|
||||||
rackId: rawRackId,
|
rackId: rawRackId,
|
||||||
status: normalizedStatus,
|
status: normalizedStatus,
|
||||||
rowNumber,
|
rowNumber
|
||||||
_autoGenerate: false
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -494,46 +499,34 @@ router.post('/import', async (req, res) => {
|
|||||||
processedData.forEach((item) => {
|
processedData.forEach((item) => {
|
||||||
const errors = [];
|
const errors = [];
|
||||||
|
|
||||||
// rackId为null表示需要自动生成,跳过格式验证
|
if (!/^RACK\d+$/.test(item.rackId)) {
|
||||||
if (item.rackId !== null) {
|
errors.push('机柜ID格式应为RACK+数字,如RACK001');
|
||||||
// 验证机柜ID格式
|
|
||||||
if (!/^RACK\d+$/.test(item.rackId)) {
|
|
||||||
errors.push('机柜ID格式应为RACK+数字,如RACK001');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!item.name || String(item.name).trim() === '') {
|
if (!item.name || String(item.name).trim() === '') {
|
||||||
errors.push('机柜名称不能为空');
|
errors.push('机柜名称不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!item.roomName || String(item.roomName).trim() === '') {
|
if (!item.roomName || String(item.roomName).trim() === '') {
|
||||||
errors.push('所属机房名称不能为空');
|
errors.push('所属机房名称不能为空');
|
||||||
} else if (!validRoomNames.has(String(item.roomName).trim())) {
|
} else if (!validRoomNames.has(String(item.roomName).trim())) {
|
||||||
errors.push(`所属机房名称不存在: ${item.roomName}`);
|
errors.push(`所属机房名称不存在: ${item.roomName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof item.height !== 'number' || item.height <= 0) {
|
if (typeof item.height !== 'number' || item.height <= 0) {
|
||||||
errors.push('高度必须是大于0的数字');
|
errors.push('高度必须是大于0的数字');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof item.maxPower !== 'number' || item.maxPower < 0) {
|
if (typeof item.maxPower !== 'number' || item.maxPower < 0) {
|
||||||
errors.push('最大功率必须是大于等于0的数字');
|
errors.push('最大功率必须是大于等于0的数字');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!item.status || !validStatuses.includes(item.status)) {
|
if (!item.status || !validStatuses.includes(item.status)) {
|
||||||
errors.push(`状态必须是以下值之一: ${validStatuses.join(', ')} (或对应中文: 启用/在用、停用/禁用、维护中)`);
|
errors.push(`状态必须是以下值之一: ${validStatuses.join(', ')}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
validationResults.push({
|
validationResults.push({ row: item.rowNumber, data: item, errors });
|
||||||
row: item.rowNumber,
|
|
||||||
data: item,
|
|
||||||
errors: errors
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (validationResults.length > 0) {
|
if (validationResults.length > 0) {
|
||||||
|
await t.rollback();
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '数据验证失败',
|
message: '数据验证失败',
|
||||||
@@ -542,87 +535,41 @@ router.post('/import', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 批量创建机柜
|
// 【优化3】批量查询已存在的机柜ID(单次查询)
|
||||||
|
const existingRacks = await Rack.findAll({
|
||||||
|
where: {
|
||||||
|
rackId: processedData.map(item => item.rackId)
|
||||||
|
},
|
||||||
|
transaction: t
|
||||||
|
});
|
||||||
|
|
||||||
|
const existingIds = new Set(existingRacks.map(rack => rack.rackId));
|
||||||
|
const newData = processedData.filter(item => !existingIds.has(item.rackId));
|
||||||
|
const duplicateCount = processedData.length - newData.length;
|
||||||
|
|
||||||
|
// 【优化4】批量插入数据
|
||||||
let createdCount = 0;
|
let createdCount = 0;
|
||||||
let duplicateCount = 0;
|
if (newData.length > 0) {
|
||||||
let autoGeneratedCount = 0;
|
const dataWithRoomId = newData.map(item => ({
|
||||||
|
rackId: item.rackId,
|
||||||
|
name: item.name,
|
||||||
|
height: item.height,
|
||||||
|
maxPower: item.maxPower,
|
||||||
|
status: item.status,
|
||||||
|
roomId: roomNameToIdMap.get(item.roomName.trim()),
|
||||||
|
currentPower: 0
|
||||||
|
}));
|
||||||
|
|
||||||
try {
|
const result = await Rack.bulkCreate(dataWithRoomId, {
|
||||||
// 获取当前最大的机柜ID序号(用于自动生成)
|
transaction: t,
|
||||||
const allRacks = await Rack.findAll({
|
ignoreDuplicates: true
|
||||||
where: {
|
|
||||||
rackId: {
|
|
||||||
[require('sequelize').Op.like]: 'RACK%'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let maxNumber = 0;
|
|
||||||
allRacks.forEach(rack => {
|
|
||||||
const match = rack.rackId.match(/^RACK(\d+)$/);
|
|
||||||
if (match) {
|
|
||||||
const num = parseInt(match[1], 10);
|
|
||||||
if (num > maxNumber) {
|
|
||||||
maxNumber = num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 为需要自动生成的记录分配rackId
|
|
||||||
const dataWithGeneratedIds = processedData.map(item => {
|
|
||||||
if (item._autoGenerate) {
|
|
||||||
maxNumber++;
|
|
||||||
autoGeneratedCount++;
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
rackId: `RACK${String(maxNumber).padStart(3, '0')}`
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 检查重复的机柜ID
|
|
||||||
const existingRacks = await Rack.findAll({
|
|
||||||
where: {
|
|
||||||
rackId: dataWithGeneratedIds.map(item => item.rackId)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const existingIds = new Set(existingRacks.map(rack => rack.rackId));
|
|
||||||
const newData = dataWithGeneratedIds.filter(item => !existingIds.has(item.rackId));
|
|
||||||
const duplicateData = dataWithGeneratedIds.filter(item => existingIds.has(item.rackId));
|
|
||||||
|
|
||||||
duplicateCount = duplicateData.length;
|
|
||||||
|
|
||||||
// 将roomName转换为roomId
|
|
||||||
const dataWithRoomId = newData.map(item => {
|
|
||||||
const trimmedRoomName = item.roomName.trim();
|
|
||||||
return {
|
|
||||||
rackId: item.rackId,
|
|
||||||
name: item.name,
|
|
||||||
height: item.height,
|
|
||||||
maxPower: item.maxPower,
|
|
||||||
status: item.status,
|
|
||||||
roomId: roomNameToIdMap.get(trimmedRoomName),
|
|
||||||
currentPower: 0
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// 只创建新的机柜
|
|
||||||
if (dataWithRoomId.length > 0) {
|
|
||||||
const result = await Rack.bulkCreate(dataWithRoomId, {
|
|
||||||
ignoreDuplicates: true // 忽略重复的机柜ID
|
|
||||||
});
|
|
||||||
createdCount = result.length;
|
|
||||||
}
|
|
||||||
} catch (dbError) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: '数据库操作失败',
|
|
||||||
error: `保存机柜数据失败: ${dbError.message}`
|
|
||||||
});
|
});
|
||||||
|
createdCount = result.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 提交事务
|
||||||
|
await t.commit();
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: '机柜导入完成',
|
message: '机柜导入完成',
|
||||||
@@ -637,6 +584,7 @@ router.post('/import', async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
await t.rollback();
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '服务器内部错误',
|
message: '服务器内部错误',
|
||||||
|
|||||||
Reference in New Issue
Block a user