-- ============================================================================================
-- 网关组件
DROP TABLE IF EXISTS "public"."gateway_component";
create table public.gateway_component
(
    id          bigint primary key,
    name        varchar(100),
    type        varchar(255),
    description varchar(500)
);

comment on table public.gateway_component is '网关组件';
comment on column public.gateway_component.id is '主键';
comment on column public.gateway_component.name is '组件名称';
comment on column public.gateway_component.type is '组件类型';
comment on column public.gateway_component.description is '组件描述';

-- 网关组件配置
DROP TABLE IF EXISTS "public"."gateway_component_config";
create table public.gateway_component_config
(
    id               bigint not null
        primary key,
    cmpt_id          bigint,
    cmpt_type        varchar(100),
    cmpt_cfg_name    varchar(255),
    cmpt_cfg_context text,
    max_instance_num integer,
    active           boolean,
    tenant_code      bigint,
    company_id       bigint,
    dept_id          bigint,
    creator          bigint,
    create_date      bigint,
    updater          bigint,
    update_date      bigint
);

comment on table public.gateway_component_config is '网关组件配置';
comment on column public.gateway_component_config.id is '主键';
comment on column public.gateway_component_config.cmpt_id is '组件id';
comment on column public.gateway_component_config.cmpt_type is '组件类型';
comment on column public.gateway_component_config.cmpt_cfg_name is '组件配置名称';
comment on column public.gateway_component_config.cmpt_cfg_context is '组件配置内容';
comment on column public.gateway_component_config.max_instance_num is '最大实例运行数';
comment on column public.gateway_component_config.active is '是否启用';

-- 网关组件配置详情（数据主题、通道、寄存器地址位等）
DROP TABLE IF EXISTS "public"."gateway_component_config_detail";
create table public.gateway_component_config_detail
(
    id             bigint primary key,
    cfg_id         bigint,
    detail_context varchar(500),
    debug          boolean,
    read_write     integer,
    remark         varchar(2000),
    creator        bigint,
    create_date    bigint,
    updater        bigint,
    update_date    bigint
);

comment on table public.gateway_component_config_detail is '网关配置详情（数据主题、通道、寄存器地址位等）';
comment on column public.gateway_component_config_detail.id is '主键';
comment on column public.gateway_component_config_detail.cfg_id is '通讯配置主键';
comment on column public.gateway_component_config_detail.detail_context is '配置详情';
comment on column public.gateway_component_config_detail.debug is '是否调试';
comment on column public.gateway_component_config_detail.read_write is '类型(0-读; 1-写; 2-读写)';
comment on column public.gateway_component_config_detail.remark is '备注';
comment on column public.gateway_component_config_detail.creator is '创建者';
comment on column public.gateway_component_config_detail.create_date is '创建时间';
comment on column public.gateway_component_config_detail.updater is '更新者';
comment on column public.gateway_component_config_detail.update_date is '更新时间';

-- 网关组件实例
DROP TABLE IF EXISTS "public"."gateway_component_instance";
create table public.gateway_component_instance
(
    id              bigint primary key,
    cmpt_id         bigint,
    cmpt_type       varchar(255),
    cmpt_cfg_id     bigint,
    instance_ip     varchar(50),
    instance_name   varchar(100),
    instance_status varchar(100),
    start_time      bigint,
    dead_dur        bigint
);

comment on table public.gateway_component_instance is '网关组件实例';
comment on column public.gateway_component_instance.id is '主键';
comment on column public.gateway_component_instance.cmpt_id is '组件id';
comment on column public.gateway_component_instance.cmpt_type is '组件类型';
comment on column public.gateway_component_instance.cmpt_cfg_id is '组件配置id';
comment on column public.gateway_component_instance.instance_ip is '实例ip';
comment on column public.gateway_component_instance.instance_name is '实例名称';
comment on column public.gateway_component_instance.instance_status is '实例状态：STARTING, WAITING, RUNNING, RESTARTING, STOP';
comment on column public.gateway_component_instance.start_time is '实例启动时间';
comment on column public.gateway_component_instance.dead_dur is '实例宕机时长';

