Files
cattleTransportation/tradeCattle/IOT_DEVICE_CLEANUP_FIX_REPORT.md
2025-10-27 17:38:20 +08:00

142 lines
5.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# IoT设备清理功能实现报告
## 问题描述
外部物联网系统可以将设备重新分配到其他项目,但该项目中的设备数据仍然保留在数据库中,导致以下问题:
- 数据库中存在不属于本项目的设备记录
- 这些设备可能会在设备列表中显示,造成混淆
- 设备数量统计不准确
## 解决方案
在设备数据同步过程中添加清理逻辑,自动删除已被重新分配到其他项目的设备。
### 实现内容
#### 1. 修改同步逻辑 (`IotDeviceSyncService.java`)
##### 添加必要的导入
```java
import java.util.Set;
import java.util.HashSet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
```
##### 在 `syncIotDeviceData()` 方法中添加设备ID集合提取
```59:65:tradeCattle/aiotagro-cattle-trade/src/main/java/com/aiotagro/cattletrade/business/service/IotDeviceSyncService.java
// 提取API返回的设备ID集合
Set<String> apiDeviceIds = new HashSet<>();
for (Map<String, Object> deviceData figuring deviceDataList) {
if (deviceData.get("deviceId") != null) {
apiDeviceIds.add(String.valueOf(deviceData.get("deviceId")));
}
}
```
##### 添加清理调用
```111:116:tradeCattle/aiotagro-cattle-trade/src/main/java/com/aiotagro/cattletrade/business/service/IotDeviceSyncService.java
// 清理已被重新分配到其他项目的设备
try {
cleanupReassignedDevices(apiDeviceIds);
} catch (Exception e) {
logger.error("清理重新分配设备失败", e);
}
```
#### 2. 新增清理方法 `cleanupReassignedDevices()`
```368:396:tradeCattle/aiotagro-cattle-trade/src/main/java/com/aiotagro/cattletrade/business/service/IotDeviceSyncService.java
private void cleanupReassignedDevices(Set<String> apiDeviceIds) {
try {
logger.info("开始清理已重新分配的设备");
// 查询数据库中所有设备
List<IotDeviceData> allDevices = iotDeviceDataMapper.selectList(null);
int deletedCount = 0;
for (IotDeviceData device : allDevices) {
// 如果设备不在API返回列表中
if (!apiDeviceIds.contains(device.getDeviceId())) {
// 只删除未被分配的设备tenantId和deliveryId都为null
if (device.getTenantId() == null && device.getDeliveryId() == null) {
iotDeviceDataMapper.deleteById(device.getId());
logger.info("删除已重新分配的设备: deviceId={}, id={}", device.getDeviceId(), device.getId());
deletedCount++;
} else {
logger.warn("设备不在API返回列表中但已被分配保留: deviceId={}, tenantId={}, deliveryId={}",
device.getDeviceId(), device.getTenantId(), device.getDeliveryId());
}
}
}
logger.info("清理完成,共删除 {} 个已重新分配的设备", deletedCount);
} catch (Exception e) {
logger.error("清理已重新分配设备时发生错误", e);
throw e;
}
}
```
### 清理逻辑说明
1. **设备对比**将数据库中所有设备与API返回的设备列表对比
2. **安全删除**:只删除满足以下条件的设备:
- 设备不在API返回列表中已被重新分配
- `tenantId` 为 null未分配给租户
- `deliveryId` 为 null未分配给装车订单
3. **保护机制**:如果设备已被分配(有 `tenantId` 或 `deliveryId`),则保留该设备,防止误删重要数据
4. **日志记录**:记录清理过程和结果,便于问题追踪
### 工作流程
1. 从外部API获取设备数据列表
2. 提取API返回的设备ID集合
3. 同步/更新API中的设备数据
4. **新增**清理数据库中不在API列表且未被分配的设备
5. 记录同步日志
### 优势
- ✅ 自动清理:在同步过程中自动清理,无需手动操作
- ✅ 安全可靠:只删除未分配的设备,已分配的设备不会被误删
- ✅ 实时更新:每次同步都会检查并清理
- ✅ 日志完整:详细的日志记录,便于问题追踪
- ✅ 错误处理:异常捕获,不影响主同步流程
### 注意事项
1. **已分配设备保护**如果设备已被分配给租户或装车订单即使不在API列表中也不会被删除
2. **数据保留**:历史数据(如设备日志)不会受影响,只删除 `iot_device_data` 表中的记录
3. **性能考虑**:每次同步都会查询所有设备,如果设备数量很大,可能需要优化查询策略
### 测试建议
1. **模拟场景**:在外部物联网系统将某个设备重新分配到其他项目
2. **执行同步**:手动触发设备同步(调用 `/api/iotSync/sync` 接口)
3. **验证结果**
- 检查数据库中该设备是否被删除
- 查看同步日志,确认清理操作记录
- 如果设备已被分配,应保留在数据库中
### 部署步骤
1. 备份当前代码和数据库
2. 更新 `IotDeviceSyncService.java` 文件
3. 重新编译并部署后端服务
4. 测试同步功能
5. 检查日志确保清理功能正常工作
## 修改文件
- `tradeCattle/aiotagro-cattle-trade/src/main/java/com/aiotagro/cattletrade/business/service/IotDeviceSyncService.java`
## 相关接口
- 设备同步接口:`POST /api/iotSync/sync`
- 设备查询接口:`POST /api/iotDevice/queryList`
## 创建时间
2025-01-16