create type enum_role_type as enum ('系统管理员', '数据消费者');

create type enum_metadata_type as enum ('目录', '库', '视图', '表', '字段', '索引', '外键', '主键', '唯一约束');

create type enum_storage_mode as enum ('全量', '增量');

create type enum_resource_type as enum ('库表', '接口', '文件', '数据服务');

create type enum_approve_state as enum ('审批中', '审批通过', '审批不通过');

create type enum_model_type as enum ('文件', '接口', '目录', '库', '视图', '表', '字段', '索引', '外键', '主键', '唯一约束');

create type enum_data_type as enum ('整型', '字符型', '布尔型');

create type enum_control_type as enum ('数字输入框', '文本框', '下拉框');

create table if not exists t_user_token
(
    token     varchar(64)                 not null
        constraint user_token_pk
            primary key,
    user_info jsonb                       not null,
    expired   timestamp(6) with time zone not null
);

create table if not exists t_user
(
    id       serial         not null
        constraint t_user_pk
            primary key,
    name     varchar(255),
    username varchar(255)   not null,
    password varchar(255)   not null,
    role     enum_role_type not null
);

comment on table t_user is '用户表';

comment on column t_user.id is '唯一标识';

comment on column t_user.name is '姓名';

comment on column t_user.username is '用户名';

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

comment on column t_user.role is '角色';

create unique index if not exists t_user_id_uindex
    on t_user (id);

create table if not exists t_resource_catalog
(
    id          serial       not null
        constraint t_resource_catalog_pk
            primary key,
    name        varchar(255) not null,
    code        varchar(255) not null,
    description varchar(255),
    parent      integer
);

comment on table t_resource_catalog is '资源目录表';

comment on column t_resource_catalog.id is '唯一标识';

comment on column t_resource_catalog.name is '名称';

comment on column t_resource_catalog.code is '代码';

comment on column t_resource_catalog.description is '描述';

comment on column t_resource_catalog.parent is '父级目录';

create unique index if not exists t_resource_catalog_id_uindex
    on t_resource_catalog (id);

create table if not exists t_tag_set
(
    id   serial       not null
        constraint t_tag_set_pk
            primary key,
    name varchar(255) not null
);

comment on table t_tag_set is '标签集信息表';

comment on column t_tag_set.id is '唯一标识';

comment on column t_tag_set.name is '标签集名称';

create unique index if not exists t_tag_set_id_uindex
    on t_tag_set (id);

create table if not exists t_tag
(
    id      serial       not null
        constraint t_tag_pk
            primary key,
    name    varchar(255) not null,
    tag_set integer      not null
        constraint t_tag_t_tag_set_id_fk
            references t_tag_set
);

comment on table t_tag is '标签表';

comment on column t_tag.id is '唯一标识';

comment on column t_tag.name is '标签名称';

comment on column t_tag.tag_set is '归属的标签集';

create unique index if not exists t_tag_id_uindex
    on t_tag (id);

create table if not exists t_metadata_database
(
    id          serial                   not null
        constraint t_metadata_database_pk
            primary key,
    name        varchar(255)             not null,
    code        varchar(255)             not null,
    type        enum_metadata_type       not null,
    description varchar(255),
    attributes  jsonb,
    catalog     integer
        constraint t_metadata_database_t_resource_catalog_id_fk
            references t_resource_catalog,
    parent      integer,
    create_by   integer                  not null
        constraint t_metadata_database_t_user_id_fk
            references t_user,
    create_at   timestamp with time zone not null,
    update_at   integer
);

comment on table t_metadata_database is '库表元数据表';

comment on column t_metadata_database.id is '唯一标识';

comment on column t_metadata_database.name is '名称';

comment on column t_metadata_database.code is '代码';

comment on column t_metadata_database.type is '元数据类型';

comment on column t_metadata_database.description is '描述（详情）';

comment on column t_metadata_database.attributes is '属性';

comment on column t_metadata_database.catalog is '资源目录';

comment on column t_metadata_database.parent is '父级元数据';

comment on column t_metadata_database.create_by is '创建者';

comment on column t_metadata_database.create_at is '创建时间';

comment on column t_metadata_database.update_at is '修改时间';

create unique index if not exists t_metadata_database_id_uindex
    on t_metadata_database (id);

