﻿CREATE TABLE "iot_IoTGateway" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTGateway" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NOT NULL,
    "IsOnline" INTEGER NOT NULL,
    "Image" TEXT NULL,
    "Version" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "Hidden" INTEGER NOT NULL
);

CREATE TABLE "iot_IoTProductCategory" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTProductCategory" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "Left" INTEGER NOT NULL,
    "Right" INTEGER NOT NULL,
    "ParentId" TEXT NULL,
    "Image" TEXT NULL,
    CONSTRAINT "FK_iot_IoTProductCategory_iot_IoTProductCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_IoTProductCategory" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "iot_PermissionCategory" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_PermissionCategory" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "Left" INTEGER NOT NULL,
    "Right" INTEGER NOT NULL,
    "ParentId" TEXT NULL,
    CONSTRAINT "FK_iot_PermissionCategory_iot_PermissionCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_PermissionCategory" ("Id") ON DELETE SET NULL
);

CREATE TABLE "iot_Role" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Role" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NOT NULL,
    "Number" TEXT NULL,
    "IsReadOnly" INTEGER NOT NULL
);

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

CREATE TABLE "iot_User" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_User" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "UserName" TEXT NOT NULL,
    "SecurityStamp" TEXT NULL,
    "PasswordHash" TEXT NULL,
    "PasswordConfirmed" INTEGER NOT NULL,
    "Email" TEXT NULL,
    "EmailConfirmed" INTEGER NOT NULL,
    "PhoneNumber" TEXT NULL,
    "PhoneNumberConfirmed" INTEGER NOT NULL,
    "RealName" TEXT NULL,
    "IdentityNumber" TEXT NULL,
    "IdentityConfirmed" INTEGER NOT NULL,
    "NickName" TEXT NULL,
    "Avatar" TEXT NULL,
    "Sex" INTEGER NULL,
    "Birthday" TEXT NULL,
    "LockoutEnabled" INTEGER NOT NULL,
    "AccessFailedCount" INTEGER NOT NULL,
    "LockoutEnd" TEXT NULL,
    "RowVersion" TEXT NULL
);

CREATE TABLE "iot_IoTScene" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTScene" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NOT NULL,
    "Image" TEXT NOT NULL,
    "Hidden" INTEGER NOT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "NodeId" TEXT NULL,
    CONSTRAINT "FK_iot_IoTScene_iot_IoTGateway_NodeId" FOREIGN KEY ("NodeId") REFERENCES "iot_IoTGateway" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTProduct" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTProduct" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NOT NULL,
    "Image" TEXT NULL,
    "Path" TEXT NULL,
    "ApiJson" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "CategoryId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTProduct_iot_IoTProductCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_IoTProductCategory" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_Permission" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Permission" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NULL,
    "CategoryId" TEXT NULL,
    CONSTRAINT "FK_iot_Permission_iot_PermissionCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_PermissionCategory" ("Id") ON DELETE SET NULL
);

CREATE TABLE "iot_UserRole" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_UserRole" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "UserId" TEXT NOT NULL,
    "RoleId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_UserRole_iot_Role_RoleId" FOREIGN KEY ("RoleId") REFERENCES "iot_Role" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_UserRole_iot_User_UserId" FOREIGN KEY ("UserId") REFERENCES "iot_User" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTSceneTimer" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTSceneTimer" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Cron" TEXT NULL,
    "Disabled" INTEGER NOT NULL,
    "SceneId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTSceneTimer_iot_IoTScene_SceneId" FOREIGN KEY ("SceneId") REFERENCES "iot_IoTScene" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTApi" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTApi" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Path" TEXT NULL,
    "Command" TEXT NULL,
    "Method" TEXT NULL,
    "ProductId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTApi_iot_IoTProduct_ProductId" FOREIGN KEY ("ProductId") REFERENCES "iot_IoTProduct" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTDevice" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTDevice" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "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,
    "ProductId" TEXT NOT NULL,
    "NodeId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTDevice_iot_IoTGateway_NodeId" FOREIGN KEY ("NodeId") REFERENCES "iot_IoTGateway" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_IoTDevice_iot_IoTProduct_ProductId" FOREIGN KEY ("ProductId") REFERENCES "iot_IoTProduct" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_RolePermission" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_RolePermission" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "RoleId" TEXT NOT NULL,
    "PermissionId" TEXT NOT NULL,
    "IsReadOnly" INTEGER NOT NULL,
    CONSTRAINT "FK_iot_RolePermission_iot_Permission_PermissionId" FOREIGN KEY ("PermissionId") REFERENCES "iot_Permission" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_RolePermission_iot_Role_RoleId" FOREIGN KEY ("RoleId") REFERENCES "iot_Role" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTParameter" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTParameter" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Type" TEXT NULL,
    "Name" TEXT NULL,
    "Description" TEXT NULL,
    "Required" INTEGER NOT NULL,
    "Maxinum" TEXT NULL,
    "Minimum" TEXT NULL,
    "ApiId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTParameter_iot_IoTApi_ApiId" FOREIGN KEY ("ApiId") REFERENCES "iot_IoTApi" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTCommand" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTCommand" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "Disabled" INTEGER NOT NULL,
    "Delay" INTEGER NOT NULL,
    "QueryString" TEXT NULL,
    "ApiId" TEXT NOT NULL,
    "DeviceId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTCommand_iot_IoTApi_ApiId" FOREIGN KEY ("ApiId") REFERENCES "iot_IoTApi" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_IoTCommand_iot_IoTDevice_DeviceId" FOREIGN KEY ("DeviceId") REFERENCES "iot_IoTDevice" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTData" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTData" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Key" TEXT NOT NULL,
    "Value" TEXT NULL,
    "Name" TEXT NULL,
    "Type" INTEGER NOT NULL,
    "Unit" TEXT NULL,
    "Description" TEXT NULL,
    "Timestamp" INTEGER NOT NULL,
    "Hidden" INTEGER NOT NULL,
    "DeviceId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTData_iot_IoTDevice_DeviceId" FOREIGN KEY ("DeviceId") REFERENCES "iot_IoTDevice" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTSceneIoTCommand" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTSceneIoTCommand" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "SceneId" TEXT NOT NULL,
    "CommandId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTSceneIoTCommand_iot_IoTCommand_CommandId" FOREIGN KEY ("CommandId") REFERENCES "iot_IoTCommand" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_IoTSceneIoTCommand_iot_IoTScene_SceneId" FOREIGN KEY ("SceneId") REFERENCES "iot_IoTScene" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_IoTSceneTigger" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTSceneTigger" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Condition" TEXT NULL,
    "Disabled" INTEGER NOT NULL,
    "DataId" TEXT NOT NULL,
    "SceneId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTSceneTigger_iot_IoTData_DataId" FOREIGN KEY ("DataId") REFERENCES "iot_IoTData" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_IoTSceneTigger_iot_IoTScene_SceneId" FOREIGN KEY ("SceneId") REFERENCES "iot_IoTScene" ("Id") ON DELETE CASCADE
);

