CREATE TABLE `iot_Category` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 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`)
);

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,
    `OrganName` longtext CHARACTER SET utf8mb4 NULL,
    `OrganNumber` longtext CHARACTER SET utf8mb4 NOT NULL,
    `CategoryName` longtext CHARACTER SET utf8mb4 NULL,
    `CategoryNumber` longtext CHARACTER SET utf8mb4 NOT 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,
    `Name` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Number` longtext CHARACTER SET utf8mb4 NULL,
    `Template` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    CONSTRAINT `PK_iot_NodeCategory` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_Organ` (
    `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,
    CONSTRAINT `PK_iot_Organ` PRIMARY KEY (`Id`)
);

CREATE TABLE `iot_PermissionCategory` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Path` longtext CHARACTER SET utf8mb4 NULL,
    `ParentId` char(36) 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,
    `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_User` (
    `Id` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `UserName` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `NickName` longtext CHARACTER SET utf8mb4 NULL,
    `Avatar` longtext CHARACTER SET utf8mb4 NULL,
    `SecurityStamp` longtext CHARACTER SET utf8mb4 NULL,
    `PasswordHash` longtext CHARACTER SET utf8mb4 NULL,
    `Email` varchar(255) CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_User` PRIMARY KEY (`Id`)
);

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

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