create table if not exists t_meta_model
(
    id             serial            not null
        constraint t_meta_model_pk
            primary key,
    model_type     enum_model_type   not null,
    attribute_name varchar(255)      not null,
    attribute_code varchar(255)      not null,
    data_type      enum_data_type    not null,
    length         integer,
    control        enum_control_type not null,
    nullable       boolean           not null,
    read_only      boolean           not null,
    description    varchar(255)
);

comment on table t_meta_model is '元模型表';

comment on column t_meta_model.id is '唯一标识';

comment on column t_meta_model.model_type is '元模型类型';

comment on column t_meta_model.attribute_name is '属性名称';

comment on column t_meta_model.attribute_code is '属性代码';

comment on column t_meta_model.data_type is '数据类型';

comment on column t_meta_model.length is '长度';

comment on column t_meta_model.control is '输入控件';

comment on column t_meta_model.nullable is '是否允许为空';

comment on column t_meta_model.read_only is '是否只读';

comment on column t_meta_model.description is '描述';

create unique index if not exists t_meta_model_id_uindex
    on t_meta_model (id);

create table if not exists t_tag_database
(
    id       serial  not null
        constraint t_tag_database_pk
            primary key,
    tag      integer not null
        constraint t_tag_database_t_tag_id_fk
            references t_tag,
    database integer not null
        constraint t_tag_database_t_metadata_database_id_fk
            references t_metadata_database
);

comment on table t_tag_database is '库表元数据标签表';

comment on column t_tag_database.id is '唯一标识';

comment on column t_tag_database.tag is '标签';

comment on column t_tag_database.database is '库表元数据';

create unique index if not exists t_tag_database_id_uindex
    on t_tag_database (id);

create table if not exists t_metadata_file
(
    id          serial                   not null
        constraint t_metadata_file_pk
            primary key,
    name        varchar(255)             not null,
    type        varchar(10)              not null,
    size        integer                  not null,
    description varchar(255),
    attributes  jsonb,
    catalog     integer                  not null
        constraint t_metadata_file_t_resource_catalog_id_fk
            references t_resource_catalog,
    create_by   integer                  not null
        constraint t_metadata_file_t_user_id_fk
            references t_user,
    create_at   timestamp with time zone not null,
    update_at   timestamp with time zone
);

comment on table t_metadata_file is '文件元数据';

comment on column t_metadata_file.id is '唯一标识';

comment on column t_metadata_file.name is '文件名称';

comment on column t_metadata_file.type is '文件类型';

comment on column t_metadata_file.size is '文件大小';

comment on column t_metadata_file.description is '文件描述';

comment on column t_metadata_file.attributes is '属性';

comment on column t_metadata_file.catalog is '资源目录';

comment on column t_metadata_file.create_by is '创建者';

comment on column t_metadata_file.create_at is '创建时间';

comment on column t_metadata_file.update_at is '修改时间';

create unique index if not exists t_metadata_file_id_uindex
    on t_metadata_file (id);

create table if not exists t_tag_file
(
    id   serial  not null
        constraint t_tag_file_pk
            primary key,
    tag  integer not null
        constraint t_tag_file_t_tag_id_fk
            references t_tag,
    file integer not null
        constraint t_tag_file_t_metadata_file_id_fk
            references t_metadata_file
);

comment on table t_tag_file is '文件元数据标签表';

comment on column t_tag_file.id is '唯一标识';

comment on column t_tag_file.tag is '标签';

comment on column t_tag_file.file is '文件元数据';

create unique index if not exists t_tag_file_id_uindex
    on t_tag_file (id);

create table if not exists t_metadata_restapi
(
    id          serial                   not null
        constraint t_metadata_restapi_pk
            primary key,
    name        varchar(255)             not null,
    url         varchar(255)             not null,
    method      varchar(10)              not null,
    query_param varchar(255),
    body_param  jsonb,
    return      jsonb,
    enabled     boolean,
    attributes  jsonb,
    catalog     integer                  not null
        constraint t_metadata_restapi_t_resource_catalog_id_fk
            references t_resource_catalog,
    create_by   integer                  not null
        constraint t_metadata_restapi_t_user_id_fk
            references t_user,
    create_at   timestamp with time zone not null,
    update_at   timestamp with time zone
);

comment on table t_metadata_restapi is '接口元数据表';

comment on column t_metadata_restapi.id is '唯一标识';

comment on column t_metadata_restapi.name is '接口名称';

comment on column t_metadata_restapi.url is '接口路由';

comment on column t_metadata_restapi.method is '请求方法';

comment on column t_metadata_restapi.query_param is '查询参数';

