-- OAuth客户端表
CREATE TABLE oauth_client (
    client_id VARCHAR(64) NOT NULL,
    client_secret VARCHAR(255),
    client_name VARCHAR(100),
    is_active VARCHAR(1) DEFAULT '1',
    create_by VARCHAR(64),
    create_time DATETIME,
    update_by VARCHAR(64),
    update_time DATETIME,
    PRIMARY KEY (client_id)
);

-- 添加表注释
COMMENT ON TABLE oauth_client IS 'OAuth客户端表';

-- 添加列注释
COMMENT ON COLUMN oauth_client.client_id IS '客户端id';
COMMENT ON COLUMN oauth_client.client_secret IS '客户端秘钥';
COMMENT ON COLUMN oauth_client.client_name IS '客户端名称';
COMMENT ON COLUMN oauth_client.is_active IS '是否激活有效（0否1是）';
COMMENT ON COLUMN oauth_client.create_by IS '创建人';
COMMENT ON COLUMN oauth_client.create_time IS '创建时间';
COMMENT ON COLUMN oauth_client.update_by IS '更新人';
COMMENT ON COLUMN oauth_client.update_time IS '更新时间';

-- OAuth授权码表
CREATE TABLE oauth_authorization_code (
    code_id VARCHAR(64) NOT NULL,
    authorization_code VARCHAR(255),
    client_id VARCHAR(64),
    user_id VARCHAR(64),
    user_name VARCHAR(100),
    scope VARCHAR(255),
    redirect_uri VARCHAR(500),
    state VARCHAR(100),
    grant_type VARCHAR(50),
    expires_at DATETIME,
    is_used VARCHAR(1) DEFAULT '0',
    is_active VARCHAR(1) DEFAULT '1',
    create_time DATETIME,
    update_time DATETIME,
    PRIMARY KEY (code_id)
);

-- 添加表注释
COMMENT ON TABLE oauth_authorization_code IS 'OAuth授权码表';

-- 添加列注释
COMMENT ON COLUMN oauth_authorization_code.code_id IS '授权码ID';
COMMENT ON COLUMN oauth_authorization_code.authorization_code IS '授权码';
COMMENT ON COLUMN oauth_authorization_code.client_id IS '客户端ID';
COMMENT ON COLUMN oauth_authorization_code.user_id IS '用户ID';
COMMENT ON COLUMN oauth_authorization_code.user_name IS '用户名';
COMMENT ON COLUMN oauth_authorization_code.scope IS '授权范围';
COMMENT ON COLUMN oauth_authorization_code.redirect_uri IS '重定向URI';
COMMENT ON COLUMN oauth_authorization_code.state IS '状态参数';
COMMENT ON COLUMN oauth_authorization_code.grant_type IS '授权类型';
COMMENT ON COLUMN oauth_authorization_code.expires_at IS '过期时间';
COMMENT ON COLUMN oauth_authorization_code.is_used IS '是否已使用（0否1是）';
COMMENT ON COLUMN oauth_authorization_code.is_active IS '是否有效（0否1是）';
COMMENT ON COLUMN oauth_authorization_code.create_time IS '创建时间';
COMMENT ON COLUMN oauth_authorization_code.update_time IS '更新时间';

-- OAuth访问令牌表
CREATE TABLE oauth_access_token (
    token_id VARCHAR(64) NOT NULL,
    access_token VARCHAR(500),
    refresh_token VARCHAR(500),
    client_id VARCHAR(64),
    user_id VARCHAR(64),
    user_name VARCHAR(100),
    scope VARCHAR(255),
    grant_type VARCHAR(50),
    expires_in DATETIME,
    refresh_expires_in DATETIME,
    is_active VARCHAR(1) DEFAULT '1',
    create_time DATETIME,
    update_time DATETIME,
    PRIMARY KEY (token_id)
);

-- 添加表注释
COMMENT ON TABLE oauth_access_token IS 'OAuth访问令牌表';

-- 添加列注释
COMMENT ON COLUMN oauth_access_token.token_id IS '访问令牌ID';
COMMENT ON COLUMN oauth_access_token.access_token IS '访问令牌';
COMMENT ON COLUMN oauth_access_token.refresh_token IS '刷新令牌';
COMMENT ON COLUMN oauth_access_token.client_id IS '客户端ID';
COMMENT ON COLUMN oauth_access_token.user_id IS '用户ID';
COMMENT ON COLUMN oauth_access_token.user_name IS '用户名';
COMMENT ON COLUMN oauth_access_token.scope IS '授权范围';
COMMENT ON COLUMN oauth_access_token.grant_type IS '授权类型';
COMMENT ON COLUMN oauth_access_token.expires_in IS '令牌过期时间';
COMMENT ON COLUMN oauth_access_token.refresh_expires_in IS '刷新令牌过期时间';
COMMENT ON COLUMN oauth_access_token.is_active IS '是否有效（0否1是）';
COMMENT ON COLUMN oauth_access_token.create_time IS '创建时间';
COMMENT ON COLUMN oauth_access_token.update_time IS '更新时间';

-- 为表添加索引
-- OAuth客户端表索引
CREATE INDEX idx_oauth_client_is_active ON oauth_client(is_active);
CREATE INDEX idx_oauth_client_create_time ON oauth_client(create_time);

-- OAuth授权码表索引
CREATE INDEX idx_oauth_auth_code_client_id ON oauth_authorization_code(client_id);
CREATE INDEX idx_oauth_auth_code_user_id ON oauth_authorization_code(user_id);
CREATE INDEX idx_oauth_auth_code_auth_code ON oauth_authorization_code(authorization_code);
CREATE INDEX idx_oauth_auth_code_expires_at ON oauth_authorization_code(expires_at);
CREATE INDEX idx_oauth_auth_code_is_used ON oauth_authorization_code(is_used);
CREATE INDEX idx_oauth_auth_code_is_active ON oauth_authorization_code(is_active);

-- OAuth访问令牌表索引
CREATE INDEX idx_oauth_access_token_client_id ON oauth_access_token(client_id);
CREATE INDEX idx_oauth_access_token_user_id ON oauth_access_token(user_id);
CREATE INDEX idx_oauth_access_token_access_token ON oauth_access_token(access_token);
CREATE INDEX idx_oauth_access_token_refresh_token ON oauth_access_token(refresh_token);
CREATE INDEX idx_oauth_access_token_expires_in ON oauth_access_token(expires_in);
CREATE INDEX idx_oauth_access_token_is_active ON oauth_access_token(is_active);