CREATE UNIQUE INDEX "IX_iot_IoTApi_ProductId_Name" ON "iot_IoTApi" ("ProductId", "Name");

CREATE INDEX "IX_iot_IoTCommand_ApiId" ON "iot_IoTCommand" ("ApiId");

CREATE INDEX "IX_iot_IoTCommand_DeviceId" ON "iot_IoTCommand" ("DeviceId");

CREATE INDEX "IX_iot_IoTData_DeviceId" ON "iot_IoTData" ("DeviceId");

CREATE INDEX "IX_iot_IoTDevice_NodeId" ON "iot_IoTDevice" ("NodeId");

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

CREATE INDEX "IX_iot_IoTDevice_ProductId" ON "iot_IoTDevice" ("ProductId");

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

CREATE INDEX "IX_iot_IoTParameter_ApiId" ON "iot_IoTParameter" ("ApiId");

CREATE INDEX "IX_iot_IoTProduct_CategoryId" ON "iot_IoTProduct" ("CategoryId");

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

CREATE UNIQUE INDEX "IX_iot_IoTProductCategory_Number" ON "iot_IoTProductCategory" ("Number");

CREATE INDEX "IX_iot_IoTProductCategory_ParentId" ON "iot_IoTProductCategory" ("ParentId");

CREATE INDEX "IX_iot_IoTScene_NodeId" ON "iot_IoTScene" ("NodeId");

CREATE INDEX "IX_iot_IoTSceneIoTCommand_CommandId" ON "iot_IoTSceneIoTCommand" ("CommandId");

CREATE INDEX "IX_iot_IoTSceneIoTCommand_SceneId" ON "iot_IoTSceneIoTCommand" ("SceneId");

CREATE INDEX "IX_iot_IoTSceneTigger_DataId" ON "iot_IoTSceneTigger" ("DataId");

CREATE INDEX "IX_iot_IoTSceneTigger_SceneId" ON "iot_IoTSceneTigger" ("SceneId");

CREATE INDEX "IX_iot_IoTSceneTimer_SceneId" ON "iot_IoTSceneTimer" ("SceneId");

CREATE INDEX "IX_iot_Permission_CategoryId" ON "iot_Permission" ("CategoryId");

CREATE UNIQUE INDEX "IX_iot_Permission_Number" ON "iot_Permission" ("Number");

CREATE UNIQUE INDEX "IX_iot_PermissionCategory_Number" ON "iot_PermissionCategory" ("Number");

CREATE INDEX "IX_iot_PermissionCategory_ParentId" ON "iot_PermissionCategory" ("ParentId");

CREATE UNIQUE INDEX "IX_iot_Role_Name" ON "iot_Role" ("Name");

CREATE INDEX "IX_iot_RolePermission_PermissionId" ON "iot_RolePermission" ("PermissionId");

CREATE UNIQUE INDEX "IX_iot_RolePermission_RoleId_PermissionId" ON "iot_RolePermission" ("RoleId", "PermissionId");

CREATE UNIQUE INDEX "IX_iot_User_Email" ON "iot_User" ("Email");

CREATE UNIQUE INDEX "IX_iot_User_PhoneNumber" ON "iot_User" ("PhoneNumber");

CREATE UNIQUE INDEX "IX_iot_User_UserName" ON "iot_User" ("UserName");

CREATE INDEX "IX_iot_UserRole_RoleId" ON "iot_UserRole" ("RoleId");

CREATE UNIQUE INDEX "IX_iot_UserRole_UserId_RoleId" ON "iot_UserRole" ("UserId", "RoleId");