comment on column t_metadata_restapi.body_param is '请求实体';

comment on column t_metadata_restapi.return is '接口返回值';

comment on column t_metadata_restapi.enabled is '是否已启用';

comment on column t_metadata_restapi.attributes is '属性';

comment on column t_metadata_restapi.catalog is '资源目录';

comment on column t_metadata_restapi.create_by is '创建者';

comment on column t_metadata_restapi.create_at is '创建时间';

comment on column t_metadata_restapi.update_at is '修改时间';

create unique index if not exists t_metadata_restapi_id_uindex
    on t_metadata_restapi (id);

create table if not exists t_tag_restapi
(
    id      serial  not null
        constraint t_tag_restapi_pk
            primary key,
    tag     integer not null
        constraint t_tag_restapi_t_tag_id_fk
            references t_tag,
    restapi integer not null
        constraint t_tag_restapi_t_metadata_restapi_id_fk
            references t_metadata_restapi
);

comment on table t_tag_restapi is '接口元数据标签表';

comment on column t_tag_restapi.id is '唯一标识';

comment on column t_tag_restapi.tag is '标签';

comment on column t_tag_restapi.restapi is '接口元数据';

create unique index if not exists t_tag_restapi_id_uindex
    on t_tag_restapi (id);

create table if not exists t_business_metadata_database
(
    id                serial                   not null
        constraint t_business_metadata_database_pk
            primary key,
    resource_name     varchar(255)             not null,
    resource_abstract varchar(255)             not null,
    resource_provider varchar(255)             not null,
    resource_category varchar(255)             not null,
    resource_id       varchar(255)             not null,
    metadata_id       varchar(255)             not null,
    create_by         integer                  not null
        constraint t_business_metadata_database_t_user_id_fk
            references t_user,
    create_at         timestamp with time zone not null,
    update_at         timestamp with time zone,
    metadata_database integer                  not null
        constraint t_business_metadata_database_t_metadata_database_id_fk
            references t_metadata_database
);

comment on table t_business_metadata_database is '库表业务元数据';

comment on column t_business_metadata_database.id is '唯一标识';

comment on column t_business_metadata_database.resource_name is '信息资源名称';

comment on column t_business_metadata_database.resource_abstract is '信息资源摘要';

comment on column t_business_metadata_database.resource_provider is '信息资源提供方';

comment on column t_business_metadata_database.resource_category is '信息资源分类';

comment on column t_business_metadata_database.resource_id is '信息资源标识符';

comment on column t_business_metadata_database.metadata_id is '元数据标识符';

comment on column t_business_metadata_database.create_by is '创建者';

comment on column t_business_metadata_database.create_at is '创建时间';

comment on column t_business_metadata_database.update_at is '修改时间';

comment on column t_business_metadata_database.metadata_database is '库表元数据';

create unique index if not exists t_business_metadata_database_id_uindex
    on t_business_metadata_database (id);

create table if not exists t_business_metadata_file
(
    id                serial                   not null
        constraint t_business_metadata_file_pk
            primary key,
    resource_name     varchar(255)             not null,
    resource_abstract varchar(255)             not null,
    resource_provider varchar(255)             not null,
    resource_category varchar(255)             not null,
    resource_id       varchar(255)             not null,
    metadata_id       varchar(255)             not null,
    create_by         integer                  not null
        constraint t_business_metadata_file_t_user_id_fk
            references t_user,
    create_at         timestamp with time zone not null,
    update_at         timestamp with time zone,
    metadata_file     integer                  not null
        constraint t_business_metadata_file_t_metadata_file_id_fk
            references t_metadata_file
);

comment on table t_business_metadata_file is '文件业务元数据';

comment on column t_business_metadata_file.id is '唯一标识';

comment on column t_business_metadata_file.resource_name is '信息资源名称';

comment on column t_business_metadata_file.resource_abstract is '信息资源摘要';

comment on column t_business_metadata_file.resource_provider is '信息资源提供方';

comment on column t_business_metadata_file.resource_category is '信息资源分类';

comment on column t_business_metadata_file.resource_id is '信息资源标识符';

comment on column t_business_metadata_file.metadata_id is '元数据标识符';

comment on column t_business_metadata_file.create_by is '创建者';

comment on column t_business_metadata_file.create_at is '创建时间';

comment on column t_business_metadata_file.update_at is '修改时间';

comment on column t_business_metadata_file.metadata_file is '文件元数据';

