﻿CREATE TABLE "iot_IoTGateway" (
    "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" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "Hidden" INTEGER NOT NULL,
    "BuildingId" TEXT NULL,
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_IoTProduct" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTProduct" PRIMARY KEY,
    "Name" TEXT NULL,
    "Number" TEXT NOT NULL,
    "Image" TEXT NULL,
    "Path" TEXT NULL,
    "ApiJson" TEXT NULL,
    "IoTProductCategoryId" TEXT NULL,
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_Setting" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Setting" PRIMARY KEY,
    "Name" TEXT NOT NULL,
    "Value" TEXT NULL,
    "Type" INTEGER NOT NULL,
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_IoTApi" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTApi" PRIMARY KEY,
    "Name" TEXT NULL,
    "Path" TEXT NULL,
    "Command" TEXT NULL,
    "Method" TEXT NULL,
    "IoTProductId" TEXT NOT NULL,
    "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" 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" TEXT NULL,
    "UserName" TEXT NULL,
    "Password" TEXT NULL,
    "ConnectId" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "Disabled" INTEGER NOT NULL,
    "IoTProductId" TEXT NOT NULL,
    "IoTGatewayId" TEXT NOT NULL,
    "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_IoTParameter" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTParameter" PRIMARY KEY,
    "Type" TEXT NULL,
    "Name" TEXT NULL,
    "Description" TEXT NULL,
    "Required" INTEGER NOT NULL,
    "Maxinum" TEXT NULL,
    "Minimum" TEXT NULL,
    "IoTApiId" TEXT NOT NULL,
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTParameter_iot_IoTApi_IoTApiId" FOREIGN KEY ("IoTApiId") REFERENCES "iot_IoTApi" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTData" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTData" PRIMARY KEY,
    "Key" TEXT NOT NULL,
    "Value" 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,
    "IoTDeviceId" TEXT NOT NULL,
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTData_iot_IoTDevice_IoTDeviceId" FOREIGN KEY ("IoTDeviceId") REFERENCES "iot_IoTDevice" ("Id") ON DELETE CASCADE
);


CREATE UNIQUE INDEX "IX_iot_IoTApi_IoTProductId_Name" ON "iot_IoTApi" ("IoTProductId", "Name");


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 INDEX "IX_iot_IoTParameter_IoTApiId" ON "iot_IoTParameter" ("IoTApiId");


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


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


