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

@@ -0,0 +1,9 @@
package cn.nopj.chaos_api.mapper;
import cn.nopj.chaos_api.domain.entity.DnsServer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DnsServerMapper extends BaseMapper<DnsServer> {
}

View File

@@ -0,0 +1,9 @@
package cn.nopj.chaos_api.mapper;
import cn.nopj.chaos_api.domain.entity.InterfaceAddressConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface InterfaceAddressConfigMapper extends BaseMapper<InterfaceAddressConfig> {
}

View File

@@ -0,0 +1,9 @@
package cn.nopj.chaos_api.mapper;
import cn.nopj.chaos_api.domain.entity.InterfaceDnsMapping;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface InterfaceDnsMappingMapper extends BaseMapper<InterfaceDnsMapping> {
}

View File

@@ -0,0 +1,9 @@
package cn.nopj.chaos_api.mapper;
import cn.nopj.chaos_api.domain.entity.NetworkInterface;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface NetworkInterfaceMapper extends BaseMapper<NetworkInterface> {
}

View File

@@ -1,9 +1,6 @@
package cn.nopj.chaos_api.domain.entity; package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import java.time.LocalDate; import java.time.LocalDate;
@@ -23,7 +20,10 @@ public class Device {
private LocalDate purchaseDate; private LocalDate purchaseDate;
private int status; private int status;
private String remark; private String remark;
private LocalDate createTime;
private LocalDate updateTime;
@TableLogic
private LocalDate deleteTime;
@TableField(exist = false) @TableField(exist = false)
private DeviceType deviceType; private DeviceType deviceType;

View File

@@ -1,18 +1,51 @@
package cn.nopj.chaos_api.domain.entity; package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.*;
import lombok.Data; import lombok.Data;
import java.time.LocalDate; import java.time.LocalDateTime;
@Data @Data
@TableName("t_device_type") @TableName("t_device_type")
public class DeviceType { public class DeviceType {
@TableId(value = "id", type = IdType.AUTO)
private Long id; private Long id;
/**
* 设备类型名称
*/
private String name; private String name;
/**
* 设备类型唯一编码
*/
private String code; private String code;
/**
* 父级类型ID (0表示顶级)
*/
private Long parentId; private Long parentId;
/**
* 备注
*/
private String remark; private String remark;
private LocalDate createTime;
private LocalDate updateTime; /**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 逻辑删除时间
*/
@TableLogic
private LocalDateTime deleteTime;
} }

View File

@@ -0,0 +1,50 @@
package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
/**
* DNS 服务器实体
* 对应表: t_dns_server
*/
@Data
@TableName("t_dns_server")
public class DnsServer {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* DNS服务器IP地址
*/
private String dnsAddress;
/**
* DNS名称 (如: Google DNS)
*/
private String name;
/**
* 描述
*/
private String description;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 逻辑删除时间
*/
@TableLogic
private LocalDateTime deleteTime;
}

View File

@@ -0,0 +1,90 @@
package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 接口地址与VLAN配置实体 (核心配置)
* 对应表: t_interface_address_config
*/
@Data
@TableName("t_interface_address_config")
public class InterfaceAddressConfig {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 关联物理接口ID
*/
private Long interfaceId;
/**
* VLAN ID (0或NULL表示Native/Untagged)
*/
private Integer vlanId;
/**
* IP地址 (支持IPv4/IPv6)
*/
private String ipAddress;
/**
* 子网掩码/CIDR前缀
*/
private String subnetMask;
/**
* 网关IP地址
*/
private String gatewayIp;
/**
* 广播地址
*/
private String broadcastAddress;
/**
* 是否为主IP (true:主IP, false:从IP/Alias)
*/
private Boolean isPrimary;
/**
* 是否启用DHCP (true:启用, false:静态)
*/
private Boolean isDhcp;
/**
* MTU值
*/
private Integer mtu;
/**
* 配置状态 (1:启用, 0:禁用)
*/
private Integer status;
/**
* 备注
*/
private String remark;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 逻辑删除时间
*/
@TableLogic
private LocalDateTime deleteTime;
}

View File

@@ -0,0 +1,38 @@
package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 接口配置与DNS关联实体
* 对应表: t_interface_dns_mapping
*/
@Data
@TableName("t_interface_dns_mapping")
public class InterfaceDnsMapping {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 接口配置ID (关联 t_interface_address_config)
*/
private Long configId;
/**
* DNS服务器ID (关联 t_dns_server)
*/
private Long dnsServerId;
/**
* 优先级 (数值越小优先级越高)
*/
private Integer priority;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,80 @@
package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 网络接口基础实体 (物理/链路层属性)
* 对应表: t_network_interface
*/
@Data
@TableName("t_network_interface")
public class NetworkInterface {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 设备ID
*/
private Long deviceId;
/**
* 父接口ID (用于子接口/聚合口成员)
*/
private Long parentId;
/**
* 接口名称 (如: eth0, GE0/0/1)
*/
private String name;
/**
* 接口类型 (1:物理口, 2:聚合口, 3:虚拟口)
*/
private Integer type;
/**
* MAC地址
*/
private String macAddress;
/**
* 物理端口速率 (Mbps)
*/
private Integer portSpeed;
/**
* 双工模式 (1:全双工, 2:半双工, 3:自适应)
*/
private Integer duplex;
/**
* 接口运行状态 (1:UP, 0:DOWN)
*/
private Integer status;
/**
* 备注
*/
private String remark;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 逻辑删除时间
*/
@TableLogic
private LocalDateTime deleteTime;
}

View File

@@ -3,11 +3,11 @@ package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@@ -65,10 +65,6 @@ public class User {
*/ */
private Boolean accountNonLocked; private Boolean accountNonLocked;
/**
* 逻辑删除 (1:已删除, 0:未删除)
*/
private Boolean deleted;
/** /**
* 备注信息 * 备注信息
@@ -84,7 +80,15 @@ public class User {
* 更新时间 * 更新时间
*/ */
private Date updateTime; private Date updateTime;
/**
* 删除时间
*/
@TableLogic
private LocalDateTime deleteTime;
@TableField(exist = false) @TableField(exist = false)
private List<Role> roles; private List<Role> roles;
} }

View File

@@ -1,28 +1,97 @@
package cn.nopj.chaos_api.dto.request; package cn.nopj.chaos_api.dto.request;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Data; import lombok.Data;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List;
/**
* 创建设备请求对象
* <p>
* 对应您的命名规范CreateDeviceRequest
* 位于 request 包下
*/
@Data @Data
public class CreateDeviceRequest { public class CreateDeviceRequest {
//限制必填 不限制特殊字符
// --- 设备基础信息 ---
@NotBlank(message = "设备名称不能为空") @NotBlank(message = "设备名称不能为空")
private String name; private String name;
@NotNull(message = "设备类型ID不能为空")
private Long typeId;
private Long locationId;
@NotBlank(message = "设备型号不能为空") @NotBlank(message = "设备型号不能为空")
private String model; private String model;
@NotBlank(message = "设备类型不能为空")
private Long typeId;
private Long locationId;
private String snmpCommunity;
@NotBlank(message = "设备厂商不能为空") @NotBlank(message = "设备厂商不能为空")
private String manufacturer; private String manufacturer;
@Pattern(regexp = "^[0-9]{4}-[0-9]{2}-[0-9]{2}$", message = "设备购买日期格式不正确")
private String snmpCommunity;
private LocalDate purchaseDate; private LocalDate purchaseDate;
private int status;
private String remark; private String remark;
@Valid
private List<NetworkInterfaceRequest> interfaces;
/**
* 网络接口请求参数
*/
@Data
public static class NetworkInterfaceRequest {
@NotBlank(message = "接口名称不能为空")
private String name;
@NotNull(message = "接口类型不能为空")
private Integer type; // 1:物理口, 2:聚合口, 3:虚拟口
@Pattern(regexp = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", message = "MAC地址格式不正确")
private String macAddress;
private Integer portSpeed;
private Integer duplex;
private String remark;
@Valid
private List<InterfaceAddressConfigRequest> addressConfigs;
}
/**
* 地址与VLAN配置请求参数
*/
@Data
public static class InterfaceAddressConfigRequest {
private Integer vlanId;
@Pattern(regexp = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", message = "IP地址格式不正确")
private String ipAddress;
private String subnetMask;
private String gatewayIp;
private String broadcastAddress;
private Boolean isPrimary;
private Boolean isDhcp;
private Integer mtu;
private List<String> dnsServers;
}
} }

View File

@@ -3,7 +3,7 @@ package cn.nopj.chaos_api.dto.response;
import cn.nopj.chaos_api.domain.entity.DeviceType; import cn.nopj.chaos_api.domain.entity.DeviceType;
import lombok.Data; import lombok.Data;
import java.time.LocalDate; import java.time.LocalDateTime;
@Data @Data
public class DeviceTypeResponse { public class DeviceTypeResponse {
@@ -12,7 +12,8 @@ public class DeviceTypeResponse {
private String code; private String code;
private Long parentId; private Long parentId;
private String remark; private String remark;
private LocalDate createTime; private LocalDateTime createTime;
private LocalDateTime updateTime;
public DeviceTypeResponse(DeviceType type){ public DeviceTypeResponse(DeviceType type){
this.id = type.getId(); this.id = type.getId();
@@ -21,5 +22,6 @@ public class DeviceTypeResponse {
this.parentId = type.getParentId(); this.parentId = type.getParentId();
this.remark = type.getRemark(); this.remark = type.getRemark();
this.createTime = type.getCreateTime(); this.createTime = type.getCreateTime();
this.updateTime = type.getUpdateTime();
} }
} }

View File

@@ -3,26 +3,36 @@ package cn.nopj.chaos_api.service.impl;
import cn.nopj.chaos_api.common.constants.ErrorCode; import cn.nopj.chaos_api.common.constants.ErrorCode;
import cn.nopj.chaos_api.common.exceotion.BizException; import cn.nopj.chaos_api.common.exceotion.BizException;
import cn.nopj.chaos_api.domain.entity.Device; import cn.nopj.chaos_api.domain.entity.Device;
import cn.nopj.chaos_api.domain.entity.InterfaceAddressConfig;
import cn.nopj.chaos_api.domain.entity.NetworkInterface;
import cn.nopj.chaos_api.dto.request.CreateDeviceRequest; import cn.nopj.chaos_api.dto.request.CreateDeviceRequest;
import cn.nopj.chaos_api.dto.request.DeviceQueryRequest; import cn.nopj.chaos_api.dto.request.DeviceQueryRequest;
import cn.nopj.chaos_api.dto.request.UpdateDeviceRequest; import cn.nopj.chaos_api.dto.request.UpdateDeviceRequest;
import cn.nopj.chaos_api.dto.response.DeviceResponse; import cn.nopj.chaos_api.dto.response.DeviceResponse;
import cn.nopj.chaos_api.mapper.DeviceMapper; import cn.nopj.chaos_api.mapper.DeviceMapper;
import cn.nopj.chaos_api.mapper.InterfaceAddressConfigMapper;
import cn.nopj.chaos_api.mapper.NetworkInterfaceMapper;
import cn.nopj.chaos_api.service.DeviceService; import cn.nopj.chaos_api.service.DeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Service @Service
@Slf4j @Slf4j
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService { public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
@Autowired
NetworkInterfaceMapper networkInterfaceMapper;
@Autowired
InterfaceAddressConfigMapper interfaceAddressConfigMapper;
@Override @Override
@Transactional @Transactional
public DeviceResponse createDevice(CreateDeviceRequest createDeviceRequest) { public DeviceResponse createDevice(CreateDeviceRequest createDeviceRequest) {
@@ -34,10 +44,37 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
device.setSnmpCommunity(createDeviceRequest.getSnmpCommunity()); device.setSnmpCommunity(createDeviceRequest.getSnmpCommunity());
device.setManufacturer(createDeviceRequest.getManufacturer()); device.setManufacturer(createDeviceRequest.getManufacturer());
device.setPurchaseDate(createDeviceRequest.getPurchaseDate()); device.setPurchaseDate(createDeviceRequest.getPurchaseDate());
device.setStatus(createDeviceRequest.getStatus());
device.setRemark(createDeviceRequest.getRemark()); device.setRemark(createDeviceRequest.getRemark());
int rows = this.baseMapper.insert(device); int rows = this.baseMapper.insert(device);
List<CreateDeviceRequest.NetworkInterfaceRequest> interfaces = createDeviceRequest.getInterfaces();
interfaces.forEach(i -> {
NetworkInterface networkInterface = new NetworkInterface();
networkInterface.setDeviceId(device.getId());
networkInterface.setName(i.getName());
networkInterface.setType(i.getType());
networkInterface.setMacAddress(i.getMacAddress());
networkInterface.setPortSpeed(i.getPortSpeed());
networkInterface.setDuplex(i.getDuplex());
networkInterface.setRemark(i.getRemark());
networkInterfaceMapper.insert(networkInterface);
i.getAddressConfigs().forEach(a -> {
InterfaceAddressConfig iac = new InterfaceAddressConfig();
iac.setVlanId(a.getVlanId());
iac.setIpAddress(a.getIpAddress());
iac.setGatewayIp(a.getGatewayIp());
iac.setSubnetMask(a.getSubnetMask());
iac.setBroadcastAddress(a.getBroadcastAddress());
iac.setIsPrimary(a.getIsPrimary());
iac.setIsDhcp(a.getIsDhcp());
iac.setMtu(a.getMtu());
interfaceAddressConfigMapper.insert(iac);
});
});
if (rows > 0){ if (rows > 0){
return new DeviceResponse(device); return new DeviceResponse(device);
}else { }else {

View File

@@ -2,7 +2,6 @@ package cn.nopj.chaos_api.service.impl;
import cn.nopj.chaos_api.domain.entity.Role; import cn.nopj.chaos_api.domain.entity.Role;
import cn.nopj.chaos_api.dto.response.OptionResponse; import cn.nopj.chaos_api.dto.response.OptionResponse;
import cn.nopj.chaos_api.dto.response.RoleResponse;
import cn.nopj.chaos_api.mapper.RoleMapper; import cn.nopj.chaos_api.mapper.RoleMapper;
import cn.nopj.chaos_api.service.RoleService; import cn.nopj.chaos_api.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

View File

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

View File

@@ -1,177 +1,257 @@
-- 1. 系统用户表 (t_user) /*
DROP TABLE IF EXISTS t_user; Navicat Premium Data Transfer
CREATE TABLE t_user ( Source Schema : chaos
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID', Target Server Type : MySQL
username VARCHAR(64) NOT NULL COMMENT '用户名/登录名', Target Server Version : 8.0+
password VARCHAR(255) NOT NULL COMMENT '密码加密存储建议BCrypt或Argon2', Date: 2024-06-12
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 '更新时间',
PRIMARY KEY (id), SET NAMES utf8mb4;
UNIQUE KEY uk_username (username) SET FOREIGN_KEY_CHECKS = 0;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-用户表';
-- ----------------------------
-- 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; -- 2. 设备信息表 (保持原有结构)
CREATE TABLE t_role ( -- ----------------------------
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID', DROP TABLE IF EXISTS `t_device`;
name VARCHAR(64) NOT NULL COMMENT '角色名称 (如: 管理员)', CREATE TABLE `t_device` (
code VARCHAR(64) NOT NULL COMMENT '角色标识 (如: admin)', `id` bigint unsigned auto_increment comment '主键ID'
status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态 (1:正常, 0:停用)', primary key,
remark VARCHAR(500) DEFAULT '' COMMENT '备注', `name` varchar(128) not null comment '设备名称',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `model` varchar(128) null comment '设备型号',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 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), create index idx_location_id on `t_device` (`location_id`);
UNIQUE KEY uk_code (code) create index idx_type_id on `t_device` (`type_id`);
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-角色表';
-- 3. 权限表 (t_permission) -- ----------------------------
DROP TABLE IF EXISTS t_permission; -- 3. 网络接口表 (结构调整移除IP/VLAN仅保留物理/链路属性)
CREATE TABLE t_permission ( -- ----------------------------
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID', DROP TABLE IF EXISTS `t_network_interface`;
parent_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '父权限ID (0为顶级)', CREATE TABLE `t_network_interface` (
name VARCHAR(64) NOT NULL COMMENT '权限名称', `id` bigint unsigned auto_increment comment '主键ID'
code VARCHAR(128) NOT NULL COMMENT '权限标识/资源路径 (RESTful 风格或权限点)', primary key,
type TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '类型 (1:目录, 2:菜单, 3:按钮)', `device_id` bigint unsigned not null comment '设备ID',
sort_order INT NOT NULL DEFAULT 0 COMMENT '排序 (数值越小越靠前)', `parent_id` bigint unsigned null comment '父接口ID (用于子接口/聚合口成员)',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `name` varchar(64) not null comment '接口名称 (如: eth0, GE0/0/1, Port-Channel1)',
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `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), create index idx_ni_device_id on `t_network_interface` (`device_id`);
UNIQUE KEY uk_code (code), create index idx_ni_mac_address on `t_network_interface` (`mac_address`);
INDEX idx_parent_id (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='系统管理-权限表';
-- -------------------------------------------------- -- ----------------------------
-- 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) create index idx_iac_interface_id on `t_interface_address_config` (`interface_id`);
-- 原脚本编号 8提前创建以供 t_device 引用 create index idx_iac_ip_address on `t_interface_address_config` (`ip_address`);
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='资产管理-设备类型表';
-- -------------------------------------------------- -- ----------------------------
-- C. 系统管理关联表 (引用 A 组中的表) -- 5. DNS 服务器字典表 (新增复用DNS)
-- -------------------------------------------------- -- ----------------------------
DROP TABLE IF EXISTS `t_dns_server`;
-- 5. 用户-角色关联表 (t_user_role) CREATE TABLE `t_dns_server` (
-- 原脚本编号 4 `id` bigint unsigned auto_increment comment '主键ID'
DROP TABLE IF EXISTS t_user_role; primary key,
CREATE TABLE t_user_role ( `dns_address` varchar(45) not null comment 'DNS服务器IP地址',
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID (代理键)', `name` varchar(64) null comment 'DNS名称 (如: Google DNS)',
user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID', `description` varchar(255) default '' null comment '描述',
role_id BIGINT UNSIGNED NOT NULL COMMENT '角色ID', `create_time` datetime default current_timestamp() not null comment '创建时间',
`update_time` datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
PRIMARY KEY (id), `delete_time` datetime null comment '逻辑删除时间',
UNIQUE KEY uk_user_role (user_id, role_id), constraint uk_dns_address
unique (`dns_address`)
CONSTRAINT fk_ur_user_id FOREIGN KEY (user_id) REFERENCES t_user (id) ON DELETE CASCADE ON UPDATE CASCADE, ) comment '资产管理-DNS服务器字典表';
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='系统管理-用户角色关联表';
-- 6. 角色-权限关联表 (t_role_permission) -- ----------------------------
-- 原脚本编号 5 -- 6. 接口配置与DNS关联表 (新增:多对多关系)
DROP TABLE IF EXISTS t_role_permission; -- ----------------------------
CREATE TABLE t_role_permission ( DROP TABLE IF EXISTS `t_interface_dns_mapping`;
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID (代理键)', CREATE TABLE `t_interface_dns_mapping` (
role_id BIGINT UNSIGNED NOT NULL COMMENT '角色ID', `id` bigint unsigned auto_increment comment '主键ID'
permission_id BIGINT UNSIGNED NOT NULL 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), create index idx_idm_config_id on `t_interface_dns_mapping` (`config_id`);
UNIQUE KEY uk_role_permission (role_id, permission_id),
CONSTRAINT fk_rp_role_id FOREIGN KEY (role_id) REFERENCES t_role (id) ON DELETE CASCADE ON UPDATE CASCADE, SET FOREIGN_KEY_CHECKS = 1;
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='系统管理-角色权限关联表';
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 '系统管理-权限表';
-- -------------------------------------------------- create index idx_parent_id
-- D. 资产管理主表 (引用 B 组中的表) on chaos.t_permission (parent_id);
-- --------------------------------------------------
-- 7. 基础设备表 (t_device) create table chaos.t_role
-- 原脚本编号 6 (
-- 依赖于 t_device_type (以及假设的 t_location) id bigint unsigned auto_increment comment '主键ID'
DROP TABLE IF EXISTS t_device; primary key,
CREATE TABLE t_device ( name varchar(64) not null comment '角色名称 (如: 管理员)',
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID', code varchar(64) not null comment '角色标识 (如: admin)',
name VARCHAR(128) NOT NULL COMMENT '设备名称', status tinyint unsigned default 1 not null comment '状态 (1:正常, 0:停用)',
model VARCHAR(128) DEFAULT NULL COMMENT '设备型号', remark varchar(500) default '' null comment '备注',
type_id BIGINT UNSIGNED DEFAULT NULL COMMENT '设备类型ID (外键关联设备类型表)', create_time datetime default current_timestamp() not null comment '创建时间',
location_id BIGINT UNSIGNED DEFAULT NULL COMMENT '位置ID (外键关联位置表)', update_time datetime default current_timestamp() not null on update current_timestamp() comment '更新时间',
snmp_community VARCHAR(128) DEFAULT NULL COMMENT 'SNMP团体名 (建议加密或隐藏)', delete_time datetime null,
manufacturer VARCHAR(128) DEFAULT NULL COMMENT '设备制造商', constraint uk_code
purchase_date DATE DEFAULT NULL COMMENT '采购日期', unique (code)
status TINYINT UNSIGNED DEFAULT 1 COMMENT '设备状态 (1:在线, 0:离线, 2:维护中)', )
remark VARCHAR(500) DEFAULT '' COMMENT '备注', 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), create table chaos.t_role_permission
INDEX idx_type_id (type_id), (
INDEX idx_location_id (location_id), 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 已经创建 create table chaos.t_user
CONSTRAINT fk_dev_type_id FOREIGN KEY (type_id) REFERENCES t_device_type (id) ON DELETE SET NULL ON UPDATE CASCADE (
-- 如果有 t_location 表,也应在此处添加外键 id bigint unsigned auto_increment comment '主键ID'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='资产管理-设备信息表'; 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='资产管理-网络接口表';