161 lines
5.4 KiB
JavaScript
161 lines
5.4 KiB
JavaScript
const mysql = require('mysql2/promise');
|
||
|
||
async function simpleReorderFarms() {
|
||
let connection;
|
||
|
||
try {
|
||
// 创建数据库连接
|
||
connection = await mysql.createConnection({
|
||
host: 'localhost',
|
||
user: 'root',
|
||
password: 'root123',
|
||
database: 'nxxmdata'
|
||
});
|
||
|
||
console.log('连接数据库成功');
|
||
|
||
// 1. 检查当前farms数据
|
||
const [farms] = await connection.execute('SELECT * FROM farms ORDER BY id ASC');
|
||
console.log(`找到 ${farms.length} 个养殖场:`);
|
||
farms.forEach(farm => {
|
||
console.log(`ID: ${farm.id}, Name: ${farm.name}`);
|
||
});
|
||
|
||
if (farms.length === 0) {
|
||
console.log('farms表为空,先恢复数据...');
|
||
|
||
// 插入基础数据
|
||
await connection.execute(`
|
||
INSERT INTO farms (name, type, location, address, contact, phone, status, created_at, updated_at) VALUES
|
||
('蓝天养鸡场', '养鸡场', '{}', '成都市锦江区蓝天路4号', '赵六', '13800138004', 'active', NOW(), NOW()),
|
||
('金山养鸭场', '养鸭场', '{}', '青岛市市南区金山路5号', '钱七', '13800138005', 'active', NOW(), NOW()),
|
||
('银河渔场', '渔场', '{}', '深圳市福田区银河路6号', '孙八', '13800138006', 'active', NOW(), NOW()),
|
||
('星空牧场', '综合农场', '{}', '重庆市渝中区星空路7号', '周九', '13800138007', 'active', NOW(), NOW()),
|
||
('彩虹农庄', '有机农场', '{}', '西安市雁塔区彩虹路8号', '吴十', '13800138008', 'active', NOW(), NOW()),
|
||
('东方养殖场', '养猪场', '{}', '福州市鼓楼区东方路9号', '郑一', '13800138009', 'active', NOW(), NOW()),
|
||
('西部牧场', '养牛场', '{}', '乌鲁木齐市天山区西部路10号', '王二', '13800138010', 'active', NOW(), NOW()),
|
||
('南方羊场', '养羊场', '{}', '昆明市五华区南方路11号', '李三', '13800138011', 'active', NOW(), NOW())
|
||
`);
|
||
|
||
console.log('数据恢复完成');
|
||
|
||
// 重新获取数据
|
||
const [newFarms] = await connection.execute('SELECT * FROM farms ORDER BY id ASC');
|
||
console.log('恢复后的数据:');
|
||
newFarms.forEach(farm => {
|
||
console.log(`ID: ${farm.id}, Name: ${farm.name}`);
|
||
});
|
||
|
||
return;
|
||
}
|
||
|
||
// 2. 开始事务
|
||
await connection.beginTransaction();
|
||
|
||
// 3. 禁用外键检查
|
||
await connection.execute('SET FOREIGN_KEY_CHECKS = 0');
|
||
|
||
// 4. 创建ID映射
|
||
const idMapping = {};
|
||
farms.forEach((farm, index) => {
|
||
idMapping[farm.id] = index + 1;
|
||
});
|
||
|
||
console.log('\nID映射:');
|
||
Object.entries(idMapping).forEach(([oldId, newId]) => {
|
||
console.log(`${oldId} -> ${newId}`);
|
||
});
|
||
|
||
// 5. 更新farms表ID
|
||
console.log('\n更新farms表ID...');
|
||
for (let i = 0; i < farms.length; i++) {
|
||
const farm = farms[i];
|
||
const newId = i + 1;
|
||
|
||
if (farm.id !== newId) {
|
||
// 先更新为临时ID避免冲突
|
||
const tempId = 1000 + newId;
|
||
await connection.execute(
|
||
'UPDATE farms SET id = ? WHERE id = ?',
|
||
[tempId, farm.id]
|
||
);
|
||
console.log(`临时更新: ${farm.id} -> ${tempId}`);
|
||
}
|
||
}
|
||
|
||
// 6. 更新为最终ID
|
||
for (let i = 0; i < farms.length; i++) {
|
||
const newId = i + 1;
|
||
const tempId = 1000 + newId;
|
||
|
||
await connection.execute(
|
||
'UPDATE farms SET id = ? WHERE id = ?',
|
||
[newId, tempId]
|
||
);
|
||
console.log(`最终更新: ${tempId} -> ${newId}`);
|
||
}
|
||
|
||
// 7. 更新相关表的外键
|
||
console.log('\n更新外键...');
|
||
|
||
// 更新animals表
|
||
for (const [oldId, newId] of Object.entries(idMapping)) {
|
||
if (oldId !== newId.toString()) {
|
||
const [result] = await connection.execute(
|
||
'UPDATE animals SET farm_id = ? WHERE farm_id = ?',
|
||
[newId, oldId]
|
||
);
|
||
console.log(`Animals: farm_id ${oldId} -> ${newId}, 影响 ${result.affectedRows} 行`);
|
||
}
|
||
}
|
||
|
||
// 更新devices表
|
||
for (const [oldId, newId] of Object.entries(idMapping)) {
|
||
if (oldId !== newId.toString()) {
|
||
const [result] = await connection.execute(
|
||
'UPDATE devices SET farm_id = ? WHERE farm_id = ?',
|
||
[newId, oldId]
|
||
);
|
||
console.log(`Devices: farm_id ${oldId} -> ${newId}, 影响 ${result.affectedRows} 行`);
|
||
}
|
||
}
|
||
|
||
// 更新alerts表
|
||
for (const [oldId, newId] of Object.entries(idMapping)) {
|
||
if (oldId !== newId.toString()) {
|
||
const [result] = await connection.execute(
|
||
'UPDATE alerts SET farm_id = ? WHERE farm_id = ?',
|
||
[newId, oldId]
|
||
);
|
||
console.log(`Alerts: farm_id ${oldId} -> ${newId}, 影响 ${result.affectedRows} 行`);
|
||
}
|
||
}
|
||
|
||
// 8. 重新启用外键检查
|
||
await connection.execute('SET FOREIGN_KEY_CHECKS = 1');
|
||
|
||
// 9. 提交事务
|
||
await connection.commit();
|
||
|
||
// 10. 验证结果
|
||
const [finalFarms] = await connection.execute('SELECT * FROM farms ORDER BY id ASC');
|
||
console.log('\n最终结果:');
|
||
finalFarms.forEach(farm => {
|
||
console.log(`ID: ${farm.id}, Name: ${farm.name}`);
|
||
});
|
||
|
||
console.log('\n✅ farms表ID重新排序完成!');
|
||
|
||
} catch (error) {
|
||
if (connection) {
|
||
await connection.rollback();
|
||
}
|
||
console.error('❌ 操作失败:', error.message);
|
||
} finally {
|
||
if (connection) {
|
||
await connection.end();
|
||
}
|
||
}
|
||
}
|
||
|
||
simpleReorderFarms(); |