create table xxl_job_group
(
    id           bigint      not null
        constraint xxl_job_group_pkey
            primary key,
    app_name     varchar(64) not null,
    title        varchar(12) not null,
    address_type smallint    not null,
    address_list varchar(512)
);

comment on column xxl_job_group.app_name is '执行器AppName';

comment on column xxl_job_group.title is '执行器名称';

comment on column xxl_job_group.address_type is '执行器地址类型：0=自动注册、1=手动录入';

comment on column xxl_job_group.address_list is '执行器地址列表，多地址逗号分隔';

alter table xxl_job_group
    owner to postgres;

create table xxl_job_info
(
    id                        bigint       not null
        constraint xxl_job_info_pkey
            primary key,
    job_group                 bigint       not null,
    job_cron                  varchar(128) not null,
    job_desc                  varchar(255) not null,
    add_time                  timestamp(6),
    update_time               timestamp(6),
    author                    varchar(64),
    alarm_email               varchar(255),
    executor_route_strategy   varchar(50),
    executor_handler          varchar(255),
    executor_param            varchar(512),
    executor_block_strategy   varchar(50),
    executor_timeout          integer      not null,
    executor_fail_retry_count integer      not null,
    glue_type                 varchar(50)  not null,
    glue_source               text,
    glue_remark               varchar(128),
    glue_updatetime           timestamp(6),
    child_jobid               varchar(255),
    trigger_status            smallint     not null,
    trigger_last_time         bigint       not null,
    trigger_next_time         bigint       not null
);

comment on column xxl_job_info.job_group is '执行器主键ID';

comment on column xxl_job_info.job_cron is '任务执行CRON';

comment on column xxl_job_info.author is '作者';

comment on column xxl_job_info.alarm_email is '报警邮件';

comment on column xxl_job_info.executor_route_strategy is '执行器路由策略';

comment on column xxl_job_info.executor_handler is '执行器任务handler';

comment on column xxl_job_info.executor_param is '执行器任务参数';

comment on column xxl_job_info.executor_block_strategy is '阻塞处理策略';

comment on column xxl_job_info.executor_timeout is '任务执行超时时间，单位秒';

comment on column xxl_job_info.executor_fail_retry_count is '失败重试次数';

comment on column xxl_job_info.glue_type is 'GLUE类型';

comment on column xxl_job_info.glue_source is 'GLUE源代码';

comment on column xxl_job_info.glue_remark is 'GLUE备注';

comment on column xxl_job_info.glue_updatetime is 'GLUE更新时间';

comment on column xxl_job_info.child_jobid is '子任务ID，多个逗号分隔';

comment on column xxl_job_info.trigger_status is '调度状态：0-停止，1-运行';

comment on column xxl_job_info.trigger_last_time is '上次调度时间';

comment on column xxl_job_info.trigger_next_time is '下次调度时间';

alter table xxl_job_info
    owner to postgres;

create table xxl_job_lock
(
    lock_name varchar(50) not null
        constraint xxl_job_lock_pkey
            primary key
);

comment on column xxl_job_lock.lock_name is '锁名称';

alter table xxl_job_lock
    owner to postgres;

create table xxl_job_log
(
    id                        bigint   not null
        constraint xxl_job_log_pkey
            primary key,
    job_group                 bigint   not null,
    job_id                    bigint   not null,
    executor_address          varchar(255),
    executor_handler          varchar(255),
    executor_param            varchar(512),
    executor_sharding_param   varchar(20),
    executor_fail_retry_count integer  not null,
    trigger_time              timestamp(6),
    trigger_code              integer  not null,
    trigger_msg               text,
    handle_time               timestamp(6),
    handle_code               integer  not null,
    handle_msg                text,
    alarm_status              smallint not null
);

comment on column xxl_job_log.job_group is '执行器主键ID';

comment on column xxl_job_log.job_id is '任务，主键ID';

comment on column xxl_job_log.executor_address is '执行器地址，本次执行的地址';

comment on column xxl_job_log.executor_handler is '执行器任务handler';

comment on column xxl_job_log.executor_param is '执行器任务参数';

comment on column xxl_job_log.executor_sharding_param is '执行器任务分片参数，格式如 1/2';

comment on column xxl_job_log.executor_fail_retry_count is '失败重试次数';

comment on column xxl_job_log.trigger_time is '调度-时间';

comment on column xxl_job_log.trigger_code is '调度-结果';

comment on column xxl_job_log.trigger_msg is '调度-日志';

comment on column xxl_job_log.handle_time is '执行-时间';

comment on column xxl_job_log.handle_code is '执行-状态';

comment on column xxl_job_log.handle_msg is '执行-日志';

comment on column xxl_job_log.alarm_status is '告警状态：0-默认、1-无需告警、2-告警成功、3-告警失败';

alter table xxl_job_log
    owner to postgres;

create index "I_handle_code"
    on xxl_job_log (handle_code);

create index "I_trigger_time"
    on xxl_job_log (trigger_time);

create table xxl_job_log_report
(
    id            bigint  not null
        constraint xxl_job_log_report_pkey
            primary key,
    trigger_day   timestamp(6),
    running_count integer not null,
    suc_count     integer not null,
    fail_count    integer not null
);

comment on column xxl_job_log_report.trigger_day is '调度-时间';

comment on column xxl_job_log_report.running_count is '运行中-日志数量';

comment on column xxl_job_log_report.suc_count is '执行成功-日志数量';

comment on column xxl_job_log_report.fail_count is '执行失败-日志数量';

alter table xxl_job_log_report
    owner to postgres;

create index i_trigger_day
    on xxl_job_log_report (trigger_day);

create table xxl_job_logglue
(
    id          bigint       not null
        constraint xxl_job_logglue_pkey
            primary key,
    job_id      bigint       not null,
    glue_type   varchar(50),
    glue_source text,
    glue_remark varchar(128) not null,
    add_time    timestamp(6),
    update_time timestamp(6)
);

comment on column xxl_job_logglue.job_id is '任务，主键ID';

comment on column xxl_job_logglue.glue_type is 'GLUE类型';

comment on column xxl_job_logglue.glue_source is 'GLUE源代码';

comment on column xxl_job_logglue.glue_remark is 'GLUE备注';

alter table xxl_job_logglue
    owner to postgres;

create table xxl_job_registry
(
    id             bigint       not null
        constraint xxl_job_registry_pkey
            primary key,
    registry_group varchar(50)  not null,
    registry_key   varchar(255) not null,
    registry_value varchar(255) not null,
    update_time    timestamp(6)
);

alter table xxl_job_registry
    owner to postgres;

create index i_g_k_v
    on xxl_job_registry (registry_group, registry_key, registry_value);

create table xxl_job_user
(
    id         bigint      not null
        constraint xxl_job_user_pkey
            primary key,
    username   varchar(50) not null,
    password   varchar(50) not null,
    role       smallint    not null,
    permission varchar(255)
);

comment on column xxl_job_user.username is '账号';

comment on column xxl_job_user.password is '密码';

comment on column xxl_job_user.role is '角色：0-普通用户、1-管理员';

comment on column xxl_job_user.permission is '权限：执行器ID列表，多个逗号分割';

alter table xxl_job_user
    owner to postgres;

create index i_username
    on xxl_job_user (username);

create function upd_timestamp() returns trigger
    language plpgsql
as
$$
begin
    new.update_time = current_timestamp;
    return new;
end
$$;

alter function upd_timestamp() owner to postgres;

