-- oauth2 support
drop table if exists oauth_client;
create table oauth_client(
    id                        bigint(20) auto_increment comment '主键',
    client_id                 varchar(64)   not null comment 'Client ID',
    client_name               varchar(64)   not null comment 'Client Name',
    description               varchar(128)  not null comment 'Client Description',
    client_secret             varchar(128)  not null comment 'Client Secret',
    client_domain             varchar(128)  not null comment 'Client Domain',
    logout_url                varchar(128)  not null comment 'Logout URL',
    trusted                   int(10)       not null comment '是否可信任',
    create_time               datetime      not null comment '创建时间',
    update_time               datetime      not null comment '更新时间',
    primary key(id),
    unique key uk_client_id(client_id),
    unique key uk_client_name(client_name)
) ENGINE=InnoDB default charset=utf8 collate=utf8_unicode_ci comment 'oauth_client';

drop table if exists oauth_authorization_code;
create table oauth_authorization_code(
    id                        bigint(20) auto_increment comment '主键',
    client_id                 varchar(64)   not null comment 'Client ID',
    user_id                   bigint(20)    not null comment 'User ID',
    session_id                varchar(64)   not null comment 'Session ID',
    authorization_code        varchar(64)   not null comment 'Authorization Code',
    create_time               datetime      not null comment '创建时间',
    update_time               datetime      not null comment '更新时间',
    primary key(id),
    unique key uk_code(authorization_code)
) ENGINE=InnoDB default charset=utf8 collate=utf8_unicode_ci comment 'oauth_authorization_code';

drop table if exists oauth_access_token;
create table oauth_access_token(
    id                        bigint(20) auto_increment comment '主键',
    client_id                 varchar(64)   not null comment 'Client ID',
    user_id                   bigint(20)    not null comment 'User ID',
    session_id                varchar(64)   not null comment 'Session ID',
    access_token              varchar(64)   not null comment 'User ID',
    refresh_token             varchar(64)   not null comment 'User ID',
    token_type                varchar(32)   not null comment '',
    expires                   bigint(20)    not null comment '',
    scope                     varchar(64)   not null comment 'User ID',
    create_time               datetime      not null comment '创建时间',
    update_time               datetime      not null comment '更新时间',
    primary key(id)
) ENGINE=InnoDB default charset=utf8 collate=utf8_unicode_ci comment 'oauth_access_token';

-- 用户已登录的系统
drop table if exists oauth_login_client;
create table oauth_login_client(
    id                        bigint(20) auto_increment comment '主键',
    user_id                   bigint(20)    not null comment 'User ID',
    session_id                varchar(64)   not null comment 'Session ID',
    client_id                 varchar(64)   not null comment 'Client ID',
    create_time               datetime      not null comment '创建时间',
    update_time               datetime      not null comment '更新时间',
    primary key(id),
    unique key uk_user_client(user_id, session_id, client_id)
) ENGINE=InnoDB default charset=utf8 collate=utf8_unicode_ci comment 'oauth_login_client';

drop table if exists oauth_user_session;
create table oauth_user_session(
    id                   varchar(64)         not null,
    app_id               bigint(20)          not null,
    user_id              bigint(20)          not null,
    user_name            varchar(32)         not null,
    nick_name            varchar(32)         not null,
    client_id            varchar(64)         not null,
    remote_ip            varchar(64)         not null,
    last_access_time     datetime            not null,
    expires              datetime            not null,
    create_time          datetime            not null,
    update_time          datetime            not null,
    primary key (id)
) ENGINE=InnoDB default charset=utf8 collate=utf8_unicode_ci comment '用户会话';

