﻿CREATE TABLE `iot_Area` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_Area` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Area_iot_Area_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_Area` (`Id`) ON DELETE SET NULL
);

CREATE TABLE `iot_Category` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Image` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_Category` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Category_iot_Category_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_Category` (`Id`) ON DELETE RESTRICT
);

CREATE TABLE `iot_LiveRecord` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DeviceNumber` longtext CHARACTER SET utf8mb4 NOT NULL,
    `DeviceName` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Value` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_LiveRecord` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_Node` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `IsOnline` tinyint(1) NOT NULL,
    `Image` longtext CHARACTER SET utf8mb4 NULL,
    `Version` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    CONSTRAINT `PK_iot_Node` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_NodeCategory` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    `Name` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Template` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_NodeCategory` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_NodeCategory_iot_NodeCategory_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_NodeCategory` (`Id`) ON DELETE RESTRICT
);

CREATE TABLE `iot_PermissionCategory` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_PermissionCategory` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_PermissionCategory_iot_PermissionCategory_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_PermissionCategory` (`Id`) ON DELETE SET NULL
);

CREATE TABLE `iot_Role` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `Number` longtext CHARACTER SET utf8mb4 NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    CONSTRAINT `PK_iot_Role` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_Setting` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Value` longtext CHARACTER SET utf8mb4 NULL,
    `Type` int NOT NULL,
    CONSTRAINT `PK_iot_Setting` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_Site` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `Icon` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `Key` longtext CHARACTER SET utf8mb4 NULL,
    `Home` longtext CHARACTER SET utf8mb4 NULL,
    `Login` longtext CHARACTER SET utf8mb4 NULL,
    `Logout` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_Site` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_User` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `UserName` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `SecurityStamp` longtext CHARACTER SET utf8mb4 NULL,
    `PasswordHash` longtext CHARACTER SET utf8mb4 NULL,
    `PasswordConfirmed` tinyint(1) NOT NULL,
    `Email` varchar(255) CHARACTER SET utf8mb4 NULL,
    `EmailConfirmed` tinyint(1) NOT NULL,
    `PhoneNumber` varchar(255) CHARACTER SET utf8mb4 NULL,
    `PhoneNumberConfirmed` tinyint(1) NOT NULL,
    `RealName` longtext CHARACTER SET utf8mb4 NULL,
    `IdentityNumber` longtext CHARACTER SET utf8mb4 NULL,
    `IdentityConfirmed` tinyint(1) NOT NULL,
    `NickName` longtext CHARACTER SET utf8mb4 NULL,
    `Avatar` longtext CHARACTER SET utf8mb4 NULL,
    `Sex` int NULL,
    `Birthday` datetime(6) NULL,
    `LockoutEnabled` tinyint(1) NOT NULL,
    `AccessFailedCount` int NOT NULL,
    `LockoutEnd` datetime(6) NULL,
    `RowVersion` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_User` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_Organ` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `Image` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `AreaId` char(36) NULL,
    CONSTRAINT `PK_iot_Organ` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Organ_iot_Area_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_Area` (`Id`) ON DELETE SET NULL,
    CONSTRAINT `FK_iot_Organ_iot_Organ_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_Organ` (`Id`) ON DELETE SET NULL
);

CREATE TABLE `iot_Product` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `Image` longtext CHARACTER SET utf8mb4 NULL,
    `Path` longtext CHARACTER SET utf8mb4 NULL,
    `ApiJson` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `CategoryId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Product` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Product_iot_Category_CategoryId` FOREIGN KEY (`CategoryId`) REFERENCES `iot_Category` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_Scene` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Image` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    `DisplayOrder` int NOT NULL,
    `NodeId` char(36) NULL,
    CONSTRAINT `PK_iot_Scene` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Scene_iot_Node_NodeId` FOREIGN KEY (`NodeId`) REFERENCES `iot_Node` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_NodeCategoryNode` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `CategoryId` char(36) NOT NULL,
    `NodeId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_NodeCategoryNode` PRIMARY KEY (`Id`),
    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_Permission` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `CategoryId` char(36) NULL,
    CONSTRAINT `PK_iot_Permission` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Permission_iot_PermissionCategory_CategoryId` FOREIGN KEY (`CategoryId`) REFERENCES `iot_PermissionCategory` (`Id`) ON DELETE SET NULL
);

