# 物联网服务数据库设计文档

## 1. 数据库环境说明

- 数据库名称: xlxumu_iot
- 字符集: utf8mb4
- 排序规则: utf8mb4_unicode_ci

## 2. 数据表设计

### 2.1 设备信息表 (devices)

存储物联网设备基本信息。

```sql
CREATE TABLE `devices` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  `device_id` VARCHAR(100) NOT NULL COMMENT '设备唯一标识',
  `name` VARCHAR(100) NOT NULL COMMENT '设备名称',
  `type` VARCHAR(50) NOT NULL COMMENT '设备类型(传感器/控制器等)',
  `model` VARCHAR(100) COMMENT '设备型号',
  `manufacturer` VARCHAR(100) COMMENT '制造商',
  `location` VARCHAR(200) COMMENT '设备位置',
  `status` VARCHAR(20) NOT NULL DEFAULT 'ACTIVE' COMMENT '设备状态(ACTIVE/INACTIVE/FAULT)',
  `last_heartbeat` TIMESTAMP NULL COMMENT '最后心跳时间',
  `firmware_version` VARCHAR(50) COMMENT '固件版本',
  `description` TEXT COMMENT '描述',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_device_id` (`device_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_type` (`type`),
  KEY `idx_status` (`status`),
  KEY `idx_last_heartbeat` (`last_heartbeat`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='设备信息表';
```

### 2.2 传感器数据表 (sensor_data)

存储传感器采集的数据。

```sql
CREATE TABLE `sensor_data` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `device_id` VARCHAR(100) NOT NULL COMMENT '设备ID',
  `sensor_type` VARCHAR(50) NOT NULL COMMENT '传感器类型(温度/湿度/光照等)',
  `value` DECIMAL(10,4) NOT NULL COMMENT '传感器数值',
  `unit` VARCHAR(20) NOT NULL COMMENT '单位',
  `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '数据时间戳',
  `location` POINT COMMENT '地理位置',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_sensor_type` (`sensor_type`),
  KEY `idx_timestamp` (`timestamp`),
  SPATIAL KEY `idx_location` (`location`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='传感器数据表';
```

### 2.3 设备告警表 (device_alerts)

存储设备告警信息。

```sql
CREATE TABLE `device_alerts` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `device_id` VARCHAR(100) NOT NULL COMMENT '设备ID',
  `alert_type` VARCHAR(50) NOT NULL COMMENT '告警类型(故障/阈值超限等)',
  `alert_level` VARCHAR(20) NOT NULL COMMENT '告警级别(INFO/WARNING/ERROR/CRITICAL)',
  `message` TEXT NOT NULL COMMENT '告警信息',
  `value` DECIMAL(10,4) COMMENT '触发告警的数值',
  `threshold` DECIMAL(10,4) COMMENT '告警阈值',
  `resolved` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否已解决',
  `resolved_at` TIMESTAMP NULL COMMENT '解决时间',
  `resolved_by` BIGINT UNSIGNED COMMENT '解决人ID',
  `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '告警时间',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_alert_type` (`alert_type`),
  KEY `idx_alert_level` (`alert_level`),
  KEY `idx_resolved` (`resolved`),
  KEY `idx_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='设备告警表';
```

### 2.4 设备控制命令表 (device_commands)

存储发送给设备的控制命令。

```sql
CREATE TABLE `device_commands` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `device_id` VARCHAR(100) NOT NULL COMMENT '设备ID',
  `command` VARCHAR(100) NOT NULL COMMENT '命令名称',
  `parameters` JSON COMMENT '命令参数',
  `status` VARCHAR(20) NOT NULL DEFAULT 'PENDING' COMMENT '命令状态(PENDING/SENT/EXECUTED/FAILED)',
  `result` TEXT COMMENT '执行结果',
  `sent_at` TIMESTAMP NULL COMMENT '发送时间',
  `executed_at` TIMESTAMP NULL COMMENT '执行时间',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_command` (`command`),
  KEY `idx_status` (`status`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='设备控制命令表';
```

### 2.5 设备配置表 (device_configs)

存储设备配置信息。

```sql
CREATE TABLE `device_configs` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `device_id` VARCHAR(100) NOT NULL COMMENT '设备ID',
  `config_key` VARCHAR(100) NOT NULL COMMENT '配置键',
  `config_value` TEXT NOT NULL COMMENT '配置值',
  `description` TEXT COMMENT '配置描述',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_device_config` (`device_id`, `config_key`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_config_key` (`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='设备配置表';
```

### 2.6 数据聚合表 (data_aggregations)

存储传感器数据的聚合统计信息。

```sql
CREATE TABLE `data_aggregations` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `device_id` VARCHAR(100) NOT NULL COMMENT '设备ID',
  `sensor_type` VARCHAR(50) NOT NULL COMMENT '传感器类型',
  `aggregation_type` VARCHAR(20) NOT NULL COMMENT '聚合类型(HOURLY/DAILY/WEEKLY/MONTHLY)',
  `timestamp` TIMESTAMP NOT NULL COMMENT '时间戳',
  `min_value` DECIMAL(10,4) COMMENT '最小值',
  `max_value` DECIMAL(10,4) COMMENT '最大值',
  `avg_value` DECIMAL(10,4) COMMENT '平均值',
  `sum_value` DECIMAL(10,4) COMMENT '总和',
  `count` INT NOT NULL DEFAULT '0' COMMENT '数据点数量',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_aggregation` (`device_id`, `sensor_type`, `aggregation_type`, `timestamp`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_sensor_type` (`sensor_type`),
  KEY `idx_aggregation_type` (`aggregation_type`),
  KEY `idx_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据聚合表';
```