| | |
| | | .isNotNull(DeviceLedger::getExternalCode) |
| | | .ne(DeviceLedger::getExternalCode, "")); |
| | | |
| | | Map<String, String> guidDeviceNameMap = iotDevices.stream() |
| | | Map<String, DeviceLedger> guidDeviceMap = iotDevices.stream() |
| | | .filter(item -> StringUtils.isNotEmpty(item.getExternalCode())) |
| | | .collect(Collectors.toMap( |
| | | item -> item.getExternalCode().trim(), |
| | | item -> StringUtils.isNotEmpty(item.getDeviceName()) ? item.getDeviceName().trim() : "", |
| | | (oldValue, newValue) -> StringUtils.isNotEmpty(oldValue) ? oldValue : newValue, |
| | | item -> item, |
| | | (oldValue, newValue) -> StringUtils.isNotEmpty(oldValue.getDeviceName()) ? oldValue : newValue, |
| | | LinkedHashMap::new |
| | | )); |
| | | List<String> guidList = new ArrayList<>(guidDeviceNameMap.keySet()); |
| | | List<String> guidList = new ArrayList<>(guidDeviceMap.keySet()); |
| | | |
| | | List<Map<String, String>> maps = realTimeEnergyConsumptionService |
| | | .getRealData(guidList); |
| | | for (Map<String, String> item : maps) { |
| | | String guid = item.get("guid"); |
| | | if (StringUtils.isNotEmpty(guid)) { |
| | | String deviceName = guidDeviceNameMap.get(guid.trim()); |
| | | if (StringUtils.isNotEmpty(deviceName)) { |
| | | item.put("deviceName", deviceName); |
| | | DeviceLedger device = guidDeviceMap.get(guid.trim()); |
| | | if (device != null) { |
| | | if (StringUtils.isNotEmpty(device.getDeviceName())) { |
| | | item.put("deviceName", device.getDeviceName().trim()); |
| | | } |
| | | if (StringUtils.isNotEmpty(device.getStorageLocation())) { |
| | | item.put("storageLocation", device.getStorageLocation().trim()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 按 deviceName 第一个字符(数字)排序 |
| | | maps.sort((a, b) -> { |
| | | String nameA = a.getOrDefault("deviceName", ""); |
| | | String nameB = b.getOrDefault("deviceName", ""); |
| | | int firstNumA = extractFirstNumber(nameA); |
| | | int firstNumB = extractFirstNumber(nameB); |
| | | return Integer.compare(firstNumA, firstNumB); |
| | | }); |
| | | return AjaxResult.success(maps); |
| | | } |
| | | |
| | |
| | | return AjaxResult.success(maps); |
| | | } |
| | | |
| | | /** |
| | | * 提取字符串开头的数字 |
| | | */ |
| | | private int extractFirstNumber(String str) { |
| | | if (str == null || str.isEmpty()) { |
| | | return Integer.MAX_VALUE; |
| | | } |
| | | StringBuilder numStr = new StringBuilder(); |
| | | for (int i = 0; i < str.length(); i++) { |
| | | char c = str.charAt(i); |
| | | if (Character.isDigit(c)) { |
| | | numStr.append(c); |
| | | } else { |
| | | break; |
| | | } |
| | | } |
| | | if (numStr.length() == 0) { |
| | | return Integer.MAX_VALUE; |
| | | } |
| | | return Integer.parseInt(numStr.toString()); |
| | | } |
| | | |
| | | } |