fastbee/docs/knowledge-base/04-er-diagrams.md
2026-05-25 20:44:59 +08:00

704 lines
16 KiB
Markdown

# 数据库关系图
本文件用 Mermaid ER 图描述 FastBee 数据库关系。除 Quartz 表外,大部分业务表没有 SQL 外键,图中的连线表示业务约定关联,主要依据字段命名、联合主键、索引和代码查询关系整理。
## 总览
```mermaid
flowchart LR
System["系统 RBAC<br/>sys_*"] --> IoT["IoT 核心<br/>产品/设备/物模型"]
IoT --> DataLog["数据日志<br/>上报/事件/下发/告警日志"]
IoT --> Gateway["网关与 Modbus<br/>子设备/轮询/点位"]
IoT --> Rule["规则与告警<br/>场景/LiteFlow/桥接"]
IoT --> Firmware["固件 OTA<br/>任务/明细"]
IoT --> Visual["可视化<br/>SceneModel/SCADA/GoView"]
IoT --> Video["视频 GB28181<br/>SIP/ZLM"]
Rule --> Notify["通知<br/>渠道/模板/日志"]
System --> OAuth[OAuth/第三方登录]
System --> Gen[代码生成/OSS/Quartz]
```
## 产品、设备、物模型
```mermaid
erDiagram
IOT_CATEGORY ||--o{ IOT_CATEGORY : parent
IOT_CATEGORY ||--o{ IOT_PRODUCT : classifies
IOT_PRODUCT ||--o{ IOT_DEVICE : owns
IOT_PRODUCT ||--o{ IOT_THINGS_MODEL : defines
IOT_PRODUCT ||--o{ IOT_THINGS_MODEL_TEMPLATE : uses
IOT_PRODUCT ||--o{ IOT_DEVICE_TEMPLATE : maps_template
IOT_THINGS_MODEL_TEMPLATE ||--o{ IOT_DEVICE_TEMPLATE : assigned
IOT_THINGS_MODEL ||--o{ IOT_THINGS_MODEL_TRANSLATE : translated
IOT_THINGS_MODEL_TEMPLATE ||--o{ IOT_THINGS_MODEL_TEMPLATE_TRANSLATE : translated
IOT_PROTOCOL ||--o{ IOT_PRODUCT : selected_by
SYS_USER ||--o{ IOT_PRODUCT : tenant_or_owner
SYS_USER ||--o{ IOT_DEVICE : tenant_or_owner
IOT_CATEGORY {
bigint category_id PK
bigint parent_id
bigint tenant_id
}
IOT_PRODUCT {
bigint product_id PK
bigint category_id
bigint tenant_id
}
IOT_DEVICE {
bigint device_id PK
bigint product_id
varchar serial_number UK
bigint tenant_id
bigint user_id
}
IOT_THINGS_MODEL {
bigint model_id PK
bigint product_id
bigint device_id
varchar serial_number
bigint tenant_id
}
IOT_THINGS_MODEL_TEMPLATE {
bigint template_id PK
bigint tenant_id
}
IOT_DEVICE_TEMPLATE {
bigint id PK
bigint product_id
bigint template_id
}
IOT_PROTOCOL {
bigint id PK
varchar protocol_code UK
}
```
## 设备归属、共享、分组
```mermaid
erDiagram
SYS_USER ||--o{ IOT_DEVICE_USER : owns
IOT_DEVICE ||--o{ IOT_DEVICE_USER : owned_by
SYS_USER ||--o{ IOT_DEVICE_SHARE : shared_to
IOT_DEVICE ||--o{ IOT_DEVICE_SHARE : shared
SYS_USER ||--o{ IOT_GROUP : creates
IOT_GROUP ||--o{ IOT_DEVICE_GROUP : contains
IOT_DEVICE ||--o{ IOT_DEVICE_GROUP : grouped
SYS_USER ||--o{ IOT_DEVICE_ALERT_USER : receives_alert
IOT_DEVICE ||--o{ IOT_DEVICE_ALERT_USER : alert_users
SYS_USER ||--o{ ORDER_CONTROL : restricted
IOT_DEVICE ||--o{ ORDER_CONTROL : command_acl
IOT_PRODUCT ||--o{ COMMAND_PREFERENCES : command_template
IOT_DEVICE ||--o{ COMMAND_PREFERENCES : device_command_template
IOT_DEVICE_USER {
bigint device_id PK
bigint user_id PK
}
IOT_DEVICE_SHARE {
bigint device_id PK
bigint user_id PK
}
IOT_GROUP {
bigint group_id PK
bigint user_id
}
IOT_DEVICE_GROUP {
bigint device_id PK
bigint group_id PK
}
IOT_DEVICE_ALERT_USER {
bigint device_id PK
bigint user_id PK
}
ORDER_CONTROL {
bigint id PK
bigint user_id
bigint device_id
bigint tenant_id
}
COMMAND_PREFERENCES {
bigint id PK
bigint product_id
varchar serial_number
}
```
## 网关、子设备、Modbus
```mermaid
erDiagram
IOT_DEVICE ||--o{ IOT_SUB_GATEWAY : gateway
IOT_DEVICE ||--o{ IOT_SUB_GATEWAY : sub_device
IOT_PRODUCT ||--o{ IOT_PRODUCT_SUB_GATEWAY : gateway_product
IOT_PRODUCT ||--o{ IOT_PRODUCT_SUB_GATEWAY : sub_product
IOT_PRODUCT ||--o{ IOT_MODBUS_CONFIG : point_config
IOT_PRODUCT ||--o{ IOT_MODBUS_PARAMS : params
IOT_PRODUCT ||--o{ IOT_PRODUCT_MODBUS_JOB : poll_template
IOT_DEVICE ||--o{ IOT_MODBUS_JOB : poll_task
IOT_VAR_TEMP ||--o{ IOT_VAR_TEMP_SALVE : slave_points
SYS_USER ||--o{ IOT_VAR_TEMP : owns
IOT_SUB_GATEWAY {
bigint id PK
bigint gw_device_id
bigint sub_device_id
int slave_id
}
IOT_PRODUCT_SUB_GATEWAY {
bigint id PK
bigint gw_product_id
bigint sub_product_id
int slave_id
}
IOT_MODBUS_CONFIG {
bigint id PK
bigint product_id
}
IOT_MODBUS_PARAMS {
bigint id PK
bigint product_id
int slave_id
}
IOT_MODBUS_JOB {
bigint task_id PK
bigint sub_device_id
bigint job_id
}
IOT_PRODUCT_MODBUS_JOB {
bigint task_id PK
bigint product_id
}
IOT_VAR_TEMP {
bigint template_id PK
bigint user_id
}
IOT_VAR_TEMP_SALVE {
bigint id PK
bigint device_temp_id
}
```
## 设备数据日志
```mermaid
erDiagram
IOT_PRODUCT ||--o{ IOT_ALERT_LOG : product_alerts
IOT_DEVICE ||--o{ IOT_DEVICE_LOG : property_logs
IOT_DEVICE ||--o{ IOT_EVENT_LOG : event_logs
IOT_DEVICE ||--o{ IOT_FUNCTION_LOG : function_logs
IOT_DEVICE ||--o{ IOT_ALERT_LOG : alert_logs
IOT_DEVICE ||--o{ IOT_SIMULATE_LOG : simulate_logs
IOT_DEVICE ||--o{ IOT_DEVICE_AIR_LOG : air_logs
SYS_USER ||--o{ IOT_DEVICE_LOG : user_scope
SYS_USER ||--o{ IOT_EVENT_LOG : user_scope
SYS_USER ||--o{ IOT_FUNCTION_LOG : operator
IOT_DEVICE_LOG {
bigint log_id PK
bigint device_id
varchar serial_number
bigint user_id
bigint tenant_id
datetime create_time
}
IOT_EVENT_LOG {
bigint log_id PK
bigint device_id
varchar serial_number
bigint user_id
bigint tenant_id
}
IOT_FUNCTION_LOG {
bigint id PK
varchar message_id
varchar serial_number
bigint user_id
}
IOT_ALERT_LOG {
bigint alert_log_id PK
varchar serial_number
bigint product_id
bigint user_id
}
IOT_SIMULATE_LOG {
bigint log_id PK
bigint device_id
varchar serial_number
}
IOT_DEVICE_AIR_LOG {
bigint id PK
bigint product_id
bigint device_id
}
```
## 规则、场景、告警、桥接
```mermaid
erDiagram
SYS_USER ||--o{ IOT_SCENE : owns
IOT_SCENE ||--o{ IOT_SCENE_DEVICE : has_trigger
IOT_SCENE ||--o{ IOT_SCENE_SCRIPT : has_action
IOT_PRODUCT ||--o{ IOT_SCENE_DEVICE : trigger_product
IOT_PRODUCT ||--o{ IOT_SCENE_SCRIPT : action_product
IOT_SCRIPT ||--o{ IOT_SCRIPT_BRIDGE : uses
BRIDGE ||--o{ IOT_SCRIPT_BRIDGE : endpoint
IOT_SCENE ||--o{ IOT_SCRIPT : liteflow_script
IOT_ALERT ||--o{ IOT_ALERT_SCENE : triggers_scene
IOT_SCENE ||--o{ IOT_ALERT_SCENE : alert_bound
IOT_ALERT ||--o{ IOT_ALERT_NOTIFY_TEMPLATE : notifies
NOTIFY_TEMPLATE ||--o{ IOT_ALERT_NOTIFY_TEMPLATE : used_by_alert
IOT_DEVICE ||--o{ IOT_DEVICE_JOB : scheduled
IOT_PRODUCT ||--o{ IOT_DEVICE_JOB : scheduled
IOT_SCENE ||--o{ IOT_DEVICE_JOB : scheduled
IOT_ALERT ||--o{ IOT_DEVICE_JOB : scheduled
IOT_SCENE {
bigint scene_id PK
bigint user_id
varchar chain_name
text el_data
int enable
}
IOT_SCENE_DEVICE {
bigint scene_device_id PK
bigint scene_id
bigint product_id
varchar serial_number
bigint script_id
}
IOT_SCENE_SCRIPT {
bigint script_id PK
bigint scene_id
bigint product_id
bigint job_id
bigint parent_id
}
IOT_SCRIPT {
bigint script_id PK
bigint scene_id
bigint product_id
bigint user_id
int enable
}
BRIDGE {
bigint id PK
}
IOT_SCRIPT_BRIDGE {
bigint script_id PK
bigint bridge_id PK
}
IOT_ALERT {
bigint alert_id PK
bigint tenant_id
}
IOT_ALERT_SCENE {
bigint alert_id PK
bigint scene_id PK
}
IOT_ALERT_NOTIFY_TEMPLATE {
bigint alert_id PK
bigint notify_template_id PK
}
IOT_DEVICE_JOB {
bigint job_id PK
bigint device_id
bigint product_id
bigint scene_id
bigint alert_id
}
```
## 通知与物模型推送
```mermaid
erDiagram
NOTIFY_CHANNEL ||--o{ NOTIFY_TEMPLATE : has
NOTIFY_CHANNEL ||--o{ NOTIFY_LOG : sends
NOTIFY_TEMPLATE ||--o{ NOTIFY_LOG : produces
IOT_DEVICE ||--o{ IOT_THINGS_MODEL_PUSH_CONFIG : push_config
IOT_PRODUCT ||--o{ IOT_THINGS_MODEL_PUSH_CONFIG : product_scope
NOTIFY_TEMPLATE ||--o{ IOT_THINGS_MODEL_PUSH_CONFIG : template
SYS_USER ||--o{ IOT_THINGS_MODEL_PUSH_CONFIG : owner
IOT_THINGS_MODEL_PUSH_CONFIG ||--o{ IOT_PUSH_MODEL_ITEM : includes
IOT_THINGS_MODEL ||--o{ IOT_PUSH_MODEL_ITEM : model_item
IOT_THINGS_MODEL_PUSH_CONFIG ||--o{ IOT_PUSH_USER : receivers
SYS_USER ||--o{ IOT_PUSH_USER : receiver
NOTIFY_CHANNEL {
bigint id PK
bigint tenant_id
}
NOTIFY_TEMPLATE {
bigint id PK
bigint channel_id
bigint tenant_id
}
NOTIFY_LOG {
bigint id PK
bigint channel_id
bigint notify_template_id
bigint tenant_id
}
IOT_THINGS_MODEL_PUSH_CONFIG {
bigint id PK
bigint product_id
bigint device_id
varchar serial_number
bigint template_id
bigint user_id
}
IOT_PUSH_MODEL_ITEM {
bigint id PK
bigint config_id
bigint model_id
}
IOT_PUSH_USER {
bigint id PK
bigint config_id
bigint user_id
}
```
## 固件 OTA
```mermaid
erDiagram
IOT_PRODUCT ||--o{ IOT_FIRMWARE : owns
IOT_FIRMWARE ||--o{ IOT_FIRMWARE_TASK : creates
IOT_FIRMWARE_TASK ||--o{ IOT_FIRMWARE_TASK_DETAIL : tracks
IOT_DEVICE ||--o{ IOT_FIRMWARE_TASK_DETAIL : target
IOT_FIRMWARE {
bigint firmware_id PK
bigint product_id
bigint tenant_id
}
IOT_FIRMWARE_TASK {
bigint id PK
bigint firmware_id
}
IOT_FIRMWARE_TASK_DETAIL {
bigint id PK
bigint task_id
varchar serial_number
varchar message_id
}
```
## SceneModel、SCADA、GoView
```mermaid
erDiagram
SCENE_MODEL ||--o{ SCENE_MODEL_DEVICE : has
SCENE_MODEL ||--o{ SCENE_MODEL_DATA : has_data
SCENE_MODEL_DEVICE ||--o{ SCENE_MODEL_DATA : source
SCENE_MODEL ||--o{ SCENE_MODEL_TAG : has_tag
SCENE_MODEL_TAG ||--o{ SCENE_TAG_POINTS : calculated_from
SCENE_MODEL_DATA ||--o{ SCENE_TAG_POINTS : point_source
IOT_GOVIEW_PROJECT ||--o{ IOT_GOVIEW_PROJECT_DATA : has_data
SCADA ||--o{ SCADA_DEVICE_BIND : binds_by_guid
IOT_DEVICE ||--o{ SCADA_DEVICE_BIND : bound
SYS_USER ||--o{ SCADA_COMPONENT : owns
SCENE_MODEL {
bigint scene_model_id PK
bigint tenant_id
}
SCENE_MODEL_DEVICE {
bigint id PK
bigint scene_model_id
bigint cus_device_id
}
SCENE_MODEL_DATA {
bigint id PK
bigint scene_model_id
bigint scene_model_device_id
bigint datasource_id
}
SCENE_MODEL_TAG {
bigint id PK
bigint scene_model_id
}
SCENE_TAG_POINTS {
bigint id PK
bigint tag_id
bigint scene_model_data_id
}
IOT_GOVIEW_PROJECT {
bigint id PK
bigint tenant_id
}
IOT_GOVIEW_PROJECT_DATA {
bigint id PK
bigint project_id
}
SCADA {
bigint id PK
bigint tenant_id
}
SCADA_COMPONENT {
bigint id PK
bigint tenant_id
bigint user_id
}
SCADA_DEVICE_BIND {
bigint id PK
varchar serial_number
varchar scada_guid
}
SCADA_ECHART {
bigint id PK
bigint tenant_id
}
SCADA_GALLERY {
bigint id PK
bigint tenant_id
}
SCADA_MODEL {
bigint id PK
bigint tenant_id
}
```
## 视频 GB28181 与媒体
```mermaid
erDiagram
IOT_PRODUCT ||--o{ SIP_CONFIG : config_for
IOT_PRODUCT ||--o{ SIP_DEVICE : product_device
SIP_DEVICE ||--o{ SIP_DEVICE_CHANNEL : has_channel
SIP_DEVICE_CHANNEL ||--o{ IOT_SIP_RELATION : bind_channel
IOT_DEVICE ||--o{ IOT_SIP_RELATION : bind_iot_device
SCENE_MODEL ||--o{ IOT_SIP_RELATION : bind_scene
MEDIA_SERVER ||--o{ SIP_DEVICE_CHANNEL : streams
SIP_CONFIG {
bigint id PK
bigint product_id
}
SIP_DEVICE {
bigint device_id PK
bigint product_id
varchar device_sip_id
}
SIP_DEVICE_CHANNEL {
bigint id PK
varchar device_sip_id PK
varchar channel_sip_id
bigint product_id
bigint user_id
}
MEDIA_SERVER {
bigint id PK
varchar server_id
bigint tenant_id
}
IOT_SIP_RELATION {
bigint id PK
varchar channel_id
bigint re_device_id
bigint re_scene_model_id
}
```
## 系统 RBAC
```mermaid
erDiagram
SYS_DEPT ||--o{ SYS_DEPT : parent
SYS_DEPT ||--o{ SYS_USER : contains
SYS_USER ||--o{ SYS_USER_ROLE : has
SYS_ROLE ||--o{ SYS_USER_ROLE : assigned
SYS_ROLE ||--o{ SYS_ROLE_MENU : grants
SYS_MENU ||--o{ SYS_ROLE_MENU : granted
SYS_MENU ||--o{ SYS_MENU : parent
SYS_ROLE ||--o{ SYS_ROLE_DEPT : data_scope
SYS_DEPT ||--o{ SYS_ROLE_DEPT : scoped
SYS_USER ||--o{ SYS_USER_POST : holds
SYS_POST ||--o{ SYS_USER_POST : assigned
SYS_USER ||--o{ SYS_USER_RELATION : hierarchy
SYS_DICT_TYPE ||--o{ SYS_DICT_DATA : has
SYS_DICT_TYPE ||--o{ SYS_DICT_TYPE_TRANSLATE : translated
SYS_DICT_DATA ||--o{ SYS_DICT_DATA_TRANSLATE : translated
SYS_MENU ||--o{ SYS_MENU_TRANSLATE : translated
SYS_USER {
bigint user_id PK
bigint dept_id
}
SYS_DEPT {
bigint dept_id PK
bigint parent_id
}
SYS_ROLE {
bigint role_id PK
}
SYS_MENU {
bigint menu_id PK
bigint parent_id
}
SYS_POST {
bigint post_id PK
}
SYS_USER_ROLE {
bigint user_id PK
bigint role_id PK
}
SYS_ROLE_MENU {
bigint role_id PK
bigint menu_id PK
}
SYS_ROLE_DEPT {
bigint role_id PK
bigint dept_id PK
}
SYS_USER_POST {
bigint user_id PK
bigint post_id PK
}
SYS_DICT_TYPE {
bigint dict_id PK
varchar dict_type UK
}
SYS_DICT_DATA {
bigint dict_code PK
varchar dict_type
}
```
## OAuth、第三方登录、App 偏好
```mermaid
erDiagram
SYS_USER ||--o{ SYS_AUTH_USER : auth_binding
SYS_USER ||--o{ IOT_SOCIAL_USER : social_binding
IOT_SOCIAL_PLATFORM ||--o{ IOT_SOCIAL_USER : platform
OAUTH_CLIENT_DETAILS ||--o{ OAUTH_ACCESS_TOKEN : issues
OAUTH_CLIENT_DETAILS ||--o{ OAUTH_REFRESH_TOKEN : refreshes
OAUTH_CLIENT_DETAILS ||--o{ OAUTH_CLIENT_TOKEN : client_token
SYS_USER ||--o{ OAUTH_ACCESS_TOKEN : token_user
SYS_USER ||--o{ APP_PREFERENCES : preferences
APP_LANGUAGE ||--o{ APP_PREFERENCES : selected_language
SYS_AUTH_USER {
bigint auth_id PK
bigint user_id
}
IOT_SOCIAL_PLATFORM {
bigint social_platform_id PK
varchar platform UK
}
IOT_SOCIAL_USER {
bigint social_user_id PK
bigint sys_user_id
varchar open_id
varchar union_id
}
OAUTH_CLIENT_DETAILS {
bigint id PK
varchar client_id
bigint tenant_id
}
OAUTH_ACCESS_TOKEN {
varchar token_id
varchar client_id
varchar user_id
}
APP_LANGUAGE {
bigint id PK
}
APP_PREFERENCES {
bigint id PK
bigint user_id
varchar language
}
```
## 内容、OSS、代码生成
```mermaid
erDiagram
NEWS_CATEGORY ||--o{ NEWS : contains
OSS_CONFIG ||--o{ OSS_DETAIL : stores
GEN_TABLE ||--o{ GEN_TABLE_COLUMN : has_column
SYS_DICT_TYPE ||--o{ GEN_TABLE_COLUMN : dict
NEWS_CATEGORY {
bigint category_id PK
}
NEWS {
bigint news_id PK
bigint category_id
}
OSS_CONFIG {
bigint id PK
bigint tenant_id
}
OSS_DETAIL {
bigint id PK
bigint tenant_id
}
GEN_TABLE {
bigint table_id PK
}
GEN_TABLE_COLUMN {
bigint column_id PK
bigint table_id
varchar dict_type
}
```
## Quartz 真实外键图
```mermaid
erDiagram
QRTZ_JOB_DETAILS ||--o{ QRTZ_TRIGGERS : has
QRTZ_TRIGGERS ||--o| QRTZ_BLOB_TRIGGERS : blob
QRTZ_TRIGGERS ||--o| QRTZ_CRON_TRIGGERS : cron
QRTZ_TRIGGERS ||--o| QRTZ_SIMPLE_TRIGGERS : simple
QRTZ_TRIGGERS ||--o| QRTZ_SIMPROP_TRIGGERS : simprop
QRTZ_JOB_DETAILS {
varchar sched_name PK
varchar job_name PK
varchar job_group PK
}
QRTZ_TRIGGERS {
varchar sched_name PK
varchar trigger_name PK
varchar trigger_group PK
varchar job_name FK
varchar job_group FK
}
QRTZ_BLOB_TRIGGERS {
varchar sched_name PK
varchar trigger_name PK
varchar trigger_group PK
}
QRTZ_CRON_TRIGGERS {
varchar sched_name PK
varchar trigger_name PK
varchar trigger_group PK
}
QRTZ_SIMPLE_TRIGGERS {
varchar sched_name PK
varchar trigger_name PK
varchar trigger_group PK
}
QRTZ_SIMPROP_TRIGGERS {
varchar sched_name PK
varchar trigger_name PK
varchar trigger_group PK
}
```
## 读图说明
- `||--o{` 表示一对多业务关系。
- `||--o|` 表示一对零或一的扩展关系。
- 图中 `PK`、`UK`、`FK` 是阅读标注;除 Quartz 图外,`FK` 不代表数据库中存在外键约束。
- 对于 `serial_number` 关联,运行时经常直接使用设备编号查找数据,但数据建模上仍应优先理解 `iot_device.device_id` 是设备主键。