﻿CREATE TABLE "iot_Area" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Area" 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,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    CONSTRAINT "FK_iot_Area_iot_Area_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_Area" ("Id") ON DELETE RESTRICT
);

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_IoTGatewayCategory" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTGatewayCategory" 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,
    "Template" TEXT NULL,
    CONSTRAINT "FK_iot_IoTGatewayCategory_iot_IoTGatewayCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_IoTGatewayCategory" ("Id") ON DELETE RESTRICT
);

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_LiveRecord" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_LiveRecord" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "DeviceNumber" TEXT NOT NULL,
    "DeviceName" TEXT NULL,
    "Name" TEXT NULL,
    "Value" TEXT 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_Site" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Site" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NOT NULL,
    "Icon" TEXT NULL,
    "Description" TEXT NULL,
    "Key" TEXT NULL,
    "Home" TEXT NULL,
    "Login" TEXT NULL,
    "Logout" TEXT NULL,
    "Disabled" INTEGER NOT NULL,
    "DisplayOrder" 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_Organ" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Organ" 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,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    "Image" TEXT NULL,
    "Description" TEXT NULL,
    "AreaId" TEXT NULL,
    "IsReadOnly" INTEGER NOT NULL,
    CONSTRAINT "FK_iot_Organ_iot_Area_AreaId" FOREIGN KEY ("AreaId") REFERENCES "iot_Area" ("Id") ON DELETE SET NULL,
    CONSTRAINT "FK_iot_Organ_iot_Organ_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_Organ" ("Id") ON DELETE RESTRICT
);

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_IoTGatewayCategoryIoTGateway" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTGatewayCategoryIoTGateway" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "CategoryId" TEXT NOT NULL,
    "NodeId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_IoTGatewayCategoryIoTGateway_iot_IoTGatewayCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_IoTGatewayCategory" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_IoTGatewayCategoryIoTGateway_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_Building" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Building" 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,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    "Image" TEXT NULL,
    "Description" TEXT NULL,
    "OrganId" TEXT NULL,
    "AreaId" TEXT NULL,
    CONSTRAINT "FK_iot_Building_iot_Area_AreaId" FOREIGN KEY ("AreaId") REFERENCES "iot_Area" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_iot_Building_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE SET NULL,
    CONSTRAINT "FK_iot_Building_iot_Building_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_Building" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "iot_Department" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Department" 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,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    "OrganId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_Department_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_Department_iot_Department_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_Department" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "iot_OrganIoTScene" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganIoTScene" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NOT NULL,
    "Image" TEXT NOT NULL,
    "Hidden" INTEGER NOT NULL,
    "DisplayOrder" INTEGER NOT NULL,
    "OrganId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganIoTScene_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_OrganRole" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganRole" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NOT NULL,
    "Number" TEXT NULL,
    "IsReadOnly" INTEGER NOT NULL,
    "OrganId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganRole_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_OrganUser" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganUser" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    "OrganId" TEXT NOT NULL,
    "UserId" TEXT NOT NULL,
    "IsDefault" INTEGER NOT NULL,
    "IsReadOnly" INTEGER NOT NULL,
    CONSTRAINT "FK_iot_OrganUser_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganUser_iot_User_UserId" FOREIGN KEY ("UserId") REFERENCES "iot_User" ("Id") ON DELETE CASCADE
);

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,
    "OrganId" TEXT NULL,
    CONSTRAINT "FK_iot_PermissionCategory_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_PermissionCategory_iot_PermissionCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_PermissionCategory" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "iot_Title" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Title" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "MinLevel" INTEGER NOT NULL,
    "MaxLevel" INTEGER NOT NULL,
    "OrganId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_Title_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("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_BuildingIoTGateway" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_BuildingIoTGateway" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "BuildingId" TEXT NOT NULL,
    "IoTGatewayId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_BuildingIoTGateway_iot_Building_BuildingId" FOREIGN KEY ("BuildingId") REFERENCES "iot_Building" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_BuildingIoTGateway_iot_IoTGateway_IoTGatewayId" FOREIGN KEY ("IoTGatewayId") REFERENCES "iot_IoTGateway" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_OrganIoTSceneTimer" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganIoTSceneTimer" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Cron" TEXT NULL,
    "Disabled" INTEGER NOT NULL,
    "OrganSceneId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganIoTSceneTimer_iot_OrganIoTScene_OrganSceneId" FOREIGN KEY ("OrganSceneId") REFERENCES "iot_OrganIoTScene" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_OrganUserRole" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganUserRole" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "IsReadOnly" INTEGER NOT NULL,
    "OrganUserId" TEXT NOT NULL,
    "OrganRoleId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganUserRole_iot_OrganRole_OrganRoleId" FOREIGN KEY ("OrganRoleId") REFERENCES "iot_OrganRole" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganUserRole_iot_OrganUser_OrganUserId" FOREIGN KEY ("OrganUserId") REFERENCES "iot_OrganUser" ("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,
    "IsReadOnly" INTEGER NOT NULL,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    "OrganId" TEXT NULL,
    "CategoryId" TEXT NULL,
    CONSTRAINT "FK_iot_Permission_iot_PermissionCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_PermissionCategory" ("Id") ON DELETE SET NULL,
    CONSTRAINT "FK_iot_Permission_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "iot_Job" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Job" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Level" INTEGER NOT NULL,
    "TitleId" TEXT NOT NULL,
    "DepartmentId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_Job_iot_Department_DepartmentId" FOREIGN KEY ("DepartmentId") REFERENCES "iot_Department" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_Job_iot_Title_TitleId" FOREIGN KEY ("TitleId") REFERENCES "iot_Title" ("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_OrganRolePermission" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganRolePermission" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "RoleId" TEXT NOT NULL,
    "PermissionId" TEXT NOT NULL,
    "IsReadOnly" INTEGER NOT NULL,
    CONSTRAINT "FK_iot_OrganRolePermission_iot_Permission_PermissionId" FOREIGN KEY ("PermissionId") REFERENCES "iot_Permission" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganRolePermission_iot_OrganRole_RoleId" FOREIGN KEY ("RoleId") REFERENCES "iot_OrganRole" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_DepartmentUser" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_DepartmentUser" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Position" TEXT NULL,
    "Type" INTEGER NOT NULL,
    "CustomType" TEXT NULL,
    "UserId" TEXT NOT NULL,
    "DeparementId" TEXT NOT NULL,
    "JobId" TEXT NULL,
    "DepartmentId" TEXT NULL,
    "OrganUserId" TEXT NULL,
    CONSTRAINT "FK_iot_DepartmentUser_iot_Department_DepartmentId" FOREIGN KEY ("DepartmentId") REFERENCES "iot_Department" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_iot_DepartmentUser_iot_Job_JobId" FOREIGN KEY ("JobId") REFERENCES "iot_Job" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_iot_DepartmentUser_iot_OrganUser_OrganUserId" FOREIGN KEY ("OrganUserId") REFERENCES "iot_OrganUser" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_iot_DepartmentUser_iot_User_UserId" FOREIGN KEY ("UserId") REFERENCES "iot_User" ("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_OrganIoTSceneIoTCommand" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganIoTSceneIoTCommand" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "OrganSceneId" TEXT NOT NULL,
    "CommandId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganIoTSceneIoTCommand_iot_IoTCommand_CommandId" FOREIGN KEY ("CommandId") REFERENCES "iot_IoTCommand" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganIoTSceneIoTCommand_iot_OrganIoTScene_OrganSceneId" FOREIGN KEY ("OrganSceneId") REFERENCES "iot_OrganIoTScene" ("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 TABLE "iot_OrganIoTSceneTigger" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganIoTSceneTigger" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Condition" TEXT NULL,
    "Disabled" INTEGER NOT NULL,
    "DataId" TEXT NOT NULL,
    "OrganSceneId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganIoTSceneTigger_iot_IoTData_DataId" FOREIGN KEY ("DataId") REFERENCES "iot_IoTData" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganIoTSceneTigger_iot_OrganIoTScene_OrganSceneId" FOREIGN KEY ("OrganSceneId") REFERENCES "iot_OrganIoTScene" ("Id") ON DELETE CASCADE
);

