From 573285575967c07ab5d219ba23c03114cec3193e Mon Sep 17 00:00:00 2001 From: gaoguangchao <443960933@qq.com> Date: Tue, 23 Jul 2024 18:19:05 +0800 Subject: [PATCH] 02 --- .../controller/DcDeviceOnlineController.java | 36 +++++++++---------- .../java/com/zc/business/utils/MathUtil.java | 5 +++ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/DcDeviceOnlineController.java b/zc-business/src/main/java/com/zc/business/controller/DcDeviceOnlineController.java index 4a70b945..e1054b1c 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcDeviceOnlineController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcDeviceOnlineController.java @@ -87,18 +87,18 @@ public class DcDeviceOnlineController extends BaseController { @RequestParam("time") String endTime, @RequestParam("type") String[] types, @RequestParam(value = "direction",required = false) String direction) { - Map> maps = new HashMap<>(); + Map> maps = new HashMap<>(); LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); List onlineSums = onlineSumService.queryByDeviceTypeAndDate(types, start.toLocalDate(), end.toLocalDate(),direction); Map> stringListMap = onlineSums.stream().collect(Collectors.groupingBy(OnlineSum::getDeviceType)); for (String type : types) { - Map map = new HashMap<>(); + Map map = new HashMap<>(); List listOfType = stringListMap.getOrDefault(type, new ArrayList<>()); Map> localDateListMap = listOfType.stream().collect(Collectors.groupingBy(OnlineSum::getStatisticalDate)); for (LocalDate date = start.toLocalDate(); date.isBefore(end.toLocalDate().plusDays(1)); date=date.plusDays(1)) { List dayList = localDateListMap.getOrDefault(date, new ArrayList<>()); - map.put(date.toString(), MathUtil.doubleTwoDecimal(dayList.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))); + map.put(date.toString(), MathUtil.doubleTwoDecimalStr(dayList.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))+"%"); } maps.put(type, new TreeMap<>(map)); } @@ -165,20 +165,20 @@ public class DcDeviceOnlineController extends BaseController { Map> typeSumGroup = onlineSums.stream().collect(Collectors.groupingBy(OnlineSum::getDeviceType)); DeviceType.toMap().forEach((k,v) -> { Map itemMap = new HashMap<>(); - itemMap.put(SUM,totalCounts.getOrDefault(k,0L));//总数 - itemMap.put(SUM_USE_STATE,useCounts.getOrDefault(k,0L));//在用数 + itemMap.put(SUM,totalCounts.getOrDefault(k,0L).toString());//总数 + itemMap.put(SUM_USE_STATE,useCounts.getOrDefault(k,0L).toString());//在用数 List onlineSumsOfType = typeSumGroup.getOrDefault(k,new ArrayList<>()); - itemMap.put(LOST_RATE, MathUtil.doubleTwoDecimal(onlineSumsOfType.stream().mapToDouble(OnlineSum::getLossRate).average().orElse(0.0))); - itemMap.put(SUCESS_RATE,MathUtil.doubleTwoDecimal(onlineSumsOfType.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))); - itemMap.put(FAIL_RATE,MathUtil.doubleTwoDecimal(onlineSumsOfType.stream().mapToDouble(OnlineSum::getOfflineRate).average().orElse(0.0))); + itemMap.put(LOST_RATE, MathUtil.doubleTwoDecimalStr(onlineSumsOfType.stream().mapToDouble(OnlineSum::getLossRate).average().orElse(0.0))+"%"); + itemMap.put(SUCESS_RATE,MathUtil.doubleTwoDecimalStr(onlineSumsOfType.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))+"%"); + itemMap.put(FAIL_RATE,MathUtil.doubleTwoDecimalStr(onlineSumsOfType.stream().mapToDouble(OnlineSum::getOfflineRate).average().orElse(0.0))+"%"); returnMap.put(v,itemMap); }); Map allMap = new HashMap<>(); - allMap.put(SUM,dcDeviceList.size());//总数 - allMap.put(SUM_USE_STATE,useDeviceList.size());//在用数 - allMap.put(LOST_RATE,MathUtil.doubleTwoDecimal(onlineSums.stream().mapToDouble(OnlineSum::getLossRate).average().orElse(0.0))); - allMap.put(SUCESS_RATE,MathUtil.doubleTwoDecimal(onlineSums.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))); - allMap.put(FAIL_RATE,MathUtil.doubleTwoDecimal(onlineSums.stream().mapToDouble(OnlineSum::getOfflineRate).average().orElse(0.0))); + allMap.put(SUM,dcDeviceList.size()+"");//总数 + allMap.put(SUM_USE_STATE,useDeviceList.size()+"");//在用数 + allMap.put(LOST_RATE,MathUtil.doubleTwoDecimalStr(onlineSums.stream().mapToDouble(OnlineSum::getLossRate).average().orElse(0.0))+"%"); + allMap.put(SUCESS_RATE,MathUtil.doubleTwoDecimalStr(onlineSums.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))+"%"); + allMap.put(FAIL_RATE,MathUtil.doubleTwoDecimalStr(onlineSums.stream().mapToDouble(OnlineSum::getOfflineRate).average().orElse(0.0))+"%"); returnMap.put(All_TYPE,allMap); Map sortMap = new TreeMap<>(); String orderRule = redisCache.getCacheObject(ORDERRULE); @@ -211,12 +211,12 @@ public class DcDeviceOnlineController extends BaseController { Map> onLineSumMap = onlineSums.stream().filter(onlineSum -> onlineSum.getRoadId() != null).collect(Collectors.groupingBy(OnlineSum::getRoadId)); for(DcRoadSection dcRoadSection : dcRoadSections) { Map itemMap = new HashMap<>(); - itemMap.put(SUM,totalRoadCounts.getOrDefault(dcRoadSection.getId().toString(),0L));//总数 - itemMap.put(SUM_USE_STATE,useRoadCounts.getOrDefault(dcRoadSection.getId().toString(),0L));//在用数 + itemMap.put(SUM,totalRoadCounts.getOrDefault(dcRoadSection.getId().toString(),0L).toString());//总数 + itemMap.put(SUM_USE_STATE,useRoadCounts.getOrDefault(dcRoadSection.getId().toString(),0L).toString());//在用数 List onlineSumsByRoad = onLineSumMap.getOrDefault(dcRoadSection.getId(),new ArrayList<>()); - itemMap.put(LOST_RATE, MathUtil.doubleTwoDecimal(onlineSumsByRoad.stream().mapToDouble(OnlineSum::getLossRate).average().orElse(0.0))); - itemMap.put(SUCESS_RATE,MathUtil.doubleTwoDecimal(onlineSumsByRoad.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))); - itemMap.put(FAIL_RATE,MathUtil.doubleTwoDecimal(onlineSumsByRoad.stream().mapToDouble(OnlineSum::getOfflineRate).average().orElse(0.0))); + itemMap.put(LOST_RATE, MathUtil.doubleTwoDecimalStr(onlineSumsByRoad.stream().mapToDouble(OnlineSum::getLossRate).average().orElse(0.0))+"%"); + itemMap.put(SUCESS_RATE,MathUtil.doubleTwoDecimalStr(onlineSumsByRoad.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0))+"%"); + itemMap.put(FAIL_RATE,MathUtil.doubleTwoDecimalStr(onlineSumsByRoad.stream().mapToDouble(OnlineSum::getOfflineRate).average().orElse(0.0))+"%"); returnMap.put(dcRoadSection.getSectionName(),itemMap); } return AjaxResult.success(returnMap); diff --git a/zc-business/src/main/java/com/zc/business/utils/MathUtil.java b/zc-business/src/main/java/com/zc/business/utils/MathUtil.java index 2255c00d..241ef7c5 100644 --- a/zc-business/src/main/java/com/zc/business/utils/MathUtil.java +++ b/zc-business/src/main/java/com/zc/business/utils/MathUtil.java @@ -15,4 +15,9 @@ public class MathUtil { BigDecimal roundedNumber = originalNumber.setScale(2, RoundingMode.HALF_UP); return roundedNumber.doubleValue(); } + public static String doubleTwoDecimalStr(double d){ + BigDecimal originalNumber = new BigDecimal(d); + BigDecimal roundedNumber = originalNumber.setScale(2, RoundingMode.HALF_UP); + return roundedNumber.toString(); + } }