-- 用户管理系统数据库迁移
-- 版本: V38
-- 描述: 创建系统用户表 (sys_user) 用于管理后台用户持久化
-- 创建时间: 2026-03-12

-- 1. 系统用户表（sys_user）
CREATE TABLE sys_user (
    id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    real_name VARCHAR(100),
    email VARCHAR(100),
    phone VARCHAR(20),
    department_id BIGINT,
    status VARCHAR(20) DEFAULT 'ACTIVE',
    is_certified BOOLEAN DEFAULT FALSE,
    is_blacklisted BOOLEAN DEFAULT FALSE,
    last_login_at TIMESTAMP,
    created_by BIGINT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 创建索引
CREATE INDEX idx_sys_user_username ON sys_user(username);
CREATE INDEX idx_sys_user_status ON sys_user(status);
CREATE INDEX idx_sys_user_department ON sys_user(department_id);

-- 2. 用户认证Token表（sys_user_token）
CREATE TABLE sys_user_token (
    id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    user_id BIGINT NOT NULL,
    token VARCHAR(255) NOT NULL UNIQUE,
    expires_at TIMESTAMP NOT NULL,
    ip_address VARCHAR(50),
    user_agent VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_user_token_user ON sys_user_token(user_id);
CREATE INDEX idx_user_token_token ON sys_user_token(token);
CREATE INDEX idx_user_token_expires ON sys_user_token(expires_at);
