CREATE TABLE IF NOT EXISTS "iot_formula"
(
    "id"           bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "name"         varchar,
    "formula_code" varchar,
    "formula"      varchar,
    "data_unit"    varchar,
    "result_type"  varchar,
    "machine_id"   bigint   NOT NULL,
    "remark"       varchar,
    "is_enable"    bit      NOT NULL,
    "creator"      varchar           DEFAULT '',
    "create_time"  datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"      varchar           DEFAULT '',
    "update_time"  datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"      bit      NOT NULL DEFAULT FALSE,
    "tenant_id"    bigint,
    PRIMARY KEY ("id")
) COMMENT '计算公式';

CREATE TABLE IF NOT EXISTS "iot_formula_detail"
(
    "id"             bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "formula_id"     bigint   NOT NULL,
    "formula_code"   varchar,
    "name"           varchar,
    "device_id"      bigint   NOT NULL,
    "attribute_id"   bigint   NOT NULL,
    "attribute_name" varchar  NOT NULL,
    "address"        varchar,
    "demo_value"     varchar,
    "sum_type"       varchar,
    "sum_range"      varchar,
    "remark"         varchar,
    "is_enable"      bit      NOT NULL,
    "creator"        varchar           DEFAULT '',
    "create_time"    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"        varchar           DEFAULT '',
    "update_time"    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"        bit      NOT NULL DEFAULT FALSE,
    "tenant_id"      bigint,
    PRIMARY KEY ("id")
) COMMENT '计算公式明细';

CREATE TABLE IF NOT EXISTS "iot_gateway"
(
    "id"             bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "admin_ip"       varchar,
    "username"       varchar,
    "password"       varchar,
    "gateway_name"   varchar,
    "remark"         varchar,
    "is_enable"      bit      NOT NULL,
    "creator"        varchar           DEFAULT '',
    "create_time"    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"        varchar           DEFAULT '',
    "update_time"    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"        bit      NOT NULL DEFAULT FALSE,
    "tenant_id"      bigint,
    "gateway_code"   varchar,
    "gateway_status" varchar,
    "topic"          varchar,
    PRIMARY KEY ("id")
) COMMENT '网关表';
CREATE TABLE IF NOT EXISTS "iot_iot_organization"
(
    "id"             bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "sort"           int,
    "worker_user_id" bigint,
    "org_id"         bigint,
    "creator"        varchar           DEFAULT '',
    "create_time"    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"        varchar           DEFAULT '',
    "update_time"    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"        bit      NOT NULL DEFAULT FALSE,
    "tenant_id"      bigint,
    "name"           varchar  NOT NULL,
    "parent_id"      bigint   NOT NULL,
    "status"         int,
    "device_type"    varchar,
    "org_class"      varchar,
    "machine_type"   varchar,
    PRIMARY KEY ("id")
) COMMENT 'IOT组织表';
CREATE TABLE IF NOT EXISTS "iot_kanban"
(
    "id"          bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "code"        varchar  NOT NULL,
    "title"       varchar  NOT NULL,
    "view_url"    varchar,
    "tags"        varchar,
    "img"         varchar,
    "remark"      varchar,
    "is_enable"   bit      NOT NULL,
    "creator"     varchar           DEFAULT '',
    "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"     varchar           DEFAULT '',
    "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"     bit      NOT NULL DEFAULT FALSE,
    "tenant_id"   bigint,
    PRIMARY KEY ("id")
) COMMENT '物联看板';
CREATE TABLE IF NOT EXISTS "iot_formula_record"
(
    "id"           bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "machine_id"   bigint   NOT NULL,
    "machine_name" varchar,
    "worker_id"    bigint,
    "worker_name"  varchar,
    "result_type"  varchar,
    "formula_id"   bigint   NOT NULL,
    "formula_code" varchar,
    "data_value"   varchar,
    "data_unit"    varchar,
    "creator"      varchar           DEFAULT '',
    "create_time"  datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"      varchar           DEFAULT '',
    "update_time"  datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"      bit      NOT NULL DEFAULT FALSE,
    "tenant_id"    bigint,
    PRIMARY KEY ("id")
) COMMENT '变量记录';
CREATE TABLE IF NOT EXISTS "iot_mqtt_record"
(
    "id"               bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "machine_id"       bigint   NOT NULL,
    "machine_name"     varchar,
    "device_data_time" varchar  NOT NULL,
    "device_data"      varchar,
    "device_id"        bigint   NOT NULL,
    "device_code"      varchar,
    "device_name"      varchar,
    "worker_id"        bigint,
    "worker_name"      varchar,
    "gateway_code"     varchar,
    "creator"          varchar           DEFAULT '',
    "create_time"      datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"          varchar           DEFAULT '',
    "update_time"      datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"          bit      NOT NULL DEFAULT FALSE,
    "tenant_id"        bigint,
    PRIMARY KEY ("id")
) COMMENT '数采记录';
CREATE TABLE IF NOT EXISTS "iot_frps_server"
(
    "id"          bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "frps_name"   varchar,
    "host_mac"    varchar,
    "host_os"     varchar,
    "host_ip"     varchar,
    "host_port"   int,
    "frps_path"   varchar,
    "frps_state"  varchar,
    "remark"      varchar,
    "is_enable"   bit      NOT NULL,
    "creator"     varchar           DEFAULT '',
    "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"     varchar           DEFAULT '',
    "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"     bit      NOT NULL DEFAULT FALSE,
    "tenant_id"   bigint,
    PRIMARY KEY ("id")
) COMMENT '代理服务器';
CREATE TABLE IF NOT EXISTS "iot_frpc_proxy_server"
(
    "id"            bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "frpc_name"     varchar,
    "local_ip"      varchar,
    "local_port"    int,
    "suffix_path"   varchar,
    "local_status"  varchar,
    "frps_id"       bigint,
    "proxy_port"    int,
    "proxy_address" varchar,
    "proxy_status"  varchar,
    "remark"        varchar,
    "is_enable"     bit      NOT NULL,
    "creator"       varchar           DEFAULT '',
    "create_time"   datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"       varchar           DEFAULT '',
    "update_time"   datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"       bit      NOT NULL DEFAULT FALSE,
    "tenant_id"     bigint,
    PRIMARY KEY ("id")
) COMMENT '代理服务';