CREATE TABLE `iot_UserRole` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `UserId` char(36) NOT NULL,
    `RoleId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_UserRole` PRIMARY KEY (`Id`),
    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_Department` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `OrganId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Department` PRIMARY KEY (`Id`),
    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 SET NULL
);

CREATE TABLE `iot_OrganNode` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `OrganId` char(36) NOT NULL,
    `NodeId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_OrganNode` PRIMARY KEY (`Id`),
    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_OrganScene` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Image` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    `DisplayOrder` int NOT NULL,
    `OrganId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_OrganScene` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_OrganScene_iot_Organ_OrganId` FOREIGN KEY (`OrganId`) REFERENCES `iot_Organ` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_OrganUser` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `OrganId` char(36) NOT NULL,
    `UserId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_OrganUser` PRIMARY KEY (`Id`),
    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_Api` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Path` longtext CHARACTER SET utf8mb4 NULL,
    `Command` longtext CHARACTER SET utf8mb4 NULL,
    `Method` longtext CHARACTER SET utf8mb4 NULL,
    `ProductId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Api` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Api_iot_Product_ProductId` FOREIGN KEY (`ProductId`) REFERENCES `iot_Product` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_Device` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `Gateway` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayName` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Tag` longtext CHARACTER SET utf8mb4 NULL,
    `IsOnline` tinyint(1) NOT NULL,
    `Icon` longtext CHARACTER SET utf8mb4 NULL,
    `Ip` longtext CHARACTER SET utf8mb4 NULL,
    `UserName` longtext CHARACTER SET utf8mb4 NULL,
    `Password` longtext CHARACTER SET utf8mb4 NULL,
    `ConnectId` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Disabled` tinyint(1) NOT NULL,
    `ProductId` char(36) NOT NULL,
    `NodeId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Device` PRIMARY KEY (`Id`),
    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` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Cron` longtext CHARACTER SET utf8mb4 NULL,
    `Disabled` tinyint(1) NOT NULL,
    `SceneId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_SceneTimer` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_SceneTimer_iot_Scene_SceneId` FOREIGN KEY (`SceneId`) REFERENCES `iot_Scene` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_RolePermission` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `RoleId` char(36) NOT NULL,
    `PermissionId` char(36) NOT NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    CONSTRAINT `PK_iot_RolePermission` PRIMARY KEY (`Id`),
    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_DepartmentUser` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `UserId` char(36) NOT NULL,
    `DeparementId` char(36) NOT NULL,
    `DepartmentId` char(36) NULL,
    CONSTRAINT `PK_iot_DepartmentUser` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_DepartmentUser_iot_Department_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `iot_Department` (`Id`) ON DELETE RESTRICT,
    CONSTRAINT `FK_iot_DepartmentUser_iot_User_UserId` FOREIGN KEY (`UserId`) REFERENCES `iot_User` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_OrganSceneTimer` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Cron` longtext CHARACTER SET utf8mb4 NULL,
    `Disabled` tinyint(1) NOT NULL,
    `OrganSceneId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_OrganSceneTimer` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_OrganSceneTimer_iot_OrganScene_OrganSceneId` FOREIGN KEY (`OrganSceneId`) REFERENCES `iot_OrganScene` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_Parameter` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Type` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `Required` tinyint(1) NOT NULL,
    `Maxinum` longtext CHARACTER SET utf8mb4 NULL,
    `Minimum` longtext CHARACTER SET utf8mb4 NULL,
    `ApiId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Parameter` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Parameter_iot_Api_ApiId` FOREIGN KEY (`ApiId`) REFERENCES `iot_Api` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_Command` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Disabled` tinyint(1) NOT NULL,
    `Delay` int NOT NULL,
    `QueryString` longtext CHARACTER SET utf8mb4 NULL,
    `ApiId` char(36) NOT NULL,
    `DeviceId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Command` PRIMARY KEY (`Id`),
    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` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Key` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Value` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Type` int NOT NULL,
    `Unit` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `Timestamp` bigint NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    `DeviceId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_Data` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Data_iot_Device_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `iot_Device` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_OrganSceneCommand` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `OrganSceneId` char(36) NOT NULL,
    `CommandId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_OrganSceneCommand` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_OrganSceneCommand_iot_Command_CommandId` FOREIGN KEY (`CommandId`) REFERENCES `iot_Command` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_OrganSceneCommand_iot_OrganScene_OrganSceneId` FOREIGN KEY (`OrganSceneId`) REFERENCES `iot_OrganScene` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_SceneCommand` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `SceneId` char(36) NOT NULL,
    `CommandId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_SceneCommand` PRIMARY KEY (`Id`),
    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_OrganSceneTigger` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Condition` longtext CHARACTER SET utf8mb4 NULL,
    `Disabled` tinyint(1) NOT NULL,
    `DataId` char(36) NOT NULL,
    `OrganSceneId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_OrganSceneTigger` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_OrganSceneTigger_iot_Data_DataId` FOREIGN KEY (`DataId`) REFERENCES `iot_Data` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_OrganSceneTigger_iot_OrganScene_OrganSceneId` FOREIGN KEY (`OrganSceneId`) REFERENCES `iot_OrganScene` (`Id`) ON DELETE CASCADE
);

CREATE TABLE `iot_SceneTigger` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Condition` longtext CHARACTER SET utf8mb4 NULL,
    `Disabled` tinyint(1) NOT NULL,
    `DataId` char(36) NOT NULL,
    `SceneId` char(36) NOT NULL,
    CONSTRAINT `PK_iot_SceneTigger` PRIMARY KEY (`Id`),
    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_Area_Number` ON `iot_Area` (`Number`);

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

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

CREATE INDEX `IX_iot_Category_ParentId` ON `iot_Category` (`ParentId`);

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 UNIQUE INDEX `IX_iot_Department_Number` ON `iot_Department` (`Number`);

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_UserId` ON `iot_DepartmentUser` (`UserId`);

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 UNIQUE INDEX `IX_iot_NodeCategory_Number` ON `iot_NodeCategory` (`Number`);

CREATE INDEX `IX_iot_NodeCategory_ParentId` ON `iot_NodeCategory` (`ParentId`);

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_Organ_ParentId` ON `iot_Organ` (`ParentId`);

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_OrganScene_OrganId` ON `iot_OrganScene` (`OrganId`);

CREATE INDEX `IX_iot_OrganSceneCommand_CommandId` ON `iot_OrganSceneCommand` (`CommandId`);

CREATE INDEX `IX_iot_OrganSceneCommand_OrganSceneId` ON `iot_OrganSceneCommand` (`OrganSceneId`);

CREATE INDEX `IX_iot_OrganSceneTigger_DataId` ON `iot_OrganSceneTigger` (`DataId`);

CREATE INDEX `IX_iot_OrganSceneTigger_OrganSceneId` ON `iot_OrganSceneTigger` (`OrganSceneId`);

CREATE INDEX `IX_iot_OrganSceneTimer_OrganSceneId` ON `iot_OrganSceneTimer` (`OrganSceneId`);

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_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_Site_Name` ON `iot_Site` (`Name`);

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

