use school_news;
-- 部门表
DROP TABLE IF EXISTS `tb_sys_dept`;
CREATE TABLE `tb_sys_dept` (
	`id` VARCHAR(50) NOT NULL COMMENT 'id',
  `dept_id` VARCHAR(50) NOT NULL COMMENT '部门ID',
  `name` VARCHAR(100) NOT NULL COMMENT '部门名称',
  `parent_id` VARCHAR(50) DEFAULT NULL COMMENT '父部门ID',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '部门描述',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_dept_id` (`dept_id`),
  KEY `idx_dept_parent` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `tb_sys_dept` (id,dept_id,name, description) VALUES ('1','root_department', '超级部门', '系统超级部门');
INSERT INTO `tb_sys_dept` (id,dept_id,name, parent_id, description) VALUES ('2','default_department', '默认部门', 'root_department', '系统默认创建的部门');

-- 角色表
DROP TABLE IF EXISTS `tb_sys_role`;
CREATE TABLE `tb_sys_role` (
	`id` VARCHAR(50) NOT NULL COMMENT 'id',
  `role_id` VARCHAR(50) NOT NULL COMMENT '角色ID',
  `name` VARCHAR(100) NOT NULL COMMENT '角色名称',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '角色描述',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_role_id` (`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `tb_sys_role` (id,role_id, name, description) VALUES ('1','admin', '管理员', '系统管理员角色');
INSERT INTO `tb_sys_role` (id,role_id, name, description) VALUES ('2','freedom', '自由角色', '自由角色');

-- 部门-角色关联
DROP TABLE IF EXISTS `tb_sys_dept_role`;
CREATE TABLE `tb_sys_dept_role` (
  `id` VARCHAR(50) NOT NULL COMMENT '部门角色ID',
  `dept_id` VARCHAR(50) NOT NULL COMMENT '部门ID',
  `role_id` VARCHAR(50) NOT NULL COMMENT '角色ID',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_dept_role` (`dept_id`, `role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `tb_sys_dept_role` (id, dept_id, role_id) VALUES 
('1', 'root_department', 'admin'),
('2', 'default_department', 'freedom');

-- 用户-角色关联
DROP TABLE IF EXISTS `tb_sys_user_dept_role`;
CREATE TABLE `tb_sys_user_dept_role` (
  `id` VARCHAR(50) NOT NULL COMMENT '用户角色ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `dept_id` VARCHAR(50) NOT NULL COMMENT '部门ID',
  `role_id` VARCHAR(50) NOT NULL COMMENT '角色ID',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_dept_role` (`user_id`, `dept_id`, `role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `tb_sys_user_dept_role` (id, user_id, dept_id, role_id) VALUES ('1', '1', 'root_department', 'admin');

-- 权限表
DROP TABLE IF EXISTS `tb_sys_permission`;
CREATE TABLE `tb_sys_permission` (
	`id` VARCHAR(50) NOT NULL COMMENT 'id',
  `permission_id` VARCHAR(50) NOT NULL COMMENT '权限ID',
  `name` VARCHAR(100) NOT NULL COMMENT '权限名称',
  `code` VARCHAR(100) NOT NULL COMMENT '权限代码',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '权限描述',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_permission_id` (`permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `tb_sys_permission` (id,permission_id, name, code, description) VALUES 
('1','perm_user_manage', '用户管理', 'user:manage', '用户管理权限'),
('2','perm_role_manage', '角色管理', 'role:manage', '角色管理权限'),
('3','perm_dept_manage', '部门管理', 'dept:manage', '部门管理权限'),
('4','perm_permission_manage', '权限管理', 'permission:manage', '权限管理权限');

-- 角色-权限关联
DROP TABLE IF EXISTS `tb_sys_role_permission`;
CREATE TABLE `tb_sys_role_permission` (
  `id` VARCHAR(50) NOT NULL COMMENT '角色权限ID',
  `role_id` VARCHAR(50) NOT NULL COMMENT '角色ID',
  `permission_id` VARCHAR(50) NOT NULL COMMENT '权限ID',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_role_permission` (`role_id`, `permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `tb_sys_role_permission` (id, role_id, permission_id) VALUES
('1', 'admin', 'perm_user_manage'),
('2', 'admin', 'perm_role_manage'),
('3', 'admin', 'perm_dept_manage'),
('4', 'admin', 'perm_permission_manage'),
('5', 'admin', 'perm_dashboard'),
('6', 'admin', 'perm_user_manage'),
('7', 'admin', 'perm_role_manage'),
('8', 'admin', 'perm_dept_manage'),
('9', 'admin', 'perm_permission_manage');

-- 菜单表
DROP TABLE IF EXISTS `tb_sys_menu`;
CREATE TABLE `tb_sys_menu` (
	`id` VARCHAR(50) NOT NULL COMMENT 'id',
  `menu_id` VARCHAR(50) NOT NULL COMMENT '菜单ID',
  `name` VARCHAR(100) NOT NULL COMMENT '菜单名称',
  `parent_id` VARCHAR(50) DEFAULT NULL COMMENT '父菜单ID',
  `url` VARCHAR(255) DEFAULT NULL COMMENT '菜单URL',
  `icon` VARCHAR(100) DEFAULT NULL COMMENT '菜单图标',
  `order_num` INT(4) DEFAULT 0 COMMENT '菜单排序号',
  `type` INT(4) DEFAULT 0 COMMENT '菜单类型（0目录 1菜单 2按钮）',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_menu_id` (`menu_id`),
  KEY `idx_menu_parent` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Insert default menus
INSERT INTO `tb_sys_menu` (id,menu_id, name, parent_id, url, icon, order_num, type) VALUES
('1','menu_dashboard', '仪表盘', NULL, '/dashboard', 'dashboard', 1, 1),
('2','menu_user_mgmt', '用户管理', NULL, '/user-management', 'user', 2, 1),
('3','menu_role_mgmt', '角色管理', NULL, '/role-management', 'team', 3, 1),
('4','menu_dept_mgmt', '部门管理', NULL, '/dept-management', 'apartment', 4, 1),
('5','menu_permission_mgmt', '权限管理', NULL, '/permission-management', 'safety-certificate', 5, 1),
('6','menu_dashboard', '仪表盘', NULL, '/dashboard', 'dashboard', 6, 1),
('7','menu_user_mgmt', '用户管理', NULL, '/user-management', 'user', 7, 1),
('8','menu_role_mgmt', '角色管理', NULL, '/role-management', 'team', 8, 1),
('9','menu_dept_mgmt', '部门管理', NULL, '/dept-management', 'apartment', 9, 1),
('10','menu_permission_mgmt', '权限管理', NULL, '/permission-management', 'safety-certificate', 10, 1); 

DROP TABLE IF EXISTS `tb_sys_menu_permission`;
CREATE TABLE `tb_sys_menu_permission` (
  `id` VARCHAR(50) NOT NULL COMMENT '菜单权限ID',
  `permission_id` VARCHAR(50) NOT NULL COMMENT '权限ID',
  `menu_id` VARCHAR(50) NOT NULL COMMENT '菜单ID',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_menu_permission` (`menu_id`, `permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
-- Insert default role-menu associations for admin role
INSERT INTO `tb_sys_menu_permission` (id, permission_id, menu_id) VALUES
('1', 'perm_dashboard', 'menu_dashboard'),
('2', 'perm_user_manage', 'menu_user_mgmt'),
('3', 'perm_role_manage', 'menu_role_mgmt'),
('4', 'perm_dept_manage', 'menu_dept_mgmt'),
('5', 'perm_permission_manage', 'menu_permission_mgmt');