ソースを参照

修复设备列表排序按照在线离线创建时间倒序排序

fuyuchuan 1 ヶ月 前
コミット
6026aaea61

+ 47 - 46
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -502,61 +502,62 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                     .like(StringUtils.isNotBlank(diRequest.getProductCode()), DmpDeviceInfo::getProductCode, diRequest.getProductCode())
                     .eq(diRequest.getServiceStatus() != null, DmpDeviceInfo::getServiceStatus, diRequest.getServiceStatus())
                     .eq(DmpDeviceInfo::getDeleteFlag, 0)
-                    .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId())
-                    .orderByDesc(DmpDeviceInfo::getId);
-            page = this.page(page, queryWrapper);
+                    .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId());
 
-            if (page.getTotal() > 0) {
-                List<String> devList = new ArrayList<>();
-                List<Integer> productIdList = new ArrayList<>();
-                for (DmpDeviceInfo deviceInfo : page.getRecords()) {
-                    if (!devList.contains(deviceInfo.getDeviceId())) {
-                        devList.add(deviceInfo.getDeviceId());
-                    }
-                    if (!productIdList.contains(deviceInfo.getProductId())) {
-                        productIdList.add(deviceInfo.getProductId());
-                    }
-                }
-
-                if (devList.size() > 0) {
-                    LambdaQueryWrapper<DmpDeviceStatus> queryWrapper1 = Wrappers.lambdaQuery();
-                    queryWrapper1.in(DmpDeviceStatus::getDeviceId, devList)
-                            .in(DmpDeviceStatus::getProductId, productIdList)
-                            .orderByAsc(DmpDeviceStatus::getDeviceStatus);
+            List<DmpDeviceInfo> allRecords = this.list(queryWrapper);
 
-                    List<DmpDeviceStatus> statusList = dmpDeviceStatusService.list(queryWrapper1);
+            if (!allRecords.isEmpty()) {
+                List<String> devList = allRecords.stream().map(DmpDeviceInfo::getDeviceId).distinct().collect(Collectors.toList());
+                List<Integer> productIdList = allRecords.stream().map(DmpDeviceInfo::getProductId).distinct().collect(Collectors.toList());
 
-                    // 创建一个Map,用于快速查找设备状态
-                    Map<String, DmpDeviceStatus> statusMap = new HashMap<>();
-                    for (DmpDeviceStatus status : statusList) {
-                        statusMap.put(status.getDeviceId(), status);
-                    }
+                LambdaQueryWrapper<DmpDeviceStatus> queryWrapper1 = Wrappers.lambdaQuery();
+                queryWrapper1.in(DmpDeviceStatus::getDeviceId, devList)
+                        .in(DmpDeviceStatus::getProductId, productIdList);
 
-                    // 重新排序设备信息,优先显示在线设备
-                    List<DmpDeviceInfo> sortedRecords = new ArrayList<>(page.getRecords());
-                    sortedRecords.sort((d1, d2) -> {
-                        Integer status1 = statusMap.get(d1.getDeviceId()).getDeviceStatus();
-                        Integer status2 = statusMap.get(d2.getDeviceId()).getDeviceStatus();
-                        // 在线设备优先(1 < 2),如果状态相同,则按id降序
-                        if (!status1.equals(status2)) {
-                            return status1.compareTo(status2);
-                        } else {
-                            return d2.getId().compareTo(d1.getId());
-                        }
-                    });
+                List<DmpDeviceStatus> statusList = dmpDeviceStatusService.list(queryWrapper1);
 
-                    // 更新页面记录
-                    page.setRecords(sortedRecords);
+                // 创建一个Map,用于快速查找设备状态
+                Map<String, DmpDeviceStatus> statusMap = new HashMap<>();
+                for (DmpDeviceStatus status : statusList) {
+                    statusMap.put(status.getDeviceId(), status);
+                }
 
-                    // 填充设备状态信息
-                    for (DmpDeviceInfo deviceInfo : page.getRecords()) {
-                        DmpDeviceStatus status = statusMap.get(deviceInfo.getDeviceId());
-                        deviceInfo.setDeviceStatus(status.getDeviceStatus());
-                        deviceInfo.setLastOnlineTime(status.getLastOnlineTime());
-                        deviceInfo.setLastOfflineTime(status.getLastOfflineTime());
+                // 重新排序设备信息,优先显示在线设备
+                allRecords.sort((d1, d2) -> {
+                    DmpDeviceStatus status1 = statusMap.get(d1.getDeviceId());
+                    DmpDeviceStatus status2 = statusMap.get(d2.getDeviceId());
+                    Integer deviceStatus1 = status1 != null ? status1.getDeviceStatus() : 2; // 默认离线
+                    Integer deviceStatus2 = status2 != null ? status2.getDeviceStatus() : 2; // 默认离线
+                    // 在线设备优先(1 < 2),如果状态相同,则按id降序
+                    return deviceStatus1.compareTo(deviceStatus2) != 0 ? deviceStatus1.compareTo(deviceStatus2) : d2.getId().compareTo(d1.getId());
+                });
+
+                // 填充设备状态信息
+                for (DmpDeviceInfo deviceInfo : allRecords) {
+                    DmpDeviceStatus status = statusMap.get(deviceInfo.getDeviceId());
+                    if (status != null) {
+                        deviceInfo.setDeviceStatus(status.getDeviceStatus()); // 填充设备状态
+                    } else {
+                        deviceInfo.setDeviceStatus(2); // 默认离线
                     }
                 }
             }
+
+            // 分页处理
+            int start = (pageCurrent - 1) * pageSize;
+            int end = Math.min(start + pageSize, allRecords.size());
+
+            // 确保 start 不超过 allRecords 的大小
+            if (start >= allRecords.size()) {
+                start = 0; // 如果 start 超出范围,重置为 0
+                end = Math.min(pageSize, allRecords.size()); // 重新计算 end
+            }
+
+            List<DmpDeviceInfo> pageRecords = allRecords.subList(start, end);
+            total = allRecords.size();
+
+            page.setRecords(pageRecords);
+            page.setTotal(total);
         }