fix:大屏查询记录GC问题

hhk
HuangHuiKang 3 weeks ago
parent d0b80b5cb7
commit cbc80dd6d1

@ -36,5 +36,5 @@ public interface DeviceOperationRecordMapper extends BaseMapperX<DeviceOperation
List<DeviceOperationRecordDO> selectLatestByDeviceAndRule(@Param("deviceIds") List<Long> deviceIds, @Param("ruleCodes") List<String> ruleCodes);
List<DeviceOperationRecordDO> selectLatestRecords(@Param("deviceIds") Collection<Long> deviceIds);
}

@ -1330,22 +1330,14 @@ public class DeviceServiceImpl implements DeviceService {
return Collections.emptyMap();
}
// 查询所有设备的最新运行记录
List<DeviceOperationRecordDO> records = deviceOperationRecordMapper.selectList(
Wrappers.<DeviceOperationRecordDO>lambdaQuery()
.in(DeviceOperationRecordDO::getDeviceId, deviceIds)
.orderByDesc(DeviceOperationRecordDO::getCreateTime));
List<DeviceOperationRecordDO> records = deviceOperationRecordMapper.selectLatestRecords(deviceIds);
// 构建设备ID到最新记录的映射
Map<Long, DeviceOperationRecordDO> latestRecordMap = new HashMap<>();
for (DeviceOperationRecordDO record : records) {
// 只保留每个设备的第一条记录(因为已经按创建时间倒序排序)
if (!latestRecordMap.containsKey(record.getDeviceId())) {
latestRecordMap.put(record.getDeviceId(), record);
}
}
return latestRecordMap;
// 直接用 Stream 转 Map
return records.stream()
.collect(Collectors.toMap(
DeviceOperationRecordDO::getDeviceId,
Function.identity()
));
}
/**

@ -73,4 +73,21 @@
WHERE tmp.rn = 1
</select>
<select id="selectLatestRecords"
resultType="cn.iocoder.yudao.module.iot.dal.dataobject.deviceoperationrecord.DeviceOperationRecordDO">
SELECT t1.*
FROM device_operation_record t1
INNER JOIN (
SELECT device_id, MAX(create_time) AS max_time
FROM device_operation_record
WHERE device_id IN
<foreach collection="deviceIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
GROUP BY device_id
) t2
ON t1.device_id = t2.device_id
AND t1.create_time = t2.max_time
</select>
</mapper>
Loading…
Cancel
Save