Kaynağa Gözat

会议设备列表查询代码优化

fuyuchuan 10 saat önce
ebeveyn
işleme
32fbfb64a4

+ 83 - 44
service-meeting/service-meeting-biz/src/main/java/com/usky/meeting/service/impl/MeetingDeviceServiceImpl.java

@@ -29,10 +29,7 @@ import org.springframework.stereotype.Repository;
 import org.springframework.stereotype.Service;
 
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -62,6 +59,7 @@ public class MeetingDeviceServiceImpl extends AbstractCrudService<MeetingDeviceM
 
     @Override
     public CommonPage<MeetingDevice> meetingDeviceList(MeetingDeviceRequestVO requestVO) {
+        LocalDateTime now = LocalDateTime.now();
         Integer current = requestVO.getCurrent();
         Integer size = requestVO.getSize();
         IPage<MeetingDevice> page = new Page<>(current, size);
@@ -130,57 +128,98 @@ public class MeetingDeviceServiceImpl extends AbstractCrudService<MeetingDeviceM
         queryWrapper4.eq(MeetingTemplate::getTenantId, tenantId);
         List<MeetingTemplate> meetingTemplateList = meetingTemplateMapper.selectList(queryWrapper4);
 
-
         if (!page.getRecords().isEmpty()) {
+            // 1. 提取设备编码并过滤空值
+            List<String> validDeviceCodes = page.getRecords().stream()
+                    .map(MeetingDevice::getDeviceCode)
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toList());
 
-            if (CollectionUtils.isNotEmpty(meetingRoomsList)) {
-                for (int i = 0; i < page.getRecords().size(); i++) {
-                    if (Objects.nonNull(page.getRecords().get(i).getRoomId())) {
-                        Long RId = page.getRecords().get(i).getRoomId();
-                        for (MeetingRoom meetingRoom : meetingRoomsList) {
-                            if (RId.equals(meetingRoom.getRoomId())) {
-                                page.getRecords().get(i).setMeetingRoom(meetingRoom);
-                                break;
-                            }
-                        }
-                    }
-
-                }
+            // 2. 批量查询心跳信息并构建Map(Java 8 兼容版)
+            Map<String, LocalDateTime> deviceHeartbeatMap;
+            if (CollectionUtils.isEmpty(validDeviceCodes)) {
+                deviceHeartbeatMap = Collections.emptyMap();
+            } else {
+                deviceHeartbeatMap = heartbeatMapper.selectList(Wrappers.<MeetingDeviceHeartbeat>lambdaQuery()
+                                .in(MeetingDeviceHeartbeat::getDeviceCode, validDeviceCodes))
+                        .stream()
+                        .collect(Collectors.toMap(
+                                MeetingDeviceHeartbeat::getDeviceCode,
+                                MeetingDeviceHeartbeat::getCreateTime,
+                                (existing, replacement) -> existing  // 解决重复key问题
+                        ));
             }
 
-            if (CollectionUtils.isNotEmpty(egDeviceList)) {
-                for (int i = 0; i < page.getRecords().size(); i++) {
-                    if (Objects.nonNull(page.getRecords().get(i).getEgDeviceId())) {
-                        Integer egDeviceId = page.getRecords().get(i).getEgDeviceId();
-                        for (EgDevice egDevice : egDeviceList) {
-                            if (egDeviceId.equals(egDevice.getId())) {
-                                page.getRecords().get(i).setEgDevice(egDevice);
-                                break;
-                            }
-                        }
-                    }
+            // 3. 设备状态赋值(逻辑不变)
+            page.getRecords().forEach(meetingDevice -> {
+                String deviceCode1 = meetingDevice.getDeviceCode();
+                boolean isHeartbeatValid = Objects.nonNull(deviceCode1)
+                        && deviceHeartbeatMap.containsKey(deviceCode1)
+                        && now.isBefore(deviceHeartbeatMap.get(deviceCode1).plusMinutes(5));
+                meetingDevice.setDeviceStatus(isHeartbeatValid ? 1 : 0);
+            });
 
-                }
-            }
+            // 4. 通用映射关联数据(逻辑不变)
+            mapAssociatedData(page.getRecords(),
+                    MeetingDevice::getRoomId,
+                    buildIdMap(meetingRoomsList, MeetingRoom::getRoomId),
+                    MeetingDevice::setMeetingRoom);
 
-            if (CollectionUtils.isNotEmpty(meetingTemplateList)) {
-                for (int i = 0; i < page.getRecords().size(); i++) {
-                    if (Objects.nonNull(page.getRecords().get(i).getTemplateId())) {
-                        Integer templateId = page.getRecords().get(i).getTemplateId();
-                        for (MeetingTemplate meetingTemplate : meetingTemplateList) {
-                            if (templateId.equals(meetingTemplate.getId())) {
-                                page.getRecords().get(i).setMeetingTemplate(meetingTemplate);
-                                break;
-                            }
-                        }
-                    }
-                }
-            }
+            mapAssociatedData(page.getRecords(),
+                    MeetingDevice::getEgDeviceId,
+                    buildIdMap(egDeviceList, EgDevice::getId),
+                    MeetingDevice::setEgDevice);
+
+            mapAssociatedData(page.getRecords(),
+                    MeetingDevice::getTemplateId,
+                    buildIdMap(meetingTemplateList, MeetingTemplate::getId),
+                    MeetingDevice::setMeetingTemplate);
         }
 
         return new CommonPage<>(page.getRecords(), page.getTotal(), size, current);
     }
 
+    /**
+     * 构建ID到对象的Map,用于快速查找
+     * @param dataList 待处理的列表
+     * @param idExtractor ID提取器
+     * @return ID到对象的Map
+     */
+    private <T, ID> Map<ID, T> buildIdMap(List<T> dataList, java.util.function.Function<T, ID> idExtractor) {
+        if (CollectionUtils.isEmpty(dataList)) {
+            return Collections.emptyMap();
+        }
+        return dataList.stream()
+                .filter(Objects::nonNull)
+                .collect(Collectors.toMap(
+                        idExtractor,
+                        t -> t,
+                        (existing, replacement) -> existing
+                ));
+    }
+
+    /**
+     * 通用关联数据映射方法
+     * @param mainList 主列表(MeetingDevice列表)
+     * @param keyExtractor 主对象的关联ID提取器
+     * @param dataMap 关联数据的ID->对象Map
+     * @param setter 主对象的关联数据设置器
+     */
+    private <T, ID, D> void mapAssociatedData(List<T> mainList,
+                                              java.util.function.Function<T, ID> keyExtractor,
+                                              Map<ID, D> dataMap,
+                                              java.util.function.BiConsumer<T, D> setter) {
+        if (CollectionUtils.isEmpty(mainList) || CollectionUtils.isEmpty(dataMap)) {
+            return;
+        }
+        mainList.forEach(item -> {
+            ID key = keyExtractor.apply(item);
+            if (Objects.nonNull(key) && dataMap.containsKey(key)) {
+                setter.accept(item, dataMap.get(key));
+            }
+        });
+    }
+
     @Override
     public void add(MeetingDevice meetingDevice) {
         if (checkNameUnique(meetingDevice)) {