create table if not exists "user"
(
    id                  serial                                 not null,
    pep_user_id         integer                                not null,
    role                character varying[],
    correlation_project integer[],
    last_in_time        timestamp with time zone,
    in_times            integer                  default 0     not null,
    online_duration     integer                  default 0,
    last_in_address     varchar(256),
    disabled            boolean                  default false not null,
    deleted             boolean                  default false not null,
    update_time         timestamp with time zone default now(),
    constraint user_pk
        primary key (id)
);

comment on column "user".pep_user_id is '项企对应用户id';

comment on column "user".role is '角色 也对应权限 admin 管理员 / all 全部角色 / data_analyst 数据分析 / after_sale 售后运维 / resource_manage 资源管理 / customer_service 客户服务';

comment on column "user".correlation_project is '关联的项目管理的项目id';

comment on column "user".online_duration is '在线时长 单位 s';

comment on column "user".last_in_address is '上次登录地点';

create unique index if not exists user_id_uindex
    on "user" (id);

create table if not exists project_correlation
(
    id               serial                   not null,
    anxin_project_id integer[]                not null,
    pep_project_id   integer[]                not null,
    create_time      timestamp with time zone not null,
    create_user      integer                  not null,
    name             varchar(64),
    constraint project_correlation_pk
        primary key (id)
);

comment on table project_correlation is '安心云项目和项目管理项目关系映射绑定';

comment on column project_correlation.pep_project_id is '项目管理的项目id';

create unique index if not exists project_correlation_id_uindex
    on project_correlation (id);

create table if not exists action_log
(
    id            serial                   not null,
    user_id       integer,
    time          timestamp with time zone not null,
    action        varchar(256),
    expand_params jsonb,
    constraint action_log_pk
        primary key (id),
    constraint action_log_user_id_fk
        foreign key (user_id) references "user"
);

comment on table action_log is '动态日志';

comment on column action_log.action is '动态内容';

create unique index if not exists action_log_id_uindex
    on action_log (id);

create table if not exists quick_link
(
    id      serial       not null,
    user_id integer      not null,
    link    varchar(256) not null,
    constraint quick_link_pk
        primary key (id),
    constraint quick_link_user_id_fk
        foreign key (user_id) references "user"
);

comment on table quick_link is '对应我的常用工具功能';

create unique index if not exists quick_link_id_uindex
    on quick_link (id);

create table if not exists project_app
(
    id         serial        not null,
    name       varchar(32)   not null,
    url        varchar(1024) not null,
    project_id integer       not null,
    constraint project_app_pk
        primary key (id),
    constraint project_app_project_correlation_id_fk
        foreign key (project_id) references project_correlation
);

create table if not exists app_alarm
(
    id             serial                   not null,
    serial_number  varchar(32),
    project_app_id integer,
    app_domain     varchar(512),
    alarm_content  varchar(1024),
    create_time    timestamp with time zone not null,
    update_time    timestamp with time zone,
    confirm        varchar(1024),
    router         varchar(1024),
    status_code    varchar(8),
    constraint app_alarm_pk
        primary key (id),
    constraint app_alarm_project_app_id_fk
        foreign key (project_app_id) references project_app
);

comment on table app_alarm is '应用异常';

comment on column app_alarm.serial_number is '自定义编号';

comment on column app_alarm.project_app_id is '对应的项目id';

comment on column app_alarm.app_domain is '应用域名';

comment on column app_alarm.alarm_content is '告警信息';

comment on column app_alarm.confirm is '确认信息';

comment on column app_alarm.router is '路由';

comment on column app_alarm.status_code is '状态码';

create unique index if not exists app_alarm_id_uindex
    on app_alarm (id);

create table if not exists app_inspection
(
    id                serial                   not null,
    project_app_id    integer,
    create_time       timestamp with time zone not null,
    screenshot        character varying[],
    noted_pep_user_id integer,
    noted_time        timestamp with time zone,
    constraint app_inspection_pk
        primary key (id),
    constraint app_inspection_project_app_id_fk
        foreign key (project_app_id) references project_app
);

comment on table app_inspection is '应用巡检';

comment on column app_inspection.screenshot is '截图存储路径';

comment on column app_inspection.noted_pep_user_id is '核验人员';

create unique index if not exists app_inspection_id_uindex
    on app_inspection (id);

create unique index if not exists project_app_id_uindex
    on project_app (id);

create table if not exists alarm_push_config
(
    id                   serial                   not null,
    name                 varchar(32)              not null,
    pep_project_id       integer[]                not null,
    alarm_type           character varying[],
    receiver_pep_user_id integer[],
    time_type            character varying[],
    create_time          timestamp with time zone not null,
    create_user_id       integer                  not null,
    disable              boolean default false    not null,
    constraint alarm_push_config_pk
        primary key (id)
);

comment on table alarm_push_config is '告警推送配置';

comment on column alarm_push_config.alarm_type is '监听的告警类型';

comment on column alarm_push_config.receiver_pep_user_id is '接收人id 项企';

comment on column alarm_push_config.time_type is '通知时效';

create unique index if not exists alarm_push_config_id_uindex
    on alarm_push_config (id);

