CREATE TABLE "iot_Category" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Category" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NULL,
    "Image" TEXT NULL
);

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_Node" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Node" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "OrganName" TEXT NULL,
    "OrganNumber" TEXT NOT 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_NodeCategory" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_NodeCategory" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Template" TEXT NULL,
    "DisplayOrder" INTEGER NOT NULL
);

CREATE TABLE "iot_Organ" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Organ" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NOT NULL,
    "Image" TEXT NULL
);

CREATE TABLE "iot_PermissionCategory" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_PermissionCategory" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Number" TEXT NULL,
    "Path" TEXT 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,
    "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,
    "NickName" TEXT NULL,
    "Avatar" TEXT NULL,
    "SecurityStamp" TEXT NULL,
    "PasswordHash" TEXT NULL,
    "Email" TEXT NULL
);

CREATE TABLE "iot_Product" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Product" 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_Product_iot_Category_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_Category" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_Scene" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Scene" 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_Scene_iot_Node_NodeId" FOREIGN KEY ("NodeId") REFERENCES "iot_Node" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "iot_NodeCategoryNode" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_NodeCategoryNode" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "CategoryId" TEXT NOT NULL,
    "NodeId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_NodeCategoryNode_iot_NodeCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_NodeCategory" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_NodeCategoryNode_iot_Node_NodeId" FOREIGN KEY ("NodeId") REFERENCES "iot_Node" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_OrganNode" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganNode" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "OrganId" TEXT NOT NULL,
    "NodeId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_OrganNode_iot_Node_NodeId" FOREIGN KEY ("NodeId") REFERENCES "iot_Node" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganNode_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("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_Api" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Api" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Path" TEXT NULL,
    "Command" TEXT NULL,
    "Method" TEXT NULL,
    "ProductId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_Api_iot_Product_ProductId" FOREIGN KEY ("ProductId") REFERENCES "iot_Product" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_Device" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Device" 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_Device_iot_Node_NodeId" FOREIGN KEY ("NodeId") REFERENCES "iot_Node" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_Device_iot_Product_ProductId" FOREIGN KEY ("ProductId") REFERENCES "iot_Product" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_SceneTimer" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_SceneTimer" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "Name" TEXT NULL,
    "Cron" TEXT NULL,
    "Disabled" INTEGER NOT NULL,
    "SceneId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_SceneTimer_iot_Scene_SceneId" FOREIGN KEY ("SceneId") REFERENCES "iot_Scene" ("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_Parameter" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Parameter" 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_Parameter_iot_Api_ApiId" FOREIGN KEY ("ApiId") REFERENCES "iot_Api" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_Command" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Command" 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_Command_iot_Api_ApiId" FOREIGN KEY ("ApiId") REFERENCES "iot_Api" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_Command_iot_Device_DeviceId" FOREIGN KEY ("DeviceId") REFERENCES "iot_Device" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_Data" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Data" 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_Data_iot_Device_DeviceId" FOREIGN KEY ("DeviceId") REFERENCES "iot_Device" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_SceneCommand" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_SceneCommand" PRIMARY KEY,
    "IsDeleted" TEXT NULL,
    "SceneId" TEXT NOT NULL,
    "CommandId" TEXT NOT NULL,
    CONSTRAINT "FK_iot_SceneCommand_iot_Command_CommandId" FOREIGN KEY ("CommandId") REFERENCES "iot_Command" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_SceneCommand_iot_Scene_SceneId" FOREIGN KEY ("SceneId") REFERENCES "iot_Scene" ("Id") ON DELETE CASCADE
);

CREATE TABLE "iot_SceneTigger" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_SceneTigger" 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_SceneTigger_iot_Data_DataId" FOREIGN KEY ("DataId") REFERENCES "iot_Data" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_SceneTigger_iot_Scene_SceneId" FOREIGN KEY ("SceneId") REFERENCES "iot_Scene" ("Id") ON DELETE CASCADE
);

CREATE UNIQUE INDEX "IX_iot_Api_ProductId_Name" ON "iot_Api" ("ProductId", "Name");

CREATE UNIQUE INDEX "IX_iot_Category_Number" ON "iot_Category" ("Number");

CREATE INDEX "IX_iot_Command_ApiId" ON "iot_Command" ("ApiId");

CREATE INDEX "IX_iot_Command_DeviceId" ON "iot_Command" ("DeviceId");

CREATE INDEX "IX_iot_Data_DeviceId" ON "iot_Data" ("DeviceId");

CREATE INDEX "IX_iot_Device_NodeId" ON "iot_Device" ("NodeId");

CREATE UNIQUE INDEX "IX_iot_Device_Number" ON "iot_Device" ("Number");

CREATE INDEX "IX_iot_Device_ProductId" ON "iot_Device" ("ProductId");

CREATE UNIQUE INDEX "IX_iot_Node_Number" ON "iot_Node" ("Number");

CREATE UNIQUE INDEX "IX_iot_NodeCategory_Name" ON "iot_NodeCategory" ("Name");

CREATE INDEX "IX_iot_NodeCategoryNode_NodeId" ON "iot_NodeCategoryNode" ("NodeId");

CREATE UNIQUE INDEX "IX_iot_NodeCategoryNode_CategoryId_NodeId" ON "iot_NodeCategoryNode" ("CategoryId", "NodeId");

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

CREATE INDEX "IX_iot_OrganNode_NodeId" ON "iot_OrganNode" ("NodeId");

CREATE UNIQUE INDEX "IX_iot_OrganNode_OrganId_NodeId" ON "iot_OrganNode" ("OrganId", "NodeId");

CREATE INDEX "IX_iot_Parameter_ApiId" ON "iot_Parameter" ("ApiId");

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 INDEX "IX_iot_Product_CategoryId" ON "iot_Product" ("CategoryId");

CREATE UNIQUE INDEX "IX_iot_Product_Number" ON "iot_Product" ("Number");

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 INDEX "IX_iot_Scene_NodeId" ON "iot_Scene" ("NodeId");

CREATE INDEX "IX_iot_SceneCommand_CommandId" ON "iot_SceneCommand" ("CommandId");

CREATE INDEX "IX_iot_SceneCommand_SceneId" ON "iot_SceneCommand" ("SceneId");

CREATE INDEX "IX_iot_SceneTigger_DataId" ON "iot_SceneTigger" ("DataId");

CREATE INDEX "IX_iot_SceneTigger_SceneId" ON "iot_SceneTigger" ("SceneId");

CREATE INDEX "IX_iot_SceneTimer_SceneId" ON "iot_SceneTimer" ("SceneId");

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

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

