-- ============================================
-- QA 数据库 DDL (UUID 版本, 12张表)
-- 所有主键和外键均为 VARCHAR(64)
-- ============================================
USE mokeegatewayqa;

CREATE TABLE IF NOT EXISTS sys_system (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    system_code VARCHAR(64) NOT NULL,
    system_name VARCHAR(128) NOT NULL,
    front_url VARCHAR(256) NOT NULL,
    backend_url VARCHAR(256) NOT NULL,
    gateway_url VARCHAR(256) NOT NULL,
    icon VARCHAR(512) DEFAULT NULL,
    sort_order INT DEFAULT 0,
    status TINYINT NOT NULL DEFAULT 1,
    description VARCHAR(512) DEFAULT NULL,
    doc_token VARCHAR(128) DEFAULT NULL,
    created_by VARCHAR(64) DEFAULT NULL,
    updated_by VARCHAR(64) DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    is_deleted TINYINT NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    -- uk_system_code/uk_gateway_url 唯一性由应用层代码校验
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='业务系统表';

CREATE TABLE IF NOT EXISTS sys_api (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    system_id VARCHAR(64) NOT NULL,
    api_path VARCHAR(256) NOT NULL,
    api_method VARCHAR(10) NOT NULL,
    api_name VARCHAR(128) DEFAULT NULL,
    api_desc VARCHAR(512) DEFAULT NULL,
    status TINYINT NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_system_id (system_id),
    UNIQUE KEY uk_system_path_method (system_id, api_path, api_method)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统放行接口表';

CREATE TABLE IF NOT EXISTS sys_user (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    username VARCHAR(64) NOT NULL,
    password VARCHAR(256) NOT NULL,
    real_name VARCHAR(64) DEFAULT NULL,
    email VARCHAR(128) DEFAULT NULL,
    phone VARCHAR(32) DEFAULT NULL,
    avatar VARCHAR(512) DEFAULT NULL,
    post VARCHAR(64) DEFAULT NULL,
    status TINYINT NOT NULL DEFAULT 1 COMMENT '0-禁用 1-正常 2-锁定',
    lock_until DATETIME DEFAULT NULL,
    login_fail_count INT NOT NULL DEFAULT 0,
    last_login_ip VARCHAR(64) DEFAULT NULL,
    last_login_time DATETIME DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    is_deleted TINYINT NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    -- 用户名/邮箱唯一性由应用层代码校验（MyBatis-Plus @TableLogic 自动过滤已删除记录）
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';

CREATE TABLE IF NOT EXISTS sys_user_system (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    user_id VARCHAR(64) NOT NULL,
    system_id VARCHAR(64) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_user_system (user_id, system_id),
    KEY idx_system_id (system_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户系统绑定表';

CREATE TABLE IF NOT EXISTS sys_role (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    system_id VARCHAR(64) NOT NULL,
    role_name VARCHAR(64) NOT NULL,
    role_code VARCHAR(64) NOT NULL,
    description VARCHAR(256) DEFAULT NULL,
    status TINYINT NOT NULL DEFAULT 1,
    created_by VARCHAR(64) DEFAULT NULL,
    updated_by VARCHAR(64) DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    is_deleted TINYINT NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    -- uk_system_role_code 唯一性由应用层代码校验
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色表';

CREATE TABLE IF NOT EXISTS sys_user_role (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    user_id VARCHAR(64) NOT NULL,
    role_id VARCHAR(64) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_user_role (user_id, role_id),
    KEY idx_role_id (role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户角色表';

CREATE TABLE IF NOT EXISTS sys_menu (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    system_id VARCHAR(64) NOT NULL,
    parent_id VARCHAR(64) NOT NULL DEFAULT '0',
    menu_name VARCHAR(64) NOT NULL,
    menu_path VARCHAR(256) DEFAULT NULL,
    component VARCHAR(256) DEFAULT NULL,
    icon VARCHAR(128) DEFAULT NULL,
    menu_type TINYINT NOT NULL COMMENT '1-目录 2-菜单 3-按钮',
    permission VARCHAR(128) DEFAULT NULL COMMENT '权限标识',
    sort_order INT NOT NULL DEFAULT 0,
    status TINYINT NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_system_id (system_id),
    KEY idx_parent_id (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='菜单表';

CREATE TABLE IF NOT EXISTS sys_role_menu (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    role_id VARCHAR(64) NOT NULL,
    menu_id VARCHAR(64) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_role_menu (role_id, menu_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色菜单表';

CREATE TABLE IF NOT EXISTS sys_login_log (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    user_id VARCHAR(64) DEFAULT NULL,
    username VARCHAR(64) NOT NULL,
    real_name VARCHAR(64) DEFAULT NULL COMMENT '用户真实姓名',
    system_id VARCHAR(64) DEFAULT NULL,
    login_ip VARCHAR(64) DEFAULT NULL,
    user_agent VARCHAR(512) DEFAULT NULL,
    browser VARCHAR(64) DEFAULT NULL,
    os VARCHAR(64) DEFAULT NULL,
    status TINYINT NOT NULL COMMENT '0-失败 1-成功',
    fail_reason VARCHAR(256) DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_user_id (user_id),
    KEY idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='登录日志表';

CREATE TABLE IF NOT EXISTS sys_operation_log (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    user_id VARCHAR(64) DEFAULT NULL,
    username VARCHAR(64) DEFAULT NULL,
    system_id VARCHAR(64) DEFAULT NULL,
    module VARCHAR(64) DEFAULT NULL,
    action VARCHAR(32) DEFAULT NULL,
    request_url VARCHAR(256) DEFAULT NULL,
    request_method VARCHAR(10) DEFAULT NULL,
    request_body TEXT DEFAULT NULL,
    response_status INT DEFAULT NULL,
    response_body TEXT DEFAULT NULL COMMENT '接口返回报文（截断至2000字符）',
    error_msg TEXT DEFAULT NULL,
    cost_time INT DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_user_id (user_id),
    KEY idx_created_at (created_at),
    KEY idx_system_id (system_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='操作日志表';

CREATE TABLE IF NOT EXISTS sys_dict (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    dict_type VARCHAR(64) NOT NULL,
    dict_label VARCHAR(128) NOT NULL,
    dict_value VARCHAR(128) NOT NULL,
    sort_order INT DEFAULT 0,
    status TINYINT NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_type_value (dict_type, dict_value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据字典表';

CREATE TABLE IF NOT EXISTS sys_file (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    system_id VARCHAR(64) DEFAULT NULL COMMENT '所属系统ID',
    file_name VARCHAR(256) NOT NULL COMMENT '原始文件名',
    file_type VARCHAR(64) DEFAULT 'other' COMMENT '文件类型(icon/avatar/doc/other)',
    file_size BIGINT DEFAULT 0 COMMENT '文件大小(字节)',
    mime_type VARCHAR(128) DEFAULT NULL COMMENT 'MIME类型',
    md5 VARCHAR(64) DEFAULT NULL COMMENT '文件MD5摘要',
    upload_user_id VARCHAR(64) DEFAULT NULL COMMENT '上传用户ID',
    file_data LONGBLOB COMMENT '文件二进制数据',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上传时间',
    PRIMARY KEY (id),
    INDEX idx_system_id (system_id),
    INDEX idx_file_type (file_type),
    INDEX idx_upload_user_id (upload_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文件表';

CREATE TABLE IF NOT EXISTS sys_email_log (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    to_address VARCHAR(256) NOT NULL COMMENT '收件人',
    subject VARCHAR(512) NOT NULL COMMENT '主题',
    content TEXT COMMENT '邮件内容',
    content_type VARCHAR(16) NOT NULL DEFAULT 'html' COMMENT '格式 html/text',
    status TINYINT NOT NULL DEFAULT 0 COMMENT '0-失败 1-成功',
    error_msg VARCHAR(1024) DEFAULT NULL COMMENT '错误信息',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发送时间',
    PRIMARY KEY (id),
    KEY idx_created_at (created_at),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='邮件发送记录表';

CREATE TABLE IF NOT EXISTS sys_open_token_log (
    id VARCHAR(64) NOT NULL COMMENT 'UUID主键',
    system_id VARCHAR(64) DEFAULT NULL COMMENT '系统ID',
    system_code VARCHAR(64) NOT NULL COMMENT '系统编码',
    token_id VARCHAR(64) NOT NULL COMMENT 'Token唯一ID',
    token_preview VARCHAR(64) DEFAULT NULL COMMENT 'Token预览',
    acquired_at DATETIME DEFAULT NULL COMMENT '获取时间',
    consumed TINYINT NOT NULL DEFAULT 0 COMMENT '0-未消费 1-已消费',
    consumed_at DATETIME DEFAULT NULL COMMENT '消费时间',
    consume_api VARCHAR(256) DEFAULT NULL COMMENT '消费接口',
    consume_ip VARCHAR(64) DEFAULT NULL COMMENT '消费IP',
    PRIMARY KEY (id),
    KEY idx_token_id (token_id),
    KEY idx_system_code (system_code),
    KEY idx_acquired_at (acquired_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='开放API Token日志表';
