ソースを参照

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

fuyuchuan 1 ヶ月 前
コミット
ed7148dbc0

+ 13 - 11
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -505,6 +505,7 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                     .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId())
                     .orderByDesc(DmpDeviceInfo::getId);
             page = this.page(page, queryWrapper);
+
             if (page.getTotal() > 0) {
                 List<String> devList = new ArrayList<>();
                 List<Integer> productIdList = new ArrayList<>();
@@ -520,7 +521,8 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                 if (devList.size() > 0) {
                     LambdaQueryWrapper<DmpDeviceStatus> queryWrapper1 = Wrappers.lambdaQuery();
                     queryWrapper1.in(DmpDeviceStatus::getDeviceId, devList)
-                            .in(DmpDeviceStatus::getProductId, productIdList);
+                            .in(DmpDeviceStatus::getProductId, productIdList)
+                            .orderByAsc(DmpDeviceStatus::getDeviceStatus);
 
                     List<DmpDeviceStatus> statusList = dmpDeviceStatusService.list(queryWrapper1);
 
@@ -533,12 +535,14 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                     // 重新排序设备信息,优先显示在线设备
                     List<DmpDeviceInfo> sortedRecords = new ArrayList<>(page.getRecords());
                     sortedRecords.sort((d1, d2) -> {
-                        Integer status1 = statusMap.getOrDefault(d1.getDeviceId(), new DmpDeviceStatus()).getDeviceStatus();
-                        Integer status2 = statusMap.getOrDefault(d2.getDeviceId(), new DmpDeviceStatus()).getDeviceStatus();
+                        Integer status1 = statusMap.get(d1.getDeviceId()).getDeviceStatus();
+                        Integer status2 = statusMap.get(d2.getDeviceId()).getDeviceStatus();
                         // 在线设备优先(1 < 2),如果状态相同,则按id降序
-                        if (status1 == null) status1 = 2; // 默认离线
-                        if (status2 == null) status2 = 2;
-                        return status1.compareTo(status2) != 0 ? status1.compareTo(status2) : d2.getId().compareTo(d1.getId());
+                        if (!status1.equals(status2)) {
+                            return status1.compareTo(status2);
+                        } else {
+                            return d2.getId().compareTo(d1.getId());
+                        }
                     });
 
                     // 更新页面记录
@@ -547,11 +551,9 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                     // 填充设备状态信息
                     for (DmpDeviceInfo deviceInfo : page.getRecords()) {
                         DmpDeviceStatus status = statusMap.get(deviceInfo.getDeviceId());
-                        if (status != null) {
-                            deviceInfo.setDeviceStatus(status.getDeviceStatus());
-                            deviceInfo.setLastOnlineTime(status.getLastOnlineTime());
-                            deviceInfo.setLastOfflineTime(status.getLastOfflineTime());
-                        }
+                        deviceInfo.setDeviceStatus(status.getDeviceStatus());
+                        deviceInfo.setLastOnlineTime(status.getLastOnlineTime());
+                        deviceInfo.setLastOfflineTime(status.getLastOfflineTime());
                     }
                 }
             }