-- ============================================
-- 权限体系增量建表 SQL
-- 适用于已有 demo 数据库，仅补建权限相关表
-- ============================================

USE `demo`;

SET @need_uk_username = (
    SELECT CASE WHEN COUNT(1) = 0 THEN 1 ELSE 0 END
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'sys_user'
      AND index_name = 'uk_username'
);
SET @sql_uk_username = IF(
    @need_uk_username = 1,
    'ALTER TABLE `sys_user` ADD UNIQUE KEY `uk_username` (`username`)',
    'SELECT 1'
);
PREPARE stmt_uk_username FROM @sql_uk_username;
EXECUTE stmt_uk_username;
DEALLOCATE PREPARE stmt_uk_username;

CREATE TABLE IF NOT EXISTS `sys_role` (
    `id`           BIGINT       NOT NULL COMMENT '主键ID',
    `role_code`    VARCHAR(64)  NOT NULL COMMENT '角色编码',
    `role_name`    VARCHAR(128) NOT NULL COMMENT '角色名称',
    `enabled`      TINYINT      NOT NULL DEFAULT 1 COMMENT '是否启用(0否 1是)',
    `sort`         INT          NOT NULL DEFAULT 0 COMMENT '排序',
    `remark`       VARCHAR(500) DEFAULT NULL COMMENT '备注',
    `deleted`      TINYINT      NOT NULL DEFAULT 0 COMMENT '逻辑删除(0正常 1删除)',
    `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_role_code` (`role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表';

CREATE TABLE IF NOT EXISTS `sys_menu` (
    `id`           BIGINT       NOT NULL COMMENT '主键ID',
    `parent_id`    BIGINT       NOT NULL DEFAULT 0 COMMENT '父级ID',
    `menu_type`    VARCHAR(16)  NOT NULL COMMENT '类型(MENU/BUTTON)',
    `menu_name`    VARCHAR(128) NOT NULL COMMENT '菜单名称',
    `path`         VARCHAR(255) DEFAULT NULL COMMENT '路由路径',
    `component`    VARCHAR(255) DEFAULT NULL COMMENT '组件路径',
    `permission`   VARCHAR(128) DEFAULT NULL COMMENT '权限标识',
    `icon`         VARCHAR(64)  DEFAULT NULL COMMENT '图标',
    `visible`      TINYINT      NOT NULL DEFAULT 1 COMMENT '是否显示(0否 1是)',
    `enabled`      TINYINT      NOT NULL DEFAULT 1 COMMENT '是否启用(0否 1是)',
    `sort`         INT          NOT NULL DEFAULT 0 COMMENT '排序',
    `remark`       VARCHAR(500) DEFAULT NULL COMMENT '备注',
    `deleted`      TINYINT      NOT NULL DEFAULT 0 COMMENT '逻辑删除(0正常 1删除)',
    `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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单权限表';

CREATE TABLE IF NOT EXISTS `sys_user_role` (
    `id`          BIGINT   NOT NULL COMMENT '主键ID',
    `user_id`     BIGINT   NOT NULL COMMENT '用户ID',
    `role_id`     BIGINT   NOT NULL COMMENT '角色ID',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_role` (`user_id`, `role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关联表';

CREATE TABLE IF NOT EXISTS `sys_role_menu` (
    `id`          BIGINT   NOT NULL COMMENT '主键ID',
    `role_id`     BIGINT   NOT NULL COMMENT '角色ID',
    `menu_id`     BIGINT   NOT NULL COMMENT '菜单ID',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_role_menu` (`role_id`, `menu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色菜单关联表';

-- 说明：
-- 1. 执行完本文件后重启应用
-- 2. 应用启动时会自动初始化 ADMIN 角色、基础菜单权限，并尝试将 admin 用户绑定为管理员
