# IoT设备日志同步功能测试脚本

## 测试步骤

### 1. 检查应用启动状态
等待应用启动完成，查看控制台日志确认：
- Spring Boot应用启动成功
- 数据库连接正常
- 定时任务已注册

### 2. 手动触发日志同步测试

**API接口**: `POST http://localhost:8080/api/deliveryDevice/manualSyncDeviceLogs`

**测试命令**:
```bash
curl -X POST http://localhost:8080/api/deliveryDevice/manualSyncDeviceLogs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

**预期结果**:
```json
{
  "code": 200,
  "msg": "设备日志同步完成",
  "data": null
}
```

### 3. 查看日志同步统计信息

**API接口**: `GET http://localhost:8080/api/deliveryDevice/getLogSyncStatistics`

**测试命令**:
```bash
curl -X GET http://localhost:8080/api/deliveryDevice/getLogSyncStatistics \
  -H "Authorization: Bearer YOUR_TOKEN"
```

**预期结果**:
```json
{
  "code": 200,
  "msg": "获取统计信息成功",
  "data": {
    "hostLogCount": 5,
    "earTagLogCount": 8,
    "collarLogCount": 3,
    "totalLogCount": 16
  }
}
```

### 4. 验证数据库数据

**检查iot_device_data表**:
```sql
SELECT device_id, device_type, device_name, voltage, temperature, steps, latitude, longitude, update_time 
FROM iot_device_data 
ORDER BY device_type, device_id;
```

**检查日志表数据**:
```sql
-- 主机日志
SELECT COUNT(*) as host_count FROM jbq_server_log;

-- 耳标日志  
SELECT COUNT(*) as ear_tag_count FROM jbq_client_log;

-- 项圈日志
SELECT COUNT(*) as collar_count FROM xq_client_log;
```

**检查最新同步的日志记录**:
```sql
-- 查看最新的主机日志
SELECT device_id, device_voltage, device_temp, latitude, longitude, create_time 
FROM jbq_server_log 
ORDER BY create_time DESC LIMIT 5;

-- 查看最新的耳标日志
SELECT device_id, device_voltage, device_temp, latitude, longitude, create_time 
FROM jbq_client_log 
ORDER BY create_time DESC LIMIT 5;

-- 查看最新的项圈日志
SELECT device_id, device_voltage, device_temp, latitude, longitude, create_time 
FROM xq_client_log 
ORDER BY create_time DESC LIMIT 5;
```

### 5. 验证定时任务

**等待60分钟后检查**:
- 查看应用日志，确认定时任务自动执行
- 检查日志表是否有新的数据插入
- 验证数据同步的准确性

### 6. 测试日志查询功能

**测试耳标日志查询**:
```bash
curl -X POST http://localhost:8080/api/deliveryDevice/getEarTagLogs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "deviceId": "2404401569",
    "deliveryId": 1
  }'
```

**测试项圈日志查询**:
```bash
curl -X POST http://localhost:8080/api/deliveryDevice/getCollarLogs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "deviceId": "2404401569", 
    "deliveryId": 1
  }'
```

**测试主机日志查询**:
```bash
curl -X POST http://localhost:8080/api/deliveryDevice/getHostLogs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "deviceId": "2404401569",
    "deliveryId": 1
  }'
```

## 预期结果

### 成功指标
1. **编译成功**: 无编译错误
2. **应用启动**: Spring Boot正常启动
3. **手动同步**: API返回成功状态
4. **数据同步**: 日志表中有对应设备类型的数据
5. **定时任务**: 60分钟后自动执行同步
6. **日志查询**: 能正确查询到设备日志数据

### 数据验证要点
1. **设备类型映射正确**: 
   - device_type=1 → jbq_server_log
   - device_type=2 → jbq_client_log  
   - device_type=4 → xq_client_log

2. **字段映射正确**:
   - voltage → device_voltage
   - temperature → device_temp
   - steps → walk_steps
   - same_day_steps → y_walk_steps

3. **时间字段正确**:
   - create_time = 当前同步时间
   - update_time = 当前同步时间
   - create_by = "SYSTEM"
   - update_by = "SYSTEM"

## 故障排除

### 常见问题
1. **编译错误**: 检查import语句和语法
2. **数据库连接失败**: 检查数据库配置
3. **权限错误**: 检查API权限配置
4. **数据为空**: 检查iot_device_data表是否有数据
5. **定时任务不执行**: 检查@Scheduled注解和Spring配置

### 调试方法
1. 查看应用日志输出
2. 检查数据库表结构和数据
3. 使用Postman或curl测试API
4. 验证定时任务配置
