From 93a5de2106623345770cf12311a4c7ee089888bc Mon Sep 17 00:00:00 2001 From: zhang1106 <849185023@qq.com> Date: Mon, 9 Feb 2026 16:22:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=AE=BE=E5=A4=87=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E9=94=AE=E5=88=A0=E9=99=A4=E6=89=80?= =?UTF-8?q?=E6=9C=89=E8=AE=BE=E5=A4=87=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 前端添加删除所有设备按钮及确认对话框 - 后端实现删除所有设备及相关数据的接口 - 更新.gitignore忽略backend/temp目录 --- .gitignore | 1 + backend/routes/devices.js | 74 +++++++++++++++++++++++++ frontend/src/pages/DeviceManagement.jsx | 30 ++++++++++ 3 files changed, 105 insertions(+) diff --git a/.gitignore b/.gitignore index 578783b..36949ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Dependencies node_modules/ +/backed/temp # Build outputs dist/ diff --git a/backend/routes/devices.js b/backend/routes/devices.js index 2f3bb60..c9f432c 100644 --- a/backend/routes/devices.js +++ b/backend/routes/devices.js @@ -1092,6 +1092,80 @@ router.delete('/batch-delete', validateBody(batchDeviceIdsSchema), async (req, r } }); +// 删除所有设备(一键清空) +router.delete('/delete-all', async (req, res) => { + const t = await sequelize.transaction(); + try { + // 获取所有设备 + const allDevices = await Device.findAll({ transaction: t }); + + if (allDevices.length === 0) { + await t.rollback(); + return res.json({ message: '没有设备需要删除', deletedCount: 0 }); + } + + const deviceIds = allDevices.map(d => d.deviceId); + + // 1. 删除相关接线 + await Cable.destroy({ + where: { + [Op.or]: [ + { sourceDeviceId: { [Op.in]: deviceIds } }, + { targetDeviceId: { [Op.in]: deviceIds } } + ] + }, + transaction: t + }); + + // 2. 删除相关网卡 + await NetworkCard.destroy({ + where: { deviceId: { [Op.in]: deviceIds } }, + transaction: t + }); + + // 3. 删除相关端口 + await DevicePort.destroy({ + where: { deviceId: { [Op.in]: deviceIds } }, + transaction: t + }); + + // 4. 解除工单关联 + await Ticket.update( + { deviceId: null }, + { where: { deviceId: { [Op.in]: deviceIds } }, transaction: t } + ); + + // 5. 更新机柜功率 + for (const device of allDevices) { + if (device.rackId) { + const rack = await Rack.findByPk(device.rackId, { transaction: t }); + if (rack) { + await rack.update({ + currentPower: Math.max(0, rack.currentPower - device.powerConsumption) + }, { transaction: t }); + } + } + } + + // 6. 删除所有设备 + const deletedCount = await Device.destroy({ + where: {}, + transaction: t + }); + + await t.commit(); + + res.json({ + message: `成功删除所有设备,共删除 ${deletedCount} 个设备`, + deletedCount + }); + } catch (error) { + await t.rollback(); + console.error('删除所有设备错误:', error); + res.status(500).json({ error: error.message, stack: error.stack }); + } +}); + // 删除设备 router.delete('/:deviceId', async (req, res) => { const t = await sequelize.transaction(); diff --git a/frontend/src/pages/DeviceManagement.jsx b/frontend/src/pages/DeviceManagement.jsx index 0297b2c..13161b4 100644 --- a/frontend/src/pages/DeviceManagement.jsx +++ b/frontend/src/pages/DeviceManagement.jsx @@ -599,6 +599,29 @@ function DeviceManagement() { }); }; + // 一键删除所有设备 + const handleDeleteAll = async () => { + Modal.confirm({ + title: '危险操作确认', + content: '确定要删除所有设备吗?此操作将清空所有设备数据,包括相关的接线、网卡、端口等信息,且不可恢复!', + okText: '确认删除所有', + okType: 'danger', + cancelText: '取消', + onOk: async () => { + try { + const response = await axios.delete('/api/devices/delete-all'); + message.success(response.data.message || '成功删除所有设备'); + setSelectedDevices([]); + setSelectAll(false); + fetchDevices(1, 10, true); + } catch (error) { + message.error('删除所有设备失败'); + console.error('删除所有设备失败:', error); + } + } + }); + }; + // 删除设备 const handleDelete = async (deviceId) => { Modal.confirm({ @@ -1562,6 +1585,13 @@ function DeviceManagement() { > 批量删除 ({selectedDevices.length}) +