CREATE TABLE IF NOT EXISTS "iot_feedback"
(
    "id"          bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "title"       varchar  NOT NULL,
    "content"     varchar,
    "tags"        varchar,
    "creator"     varchar           DEFAULT '',
    "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"     varchar           DEFAULT '',
    "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"     bit      NOT NULL DEFAULT FALSE,
    "tenant_id"   bigint   NOT NULL,
    PRIMARY KEY ("id")
) COMMENT '用户反馈';

CREATE TABLE IF NOT EXISTS "iot_mqtt_data_record"
(
    "id"          bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "device_name" varchar,
    "attribute"   varchar,
    "attr_value"  varchar,
    "device_time" varchar  NOT NULL,
    "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY ("id")
) COMMENT '设备数据记录';

CREATE TABLE IF NOT EXISTS "iot_alert"
(
    "id"                 bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "alert_code"         varchar  NOT NULL,
    "alert_name"         varchar  NOT NULL,
    "alert_type"         varchar,
    "alert_level"        varchar,
    "content"            varchar,
    "condition"          varchar,
    "condition_formula"  varchar,
    "alert_audio"        varchar  NOT NULL,
    "is_repeat"          bit      NOT NULL,
    "no_repeat_duration" bigint   NOT NULL,
    "is_enable"          bit      NOT NULL,
    "creator"            varchar           DEFAULT '',
    "create_time"        datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"            varchar           DEFAULT '',
    "update_time"        datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"            bit      NOT NULL DEFAULT FALSE,
    "tenant_id"          bigint,
    PRIMARY KEY ("id")
) COMMENT '告警配置';
CREATE TABLE IF NOT EXISTS "iot_alert_record"
(
    "id"                 bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "alert_code"         varchar  NOT NULL,
    "alert_name"         varchar  NOT NULL,
    "alert_type"         varchar,
    "alert_level"        varchar,
    "content"            varchar,
    "condition"          varchar,
    "condition_formula"  varchar,
    "data_value"         varchar,
    "data_type"          varchar,
    "data_unit"          varchar,
    "data_type_remark"   varchar,
    "creator"            varchar           DEFAULT '',
    "create_time"        datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"            varchar           DEFAULT '',
    "update_time"        datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"            bit      NOT NULL DEFAULT FALSE,
    "tenant_id"          bigint,
    "alert_id"           bigint,
    "alert_audio"        varchar,
    "is_repeat"          bit,
    "no_repeat_duration" bigint,
    "data_id"            bigint,
    "data_code"          varchar,
    "data_name"          varchar,
    PRIMARY KEY ("id")
) COMMENT '告警记录';
CREATE TABLE IF NOT EXISTS "iot_device"
(
    "id"                bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "device_code"       varchar,
    "device_name"       varchar,
    "device_type"       varchar,
    "status"            varchar,
    "read_topic"        varchar,
    "write_topic"       varchar,
    "gateway_id"        bigint   NOT NULL,
    "device_brand_id"   bigint   NOT NULL,
    "off_line_duration" bigint   NOT NULL,
    "last_online_time"  varchar  NOT NULL,
    "remark"            varchar,
    "is_enable"         bit      NOT NULL,
    "creator"           varchar           DEFAULT '',
    "create_time"       datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"           varchar           DEFAULT '',
    "update_time"       datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"           bit      NOT NULL DEFAULT FALSE,
    "tenant_id"         bigint,
    PRIMARY KEY ("id")
) COMMENT '物联设备表';

CREATE TABLE IF NOT EXISTS "iot_device_attribute"
(
    "id"               bigint   NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "attribute_code"   varchar  NOT NULL,
    "attribute_name"   varchar  NOT NULL,
    "address"          varchar,
    "description"      varchar,
    "attribute_type"   varchar,
    "io_type"          varchar,
    "data_type"        varchar,
    "data_type_remark" varchar,
    "data_unit"        varchar,
    "data_formula"     varchar,
    "gateway_id"       bigint,
    "device_id"        bigint,
    "alert_id"         bigint,
    "remark"           varchar,
    "is_enable"        bit      NOT NULL,
    "creator"          varchar           DEFAULT '',
    "create_time"      datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater"          varchar           DEFAULT '',
    "update_time"      datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    "deleted"          bit      NOT NULL DEFAULT FALSE,
    "tenant_id"        bigint,
    PRIMARY KEY ("id")
) COMMENT '设备属性表';
