create table admin_data_auth
(
    id              int auto_increment
        primary key,
    env             varchar(255)  not null,
    sort            int default 0 not null,
    func_name       varchar(64) null,
    func_code       varchar(64)   not null,
    page_range_desc varchar(255) null,
    remark          varchar(255) null comment '备注',
    create_time     datetime      not null,
    update_time     datetime      not null,
    status          int default 0 not null
);

create index env_index
    on admin_data_auth (env);

create table admin_organ
(
    id          int auto_increment
        primary key,
    env         varchar(255) default '' not null comment '环境',
    name        varchar(255)            not null comment '部门名称',
    parent_id   int null comment '父节点',
    level       int          default 0  not null comment '层级，从1开始',
    sort        int          default 0  not null comment '排序，从小到大',
    hidden      int          default 0  not null comment '0:显示 1:隐藏',
    remark      varchar(255) null,
    create_time datetime                not null,
    update_time datetime                not null,
    status      int          default 0  not null
);

create index env_index
    on admin_organ (env);

create table admin_role_ref_data_auth
(
    id        int auto_increment
        primary key,
    role_id   int           not null,
    func_code varchar(64)   not null,
    auth_type int default 1 not null,
    constraint role_id_func_code_index
        unique (role_id, func_code)
);

create index role_id_index
    on admin_role_ref_data_auth (role_id);

create table promotion_advertiser
(
    advertiser_id      varchar(64)                           not null
        primary key,
    advertiser_name    varchar(255) null,
    advertiser_subject varchar(255) null,
    grant_source       varchar(64) null,
    rebate double default 1                 not null,
    remark             varchar(255) null,
    create_time        datetime    default CURRENT_TIMESTAMP not null,
    update_time        datetime    default CURRENT_TIMESTAMP not null,
    status             int         default 0                 not null,
    company            varchar(64)                           not null,
    app_code           varchar(64) default ''                not null,
    opt_user_id        bigint      default 0                 not null,
    auth_status        int         default 0                 not null,
    auth_time          datetime null,
    put_status         int         default 0                 not null,
    put_update_time    datetime null
);

create index app_code_index
    on promotion_advertiser (app_code);

create index company_index
    on promotion_advertiser (company);

create index opt_user_id_index
    on promotion_advertiser (opt_user_id);


alter table admin_user
    add organ_id int null after nickname;

alter table admin_role_ref_auth
    alter column env set default '';