-- 网关组件数据接入日志
DROP TABLE IF EXISTS "public"."gateway_component_log";
create table public.gateway_component_log
(
    id                 bigint not null
        primary key,
    cmpt_cfg_id        bigint,
    cmpt_cfg_detail_id bigint,
    addr               varchar(255),
    input_msg          varchar,
    output_msg         varchar,
    error_msg          varchar,
    origin             varchar(50),
    debug              boolean,
    creator            bigint,
    create_date        bigint,
    updater            bigint,
    update_date        bigint
);

comment on table public.gateway_component_log is '网关组件数据接入日志';
comment on column public.gateway_component_log.id is '主键';
comment on column public.gateway_component_log.cmpt_cfg_id is '组件配置id';
comment on column public.gateway_component_log.cmpt_cfg_detail_id is '组件配置详情id';
comment on column public.gateway_component_log.addr is '通道';
comment on column public.gateway_component_log.input_msg is '原始数据';
comment on column public.gateway_component_log.output_msg is '转换后数据';
comment on column public.gateway_component_log.error_msg is '错误消息';
comment on column public.gateway_component_log.origin is '数据来源';
comment on column public.gateway_component_log.debug is '是否调试模式日志';
comment on column public.gateway_component_log.creator is '创建者';
comment on column public.gateway_component_log.create_date is '创建时间';
comment on column public.gateway_component_log.updater is '更新者';
comment on column public.gateway_component_log.update_date is '更新时间';

-- 网关设备控制
DROP TABLE IF EXISTS "public"."gateway_device_control";
create table public.gateway_device_control
(
    id                 bigint not null,
    name               varchar(255),
    thing_name         varchar(50),
    thing_code         varchar(50),
    attr_name          varchar(50),
    attr_code          varchar(50),
    cmpt_type          varchar(50),
    cmpt_cfg_id        bigint,
    cmpt_cfg_detail_id bigint,
    query_msg          varchar(5000),
    ctl_body           varchar,
    ctl_type           varchar(5),
    ctl_json           varchar(10240),
    user_list          varchar(2550),
    tenant_code        bigint,
    company_id         bigint,
    dept_id            bigint,
    creator            bigint,
    create_date        bigint,
    updater            bigint,
    update_date        bigint
);

comment on table public.gateway_device_control is '设备控制';
comment on column public.gateway_device_control.id is '主键';
comment on column public.gateway_device_control.name is '控制名称';
comment on column public.gateway_device_control.thing_name is '物名称';
comment on column public.gateway_device_control.thing_code is '物编码';
comment on column public.gateway_device_control.attr_name is '属性名称';
comment on column public.gateway_device_control.attr_code is '属性编码';
comment on column public.gateway_device_control.cmpt_cfg_id is '组件配置id';
comment on column public.gateway_device_control.cmpt_cfg_detail_id is '组件配置详情id';
comment on column public.gateway_device_control.query_msg is '查询参数';
comment on column public.gateway_device_control.ctl_body is '控制逻辑';
comment on column public.gateway_device_control.ctl_type is '控制类型，1图，2切换，3数值';
comment on column public.gateway_device_control.ctl_json is ' 控制样式json';
comment on column public.gateway_device_control.user_list is '用户列表，多个用户以英文逗号分割';
comment on column public.gateway_device_control.tenant_code is '所属企业(租户code)';
comment on column public.gateway_device_control.company_id is '企业详情id';
comment on column public.gateway_device_control.dept_id is '部门id';
comment on column public.gateway_device_control.creator is '创建者';
comment on column public.gateway_device_control.create_date is '创建时间';
comment on column public.gateway_device_control.updater is '更新者';
comment on column public.gateway_device_control.update_date is '更新时间';


-- ============================================================================================
-- 设备自动控制配置
DROP TABLE IF EXISTS "public"."iot_device_auto_control_config";
create table public.iot_device_auto_control_config
(
    id             bigint primary key,
    filter_rule_id bigint,
    control_id     bigint,
    remark         varchar(255),
    msg_push_id    bigint,
    tenant_code    bigint,
    company_id     bigint,
    dept_id        bigint,
    creator        bigint,
    create_date    bigint,
    updater        bigint,
    update_date    bigint
);

