﻿CREATE TABLE "iot_IoTGateway" (
    -- 网关

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTGateway" PRIMARY KEY,

    -- 节点名称
    "Name" TEXT NULL,

    -- 节点编号
    "Number" TEXT NOT NULL,

    -- 在线
    "IsOnline" INTEGER NOT NULL,

    -- 图片
    "Image" TEXT NULL,

    -- 版本
    "Version" TEXT NULL,

    -- Password
    "Password" TEXT NULL,

    -- 序号
    "DisplayOrder" INTEGER NOT NULL,

    -- 隐藏
    "Hidden" INTEGER NOT NULL,

    -- BuildingId
    "BuildingId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_IoTProduct" (
    -- 产品

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTProduct" PRIMARY KEY,

    -- 产品名称
    "Name" TEXT NULL,

    -- 产品型号
    "Number" TEXT NOT NULL,

    -- 图标
    "Image" TEXT NULL,

    -- 路径
    "Path" TEXT NULL,

    -- ApiJson
    "ApiJson" TEXT NULL,

    -- IoTProductCategoryId
    "IoTProductCategoryId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_Setting" (
    -- 配置

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Setting" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Value
    "Value" TEXT NULL,

    -- Type
    "Type" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_IoTApi" (
    -- 接口

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTApi" PRIMARY KEY,

    -- Name
    "Name" TEXT NULL,

    -- Path
    "Path" TEXT NULL,

    -- Command
    "Command" TEXT NULL,

    -- Method
    "Method" TEXT NULL,

    -- IoTProductId
    "IoTProductId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTApi_iot_IoTProduct_IoTProductId" FOREIGN KEY ("IoTProductId") REFERENCES "iot_IoTProduct" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTDevice" (
    -- 设备

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTDevice" PRIMARY KEY,

    -- 名称
    "Name" TEXT NULL,

    -- 编号
    "Number" TEXT NOT NULL,

    -- 网关编号
    "Gateway" TEXT NULL,

    -- 显示名称
    "DisplayName" TEXT NOT NULL,

    -- 标记
    "Tag" TEXT NULL,

    -- 在线状态
    "IsOnline" INTEGER NOT NULL,

    -- 图标
    "Icon" TEXT NULL,

    -- IP
    "Ip" TEXT NULL,

    -- 用户名
    "UserName" TEXT NULL,

    -- 密码
    "Password" TEXT NULL,

    -- 连接Id
    "ConnectId" TEXT NULL,

    -- 序号
    "DisplayOrder" INTEGER NOT NULL,

    -- 隐藏
    "Disabled" INTEGER NOT NULL,

    -- 产品Id
    "IoTProductId" TEXT NOT NULL,

    -- 节点Id
    "IoTGatewayId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTDevice_iot_IoTGateway_IoTGatewayId" FOREIGN KEY ("IoTGatewayId") REFERENCES "iot_IoTGateway" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_IoTDevice_iot_IoTProduct_IoTProductId" FOREIGN KEY ("IoTProductId") REFERENCES "iot_IoTProduct" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTData" (
    -- 数据

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTData" PRIMARY KEY,

    -- 键
    "Key" TEXT NOT NULL,

    -- 值
    "Value" TEXT NULL,

    -- IntValue
    "IntValue" INTEGER NULL,

    -- DoubleValue
    "DoubleValue" REAL NULL,

    -- DateTimeValue
    "DateTimeValue" TEXT NULL,

    -- 枚举候选值列表
    "EnumValues" TEXT NULL,

    -- 名称
    "Name" TEXT NULL,

    -- 数据分类
    "DataType" INTEGER NOT NULL,

    -- 数值类型
    "ValueType" INTEGER NOT NULL,

    -- 对象模式
    "ValueSchema" TEXT NULL,

    -- 单位
    "Unit" TEXT NULL,

    -- 描述
    "Description" TEXT NULL,

    -- 时间戳
    "Timestamp" INTEGER NOT NULL,

    -- 隐藏
    "Hidden" INTEGER NOT NULL,

    -- 设备Id
    "IoTDeviceId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTData_iot_IoTDevice_IoTDeviceId" FOREIGN KEY ("IoTDeviceId") REFERENCES "iot_IoTDevice" ("Id") ON DELETE CASCADE
);


CREATE INDEX "IX_iot_IoTApi_IoTProductId" ON "iot_IoTApi" ("IoTProductId");


CREATE INDEX "IX_iot_IoTData_IoTDeviceId" ON "iot_IoTData" ("IoTDeviceId");


CREATE INDEX "IX_iot_IoTDevice_IoTGatewayId" ON "iot_IoTDevice" ("IoTGatewayId");


CREATE INDEX "IX_iot_IoTDevice_IoTProductId" ON "iot_IoTDevice" ("IoTProductId");


CREATE UNIQUE INDEX "IX_iot_IoTDevice_Number" ON "iot_IoTDevice" ("Number");


CREATE UNIQUE INDEX "IX_iot_IoTGateway_Number" ON "iot_IoTGateway" ("Number");


CREATE UNIQUE INDEX "IX_iot_IoTProduct_Number" ON "iot_IoTProduct" ("Number");


CREATE UNIQUE INDEX "IX_iot_Setting_Name" ON "iot_Setting" ("Name");


