﻿CREATE TABLE "AppModule" (
    -- 模块

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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 "Area" (
    -- 区域

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Area_Area_ParentId" FOREIGN KEY ("ParentId") REFERENCES "Area" ("Id") ON DELETE RESTRICT
);


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

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

    -- Name
    "Name" TEXT NOT NULL,

    -- Key
    "Key" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "IoTProductCategory" (
    -- 分类

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTProductCategory_IoTProductCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "IoTProductCategory" ("Id") ON DELETE RESTRICT
);


CREATE TABLE "LiveRecord" (
    -- 录像

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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 "Setting" (
    -- 配置

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

    -- Name
    "Name" TEXT NOT NULL,

    -- Value
    "Value" TEXT NULL,

    -- Type
    "Type" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL
);


CREATE TABLE "Site" (
    -- 应用

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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 "Statistic" (
    -- 数据统计

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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 "User" (
    -- 用户

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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 "PermissionCategory" (
    -- 权限分类

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_PermissionCategory_AppModule_AppModuleId" FOREIGN KEY ("AppModuleId") REFERENCES "AppModule" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_PermissionCategory_PermissionCategory_ParentId" FOREIGN KEY ("ParentId") REFERENCES "PermissionCategory" ("Id") ON DELETE RESTRICT
);


CREATE TABLE "Organ" (
    -- 机构

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Organ_Area_AreaId" FOREIGN KEY ("AreaId") REFERENCES "Area" ("Id") ON DELETE SET NULL,
    CONSTRAINT "FK_Organ_Organ_ParentId" FOREIGN KEY ("ParentId") REFERENCES "Organ" ("Id") ON DELETE RESTRICT
);


CREATE TABLE "DictionaryItem" (
    -- 字典

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_DictionaryItem_DictionaryCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "DictionaryCategory" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTProduct" (
    -- 产品

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTProduct_IoTProductCategory_IoTProductCategoryId" FOREIGN KEY ("IoTProductCategoryId") REFERENCES "IoTProductCategory" ("Id") ON DELETE SET NULL
);


CREATE TABLE "Permission" (
    -- 权限

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Permission_PermissionCategory_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "PermissionCategory" ("Id") ON DELETE SET NULL
);


CREATE TABLE "Building" (
    -- 房间

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Building_Building_ParentId" FOREIGN KEY ("ParentId") REFERENCES "Building" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_Building_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "Organ" ("Id") ON DELETE CASCADE
);


CREATE TABLE "Department" (
    -- 部门

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Department_Department_ParentId" FOREIGN KEY ("ParentId") REFERENCES "Department" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_Department_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "Organ" ("Id") ON DELETE CASCADE
);


CREATE TABLE "OrganUser" (
    -- 机构用户

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_OrganUser_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "Organ" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_OrganUser_User_UserId" FOREIGN KEY ("UserId") REFERENCES "User" ("Id") ON DELETE CASCADE
);


CREATE TABLE "Role" (
    -- 角色

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Role_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "Organ" ("Id") ON DELETE CASCADE
);


CREATE TABLE "Title" (
    -- 职务

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Title_Organ_OrganId" FOREIGN KEY ("OrganId") REFERENCES "Organ" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTApi" (
    -- 接口

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTApi_IoTProduct_IoTProductId" FOREIGN KEY ("IoTProductId") REFERENCES "IoTProduct" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTGateway" (
    -- 网关

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTGateway_Building_BuildingId" FOREIGN KEY ("BuildingId") REFERENCES "Building" ("Id") ON DELETE SET NULL
);


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

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTScene_Building_BuildingId" FOREIGN KEY ("BuildingId") REFERENCES "Building" ("Id") ON DELETE CASCADE
);


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

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

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- OrganUserId
    "OrganUserId" TEXT NOT NULL,

    -- OrganRoleId
    "OrganRoleId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_OrganUserRole_OrganUser_OrganUserId" FOREIGN KEY ("OrganUserId") REFERENCES "OrganUser" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_OrganUserRole_Role_OrganRoleId" FOREIGN KEY ("OrganRoleId") REFERENCES "Role" ("Id") ON DELETE CASCADE
);


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

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

    -- RoleId
    "RoleId" TEXT NOT NULL,

    -- PermissionId
    "PermissionId" TEXT NOT NULL,

    -- IsReadOnly
    "IsReadOnly" INTEGER NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_RolePermission_Permission_PermissionId" FOREIGN KEY ("PermissionId") REFERENCES "Permission" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_RolePermission_Role_RoleId" FOREIGN KEY ("RoleId") REFERENCES "Role" ("Id") ON DELETE CASCADE
);


CREATE TABLE "Job" (
    -- 职位

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_Job_Department_DepartmentId" FOREIGN KEY ("DepartmentId") REFERENCES "Department" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_Job_Title_TitleId" FOREIGN KEY ("TitleId") REFERENCES "Title" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTParameter" (
    -- 参数

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTParameter_IoTApi_IoTApiId" FOREIGN KEY ("IoTApiId") REFERENCES "IoTApi" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTDevice" (
    -- 设备

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTDevice_IoTGateway_IoTGatewayId" FOREIGN KEY ("IoTGatewayId") REFERENCES "IoTGateway" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_IoTDevice_IoTProduct_IoTProductId" FOREIGN KEY ("IoTProductId") REFERENCES "IoTProduct" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTTimer" (
    -- 定时器

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTTimer_IoTScene_IoTSceneId" FOREIGN KEY ("IoTSceneId") REFERENCES "IoTScene" ("Id") ON DELETE CASCADE
);


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

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_DepartmentUser_Department_DeparementId" FOREIGN KEY ("DeparementId") REFERENCES "Department" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_DepartmentUser_Job_JobId" FOREIGN KEY ("JobId") REFERENCES "Job" ("Id") ON DELETE SET NULL,
    CONSTRAINT "FK_DepartmentUser_OrganUser_OrganUserId" FOREIGN KEY ("OrganUserId") REFERENCES "OrganUser" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTCommand" (
    -- 命令

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTCommand_IoTApi_ApiId" FOREIGN KEY ("ApiId") REFERENCES "IoTApi" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_IoTCommand_IoTDevice_DeviceId" FOREIGN KEY ("DeviceId") REFERENCES "IoTDevice" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTData" (
    -- 数据

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTData_IoTDevice_IoTDeviceId" FOREIGN KEY ("IoTDeviceId") REFERENCES "IoTDevice" ("Id") ON DELETE CASCADE
);


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

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

    -- IoTSceneId
    "IoTSceneId" TEXT NOT NULL,

    -- IoTCommandId
    "IoTCommandId" TEXT NOT NULL,

    -- IsDeleted
    "IsDeleted" TEXT NULL,
    CONSTRAINT "FK_IoTSceneIoTCommand_IoTCommand_IoTCommandId" FOREIGN KEY ("IoTCommandId") REFERENCES "IoTCommand" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_IoTSceneIoTCommand_IoTScene_IoTSceneId" FOREIGN KEY ("IoTSceneId") REFERENCES "IoTScene" ("Id") ON DELETE CASCADE
);


CREATE TABLE "IoTTigger" (
    -- 触发器

    -- Id
    "Id" TEXT NOT NULL CONSTRAINT "PK_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_IoTTigger_IoTData_IoTDataId" FOREIGN KEY ("IoTDataId") REFERENCES "IoTData" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_IoTTigger_IoTScene_IoTSceneId" FOREIGN KEY ("IoTSceneId") REFERENCES "IoTScene" ("Id") ON DELETE CASCADE
);


CREATE UNIQUE INDEX "IX_Area_Number" ON "Area" ("Number");


CREATE INDEX "IX_Area_ParentId" ON "Area" ("ParentId");


CREATE UNIQUE INDEX "IX_Building_OrganId_ParentId_Number" ON "Building" ("OrganId", "ParentId", "Number");


CREATE INDEX "IX_Building_ParentId" ON "Building" ("ParentId");


CREATE UNIQUE INDEX "IX_Department_OrganId_Number" ON "Department" ("OrganId", "Number");


CREATE INDEX "IX_Department_ParentId" ON "Department" ("ParentId");


CREATE UNIQUE INDEX "IX_DepartmentUser_DeparementId_OrganUserId" ON "DepartmentUser" ("DeparementId", "OrganUserId");


CREATE INDEX "IX_DepartmentUser_JobId" ON "DepartmentUser" ("JobId");


CREATE INDEX "IX_DepartmentUser_OrganUserId" ON "DepartmentUser" ("OrganUserId");


CREATE UNIQUE INDEX "IX_DictionaryCategory_Key" ON "DictionaryCategory" ("Key");


CREATE UNIQUE INDEX "IX_DictionaryItem_CategoryId_Value" ON "DictionaryItem" ("CategoryId", "Value");


CREATE UNIQUE INDEX "IX_IoTApi_IoTProductId_Name" ON "IoTApi" ("IoTProductId", "Name");


CREATE INDEX "IX_IoTCommand_ApiId" ON "IoTCommand" ("ApiId");


CREATE INDEX "IX_IoTCommand_DeviceId" ON "IoTCommand" ("DeviceId");


CREATE INDEX "IX_IoTData_IoTDeviceId" ON "IoTData" ("IoTDeviceId");


CREATE INDEX "IX_IoTDevice_IoTGatewayId" ON "IoTDevice" ("IoTGatewayId");


CREATE INDEX "IX_IoTDevice_IoTProductId" ON "IoTDevice" ("IoTProductId");


CREATE UNIQUE INDEX "IX_IoTDevice_Number" ON "IoTDevice" ("Number");


CREATE INDEX "IX_IoTGateway_BuildingId" ON "IoTGateway" ("BuildingId");


CREATE UNIQUE INDEX "IX_IoTGateway_Number" ON "IoTGateway" ("Number");


CREATE INDEX "IX_IoTParameter_IoTApiId" ON "IoTParameter" ("IoTApiId");


CREATE INDEX "IX_IoTProduct_IoTProductCategoryId" ON "IoTProduct" ("IoTProductCategoryId");


CREATE UNIQUE INDEX "IX_IoTProduct_Number" ON "IoTProduct" ("Number");


CREATE UNIQUE INDEX "IX_IoTProductCategory_Number" ON "IoTProductCategory" ("Number");


CREATE INDEX "IX_IoTProductCategory_ParentId" ON "IoTProductCategory" ("ParentId");


CREATE INDEX "IX_IoTScene_BuildingId" ON "IoTScene" ("BuildingId");


CREATE INDEX "IX_IoTSceneIoTCommand_IoTCommandId" ON "IoTSceneIoTCommand" ("IoTCommandId");


CREATE INDEX "IX_IoTSceneIoTCommand_IoTSceneId" ON "IoTSceneIoTCommand" ("IoTSceneId");


CREATE INDEX "IX_IoTTigger_IoTDataId" ON "IoTTigger" ("IoTDataId");


CREATE INDEX "IX_IoTTigger_IoTSceneId" ON "IoTTigger" ("IoTSceneId");


CREATE INDEX "IX_IoTTimer_IoTSceneId" ON "IoTTimer" ("IoTSceneId");


CREATE INDEX "IX_Job_DepartmentId" ON "Job" ("DepartmentId");


CREATE INDEX "IX_Job_TitleId" ON "Job" ("TitleId");


CREATE INDEX "IX_Organ_AreaId" ON "Organ" ("AreaId");


CREATE UNIQUE INDEX "IX_Organ_Name" ON "Organ" ("Name");


CREATE UNIQUE INDEX "IX_Organ_Number" ON "Organ" ("Number");


CREATE INDEX "IX_Organ_ParentId" ON "Organ" ("ParentId");


CREATE UNIQUE INDEX "IX_OrganUser_OrganId_UserId" ON "OrganUser" ("OrganId", "UserId");


CREATE INDEX "IX_OrganUser_UserId" ON "OrganUser" ("UserId");


CREATE INDEX "IX_OrganUserRole_OrganRoleId" ON "OrganUserRole" ("OrganRoleId");


CREATE UNIQUE INDEX "IX_OrganUserRole_OrganUserId_OrganRoleId" ON "OrganUserRole" ("OrganUserId", "OrganRoleId");


CREATE INDEX "IX_Permission_CategoryId" ON "Permission" ("CategoryId");


CREATE UNIQUE INDEX "IX_Permission_Number" ON "Permission" ("Number");


CREATE INDEX "IX_PermissionCategory_AppModuleId" ON "PermissionCategory" ("AppModuleId");


CREATE INDEX "IX_PermissionCategory_ParentId" ON "PermissionCategory" ("ParentId");


CREATE UNIQUE INDEX "IX_Role_OrganId_Number" ON "Role" ("OrganId", "Number");


CREATE INDEX "IX_RolePermission_PermissionId" ON "RolePermission" ("PermissionId");


CREATE UNIQUE INDEX "IX_RolePermission_RoleId_PermissionId" ON "RolePermission" ("RoleId", "PermissionId");


CREATE UNIQUE INDEX "IX_Setting_Name" ON "Setting" ("Name");


CREATE UNIQUE INDEX "IX_Site_Name" ON "Site" ("Name");


CREATE UNIQUE INDEX "IX_Statistic_Key" ON "Statistic" ("Key");


CREATE INDEX "IX_Title_OrganId" ON "Title" ("OrganId");


CREATE UNIQUE INDEX "IX_User_Email" ON "User" ("Email");


CREATE UNIQUE INDEX "IX_User_PhoneNumber" ON "User" ("PhoneNumber");


CREATE UNIQUE INDEX "IX_User_UserName" ON "User" ("UserName");