CREATE UNIQUE INDEX "IX_iot_Area_Number" ON "iot_Area" ("Number");

CREATE INDEX "IX_iot_Area_ParentId" ON "iot_Area" ("ParentId");

CREATE INDEX "IX_iot_Building_AreaId" ON "iot_Building" ("AreaId");

CREATE UNIQUE INDEX "IX_iot_Building_Number" ON "iot_Building" ("Number");

CREATE INDEX "IX_iot_Building_OrganId" ON "iot_Building" ("OrganId");

CREATE INDEX "IX_iot_Building_ParentId" ON "iot_Building" ("ParentId");

CREATE INDEX "IX_iot_BuildingIoTGateway_IoTGatewayId" ON "iot_BuildingIoTGateway" ("IoTGatewayId");

CREATE UNIQUE INDEX "IX_iot_BuildingIoTGateway_BuildingId_IoTGatewayId" ON "iot_BuildingIoTGateway" ("BuildingId", "IoTGatewayId");

CREATE INDEX "IX_iot_Department_ParentId" ON "iot_Department" ("ParentId");

CREATE UNIQUE INDEX "IX_iot_Department_OrganId_Number" ON "iot_Department" ("OrganId", "Number");

CREATE INDEX "IX_iot_DepartmentUser_DepartmentId" ON "iot_DepartmentUser" ("DepartmentId");

