﻿CREATE TABLE "iot_AppModule" (
    -- 模块

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_AppModule" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NULL,

    -- Url
    "Url" TEXT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_Area" (
    -- 区域

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Area" PRIMARY KEY,

    -- Type
    "Type" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- Left
    "Left" INTEGER NOT NULL,

    -- Right
    "Right" INTEGER NOT NULL,

    -- ParentId
    "ParentId" TEXT NULL,
    CONSTRAINT "FK_iot_Area_iot_Area_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_Area" ("Id") ON DELETE RESTRICT
);


CREATE TABLE "iot_DictionaryCategory" (
    -- 字典分类

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_DictionaryCategory" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Key
    "Key" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_IoTProductCategory" (
    -- 分类

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTProductCategory" PRIMARY KEY,

    -- 分类图标
    "Image" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- Left
    "Left" INTEGER NOT NULL,

    -- Right
    "Right" INTEGER NOT NULL,

    -- ParentId
    "ParentId" TEXT NULL,
    CONSTRAINT "FK_iot_IoTProductCategory_iot_IoTProductCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_IoTProductCategory" ("Id") ON DELETE RESTRICT
);


CREATE TABLE "iot_LiveRecord" (
    -- 回放

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_LiveRecord" PRIMARY KEY,

    -- DeviceNumber
    "DeviceNumber" TEXT NOT NULL,

    -- DeviceName
    "DeviceName" TEXT NULL,

    -- Name
    "Name" TEXT NULL,

    -- Value
    "Value" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_Setting" (
    -- 配置

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Setting" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Value
    "Value" TEXT NULL,

    -- Type
    "Type" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_Site" (
    -- 应用

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Site" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Icon
    "Icon" TEXT NULL,

    -- Description
    "Description" TEXT NULL,

    -- Key
    "Key" TEXT NULL,

    -- Home
    "Home" TEXT NULL,

    -- Login
    "Login" TEXT NULL,

    -- Logout
    "Logout" TEXT NULL,

    -- Disabled
    "Disabled" INTEGER NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_Statistic" (
    -- 数据统计

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Statistic" PRIMARY KEY,

    -- Key
    "Key" TEXT NULL,

    -- Value
    "Value" TEXT NULL,

    -- IntValue
    "IntValue" INTEGER NULL,

    -- DoubleValue
    "DoubleValue" REAL NULL,

    -- DateTimeValue
    "DateTimeValue" TEXT NULL,

    -- Type
    "Type" INTEGER NOT NULL,

    -- UpdateAt
    "UpdateAt" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_User" (
    -- 用户

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_User" PRIMARY KEY,

    -- UserName
    "UserName" TEXT NOT NULL,

    -- SecurityStamp
    "SecurityStamp" TEXT NULL,

    -- PasswordHash
    "PasswordHash" TEXT NULL,

    -- PasswordConfirmed
    "PasswordConfirmed" INTEGER NOT NULL,

    -- Email
    "Email" TEXT NULL,

    -- EmailConfirmed
    "EmailConfirmed" INTEGER NOT NULL,

    -- PhoneNumber
    "PhoneNumber" TEXT NULL,

    -- PhoneNumberConfirmed
    "PhoneNumberConfirmed" INTEGER NOT NULL,

    -- RealName
    "RealName" TEXT NULL,

    -- IdentityNumber
    "IdentityNumber" TEXT NULL,

    -- IdentityConfirmed
    "IdentityConfirmed" INTEGER NOT NULL,

    -- NickName
    "NickName" TEXT NULL,

    -- Avatar
    "Avatar" TEXT NULL,

    -- Sex
    "Sex" INTEGER NULL,

    -- Birthday
    "Birthday" TEXT NULL,

    -- LockoutEnabled
    "LockoutEnabled" INTEGER NOT NULL,

    -- AccessFailedCount
    "AccessFailedCount" INTEGER NOT NULL,

    -- LockoutEnd
    "LockoutEnd" TEXT NULL,

    -- RowVersion
    "RowVersion" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "iot_PermissionCategory" (
    -- 权限分类

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_PermissionCategory" PRIMARY KEY,

    -- AppModuleId
    "AppModuleId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- Left
    "Left" INTEGER NOT NULL,

    -- Right
    "Right" INTEGER NOT NULL,

    -- ParentId
    "ParentId" TEXT NULL,
    CONSTRAINT "FK_iot_PermissionCategory_iot_AppModule_AppModuleId" FOREIGN KEY ("AppModuleId") REFERENCES "iot_AppModule" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_PermissionCategory_iot_PermissionCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "iot_PermissionCategory" ("Id") ON DELETE RESTRICT
);


CREATE TABLE "iot_Organ" (
    -- 机构

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Organ" PRIMARY KEY,

    -- Type
    "Type" TEXT NULL,

    -- Image
    "Image" TEXT NULL,

    -- Description
    "Description" TEXT NULL,

    -- AreaId
    "AreaId" TEXT NULL,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- Left
    "Left" INTEGER NOT NULL,

    -- Right
    "Right" INTEGER NOT NULL,

    -- ParentId
    "ParentId" TEXT NULL,
    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_DictionaryItem" (
    -- 字典

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_DictionaryItem" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Value
    "Value" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- CategoryId
    "CategoryId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_DictionaryItem_iot_DictionaryCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_DictionaryCategory" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTProduct" (
    -- 产品

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTProduct" PRIMARY KEY,

    -- 产品名称
    "Name" TEXT NULL,

    -- 产品型号
    "Number" TEXT NOT NULL,

    -- 图标
    "Image" TEXT NULL,

    -- Template
    "Template" TEXT NULL,

    -- 路径
    "Path" TEXT NULL,

    -- ApiJson
    "ApiJson" TEXT NULL,

    -- 序号
    "Order" INTEGER NOT NULL,

    -- IoTProductCategoryId
    "IoTProductCategoryId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTProduct_iot_IoTProductCategory_IoTProductCategoryId" FOREIGN KEY ("IoTProductCategoryId") REFERENCES "iot_IoTProductCategory" ("Id") ON DELETE SET NULL
);


CREATE TABLE "iot_Permission" (
    -- 权限

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Permission" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- Hide
    "Hide" INTEGER NOT NULL,

    -- Type
    "Type" INTEGER NOT NULL,

    -- CategoryId
    "CategoryId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_Permission_iot_PermissionCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "iot_PermissionCategory" ("Id") ON DELETE SET NULL
);


CREATE TABLE "iot_Building" (
    -- 房间

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Building" PRIMARY KEY,

    -- Type
    "Type" TEXT NULL,

    -- Image
    "Image" TEXT NULL,

    -- Description
    "Description" TEXT NULL,

    -- OrganId
    "OrganId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- Left
    "Left" INTEGER NOT NULL,

    -- Right
    "Right" INTEGER NOT NULL,

    -- ParentId
    "ParentId" TEXT NULL,
    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
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Department" PRIMARY KEY,

    -- Type
    "Type" TEXT NULL,

    -- OrganId
    "OrganId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- Order
    "Order" INTEGER NOT NULL,

    -- Left
    "Left" INTEGER NOT NULL,

    -- Right
    "Right" INTEGER NOT NULL,

    -- ParentId
    "ParentId" TEXT NULL,
    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_OrganUser" (
    -- 机构用户

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganUser" PRIMARY KEY,

    -- Type
    "Type" TEXT NULL,

    -- OrganId
    "OrganId" TEXT NOT NULL,

    -- UserId
    "UserId" TEXT NOT NULL,

    -- IsDefault
    "IsDefault" INTEGER NOT NULL,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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_Role" (
    -- 角色

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Role" PRIMARY KEY,

    -- Name
    "Name" TEXT NOT NULL,

    -- Number
    "Number" TEXT NOT NULL,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- OrganId
    "OrganId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_Role_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_Title" (
    -- 职务

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Title" PRIMARY KEY,

    -- Name
    "Name" TEXT NULL,

    -- MinLevel
    "MinLevel" INTEGER NOT NULL,

    -- MaxLevel
    "MaxLevel" INTEGER NOT NULL,

    -- OrganId
    "OrganId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_Title_iot_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "iot_Organ" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTApi" (
    -- 接口

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTApi" PRIMARY KEY,

    -- Name
    "Name" TEXT NULL,

    -- Path
    "Path" TEXT NULL,

    -- Command
    "Command" TEXT NULL,

    -- Method
    "Method" TEXT NULL,

    -- IoTProductId
    "IoTProductId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTApi_iot_IoTProduct_IoTProductId" FOREIGN KEY ("IoTProductId") REFERENCES "iot_IoTProduct" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTGateway" (
    -- 网关

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTGateway" PRIMARY KEY,

    -- 节点名称
    "Name" TEXT NULL,

    -- 节点编号
    "Number" TEXT NOT NULL,

    -- 在线
    "IsOnline" INTEGER NOT NULL,

    -- 图片
    "Image" TEXT NULL,

    -- 版本
    "Version" TEXT NULL,

    -- Password
    "Password" TEXT NULL,

    -- 序号
    "Order" INTEGER NOT NULL,

    -- 隐藏
    "Hidden" INTEGER NOT NULL,

    -- BuildingId
    "BuildingId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTGateway_iot_Building_BuildingId" FOREIGN KEY ("BuildingId") REFERENCES "iot_Building" ("Id") ON DELETE SET NULL
);


CREATE TABLE "iot_IoTScene" (
    -- 机构场景

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTScene" PRIMARY KEY,

    -- 名称
    "Name" TEXT NOT NULL,

    -- 图标
    "Image" TEXT NOT NULL,

    -- 隐藏
    "Hidden" INTEGER NOT NULL,

    -- 序号
    "Order" INTEGER NOT NULL,

    -- BuildingId
    "BuildingId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTScene_iot_Building_BuildingId" FOREIGN KEY ("BuildingId") REFERENCES "iot_Building" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_OrganUserRole" (
    -- 用户角色

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_OrganUserRole" PRIMARY KEY,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- OrganUserId
    "OrganUserId" TEXT NOT NULL,

    -- OrganRoleId
    "OrganRoleId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_OrganUserRole_iot_OrganUser_OrganUserId" FOREIGN KEY ("OrganUserId") REFERENCES "iot_OrganUser" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_iot_OrganUserRole_iot_Role_OrganRoleId" FOREIGN KEY ("OrganRoleId") REFERENCES "iot_Role" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_RolePermission" (
    -- 角色权限

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_RolePermission" PRIMARY KEY,

    -- RoleId
    "RoleId" TEXT NOT NULL,

    -- PermissionId
    "PermissionId" TEXT NOT NULL,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT 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_Job" (
    -- 职位

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_Job" PRIMARY KEY,

    -- Name
    "Name" TEXT NULL,

    -- Level
    "Level" INTEGER NOT NULL,

    -- TitleId
    "TitleId" TEXT NOT NULL,

    -- DepartmentId
    "DepartmentId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTParameter" PRIMARY KEY,

    -- Type
    "Type" TEXT NULL,

    -- Name
    "Name" TEXT NULL,

    -- Description
    "Description" TEXT NULL,

    -- Required
    "Required" INTEGER NOT NULL,

    -- Maxinum
    "Maxinum" TEXT NULL,

    -- Minimum
    "Minimum" TEXT NULL,

    -- IoTApiId
    "IoTApiId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTParameter_iot_IoTApi_IoTApiId" FOREIGN KEY ("IoTApiId") REFERENCES "iot_IoTApi" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTDevice" (
    -- 设备

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTDevice" PRIMARY KEY,

    -- 名称
    "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
    "Ip" TEXT NULL,

    -- 用户名
    "UserName" TEXT NULL,

    -- 密码
    "Password" TEXT NULL,

    -- 连接Id
    "ConnectId" TEXT NULL,

    -- 序号
    "Order" INTEGER NOT NULL,

    -- 隐藏
    "Disabled" INTEGER NOT NULL,

    -- 产品Id
    "IoTProductId" TEXT NOT NULL,

    -- 节点Id
    "IoTGatewayId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTTimer" PRIMARY KEY,

    -- Name
    "Name" TEXT NULL,

    -- Cron
    "Cron" TEXT NULL,

    -- 禁用
    "Disabled" INTEGER NOT NULL,

    -- IoTSceneId
    "IoTSceneId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTTimer_iot_IoTScene_IoTSceneId" FOREIGN KEY ("IoTSceneId") REFERENCES "iot_IoTScene" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_DepartmentUser" (
    -- 部门用户

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_DepartmentUser" PRIMARY KEY,

    -- Position
    "Position" TEXT NULL,

    -- Type
    "Type" TEXT NULL,

    -- OrganUserId
    "OrganUserId" TEXT NOT NULL,

    -- DeparementId
    "DeparementId" TEXT NOT NULL,

    -- JobId
    "JobId" TEXT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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
);


CREATE TABLE "iot_IoTCommand" (
    -- 命令

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTCommand" PRIMARY KEY,

    -- 命令名称
    "Name" TEXT NULL,

    -- 序号
    "Order" INTEGER NOT NULL,

    -- 隐藏
    "Disabled" INTEGER NOT NULL,

    -- 延迟
    "Delay" INTEGER NOT NULL,

    -- QueryString
    "QueryString" TEXT NULL,

    -- ApiId
    "ApiId" TEXT NOT NULL,

    -- DeviceId
    "DeviceId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTData" PRIMARY KEY,

    -- 键
    "Key" TEXT NOT NULL,

    -- 值
    "Value" TEXT NULL,

    -- IntValue
    "IntValue" INTEGER NULL,

    -- DoubleValue
    "DoubleValue" REAL NULL,

    -- DateTimeValue
    "DateTimeValue" TEXT NULL,

    -- 枚举候选值列表
    "EnumValues" TEXT NULL,

    -- 名称
    "Name" TEXT NULL,

    -- 数据分类
    "DataType" INTEGER NOT NULL,

    -- 数值类型
    "ValueType" INTEGER NOT NULL,

    -- 对象模式
    "ValueSchema" TEXT NULL,

    -- 单位
    "Unit" TEXT NULL,

    -- 描述
    "Description" TEXT NULL,

    -- 时间戳
    "Timestamp" INTEGER NOT NULL,

    -- 隐藏
    "Hidden" INTEGER NOT NULL,

    -- 设备Id
    "IoTDeviceId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_iot_IoTData_iot_IoTDevice_IoTDeviceId" FOREIGN KEY ("IoTDeviceId") REFERENCES "iot_IoTDevice" ("Id") ON DELETE CASCADE
);


CREATE TABLE "iot_IoTSceneIoTCommand" (
    -- 场景命令

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTSceneIoTCommand" PRIMARY KEY,

    -- IoTSceneId
    "IoTSceneId" TEXT NOT NULL,

    -- IoTCommandId
    "IoTCommandId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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
    "Id" TEXT NOT NULL CONSTRAINT "PK_iot_IoTTigger" PRIMARY KEY,

    -- Name
    "Name" TEXT NULL,

    -- Type
    "Type" INTEGER NOT NULL,

    -- Condition
    "Condition" TEXT NULL,

    -- Start
    "Start" TEXT NOT NULL,

    -- End
    "End" TEXT NOT NULL,

    -- 禁用
    "Disabled" INTEGER NOT NULL,

    -- IoTDataId
    "IoTDataId" TEXT NOT NULL,

    -- IoTSceneId
    "IoTSceneId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    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
);


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_DictionaryCategory_Key" ON "iot_DictionaryCategory" ("Key");


CREATE UNIQUE INDEX "IX_iot_DictionaryItem_CategoryId_Value" ON "iot_DictionaryItem" ("CategoryId", "Value");


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_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_AppModuleId" ON "iot_PermissionCategory" ("AppModuleId");


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


CREATE UNIQUE INDEX "IX_iot_Role_OrganId_Number" ON "iot_Role" ("OrganId", "Number");


CREATE INDEX "IX_iot_RolePermission_PermissionId" ON "iot_RolePermission" ("PermissionId");


CREATE UNIQUE INDEX "IX_iot_RolePermission_RoleId_PermissionId" ON "iot_RolePermission" ("RoleId", "PermissionId");


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