create unique index if not exists t_business_metadata_file_id_uindex
    on t_business_metadata_file (id);

create table if not exists t_business_metadata_restapi
(
    id                serial                   not null
        constraint t_business_metadata_restapi_pk
            primary key,
    resource_name     varchar(255)             not null,
    resource_abstract varchar(255)             not null,
    resource_provider varchar(255)             not null,
    resource_category varchar(255)             not null,
    resource_id       varchar(255)             not null,
    metadata_id       varchar(255)             not null,
    create_by         integer                  not null
        constraint t_business_metadata_restapi_t_user_id_fk
            references t_user,
    create_at         timestamp with time zone not null,
    update_at         timestamp with time zone,
    metadata_restapi  integer                  not null
        constraint t_business_metadata_restapi_t_metadata_restapi_id_fk
            references t_metadata_restapi
);

comment on table t_business_metadata_restapi is '接口业务元数据表';

comment on column t_business_metadata_restapi.id is '唯一标识';

comment on column t_business_metadata_restapi.resource_name is '信息资源名称';

comment on column t_business_metadata_restapi.resource_abstract is '信息资源摘要';

comment on column t_business_metadata_restapi.resource_provider is '信息资源提供方';

comment on column t_business_metadata_restapi.resource_category is '信息资源分类';

comment on column t_business_metadata_restapi.resource_id is '信息资源标识符';

comment on column t_business_metadata_restapi.metadata_id is '元数据标识符';

comment on column t_business_metadata_restapi.create_by is '创建者';

comment on column t_business_metadata_restapi.create_at is '创建时间';

comment on column t_business_metadata_restapi.update_at is '修改时间';

comment on column t_business_metadata_restapi.metadata_restapi is '接口元数据';

create unique index if not exists t_business_metadata_restapi_id_uindex
    on t_business_metadata_restapi (id);

create table if not exists t_adapter
(
    id              serial       not null
        constraint t_adapter_pk
            primary key,
    adapter_name    varchar(255) not null,
    adapter_version varchar(10)  not null,
    tool_name       varchar(255) not null,
    description     varchar(255),
    config          jsonb        not null
);

comment on table t_adapter is '适配器信息表';

comment on column t_adapter.id is 'ID唯一标识';

comment on column t_adapter.adapter_name is '适配器名称';

comment on column t_adapter.adapter_version is '适配器版本';

comment on column t_adapter.tool_name is '工具名称';

comment on column t_adapter.description is '描述';

comment on column t_adapter.config is '配置信息
多个参数信息，每个参数组成：
{param:参数名,
title:标题,
default:默认值,
required:是否必填,
description:描述}';

create unique index if not exists t_adapter_id_uindex
    on t_adapter (id);

create table if not exists t_data_source
(
    id          serial                not null
        constraint t_data_source_pk
            primary key,
    name        varchar(255)          not null,
    audited     boolean default false not null,
    adapter     integer               not null
        constraint t_data_source_t_adapter_id_fk
            references t_adapter,
    mount_path  integer               not null
        constraint t_data_source_t_resource_catalog_id_fk
            references t_resource_catalog,
    description varchar(255),
    config      jsonb                 not null
);

comment on table t_data_source is '数据源信息表';

comment on column t_data_source.id is 'ID唯一标识';

comment on column t_data_source.name is '数据源名称';

comment on column t_data_source.audited is '是否审核';

comment on column t_data_source.adapter is '适配器';

comment on column t_data_source.mount_path is '数据源挂载路径';

comment on column t_data_source.description is '描述';

comment on column t_data_source.config is '数据源参数配置';

create unique index if not exists t_data_source_id_uindex
    on t_data_source (id);

create table if not exists t_acquisition_task
(
    id               serial               not null
        constraint t_acquisition_task_pk
            primary key,
    task_name        varchar(255)         not null,
    data_source      integer              not null
        constraint t_acquisition_task_t_data_source_id_fk
            references t_data_source,
    storage_strategy enum_storage_mode    not null,
    auto_released    boolean default true not null,
    description      varchar(255),
    enabled          boolean              not null,
    cron             varchar(255)         not null,
    retried          boolean              not null,
    retry_count      integer,
    retry_time       integer
);

comment on table t_acquisition_task is '采集任务表';

comment on column t_acquisition_task.id is 'ID唯一标识';

comment on column t_acquisition_task.task_name is '任务名称';

comment on column t_acquisition_task.data_source is '数据源';