CREATE INDEX "IX_iot_DepartmentUser_JobId" ON "iot_DepartmentUser" ("JobId");

CREATE INDEX "IX_iot_DepartmentUser_OrganUserId" ON "iot_DepartmentUser" ("OrganUserId");

CREATE INDEX "IX_iot_DepartmentUser_UserId" ON "iot_DepartmentUser" ("UserId");

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_IoTGatewayCategory_ParentId" ON "iot_IoTGatewayCategory" ("ParentId");

CREATE INDEX "IX_iot_IoTGatewayCategoryIoTGateway_NodeId" ON "iot_IoTGatewayCategoryIoTGateway" ("NodeId");

CREATE UNIQUE INDEX "IX_iot_IoTGatewayCategoryIoTGateway_CategoryId_NodeId" ON "iot_IoTGatewayCategoryIoTGateway" ("CategoryId", "NodeId");

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_Job_DepartmentId" ON "iot_Job" ("DepartmentId");

CREATE INDEX "IX_iot_Job_TitleId" ON "iot_Job" ("TitleId");

CREATE INDEX "IX_iot_Organ_AreaId" ON "iot_Organ" ("AreaId");

CREATE UNIQUE INDEX "IX_iot_Organ_Name" ON "iot_Organ" ("Name");

CREATE UNIQUE INDEX "IX_iot_Organ_Number" ON "iot_Organ" ("Number");

CREATE INDEX "IX_iot_Organ_ParentId" ON "iot_Organ" ("ParentId");

CREATE INDEX "IX_iot_OrganIoTScene_OrganId" ON "iot_OrganIoTScene" ("OrganId");

CREATE INDEX "IX_iot_OrganIoTSceneIoTCommand_CommandId" ON "iot_OrganIoTSceneIoTCommand" ("CommandId");

CREATE INDEX "IX_iot_OrganIoTSceneIoTCommand_OrganSceneId" ON "iot_OrganIoTSceneIoTCommand" ("OrganSceneId");

CREATE INDEX "IX_iot_OrganIoTSceneTigger_DataId" ON "iot_OrganIoTSceneTigger" ("DataId");

CREATE INDEX "IX_iot_OrganIoTSceneTigger_OrganSceneId" ON "iot_OrganIoTSceneTigger" ("OrganSceneId");

CREATE INDEX "IX_iot_OrganIoTSceneTimer_OrganSceneId" ON "iot_OrganIoTSceneTimer" ("OrganSceneId");

CREATE UNIQUE INDEX "IX_iot_OrganRole_OrganId_Name" ON "iot_OrganRole" ("OrganId", "Name");

CREATE INDEX "IX_iot_OrganRolePermission_PermissionId" ON "iot_OrganRolePermission" ("PermissionId");

CREATE UNIQUE INDEX "IX_iot_OrganRolePermission_RoleId_PermissionId" ON "iot_OrganRolePermission" ("RoleId", "PermissionId");

CREATE INDEX "IX_iot_OrganUser_UserId" ON "iot_OrganUser" ("UserId");

CREATE UNIQUE INDEX "IX_iot_OrganUser_OrganId_UserId" ON "iot_OrganUser" ("OrganId", "UserId");

CREATE INDEX "IX_iot_OrganUserRole_OrganRoleId" ON "iot_OrganUserRole" ("OrganRoleId");

CREATE UNIQUE INDEX "IX_iot_OrganUserRole_OrganUserId_OrganRoleId" ON "iot_OrganUserRole" ("OrganUserId", "OrganRoleId");

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

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

CREATE INDEX "IX_iot_Permission_OrganId" ON "iot_Permission" ("OrganId");

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

CREATE UNIQUE INDEX "IX_iot_PermissionCategory_OrganId_Name" ON "iot_PermissionCategory" ("OrganId", "Name");

CREATE UNIQUE INDEX "IX_iot_Site_Name" ON "iot_Site" ("Name");

CREATE INDEX "IX_iot_Title_OrganId" ON "iot_Title" ("OrganId");

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");

