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


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


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


CREATE TABLE `iot_PermissionCategory` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Number` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Number',
    `DisplayOrder` int NOT NULL COMMENT 'DisplayOrder',
    `Left` int NOT NULL COMMENT 'Left',
    `Right` int NOT NULL COMMENT 'Right',
    `ParentId` char(36) NULL COMMENT 'ParentId',
    CONSTRAINT `PK_iot_PermissionCategory` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_PermissionCategory_iot_PermissionCategory_ParentId` FOREIGN KEY (`ParentId`) REFERENCES `iot_PermissionCategory` (`Id`) ON DELETE RESTRICT
) COMMENT '权限分类';


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


CREATE TABLE `iot_Site` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT 'Name',
    `Icon` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Icon',
    `Description` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Description',
    `Key` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Key',
    `Home` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Home',
    `Login` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Login',
    `Logout` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Logout',
    `Disabled` tinyint(1) NOT NULL COMMENT 'Disabled',
    `DisplayOrder` int NOT NULL COMMENT 'DisplayOrder',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_Site` PRIMARY KEY (`Id`)
) COMMENT '应用';


CREATE TABLE `iot_Statistic` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Key` varchar(255) CHARACTER SET utf8mb4 NULL COMMENT 'Key',
    `Value` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Value',
    `IntValue` int NULL COMMENT 'IntValue',
    `DoubleValue` double NULL COMMENT 'DoubleValue',
    `DateTimeValue` datetime(6) NULL COMMENT 'DateTimeValue',
    `Type` int NOT NULL COMMENT 'Type',
    `UpdateAt` datetime(6) NOT NULL COMMENT 'UpdateAt',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_Statistic` PRIMARY KEY (`Id`)
) COMMENT '数据统计';


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


CREATE TABLE `iot_Organ` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Type` int NOT NULL COMMENT 'Type',
    `CustomType` longtext CHARACTER SET utf8mb4 NULL COMMENT 'CustomType',
    `Image` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Image',
    `Description` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Description',
    `AreaId` char(36) NULL COMMENT 'AreaId',
    `IsReadOnly` tinyint(1) NOT NULL COMMENT 'IsReadOnly',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    `Name` varchar(255) CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL COMMENT 'Number',
    `DisplayOrder` int NOT NULL COMMENT 'DisplayOrder',
    `Left` int NOT NULL COMMENT 'Left',
    `Right` int NOT NULL COMMENT 'Right',
    `ParentId` char(36) NULL COMMENT 'ParentId',
    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
) COMMENT '机构';


CREATE TABLE `iot_IoTProduct` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT '产品名称',
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT '产品型号',
    `Image` longtext CHARACTER SET utf8mb4 NULL COMMENT '图标',
    `Path` longtext CHARACTER SET utf8mb4 NULL COMMENT '路径',
    `ApiJson` longtext CHARACTER SET utf8mb4 NULL COMMENT 'ApiJson',
    `DisplayOrder` int NOT NULL COMMENT '序号',
    `IoTProductCategoryId` char(36) NULL COMMENT 'IoTProductCategoryId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '产品';


CREATE TABLE `iot_Permission` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT 'Name',
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT 'Number',
    `IsReadOnly` tinyint(1) NOT NULL COMMENT 'IsReadOnly',
    `Type` int NOT NULL COMMENT 'Type',
    `CategoryId` char(36) NULL COMMENT 'CategoryId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '权限';


CREATE TABLE `iot_Building` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Type` int NOT NULL COMMENT 'Type',
    `CustomType` longtext CHARACTER SET utf8mb4 NULL COMMENT 'CustomType',
    `Image` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Image',
    `Description` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Description',
    `OrganId` char(36) NOT NULL COMMENT 'OrganId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL COMMENT 'Number',
    `DisplayOrder` int NOT NULL COMMENT 'DisplayOrder',
    `Left` int NOT NULL COMMENT 'Left',
    `Right` int NOT NULL COMMENT 'Right',
    `ParentId` char(36) NULL COMMENT 'ParentId',
    CONSTRAINT `PK_iot_Building` PRIMARY KEY (`Id`),
    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
) COMMENT '房间';


CREATE TABLE `iot_Department` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Type` int NOT NULL COMMENT 'Type',
    `CustomType` longtext CHARACTER SET utf8mb4 NULL COMMENT 'CustomType',
    `OrganId` char(36) NOT NULL COMMENT 'OrganId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Number` varchar(255) CHARACTER SET utf8mb4 NULL COMMENT 'Number',
    `DisplayOrder` int NOT NULL COMMENT 'DisplayOrder',
    `Left` int NOT NULL COMMENT 'Left',
    `Right` int NOT NULL COMMENT 'Right',
    `ParentId` char(36) NULL COMMENT 'ParentId',
    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
) COMMENT '部门';


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


