-- Novalon管理系统完整数据库初始化脚本
-- 数据库: PostgreSQL

-- 用户表
CREATE TABLE IF NOT EXISTS users (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(100),
    phone VARCHAR(20),
    role_id BIGINT,
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 角色表
CREATE TABLE IF NOT EXISTS roles (
    id BIGSERIAL PRIMARY KEY,
    role_name VARCHAR(100) NOT NULL,
    role_key VARCHAR(100) NOT NULL UNIQUE,
    role_sort INTEGER DEFAULT 0,
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 菜单表
CREATE TABLE IF NOT EXISTS menus (
    id BIGSERIAL PRIMARY KEY,
    menu_name VARCHAR(50) NOT NULL,
    parent_id BIGINT DEFAULT 0,
    order_num INTEGER DEFAULT 0,
    menu_type VARCHAR(1) DEFAULT 'C',
    perms VARCHAR(100),
    component VARCHAR(200),
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 字典类型表
CREATE TABLE IF NOT EXISTS sys_dict_type (
    id BIGSERIAL PRIMARY KEY,
    dict_name VARCHAR(100) NOT NULL,
    dict_type VARCHAR(100) NOT NULL UNIQUE,
    status VARCHAR(1) DEFAULT '0',
    remark VARCHAR(500),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 字典数据表
CREATE TABLE IF NOT EXISTS sys_dict_data (
    id BIGSERIAL PRIMARY KEY,
    dict_sort INTEGER DEFAULT 0,
    dict_label VARCHAR(100) NOT NULL,
    dict_value VARCHAR(100) NOT NULL,
    dict_type VARCHAR(100) NOT NULL,
    css_class VARCHAR(100),
    list_class VARCHAR(100),
    is_default VARCHAR(1) DEFAULT 'N',
    status VARCHAR(1) DEFAULT '0',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 系统配置表
CREATE TABLE IF NOT EXISTS sys_config (
    id BIGSERIAL PRIMARY KEY,
    config_name VARCHAR(100) NOT NULL,
    config_key VARCHAR(100) NOT NULL UNIQUE,
    config_value VARCHAR(500) NOT NULL,
    config_type VARCHAR(1) DEFAULT 'N',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 登录日志表
CREATE TABLE IF NOT EXISTS sys_login_log (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(50),
    ip VARCHAR(50),
    location VARCHAR(255),
    browser VARCHAR(50),
    os VARCHAR(50),
    status VARCHAR(1),
    message VARCHAR(255),
    login_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 异常日志表
CREATE TABLE IF NOT EXISTS sys_exception_log (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(50),
    title VARCHAR(100),
    exception_name VARCHAR(100),
    method_name VARCHAR(100),
    method_params TEXT,
    exception_msg TEXT,
    exception_stack TEXT,
    ip VARCHAR(50),
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 操作日志表
CREATE TABLE IF NOT EXISTS sys_operation_log (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(50),
    operation VARCHAR(50),
    method VARCHAR(200),
    params TEXT,
    status VARCHAR(1),
    duration INTEGER,
    ip VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 系统公告表
CREATE TABLE IF NOT EXISTS sys_notice (
    id BIGSERIAL PRIMARY KEY,
    notice_title VARCHAR(100) NOT NULL,
    notice_type VARCHAR(1) DEFAULT '1',
    notice_content TEXT,
    status VARCHAR(1) DEFAULT '0',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 文件管理表
CREATE TABLE IF NOT EXISTS sys_file (
    id BIGSERIAL PRIMARY KEY,
    file_name VARCHAR(255) NOT NULL,
    file_path VARCHAR(500) NOT NULL,
    file_size VARCHAR(50),
    file_type VARCHAR(50),
    storage_type VARCHAR(20) DEFAULT 'local',
    create_by VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 用户消息表（消息推送用）
CREATE TABLE IF NOT EXISTS sys_user_message (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    title VARCHAR(100),
    content TEXT,
    message_type VARCHAR(1) DEFAULT '1',
    is_read VARCHAR(1) DEFAULT '0',
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
CREATE INDEX IF NOT EXISTS idx_users_role_id ON users(role_id);
CREATE INDEX IF NOT EXISTS idx_roles_role_key ON roles(role_key);
CREATE INDEX IF NOT EXISTS idx_menus_parent_id ON menus(parent_id);
CREATE INDEX IF NOT EXISTS idx_sys_dict_type_dict_type ON sys_dict_type(dict_type);
CREATE INDEX IF NOT EXISTS idx_sys_dict_data_dict_type ON sys_dict_data(dict_type);
CREATE INDEX IF NOT EXISTS idx_sys_config_config_key ON sys_config(config_key);
CREATE INDEX IF NOT EXISTS idx_sys_login_log_username ON sys_login_log(username);
CREATE INDEX IF NOT EXISTS idx_sys_login_log_login_time ON sys_login_log(login_time);
CREATE INDEX IF NOT EXISTS idx_sys_exception_log_create_time ON sys_exception_log(create_time);
CREATE INDEX IF NOT EXISTS idx_sys_operation_log_username ON sys_operation_log(username);
CREATE INDEX IF NOT EXISTS idx_sys_operation_log_created_at ON sys_operation_log(created_at);
CREATE INDEX IF NOT EXISTS idx_sys_notice_status ON sys_notice(status);
CREATE INDEX IF NOT EXISTS idx_sys_file_create_by ON sys_file(create_by);
CREATE INDEX IF NOT EXISTS idx_sys_user_message_user_id ON sys_user_message(user_id);
CREATE INDEX IF NOT EXISTS idx_sys_user_message_is_read ON sys_user_message(is_read);

-- 初始化默认管理员用户
INSERT INTO users (username, password, email, role_id, status) VALUES
('admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z2EHCDHhK6VbJyS0qE', 'admin@novalon.com', 1, 1)
ON CONFLICT (username) DO NOTHING;

-- 初始化默认角色
INSERT INTO roles (role_name, role_key, role_sort, status) VALUES
('超级管理员', 'admin', 1, 1),
('普通用户', 'user', 2, 1)
ON CONFLICT (role_key) DO NOTHING;

-- 初始化默认菜单
INSERT INTO menus (menu_name, parent_id, order_num, menu_type, perms, component, status) VALUES
('系统管理', 0, 1, 'M', '', '', 1),
('用户管理', 1, 1, 'C', 'system:user:list', 'system/UserManagement', 1),
('角色管理', 1, 2, 'C', 'system:role:list', 'system/RoleManagement', 1),
('菜单管理', 1, 3, 'C', 'system:menu:list', 'system/MenuManagement', 1),
('配置管理', 0, 2, 'M', '', '', 1),
('系统配置', 5, 1, 'C', 'system:config:list', 'config/ConfigManagement', 1),
('字典管理', 5, 2, 'C', 'system:dict:list', 'config/DictManagement', 1),
('文件管理', 0, 3, 'M', '', '', 1),
('文件列表', 8, 1, 'C', 'system:file:list', 'file/FileManagement', 1),
('通知管理', 0, 4, 'M', '', '', 1),
('通知公告', 10, 1, 'C', 'system:notice:list', 'notify/NoticeManagement', 1),
('审计管理', 0, 5, 'M', '', '', 1),
('登录日志', 12, 1, 'C', 'system:log:login', 'audit/LoginLog', 1),
('操作日志', 12, 2, 'C', 'system:log:operation', 'audit/OperationLog', 1)
ON CONFLICT DO NOTHING;

-- 初始化默认系统配置数据
INSERT INTO sys_config (config_name, config_key, config_value, config_type) VALUES
('系统名称', 'sys.system.name', 'Novalon管理系统', 'Y'),
('系统版本', 'sys.system.version', '1.0.0', 'Y'),
('文件上传最大大小', 'sys.file.maxSize', '10485760', 'Y'),
('文件上传允许类型', 'sys.file.allowedTypes', 'jpg,jpeg,png,pdf,doc,docx,xls,xlsx', 'Y'),
('会话超时时间(分钟)', 'sys.session.timeout', '30', 'Y'),
('密码最小长度', 'sys.password.minLength', '6', 'Y')
ON CONFLICT (config_key) DO NOTHING;

-- 初始化默认字典类型
INSERT INTO sys_dict_type (dict_name, dict_type, status, remark) VALUES
('用户性别', 'sys_user_sex', '0', '用户性别列表'),
('菜单状态', 'sys_show_hide', '0', '菜单显示状态'),
('系统开关', 'sys_normal_disable', '0', '系统开关状态'),
('任务状态', 'sys_job_status', '0', '定时任务状态'),
('任务分组', 'sys_job_group', '0', '定时任务分组'),
('系统是否', 'sys_yes_no', '0', '系统是否列表')
ON CONFLICT (dict_type) DO NOTHING;

-- 初始化默认字典数据
INSERT INTO sys_dict_data (dict_label, dict_value, dict_type, dict_sort, is_default, status) VALUES
('男', '0', 'sys_user_sex', 1, 'Y', '0'),
('女', '1', 'sys_user_sex', 2, 'N', '0'),
('未知', '2', 'sys_user_sex', 3, 'N', '0'),
('显示', '0', 'sys_show_hide', 1, 'Y', '0'),
('隐藏', '1', 'sys_show_hide', 2, 'N', '0'),
('正常', '0', 'sys_normal_disable', 1, 'Y', '0'),
('停用', '1', 'sys_normal_disable', 2, 'N', '0'),
('是', 'Y', 'sys_yes_no', 1, 'Y', '0'),
('否', 'N', 'sys_yes_no', 2, 'N', '0')
ON CONFLICT DO NOTHING;
