feat(device): 完善设备及网络接口管理功能

- 新增网络接口及地址配置相关实体类与映射
- 扩展 CreateDeviceRequest 支持嵌套接口与地址配置
- 调整设备类型实体类字段并增强逻辑删除支持
- 优化数据表结构,分离接口属性与地址配置
- 新增 DNS 服务器及相关映射实体支持
- 实现设备创建事务中同步保存接口与地址信息
- 调整 MyBatis Plus 逻辑删除配置与时间字段类型
- 重构 data.sql 初始化脚本,完善表间外键约束
This commit is contained in:
Chaos
2025-11-28 21:46:52 +08:00
parent 3f8dc871ab
commit cc70d867c1
17 changed files with 705 additions and 184 deletions

View File

@@ -31,6 +31,9 @@ mybatis-plus:
global-config:
db-config:
id-type: auto
logic-not-delete-value:
logic-delete-value: "NOW()"
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

View File

@@ -1,177 +1,257 @@
-- 1. 系统用户表 (t_user)
DROP TABLE IF EXISTS t_user;
CREATE TABLE t_user (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID',
username VARCHAR(64) NOT NULL COMMENT '用户名/登录名',
password VARCHAR(255) NOT NULL COMMENT '密码加密存储建议BCrypt或Argon2',
nickname VARCHAR(64) DEFAULT '' COMMENT '用户昵称',
enabled TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态 (1:启用, 0:禁用)',
account_non_expired TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '账号未过期 (1:是, 0:否)',
credentials_non_expired TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '凭证未过期 (1:是, 0:否)',
account_non_locked TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '账号未锁定 (1:是, 0:否)',
deleted TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '逻辑删除 (1:已删除, 0:未删除)',
remark VARCHAR(500) DEFAULT '' COMMENT '备注信息',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
/*
Navicat Premium Data Transfer
Source Schema : chaos
Target Server Type : MySQL
Target Server Version : 8.0+
Date: 2024-06-12
*/
PRIMARY KEY (id),
UNIQUE KEY uk_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-用户表';
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- 1. 设备类型表 (保持原有结构)
-- ----------------------------
DROP TABLE IF EXISTS `t_device_type`;
CREATE TABLE `t_device_type` (
`id` bigint unsigned auto_increment comment '主键ID'
primary key,
`name` varchar(128) not null comment '设备类型名称',
`code` varchar(64) null comment '设备类型唯一编码',
`parent_id` bigint unsigned default 0 null comment '父级类型ID (0表示顶级)',
`remark` varchar(500) default '' null comment '备注',
`create_time` datetime default current_timestamp() not null comment '创建时间',
`update_time` datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
`delete_time` datetime null comment '逻辑删除时间',
constraint uk_code
unique (`code`),
constraint uk_name
unique (`name`)
) comment '资产管理-设备类型表';
create index idx_parent_id on `t_device_type` (`parent_id`);
-- 2. 角色/用户组表 (t_role)
DROP TABLE IF EXISTS t_role;
CREATE TABLE t_role (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID',
name VARCHAR(64) NOT NULL COMMENT '角色名称 (如: 管理员)',
code VARCHAR(64) NOT NULL COMMENT '角色标识 (如: admin)',
status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态 (1:正常, 0:停用)',
remark VARCHAR(500) DEFAULT '' COMMENT '备注',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
-- ----------------------------
-- 2. 设备信息表 (保持原有结构)
-- ----------------------------
DROP TABLE IF EXISTS `t_device`;
CREATE TABLE `t_device` (
`id` bigint unsigned auto_increment comment '主键ID'
primary key,
`name` varchar(128) not null comment '设备名称',
`model` varchar(128) null comment '设备型号',
`type_id` bigint unsigned null comment '设备类型ID (外键关联设备类型表)',
`location_id` bigint unsigned null comment '位置ID (外键关联位置表)',
`snmp_community` varchar(128) null comment 'SNMP团体名 (建议加密或隐藏)',
`manufacturer` varchar(128) null comment '设备制造商',
`purchase_date` date null comment '采购日期',
`status` tinyint unsigned default 1 null comment '设备状态 (1:在线, 0:离线, 2:维护中)',
`remark` varchar(500) default '' null comment '备注',
`create_time` datetime default current_timestamp() not null comment '创建时间',
`update_time` datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
`delete_time` datetime null comment '逻辑删除时间',
constraint fk_dev_type_id
foreign key (`type_id`) references `t_device_type` (`id`)
on update cascade on delete set null
) comment '资产管理-设备信息表';
PRIMARY KEY (id),
UNIQUE KEY uk_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-角色表';
create index idx_location_id on `t_device` (`location_id`);
create index idx_type_id on `t_device` (`type_id`);
-- 3. 权限表 (t_permission)
DROP TABLE IF EXISTS t_permission;
CREATE TABLE t_permission (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID',
parent_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '父权限ID (0为顶级)',
name VARCHAR(64) NOT NULL COMMENT '权限名称',
code VARCHAR(128) NOT NULL COMMENT '权限标识/资源路径 (RESTful 风格或权限点)',
type TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '类型 (1:目录, 2:菜单, 3:按钮)',
sort_order INT NOT NULL DEFAULT 0 COMMENT '排序 (数值越小越靠前)',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
-- ----------------------------
-- 3. 网络接口表 (结构调整移除IP/VLAN仅保留物理/链路属性)
-- ----------------------------
DROP TABLE IF EXISTS `t_network_interface`;
CREATE TABLE `t_network_interface` (
`id` bigint unsigned auto_increment comment '主键ID'
primary key,
`device_id` bigint unsigned not null comment '设备ID',
`parent_id` bigint unsigned null comment '父接口ID (用于子接口/聚合口成员)',
`name` varchar(64) not null comment '接口名称 (如: eth0, GE0/0/1, Port-Channel1)',
`type` tinyint unsigned default 1 not null comment '接口类型 (1:物理口, 2:聚合口, 3:虚拟口)',
`mac_address` varchar(17) null comment 'MAC地址 (格式: AA:BB:CC:DD:EE:FF)',
`port_speed` int unsigned default 0 null comment '物理端口速率 (Mbps)',
`duplex` tinyint unsigned default 1 null comment '双工模式 (1:全双工, 2:半双工, 3:自适应)',
`status` tinyint unsigned default 1 null comment '接口运行状态 (1:UP, 0:DOWN)',
`remark` varchar(500) default '' null comment '备注',
`create_time` datetime default current_timestamp() not null comment '创建时间',
`update_time` datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
`delete_time` datetime null comment '逻辑删除时间',
constraint fk_ni_device_id
foreign key (`device_id`) references `t_device` (`id`)
on update cascade on delete cascade,
constraint fk_ni_parent_id
foreign key (`parent_id`) references `t_network_interface` (`id`)
on update cascade on delete set null
) comment '资产管理-网络接口基础表';
PRIMARY KEY (id),
UNIQUE KEY uk_code (code),
INDEX idx_parent_id (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-权限表';
create index idx_ni_device_id on `t_network_interface` (`device_id`);
create index idx_ni_mac_address on `t_network_interface` (`mac_address`);
-- --------------------------------------------------
-- B. 资产管理基础表 (无外部业务依赖, 但会被 t_device 引用)
-- --------------------------------------------------
-- ----------------------------
-- 4. 接口地址配置表 (新增核心表支持多IP/多VLAN/广播地址)
-- ----------------------------
DROP TABLE IF EXISTS `t_interface_address_config`;
CREATE TABLE `t_interface_address_config` (
`id` bigint unsigned auto_increment comment '主键ID'
primary key,
`interface_id` bigint unsigned not null comment '关联物理接口ID',
`vlan_id` smallint unsigned default 0 null comment 'VLAN ID (0或NULL表示Native/Untagged)',
`ip_address` varchar(45) null comment 'IP地址 (支持IPv4/IPv6)',
`subnet_mask` varchar(45) null comment '子网掩码/CIDR前缀',
`gateway_ip` varchar(45) null comment '网关IP地址',
`broadcast_address` varchar(45) null comment '广播地址 (新增)',
`is_primary` tinyint unsigned default 1 not null comment '是否为主IP (1:主IP, 0:从IP/Alias)',
`is_dhcp` tinyint unsigned default 0 not null comment '是否启用DHCP (1:启用, 0:静态)',
`mtu` int unsigned default 1500 null comment 'MTU值',
`status` tinyint unsigned default 1 null comment '配置状态 (1:启用, 0:禁用)',
`remark` varchar(500) default '' null comment '备注',
`create_time` datetime default current_timestamp() not null comment '创建时间',
`update_time` datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
`delete_time` datetime null comment '逻辑删除时间',
constraint fk_iac_interface_id
foreign key (`interface_id`) references `t_network_interface` (`id`)
on update cascade on delete cascade
) comment '资产管理-接口地址与VLAN配置表';
-- 4. 设备类型表 (t_device_type)
-- 原脚本编号 8提前创建以供 t_device 引用
DROP TABLE IF EXISTS t_device_type;
CREATE TABLE t_device_type (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID',
name VARCHAR(128) NOT NULL COMMENT '设备类型名称',
code VARCHAR(64) UNIQUE DEFAULT NULL COMMENT '设备类型唯一编码',
parent_id BIGINT UNSIGNED DEFAULT 0 COMMENT '父级类型ID (0表示顶级)',
remark VARCHAR(500) DEFAULT '' COMMENT '备注',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (id),
UNIQUE KEY uk_name (name),
UNIQUE KEY uk_code (code),
INDEX idx_parent_id (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='资产管理-设备类型表';
create index idx_iac_interface_id on `t_interface_address_config` (`interface_id`);
create index idx_iac_ip_address on `t_interface_address_config` (`ip_address`);
-- --------------------------------------------------
-- C. 系统管理关联表 (引用 A 组中的表)
-- --------------------------------------------------
-- 5. 用户-角色关联表 (t_user_role)
-- 原脚本编号 4
DROP TABLE IF EXISTS t_user_role;
CREATE TABLE t_user_role (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID (代理键)',
user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
role_id BIGINT UNSIGNED NOT NULL COMMENT '角色ID',
PRIMARY KEY (id),
UNIQUE KEY uk_user_role (user_id, role_id),
CONSTRAINT fk_ur_user_id FOREIGN KEY (user_id) REFERENCES t_user (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_ur_role_id FOREIGN KEY (role_id) REFERENCES t_role (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-用户角色关联表';
-- ----------------------------
-- 5. DNS 服务器字典表 (新增复用DNS)
-- ----------------------------
DROP TABLE IF EXISTS `t_dns_server`;
CREATE TABLE `t_dns_server` (
`id` bigint unsigned auto_increment comment '主键ID'
primary key,
`dns_address` varchar(45) not null comment 'DNS服务器IP地址',
`name` varchar(64) null comment 'DNS名称 (如: Google DNS)',
`description` varchar(255) default '' null comment '描述',
`create_time` datetime default current_timestamp() not null comment '创建时间',
`update_time` datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
`delete_time` datetime null comment '逻辑删除时间',
constraint uk_dns_address
unique (`dns_address`)
) comment '资产管理-DNS服务器字典表';
-- 6. 角色-权限关联表 (t_role_permission)
-- 原脚本编号 5
DROP TABLE IF EXISTS t_role_permission;
CREATE TABLE t_role_permission (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID (代理键)',
role_id BIGINT UNSIGNED NOT NULL COMMENT '角色ID',
permission_id BIGINT UNSIGNED NOT NULL COMMENT '权限ID',
-- ----------------------------
-- 6. 接口配置与DNS关联表 (新增:多对多关系)
-- ----------------------------
DROP TABLE IF EXISTS `t_interface_dns_mapping`;
CREATE TABLE `t_interface_dns_mapping` (
`id` bigint unsigned auto_increment comment '主键ID'
primary key,
`config_id` bigint unsigned not null comment '接口配置ID (关联 t_interface_address_config)',
`dns_server_id` bigint unsigned not null comment 'DNS服务器ID (关联 t_dns_server)',
`priority` int unsigned default 1 not null comment '优先级 (数值越小优先级越高)',
`create_time` datetime default current_timestamp() not null comment '创建时间',
constraint uk_config_dns
unique (`config_id`, `dns_server_id`),
constraint fk_idm_config_id
foreign key (`config_id`) references `t_interface_address_config` (`id`)
on update cascade on delete cascade,
constraint fk_idm_dns_id
foreign key (`dns_server_id`) references `t_dns_server` (`id`)
on update cascade on delete cascade
) comment '资产管理-接口配置DNS关联表';
PRIMARY KEY (id),
UNIQUE KEY uk_role_permission (role_id, permission_id),
create index idx_idm_config_id on `t_interface_dns_mapping` (`config_id`);
CONSTRAINT fk_rp_role_id FOREIGN KEY (role_id) REFERENCES t_role (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_rp_permission_id FOREIGN KEY (permission_id) REFERENCES t_permission (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-角色权限关联表';
SET FOREIGN_KEY_CHECKS = 1;
create table chaos.t_permission
(
id bigint unsigned auto_increment comment '主键ID'
primary key,
parent_id bigint unsigned default 0 not null comment '父权限ID (0为顶级)',
name varchar(64) not null comment '权限名称',
code varchar(128) not null comment '权限标识/资源路径 (RESTful 风格或权限点)',
type tinyint unsigned default 1 not null comment '类型 (1:目录, 2:菜单, 3:按钮)',
sort_order int default 0 not null comment '排序 (数值越小越靠前)',
create_time datetime default current_timestamp() not null comment '创建时间',
update_time datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
delete_time datetime null,
constraint uk_code
unique (code)
)
comment '系统管理-权限表';
-- --------------------------------------------------
-- D. 资产管理主表 (引用 B 组中的表)
-- --------------------------------------------------
create index idx_parent_id
on chaos.t_permission (parent_id);
-- 7. 基础设备表 (t_device)
-- 原脚本编号 6
-- 依赖于 t_device_type (以及假设的 t_location)
DROP TABLE IF EXISTS t_device;
CREATE TABLE t_device (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID',
name VARCHAR(128) NOT NULL COMMENT '设备名称',
model VARCHAR(128) DEFAULT NULL COMMENT '设备型号',
type_id BIGINT UNSIGNED DEFAULT NULL COMMENT '设备类型ID (外键关联设备类型表)',
location_id BIGINT UNSIGNED DEFAULT NULL COMMENT '位置ID (外键关联位置表)',
snmp_community VARCHAR(128) DEFAULT NULL COMMENT 'SNMP团体名 (建议加密或隐藏)',
manufacturer VARCHAR(128) DEFAULT NULL COMMENT '设备制造商',
purchase_date DATE DEFAULT NULL COMMENT '采购日期',
status TINYINT UNSIGNED DEFAULT 1 COMMENT '设备状态 (1:在线, 0:离线, 2:维护中)',
remark VARCHAR(500) DEFAULT '' COMMENT '备注',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
create table chaos.t_role
(
id bigint unsigned auto_increment comment '主键ID'
primary key,
name varchar(64) not null comment '角色名称 (如: 管理员)',
code varchar(64) not null comment '角色标识 (如: admin)',
status tinyint unsigned default 1 not null comment '状态 (1:正常, 0:停用)',
remark varchar(500) default '' null comment '备注',
create_time datetime default current_timestamp() not null comment '创建时间',
update_time datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
delete_time datetime null,
constraint uk_code
unique (code)
)
comment '系统管理-角色表';
PRIMARY KEY (id),
INDEX idx_type_id (type_id),
INDEX idx_location_id (location_id),
create table chaos.t_role_permission
(
id bigint unsigned auto_increment comment '主键ID (代理键)'
primary key,
role_id bigint unsigned not null comment '角色ID',
permission_id bigint unsigned not null comment '权限ID',
constraint uk_role_permission
unique (role_id, permission_id),
constraint fk_rp_permission_id
foreign key (permission_id) references chaos.t_permission (id)
on update cascade on delete cascade,
constraint fk_rp_role_id
foreign key (role_id) references chaos.t_role (id)
on update cascade on delete cascade
)
comment '系统管理-角色权限关联表';
-- 确保 t_device_type 已经创建
CONSTRAINT fk_dev_type_id FOREIGN KEY (type_id) REFERENCES t_device_type (id) ON DELETE SET NULL ON UPDATE CASCADE
-- 如果有 t_location 表,也应在此处添加外键
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='资产管理-设备信息表';
create table chaos.t_user
(
id bigint unsigned auto_increment comment '主键ID'
primary key,
username varchar(64) not null comment '用户名/登录名',
password varchar(255) not null comment '密码加密存储建议BCrypt或Argon2',
nickname varchar(64) default '' null comment '用户昵称',
avatar varchar(255) null comment '头像',
enabled tinyint unsigned default 1 not null comment '状态 (1:启用, 0:禁用)',
account_non_expired tinyint unsigned default 1 not null comment '账号未过期 (1:是, 0:否)',
credentials_non_expired tinyint unsigned default 1 not null comment '凭证未过期 (1:是, 0:否)',
account_non_locked tinyint unsigned default 1 not null comment '账号未锁定 (1:是, 0:否)',
remark varchar(500) default '' null comment '备注信息',
create_time datetime default current_timestamp() not null comment '创建时间',
update_time datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
delete_time datetime null,
constraint uk_username
unique (username)
)
comment '系统管理-用户表';
create table chaos.t_user_role
(
id bigint unsigned auto_increment comment '主键ID (代理键)'
primary key,
user_id bigint unsigned not null comment '用户ID',
role_id bigint unsigned not null comment '角色ID',
constraint uk_user_role
unique (user_id, role_id),
constraint fk_ur_role_id
foreign key (role_id) references chaos.t_role (id)
on update cascade on delete cascade,
constraint fk_ur_user_id
foreign key (user_id) references chaos.t_user (id)
on update cascade on delete cascade
)
comment '系统管理-用户角色关联表';
-- --------------------------------------------------
-- E. 资产管理子表 (引用 D 组中的表)
-- --------------------------------------------------
-- 8. 网络接口表 (t_network_interface)
-- 原脚本编号 7
-- 依赖于 t_device
DROP TABLE IF EXISTS t_network_interface;
CREATE TABLE t_network_interface (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID',
device_id BIGINT UNSIGNED NOT NULL COMMENT '设备ID',
parent_id BIGINT UNSIGNED DEFAULT NULL COMMENT '父接口ID (用于子接口/聚合口,自关联)',
name VARCHAR(64) NOT NULL COMMENT '接口名称 (如: eth0, GE0/0/1)',
mac_address VARCHAR(17) DEFAULT NULL COMMENT 'MAC地址 (格式: AA:BB:CC:DD:EE:FF)',
ip_address VARCHAR(45) DEFAULT NULL COMMENT 'IP地址 (支持IPv4/IPv6)',
subnet_mask VARCHAR(45) DEFAULT NULL COMMENT '子网掩码/前缀长度',
gateway_ip VARCHAR(45) DEFAULT NULL COMMENT '网关IP地址',
vlan_id SMALLINT UNSIGNED DEFAULT NULL COMMENT 'VLAN ID (1-4094, SMALLINT更合适)',
port_speed INT UNSIGNED DEFAULT 0 COMMENT '端口速率 (Mbps)',
status TINYINT UNSIGNED DEFAULT 1 COMMENT '接口状态 (1:UP, 0:DOWN)',
remark VARCHAR(500) DEFAULT '' COMMENT '备注',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (id),
INDEX idx_mac_address (mac_address),
INDEX idx_ip_address (ip_address),
CONSTRAINT fk_ni_device_id FOREIGN KEY (device_id) REFERENCES t_device (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_ni_parent_id FOREIGN KEY (parent_id) REFERENCES t_network_interface (id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='资产管理-网络接口表';