comment on table public.iot_device_auto_control_config is '设备自动控制配置';
comment on column public.iot_device_auto_control_config.id is '主键';
comment on column public.iot_device_auto_control_config.filter_rule_id is '过滤规则id';
comment on column public.iot_device_auto_control_config.control_id is '控制指令id，gateway_device_control表主键';
comment on column public.iot_device_auto_control_config.remark is '备注';
comment on column public.iot_device_auto_control_config.msg_push_id is '消息推送设置主键';
comment on column public.iot_device_auto_control_config.creator is '创建者';
comment on column public.iot_device_auto_control_config.create_date is '创建时间';
comment on column public.iot_device_auto_control_config.updater is '更新者';
comment on column public.iot_device_auto_control_config.update_date is '更新时间';

-- ============================================================================================
-- 新告警配置
DROP TABLE IF EXISTS "public"."alert_config";
create table public.alert_config
(
    id             bigint primary key,
    filter_rule_id bigint,
    alert_name     varchar(255),
    alert_type     varchar(255),
    alert_level    varchar(255),
    alert_content  text,
    repeat         boolean,
    remark         varchar(255),
    begin_time     bigint,
    end_time       bigint,
    msg_push_id    bigint,
    tenant_code    bigint,
    company_id     bigint,
    dept_id        bigint,
    creator        bigint,
    create_date    bigint,
    updater        bigint,
    update_date    bigint
);

comment on table public.alert_config is '通用告警配置';
comment on column public.alert_config.id is '主键';
comment on column public.alert_config.filter_rule_id is '过滤规则id';
comment on column public.alert_config.alert_name is '告警名称';
comment on column public.alert_config.alert_type is '告警类型';
comment on column public.alert_config.alert_level is '告警等级';
comment on column public.alert_config.alert_content is '告警内容';
comment on column public.alert_config.repeat is '是否可重复';
comment on column public.alert_config.remark is '备注';
comment on column public.alert_config.begin_time is '告警起始时间';
comment on column public.alert_config.end_time is '告警结束时间';
comment on column public.alert_config.msg_push_id is '消息推送设置主键';
comment on column public.alert_config.creator is '创建者';
comment on column public.alert_config.create_date is '创建时间';
comment on column public.alert_config.updater is '更新者';
comment on column public.alert_config.update_date is '更新时间';

-- 新告警操作日志
DROP TABLE IF EXISTS "public"."alert_dispose_log";
create table public.alert_dispose_log
(
    id             bigint primary key,
    alert_log_id   bigint,
    describe       varchar,
    status         varchar(25),
    images         varchar,
    dispose_result varchar(255),
    creator        bigint,
    create_date    bigint,
    updater        bigint,
    update_date    bigint
);

comment on table public.alert_dispose_log is '告警处理日志';
comment on column public.alert_dispose_log.alert_log_id is '告警记录';
comment on column public.alert_dispose_log.describe is '问题描述';
comment on column public.alert_dispose_log.status is '处理状态：0-待处理，1-处理中，2-已处理';
comment on column public.alert_dispose_log.images is '图片列表';
comment on column public.alert_dispose_log.dispose_result is '处理结果';

-- 新告警日志
DROP TABLE IF EXISTS "public"."alert_log";
create table public.alert_log
(
    id              bigint primary key,
    config_id       bigint,
    content         varchar,
    status          varchar(25),
    alert_time      bigint,
    dispose_time    bigint,
    dispose_user_id bigint,
    tenant_code     bigint,
    company_id      bigint,
    dept_id         bigint,
    creator         bigint,
    create_date     bigint,
    updater         bigint,
    update_date     bigint,
    constraint alert_log_inx_cfg_id_time
        unique (config_id, alert_time)
);

comment on table public.alert_log is '告警日志';
comment on column public.alert_log.id is '主键';
comment on column public.alert_log.config_id is '告警配置id';
comment on column public.alert_log.content is '告警内容+过滤规则公式';
comment on column public.alert_log.status is '处理状态：0-待处理，1-处理中，2-已处理';
comment on column public.alert_log.alert_time is '告警时间';
comment on column public.alert_log.dispose_time is '处理时间';
comment on column public.alert_log.dispose_user_id is '处理人';

