Ver código fonte

解决平均数

wukai 1 mês atrás
pai
commit
7be391e615

+ 16 - 8
jjt-biz/src/main/java/com/jjt/inventory/service/impl/InventoryServiceImpl.java

@@ -266,8 +266,8 @@ public class InventoryServiceImpl implements IInventoryService {
         params.put("sTime", DateUtils.toDate(st));
         params.put("eTime", DateUtils.toDate(ed));
         search.setParams(params);
-        Map<Integer, List<TwinCalcHourBp>> hourMap = hourBpService.selectTwinCalcHourBpList(search).stream().collect(Collectors.groupingBy(TwinCalcHourBp::getHour));
-
+        List<TwinCalcHourBp> hourBps = hourBpService.selectTwinCalcHourBpList(search);
+        Map<Integer, List<TwinCalcHourBp>> hourMap = hourBps.stream().collect(Collectors.groupingBy(TwinCalcHourBp::getHour));
         // 统计方法
         BiFunction<Integer, ToIntFunction<TwinCalcHourBp>, Pair<Integer, Integer>> stats;
         stats = (defaultHour, mapper) -> {
@@ -275,9 +275,6 @@ public class InventoryServiceImpl implements IInventoryService {
                     .max(Comparator.comparingInt(entry -> entry.getValue().stream().mapToInt(mapper).sum()));
             Optional<Map.Entry<Integer, List<TwinCalcHourBp>>> minEntry = hourMap.entrySet().stream()
                     .min(Comparator.comparingInt(entry -> entry.getValue().stream().mapToInt(mapper).sum()));
-
-//            int max = maxEntry.map(e -> e.getValue().stream().mapToInt(mapper).sum()).orElse(0);
-//            int min = minEntry.map(e -> e.getValue().stream().mapToInt(mapper).sum()).orElse(0);
             int maxHour = maxEntry.map(Map.Entry::getKey).orElse(defaultHour);
             int minHour = minEntry.map(Map.Entry::getKey).orElse(defaultHour);
 
@@ -290,28 +287,35 @@ public class InventoryServiceImpl implements IInventoryService {
         int rollMinHour = rollStats.getValue();
         int rollMax = hourMap.getOrDefault(rollMaxHour, Collections.emptyList()).stream().mapToInt(TwinCalcHourBp::getRollNum).sum();
         int rollMin = hourMap.getOrDefault(rollMinHour, Collections.emptyList()).stream().mapToInt(TwinCalcHourBp::getRollNum).sum();
-
+        BigDecimal rollAvg = BigDecimal.valueOf(hourBps.stream().mapToInt(TwinCalcHourBp::getRollNum).sum())
+                .divide(BigDecimal.valueOf(hourMap.size()), 2, RoundingMode.HALF_UP);
         // 米数统计
         Pair<Integer, Integer> meterStats = stats.apply(-1, TwinCalcHourBp::getMeterNum);
         int meterMaxHour = meterStats.getKey();
         int meterMinHour = meterStats.getValue();
         int meterMax = hourMap.getOrDefault(meterMaxHour, Collections.emptyList()).stream().mapToInt(TwinCalcHourBp::getMeterNum).sum();
         int meterMin = hourMap.getOrDefault(meterMinHour, Collections.emptyList()).stream().mapToInt(TwinCalcHourBp::getMeterNum).sum();
-
+        BigDecimal meterAvg = BigDecimal.valueOf(hourBps.stream().mapToInt(TwinCalcHourBp::getMeterNum).sum())
+                .divide(BigDecimal.valueOf(hourMap.size()), 2, RoundingMode.HALF_UP);
         // 重量统计
         Pair<Integer, Integer> weightStats = stats.apply(-1, TwinCalcHourBp::getWeightNum);
         int weightMaxHour = weightStats.getKey();
         int weightMinHour = weightStats.getValue();
         int weightMax = hourMap.getOrDefault(weightMaxHour, Collections.emptyList()).stream().mapToInt(TwinCalcHourBp::getWeightNum).sum();
         int weightMin = hourMap.getOrDefault(weightMinHour, Collections.emptyList()).stream().mapToInt(TwinCalcHourBp::getWeightNum).sum();
+        BigDecimal weightAvg = BigDecimal.valueOf(hourBps.stream().mapToInt(TwinCalcHourBp::getWeightNum).sum())
+                .divide(BigDecimal.valueOf(hourMap.size()), 2, RoundingMode.HALF_UP);
 
         TwinCalcDayBp day = new TwinCalcDayBp();
         day.setRollMax(rollMax);
         day.setRollMin(rollMin);
+        day.setRollAvg(rollAvg);
         day.setMeterMax(meterMax);
         day.setMeterMin(meterMin);
+        day.setMeterAvg(meterAvg);
         day.setWeightMax(weightMax);
         day.setWeightMin(weightMin);
+        day.setWeightAvg(weightAvg);
         day.setRollData(rollMaxHour + "," + rollMinHour);
         day.setMeterData(meterMaxHour + "," + meterMinHour);
         day.setWeightData(weightMaxHour + "," + weightMinHour);
@@ -332,7 +336,10 @@ public class InventoryServiceImpl implements IInventoryService {
         params.put("sTime", DateUtils.toDate(st));
         params.put("eTime", DateUtils.toDate(ed));
         search.setParams(params);
-        Map<Integer, List<TwinCalcHourPt>> hourMap = hourPtService.selectTwinCalcHourPtList(search).stream().collect(Collectors.groupingBy(TwinCalcHourPt::getHour));
+        List<TwinCalcHourPt> hourPts = hourPtService.selectTwinCalcHourPtList(search);
+        Map<Integer, List<TwinCalcHourPt>> hourMap = hourPts.stream().collect(Collectors.groupingBy(TwinCalcHourPt::getHour));
+        BigDecimal avg = BigDecimal.valueOf(hourPts.stream().mapToInt(TwinCalcHourPt::getNum).average().orElse(0)).setScale(2, RoundingMode.HALF_UP);
+
         //找到最大值所在的小时及其小时值
         Optional<Map.Entry<Integer, List<TwinCalcHourPt>>> maxEntry = hourMap.entrySet().stream()
                 .max(Comparator.comparingInt(entry -> entry.getValue().stream().mapToInt(TwinCalcHourPt::getNum).sum()));
@@ -343,6 +350,7 @@ public class InventoryServiceImpl implements IInventoryService {
         day.setMaxNum(max);
         BigDecimal rate = BigDecimal.valueOf(max).divide(BigDecimal.valueOf(maxLimit), 4, RoundingMode.HALF_UP);
         day.setRate(rate);
+        day.setAvgNum(avg);
         List<Map<String, Object>> dataList = new ArrayList<>();
         tempList.stream().filter(item -> item.getNum() > 0).forEach(item -> {
             BigDecimal ratio = BigDecimal.valueOf(item.getNum()).divide(BigDecimal.valueOf(max), 4, RoundingMode.HALF_UP);