| | |
| | | |
| | | import com.ruoyi.http.vo.StatisticEleRecordVo; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.LocalDate; |
| | | import java.time.YearMonth; |
| | | import java.time.format.DateTimeFormatter; |
| | |
| | | public final class StatisticEleAggregateUtil { |
| | | |
| | | private static final DateTimeFormatter DAY_FMT = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | private static final int CONSUMPTION_SCALE = StatisticEleReadingUtil.CONSUMPTION_SCALE; |
| | | private static final int SUMMARY_SCALE = 2; |
| | | |
| | | private StatisticEleAggregateUtil() { |
| | | } |
| | |
| | | |
| | | /** 明细记录总用电量(与数据采集页求和方式一致) */ |
| | | public static double sumRecordsTotal(List<StatisticEleRecordVo> records) { |
| | | return records.stream() |
| | | BigDecimal total = records.stream() |
| | | .map(StatisticEleRecordVo::getTotalConsumption) |
| | | .filter(v -> v != null) |
| | | .mapToDouble(Double::doubleValue) |
| | | .sum(); |
| | | .map(BigDecimal::valueOf) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | return roundSummary(total); |
| | | } |
| | | |
| | | public static Function<String, String> bucketFn(String dimension) { |
| | |
| | | metrics.setMinConsumption(0.0); |
| | | return metrics; |
| | | } |
| | | List<Double> values = buckets.stream() |
| | | List<BigDecimal> values = buckets.stream() |
| | | .map(StatisticEleRecordVo::getTotalConsumption) |
| | | .filter(v -> v != null) |
| | | .map(BigDecimal::valueOf) |
| | | .collect(Collectors.toList()); |
| | | double total = values.stream().mapToDouble(Double::doubleValue).sum(); |
| | | metrics.setTotalConsumption(round(total)); |
| | | metrics.setAvgConsumption(round(total / values.size())); |
| | | metrics.setMaxConsumption(round(values.stream().mapToDouble(Double::doubleValue).max().orElse(0))); |
| | | metrics.setMinConsumption(round(values.stream().mapToDouble(Double::doubleValue).min().orElse(0))); |
| | | BigDecimal total = values.stream().reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | metrics.setTotalConsumption(roundSummary(total)); |
| | | metrics.setAvgConsumption(roundSummary(total.divide( |
| | | BigDecimal.valueOf(values.size()), CONSUMPTION_SCALE, RoundingMode.HALF_UP))); |
| | | metrics.setMaxConsumption(roundSummary(values.stream().max(BigDecimal::compareTo).orElse(BigDecimal.ZERO))); |
| | | metrics.setMinConsumption(roundSummary(values.stream().min(BigDecimal::compareTo).orElse(BigDecimal.ZERO))); |
| | | return metrics; |
| | | } |
| | | |
| | |
| | | |
| | | private static List<StatisticEleRecordVo> sorted(Map<String, StatisticEleRecordVo> map) { |
| | | return map.values().stream() |
| | | .peek(vo -> normalizeConsumptions(vo)) |
| | | .sorted(Comparator.comparing(StatisticEleRecordVo::getTimeKey)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | |
| | | } |
| | | |
| | | private static Double add(Double a, Double b) { |
| | | return (a == null ? 0.0 : a) + (b == null ? 0.0 : b); |
| | | BigDecimal sum = BigDecimal.valueOf(a == null ? 0.0 : a) |
| | | .add(BigDecimal.valueOf(b == null ? 0.0 : b)); |
| | | return roundConsumption(sum); |
| | | } |
| | | |
| | | private static double round(double value) { |
| | | return Math.round(value * 100.0) / 100.0; |
| | | /** 统一电量字段精度(明细展示) */ |
| | | public static void normalizeConsumptions(StatisticEleRecordVo vo) { |
| | | if (vo == null) { |
| | | return; |
| | | } |
| | | vo.setTotalConsumption(roundConsumption(vo.getTotalConsumption())); |
| | | vo.setSharpConsumption(roundConsumption(vo.getSharpConsumption())); |
| | | vo.setPeakConsumption(roundConsumption(vo.getPeakConsumption())); |
| | | vo.setFlatConsumption(roundConsumption(vo.getFlatConsumption())); |
| | | vo.setValleyConsumption(roundConsumption(vo.getValleyConsumption())); |
| | | } |
| | | |
| | | public static void normalizeConsumptions(List<StatisticEleRecordVo> records) { |
| | | if (records == null) { |
| | | return; |
| | | } |
| | | records.forEach(vo -> normalizeConsumptions(vo)); |
| | | } |
| | | |
| | | private static Double roundConsumption(Double value) { |
| | | if (value == null) { |
| | | return null; |
| | | } |
| | | return roundConsumption(BigDecimal.valueOf(value)); |
| | | } |
| | | |
| | | private static Double roundConsumption(BigDecimal value) { |
| | | if (value == null) { |
| | | return null; |
| | | } |
| | | return value.setScale(CONSUMPTION_SCALE, RoundingMode.HALF_UP).doubleValue(); |
| | | } |
| | | |
| | | private static double roundSummary(BigDecimal value) { |
| | | return value.setScale(SUMMARY_SCALE, RoundingMode.HALF_UP).doubleValue(); |
| | | } |
| | | |
| | | public record HourRange(String startTime, String endTime) {} |