CREATE TABLE `iot_OrganUser` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Type` int NOT NULL COMMENT 'Type',
    `CustomType` longtext CHARACTER SET utf8mb4 NULL COMMENT 'CustomType',
    `OrganId` char(36) NOT NULL COMMENT 'OrganId',
    `UserId` char(36) NOT NULL COMMENT 'UserId',
    `IsDefault` tinyint(1) NOT NULL COMMENT 'IsDefault',
    `IsReadOnly` tinyint(1) NOT NULL COMMENT 'IsReadOnly',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '机构用户';


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


CREATE TABLE `iot_IoTApi` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` varchar(255) CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Path` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Path',
    `Command` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Command',
    `Method` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Method',
    `IoTProductId` char(36) NOT NULL COMMENT 'IoTProductId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_IoTApi` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTApi_iot_IoTProduct_IoTProductId` FOREIGN KEY (`IoTProductId`) REFERENCES `iot_IoTProduct` (`Id`) ON DELETE CASCADE
) COMMENT '接口';


CREATE TABLE `iot_IoTGateway` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT '节点名称',
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT '节点编号',
    `IsOnline` tinyint(1) NOT NULL COMMENT '在线',
    `Image` longtext CHARACTER SET utf8mb4 NULL COMMENT '图片',
    `Version` longtext CHARACTER SET utf8mb4 NULL COMMENT '版本',
    `Password` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Password',
    `DisplayOrder` int NOT NULL COMMENT '序号',
    `Hidden` tinyint(1) NOT NULL COMMENT '隐藏',
    `BuildingId` char(36) NULL COMMENT 'BuildingId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '网关';


CREATE TABLE `iot_IoTScene` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '名称',
    `Image` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '图标',
    `Hidden` tinyint(1) NOT NULL COMMENT '隐藏',
    `DisplayOrder` int NOT NULL COMMENT '序号',
    `BuildingId` char(36) NOT NULL COMMENT 'BuildingId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_IoTScene` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTScene_iot_Building_BuildingId` FOREIGN KEY (`BuildingId`) REFERENCES `iot_Building` (`Id`) ON DELETE CASCADE
) COMMENT '机构场景';


CREATE TABLE `iot_OrganRolePermission` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `RoleId` char(36) NOT NULL COMMENT 'RoleId',
    `PermissionId` char(36) NOT NULL COMMENT 'PermissionId',
    `IsReadOnly` tinyint(1) NOT NULL COMMENT 'IsReadOnly',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '角色权限';


CREATE TABLE `iot_OrganUserRole` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `IsReadOnly` tinyint(1) NOT NULL COMMENT 'IsReadOnly',
    `OrganUserId` char(36) NOT NULL COMMENT 'OrganUserId',
    `OrganRoleId` char(36) NOT NULL COMMENT 'OrganRoleId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '用户角色';


CREATE TABLE `iot_Job` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Level` int NOT NULL COMMENT 'Level',
    `TitleId` char(36) NOT NULL COMMENT 'TitleId',
    `DepartmentId` char(36) NOT NULL COMMENT 'DepartmentId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '职位';


CREATE TABLE `iot_IoTParameter` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Type` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Type',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Description` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Description',
    `Required` tinyint(1) NOT NULL COMMENT 'Required',
    `Maxinum` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Maxinum',
    `Minimum` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Minimum',
    `IoTApiId` char(36) NOT NULL COMMENT 'IoTApiId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_IoTParameter` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTParameter_iot_IoTApi_IoTApiId` FOREIGN KEY (`IoTApiId`) REFERENCES `iot_IoTApi` (`Id`) ON DELETE CASCADE
) COMMENT '参数';


CREATE TABLE `iot_IoTDevice` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT '名称',
    `Number` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT '编号',
    `Gateway` longtext CHARACTER SET utf8mb4 NULL COMMENT '网关编号',
    `DisplayName` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '显示名称',
    `Tag` longtext CHARACTER SET utf8mb4 NULL COMMENT '标记',
    `IsOnline` tinyint(1) NOT NULL COMMENT '在线状态',
    `Icon` longtext CHARACTER SET utf8mb4 NULL COMMENT '图标',
    `Ip` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IP',
    `UserName` longtext CHARACTER SET utf8mb4 NULL COMMENT '用户名',
    `Password` longtext CHARACTER SET utf8mb4 NULL COMMENT '密码',
    `ConnectId` longtext CHARACTER SET utf8mb4 NULL COMMENT '连接Id',
    `DisplayOrder` int NOT NULL COMMENT '序号',
    `Disabled` tinyint(1) NOT NULL COMMENT '隐藏',
    `IoTProductId` char(36) NOT NULL COMMENT '产品Id',
    `IoTGatewayId` char(36) NOT NULL COMMENT '节点Id',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '设备';