comment on column t_acquisition_task.storage_strategy is '入库策略';

comment on column t_acquisition_task.auto_released is '是否自动发布';

comment on column t_acquisition_task.description is '描述';

comment on column t_acquisition_task.enabled is '是否已启用';

comment on column t_acquisition_task.cron is 'cron表达式';

comment on column t_acquisition_task.retried is '是否重试';

comment on column t_acquisition_task.retry_count is '重试次数';

comment on column t_acquisition_task.retry_time is '重试间隔时间，单位：分钟';

create unique index if not exists t_acquisition_task_id_uindex
    on t_acquisition_task (id);

create table if not exists t_acquisition_log
(
    id         serial                   not null
        constraint t_acquisition_log_pk
            primary key,
    task       integer                  not null
        constraint t_acquisition_log_t_acquisition_task_id_fk
            references t_acquisition_task,
    success    boolean                  not null,
    start_time timestamp with time zone not null,
    end_time   timestamp with time zone not null,
    details    varchar(255)             not null
);

comment on table t_acquisition_log is '采集日志表';

comment on column t_acquisition_log.id is 'ID唯一标识';

comment on column t_acquisition_log.task is '采集任务';

comment on column t_acquisition_log.success is '是否采集成功';

comment on column t_acquisition_log.start_time is '任务开始时间';

comment on column t_acquisition_log.end_time is '任务结束时间';

comment on column t_acquisition_log.details is '采集详情';

create unique index if not exists t_acquisition_log_id_uindex
    on t_acquisition_log (id);

create table if not exists t_resource_consumption
(
    id              serial                   not null
        constraint t_resource_consumption_pk
            primary key,
    resource_name   varchar(255)             not null,
    resource_type   enum_resource_type       not null,
    apply_by        integer                  not null
        constraint t_resource_consumption_t_user_id_fk
            references t_user,
    apply_at        timestamp with time zone not null,
    requirements    varchar(255)             not null,
    approve_state   enum_approve_state       not null,
    approve_by      integer
        constraint t_resource_consumption_t_user_id_fk_2
            references t_user,
    approve_at      timestamp with time zone,
    approve_remarks varchar(255),
    token           varchar(255)
);

comment on table t_resource_consumption is '资源消费表';

comment on column t_resource_consumption.id is 'ID唯一标识';

comment on column t_resource_consumption.resource_name is '资源名称';

comment on column t_resource_consumption.resource_type is '资源类型';

comment on column t_resource_consumption.apply_by is '申请人';

comment on column t_resource_consumption.apply_at is '申请时间';

comment on column t_resource_consumption.requirements is '需求描述';

comment on column t_resource_consumption.approve_state is '审批状态';

comment on column t_resource_consumption.approve_by is '审批人';

comment on column t_resource_consumption.approve_at is '审批时间';

comment on column t_resource_consumption.approve_remarks is '审批意见';

comment on column t_resource_consumption.token is '令牌';

create unique index if not exists t_resource_consumption_id_uindex
    on t_resource_consumption (id);

create table if not exists t_alarms
(
    id      serial                   not null
        constraint t_alarms_pk
            primary key,
    content varchar(255)             not null,
    level   integer                  not null,
    time    timestamp with time zone not null
);

comment on table t_alarms is '预警表';

comment on column t_alarms.id is 'ID唯一标识';

comment on column t_alarms.content is '预警内容';

comment on column t_alarms.level is '预警等级';

comment on column t_alarms.time is '预警时间';

create unique index if not exists t_alarms_id_uindex
    on t_alarms (id);

create table if not exists t_resource_access
(
    id             serial                   not null
        constraint t_resource_access_pk
            primary key,
    resource_name  varchar(255)             not null,
    resource_desc  varchar(255),
    resource_count integer                  not null,
    resource_type  enum_resource_type       not null,
    "user"         integer                  not null
        constraint t_resource_access_t_user_id_fk
            references t_user,
    time           timestamp with time zone not null
);

comment on table t_resource_access is '资源访问记录表';

comment on column t_resource_access.id is 'ID唯一标识';

comment on column t_resource_access.resource_name is '资源名称';

comment on column t_resource_access.resource_desc is '资源描述';

comment on column t_resource_access.resource_count is '资源数';

comment on column t_resource_access.resource_type is '资源类型';

comment on column t_resource_access."user" is '访问用户';

comment on column t_resource_access.time is '访问时间';

create unique index if not exists t_resource_access_id_uindex
    on t_resource_access (id);