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


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


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


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


CREATE TABLE `iot_Site` (
    `Id` char(36) NOT 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,
    `Disabled` tinyint(1) NOT NULL,
    `DisplayOrder` int NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_Site` PRIMARY KEY (`Id`)
);


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


CREATE TABLE `iot_Organ` (
    `Id` char(36) NOT NULL,
    `Type` int NOT NULL,
    `CustomType` longtext CHARACTER SET utf8mb4 NULL,
    `Image` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `AreaId` char(36) NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` varchar(255) CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    CONSTRAINT `PK_iot_Organ` PRIMARY KEY (`Id`),
    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_IoTProduct` (
    `Id` char(36) NOT 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,
    `IoTProductCategoryId` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTProduct` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTProduct_iot_IoTProductCategory_IoTProductCategoryId` FOREIGN KEY (`IoTProductCategoryId`) REFERENCES `iot_IoTProductCategory` (`Id`) ON DELETE SET NULL
);


CREATE TABLE `iot_Building` (
    `Id` char(36) NOT NULL,
    `Type` int NOT NULL,
    `CustomType` longtext CHARACTER SET utf8mb4 NULL,
    `Image` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `OrganId` char(36) NOT NULL,
    `AreaId` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    CONSTRAINT `PK_iot_Building` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Building_iot_Area_AreaId` FOREIGN KEY (`AreaId`) REFERENCES `iot_Area` (`Id`) ON DELETE RESTRICT,
    CONSTRAINT `FK_iot_Building_iot_Building_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_Building` (`Id`) ON DELETE RESTRICT,
    CONSTRAINT `FK_iot_Building_iot_Organ_OrganId` FOREIGN KEY (`OrganId`) REFERENCES `iot_Organ` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_Department` (
    `Id` char(36) NOT NULL,
    `Type` int NOT NULL,
    `CustomType` longtext CHARACTER SET utf8mb4 NULL,
    `OrganId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    CONSTRAINT `PK_iot_Department` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Department_iot_Department_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_Department` (`Id`) ON DELETE RESTRICT,
    CONSTRAINT `FK_iot_Department_iot_Organ_OrganId` FOREIGN KEY (`OrganId`) REFERENCES `iot_Organ` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_OrganRole` (
    `Id` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    `OrganId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_OrganRole` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_OrganRole_iot_Organ_OrganId` FOREIGN KEY (`OrganId`) REFERENCES `iot_Organ` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_OrganUser` (
    `Id` char(36) NOT NULL,
    `Type` int NOT NULL,
    `CustomType` longtext CHARACTER SET utf8mb4 NULL,
    `OrganId` char(36) NOT NULL,
    `UserId` char(36) NOT NULL,
    `IsDefault` tinyint(1) NOT NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 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_PermissionCategory` (
    `Id` char(36) NOT NULL,
    `OrganId` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Number` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Left` int NOT NULL,
    `Right` int NOT NULL,
    `ParentId` char(36) NULL,
    CONSTRAINT `PK_iot_PermissionCategory` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_PermissionCategory_iot_Organ_OrganId` FOREIGN KEY (`OrganId`) REFERENCES `iot_Organ` (`Id`) ON DELETE RESTRICT,
    CONSTRAINT `FK_iot_PermissionCategory_iot_PermissionCategory_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_PermissionCategory` (`Id`) ON DELETE RESTRICT
);


CREATE TABLE `iot_Title` (
    `Id` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `MinLevel` int NOT NULL,
    `MaxLevel` int NOT NULL,
    `OrganId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_Title` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_Title_iot_Organ_OrganId` FOREIGN KEY (`OrganId`) REFERENCES `iot_Organ` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_IoTApi` (
    `Id` char(36) NOT 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,
    `IoTProductId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTApi` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTApi_iot_IoTProduct_IoTProductId` FOREIGN KEY (`IoTProductId`) REFERENCES `iot_IoTProduct` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_IoTGateway` (
    `Id` char(36) 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,
    `Password` longtext CHARACTER SET utf8mb4 NULL,
    `DisplayOrder` int NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    `BuildingId` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTGateway` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTGateway_iot_Building_BuildingId` FOREIGN KEY (`BuildingId`) REFERENCES `iot_Building` (`Id`) ON DELETE SET NULL
);


CREATE TABLE `iot_IoTScene` (
    `Id` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Image` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    `DisplayOrder` int NOT NULL,
    `BuildingId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTScene` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTScene_iot_Building_BuildingId` FOREIGN KEY (`BuildingId`) REFERENCES `iot_Building` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_OrganUserRole` (
    `Id` char(36) NOT NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    `OrganUserId` char(36) NOT NULL,
    `OrganRoleId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_OrganUserRole` PRIMARY KEY (`Id`),
    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` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    `Type` int NOT NULL,
    `CategoryId` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 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_Job` (
    `Id` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Level` int NOT NULL,
    `TitleId` char(36) NOT NULL,
    `DepartmentId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_Job` PRIMARY KEY (`Id`),
    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` char(36) NOT 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,
    `IoTApiId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTParameter` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTParameter_iot_IoTApi_IoTApiId` FOREIGN KEY (`IoTApiId`) REFERENCES `iot_IoTApi` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_IoTDevice` (
    `Id` char(36) NOT 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,
    `IoTProductId` char(36) NOT NULL,
    `IoTGatewayId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTDevice` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTDevice_iot_IoTGateway_IoTGatewayId` FOREIGN KEY (`IoTGatewayId`) REFERENCES `iot_IoTGateway` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_IoTDevice_iot_IoTProduct_IoTProductId` FOREIGN KEY (`IoTProductId`) REFERENCES `iot_IoTProduct` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_IoTTimer` (
    `Id` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Cron` longtext CHARACTER SET utf8mb4 NULL,
    `Disabled` tinyint(1) NOT NULL,
    `IoTSceneId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTTimer` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTTimer_iot_IoTScene_IoTSceneId` FOREIGN KEY (`IoTSceneId`) REFERENCES `iot_IoTScene` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_OrganRolePermission` (
    `Id` char(36) NOT NULL,
    `RoleId` char(36) NOT NULL,
    `PermissionId` char(36) NOT NULL,
    `IsReadOnly` tinyint(1) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_OrganRolePermission` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_OrganRolePermission_iot_OrganRole_RoleId` FOREIGN KEY (`RoleId`) REFERENCES `iot_OrganRole` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_OrganRolePermission_iot_Permission_PermissionId` FOREIGN KEY (`PermissionId`) REFERENCES `iot_Permission` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_DepartmentUser` (
    `Id` char(36) NOT NULL,
    `Position` longtext CHARACTER SET utf8mb4 NULL,
    `Type` int NOT NULL,
    `CustomType` longtext CHARACTER SET utf8mb4 NULL,
    `UserId` char(36) NOT NULL,
    `DeparementId` char(36) NOT NULL,
    `JobId` char(36) NULL,
    `DepartmentId` char(36) NULL,
    `OrganUserId` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 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_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_IoTCommand` (
    `Id` char(36) NOT 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,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTCommand` PRIMARY KEY (`Id`),
    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` char(36) NOT NULL,
    `Key` longtext CHARACTER SET utf8mb4 NOT NULL,
    `Value` longtext CHARACTER SET utf8mb4 NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `DataType` int NOT NULL,
    `ValueType` int NOT NULL,
    `ValueSchema` longtext CHARACTER SET utf8mb4 NULL,
    `Unit` longtext CHARACTER SET utf8mb4 NULL,
    `Description` longtext CHARACTER SET utf8mb4 NULL,
    `Timestamp` bigint NOT NULL,
    `Hidden` tinyint(1) NOT NULL,
    `IoTDeviceId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTData` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTData_iot_IoTDevice_IoTDeviceId` FOREIGN KEY (`IoTDeviceId`) REFERENCES `iot_IoTDevice` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_IoTSceneIoTCommand` (
    `Id` char(36) NOT NULL,
    `IoTSceneId` char(36) NOT NULL,
    `IoTCommandId` char(36) NOT NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTSceneIoTCommand` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTSceneIoTCommand_iot_IoTCommand_IoTCommandId` FOREIGN KEY (`IoTCommandId`) REFERENCES `iot_IoTCommand` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_IoTSceneIoTCommand_iot_IoTScene_IoTSceneId` FOREIGN KEY (`IoTSceneId`) REFERENCES `iot_IoTScene` (`Id`) ON DELETE CASCADE
);


CREATE TABLE `iot_IoTTigger` (
    `Id` char(36) NOT NULL,
    `Name` longtext CHARACTER SET utf8mb4 NULL,
    `Type` int NOT NULL,
    `Condition` longtext CHARACTER SET utf8mb4 NULL,
    `Start` datetime(6) NOT NULL,
    `End` datetime(6) NOT NULL,
    `Disabled` tinyint(1) NOT NULL,
    `IoTDataId` char(36) NOT NULL,
    `IoTSceneId` char(36) NOT NULL,
    `IoTDataId1` char(36) NULL,
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL,
    CONSTRAINT `PK_iot_IoTTigger` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTTigger_iot_IoTData_IoTDataId` FOREIGN KEY (`IoTDataId`) REFERENCES `iot_IoTData` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_IoTTigger_iot_IoTData_IoTDataId1` FOREIGN KEY (`IoTDataId1`) REFERENCES `iot_IoTData` (`Id`) ON DELETE RESTRICT,
    CONSTRAINT `FK_iot_IoTTigger_iot_IoTScene_IoTSceneId` FOREIGN KEY (`IoTSceneId`) REFERENCES `iot_IoTScene` (`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_OrganId_ParentId_Number` ON `iot_Building` (`OrganId`, `ParentId`, `Number`);


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


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


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


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_IoTProductId_Name` ON `iot_IoTApi` (`IoTProductId`, `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_IoTDeviceId` ON `iot_IoTData` (`IoTDeviceId`);


CREATE INDEX `IX_iot_IoTDevice_IoTGatewayId` ON `iot_IoTDevice` (`IoTGatewayId`);


CREATE INDEX `IX_iot_IoTDevice_IoTProductId` ON `iot_IoTDevice` (`IoTProductId`);


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


CREATE INDEX `IX_iot_IoTGateway_BuildingId` ON `iot_IoTGateway` (`BuildingId`);


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


CREATE INDEX `IX_iot_IoTParameter_IoTApiId` ON `iot_IoTParameter` (`IoTApiId`);


CREATE INDEX `IX_iot_IoTProduct_IoTProductCategoryId` ON `iot_IoTProduct` (`IoTProductCategoryId`);


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_BuildingId` ON `iot_IoTScene` (`BuildingId`);


CREATE INDEX `IX_iot_IoTSceneIoTCommand_IoTCommandId` ON `iot_IoTSceneIoTCommand` (`IoTCommandId`);


CREATE INDEX `IX_iot_IoTSceneIoTCommand_IoTSceneId` ON `iot_IoTSceneIoTCommand` (`IoTSceneId`);


CREATE INDEX `IX_iot_IoTTigger_IoTDataId` ON `iot_IoTTigger` (`IoTDataId`);


CREATE INDEX `IX_iot_IoTTigger_IoTDataId1` ON `iot_IoTTigger` (`IoTDataId1`);


CREATE INDEX `IX_iot_IoTTigger_IoTSceneId` ON `iot_IoTTigger` (`IoTSceneId`);


CREATE INDEX `IX_iot_IoTTimer_IoTSceneId` ON `iot_IoTTimer` (`IoTSceneId`);


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 UNIQUE INDEX `IX_iot_OrganRole_OrganId_Number` ON `iot_OrganRole` (`OrganId`, `Number`);


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


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


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


CREATE INDEX `IX_iot_OrganUser_UserId` ON `iot_OrganUser` (`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_PermissionCategory_OrganId` ON `iot_PermissionCategory` (`OrganId`);


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


CREATE UNIQUE INDEX `IX_iot_Setting_Name` ON `iot_Setting` (`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`);