CREATE TABLE `iot_IoTTimer` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Cron` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Cron',
    `Disabled` tinyint(1) NOT NULL COMMENT '禁用',
    `IoTSceneId` char(36) NOT NULL COMMENT 'IoTSceneId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_IoTTimer` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTTimer_iot_IoTScene_IoTSceneId` FOREIGN KEY (`IoTSceneId`) REFERENCES `iot_IoTScene` (`Id`) ON DELETE CASCADE
) COMMENT '机构定时器';


CREATE TABLE `iot_DepartmentUser` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Position` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Position',
    `Type` int NOT NULL COMMENT 'Type',
    `CustomType` longtext CHARACTER SET utf8mb4 NULL COMMENT 'CustomType',
    `OrganUserId` char(36) NOT NULL COMMENT 'OrganUserId',
    `DeparementId` char(36) NOT NULL COMMENT 'DeparementId',
    `JobId` char(36) NULL COMMENT 'JobId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_DepartmentUser` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_DepartmentUser_iot_Department_DeparementId` FOREIGN KEY (`DeparementId`) REFERENCES `iot_Department` (`Id`) ON DELETE CASCADE,
    CONSTRAINT `FK_iot_DepartmentUser_iot_Job_JobId` FOREIGN KEY (`JobId`) REFERENCES `iot_Job` (`Id`) ON DELETE SET NULL,
    CONSTRAINT `FK_iot_DepartmentUser_iot_OrganUser_OrganUserId` FOREIGN KEY (`OrganUserId`) REFERENCES `iot_OrganUser` (`Id`) ON DELETE CASCADE
) COMMENT '部门用户';


CREATE TABLE `iot_IoTCommand` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT '命令名称',
    `DisplayOrder` int NOT NULL COMMENT '序号',
    `Disabled` tinyint(1) NOT NULL COMMENT '隐藏',
    `Delay` int NOT NULL COMMENT '延迟',
    `QueryString` longtext CHARACTER SET utf8mb4 NULL COMMENT 'QueryString',
    `ApiId` char(36) NOT NULL COMMENT 'ApiId',
    `DeviceId` char(36) NOT NULL COMMENT 'DeviceId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '命令';


CREATE TABLE `iot_IoTData` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Key` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '键',
    `Value` longtext CHARACTER SET utf8mb4 NULL COMMENT '值',
    `IntValue` int NULL COMMENT 'IntValue',
    `DoubleValue` double NULL COMMENT 'DoubleValue',
    `DateTimeValue` datetime(6) NULL COMMENT 'DateTimeValue',
    `EnumValues` longtext CHARACTER SET utf8mb4 NULL COMMENT '枚举候选值列表',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT '名称',
    `DataType` int NOT NULL COMMENT '数据分类',
    `ValueType` int NOT NULL COMMENT '数值类型',
    `ValueSchema` longtext CHARACTER SET utf8mb4 NULL COMMENT '对象模式',
    `Unit` longtext CHARACTER SET utf8mb4 NULL COMMENT '单位',
    `Description` longtext CHARACTER SET utf8mb4 NULL COMMENT '描述',
    `Timestamp` bigint NOT NULL COMMENT '时间戳',
    `Hidden` tinyint(1) NOT NULL COMMENT '隐藏',
    `IoTDeviceId` char(36) NOT NULL COMMENT '设备Id',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    CONSTRAINT `PK_iot_IoTData` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_iot_IoTData_iot_IoTDevice_IoTDeviceId` FOREIGN KEY (`IoTDeviceId`) REFERENCES `iot_IoTDevice` (`Id`) ON DELETE CASCADE
) COMMENT '数据';


CREATE TABLE `iot_IoTSceneIoTCommand` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `IoTSceneId` char(36) NOT NULL COMMENT 'IoTSceneId',
    `IoTCommandId` char(36) NOT NULL COMMENT 'IoTCommandId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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
) COMMENT '场景命令';


CREATE TABLE `iot_IoTTigger` (
    `Id` char(36) NOT NULL COMMENT 'Id',
    `Name` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Name',
    `Type` int NOT NULL COMMENT 'Type',
    `Condition` longtext CHARACTER SET utf8mb4 NULL COMMENT 'Condition',
    `Start` datetime(6) NOT NULL COMMENT 'Start',
    `End` datetime(6) NOT NULL COMMENT 'End',
    `Disabled` tinyint(1) NOT NULL COMMENT '禁用',
    `IoTDataId` char(36) NOT NULL COMMENT 'IoTDataId',
    `IoTSceneId` char(36) NOT NULL COMMENT 'IoTSceneId',
    `IsDeleted` longtext CHARACTER SET utf8mb4 NULL COMMENT 'IsDeleted',
    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_IoTScene_IoTSceneId` FOREIGN KEY (`IoTSceneId`) REFERENCES `iot_IoTScene` (`Id`) ON DELETE CASCADE
) COMMENT '机构触发器';


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_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 UNIQUE INDEX `IX_iot_DepartmentUser_DeparementId_OrganUserId` ON `iot_DepartmentUser` (`DeparementId`, `OrganUserId`);


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


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


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_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_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 UNIQUE INDEX `IX_iot_Statistic_Key` ON `iot_Statistic` (`Key`);


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


