yuan
3 天以前 a98454d581018eeee3ca8e7f9b381bee6608eb2d
src/main/java/com/ruoyi/http/util/StatisticEleAggregateUtil.java
@@ -66,10 +66,84 @@
            mergeInto(map, key, bucket, record);
            StatisticEleRecordVo agg = map.get(key);
            agg.setMeterId(record.getMeterId());
            agg.setAddress(record.getAddress());
            agg.setCollectorNo(record.getCollectorNo());
            mergeMeterInfo(agg, record);
        }
        return sorted(map);
    }
    private static void mergeMeterInfo(StatisticEleRecordVo agg, StatisticEleRecordVo record) {
        if (hasText(record.getMeterName())) {
            agg.setMeterName(record.getMeterName());
        }
        if (hasText(record.getAddress())) {
            agg.setAddress(record.getAddress());
        }
        mergeTimeRange(agg, record);
    }
    private static void mergeTimeRange(StatisticEleRecordVo agg, StatisticEleRecordVo record) {
        if (hasText(record.getStartTime())) {
            if (!hasText(agg.getStartTime()) || record.getStartTime().compareTo(agg.getStartTime()) < 0) {
                agg.setStartTime(record.getStartTime());
            }
        }
        if (hasText(record.getEndTime())) {
            if (!hasText(agg.getEndTime()) || record.getEndTime().compareTo(agg.getEndTime()) > 0) {
                agg.setEndTime(record.getEndTime());
            }
        }
    }
    /** 汇总后若起止时间为空,按 timeKey 推导 */
    private static void fillTimeRangeIfEmpty(StatisticEleRecordVo vo) {
        if (hasText(vo.getStartTime()) && hasText(vo.getEndTime())) {
            return;
        }
        String timeKey = vo.getTimeKey();
        if (!hasText(timeKey)) {
            return;
        }
        if (timeKey.contains("Q")) {
            String[] parts = timeKey.split("Q");
            if (parts.length != 2) {
                return;
            }
            int year = Integer.parseInt(parts[0]);
            int quarter = Integer.parseInt(parts[1]);
            int startMonth = (quarter - 1) * 3 + 1;
            int endMonth = startMonth + 2;
            YearMonth endYm = YearMonth.of(year, endMonth);
            vo.setStartTime(String.format("%04d-%02d-01 00:00:00", year, startMonth));
            vo.setEndTime(String.format("%04d-%02d-%02d 23:59:59", year, endMonth, endYm.lengthOfMonth()));
            return;
        }
        if (timeKey.length() == 4) {
            vo.setStartTime(timeKey + "-01-01 00:00:00");
            vo.setEndTime(timeKey + "-12-31 23:59:59");
            return;
        }
        if (timeKey.length() == 6) {
            YearMonth ym = YearMonth.parse(timeKey, DateTimeFormatter.ofPattern("yyyyMM"));
            vo.setStartTime(String.format("%04d-%02d-01 00:00:00", ym.getYear(), ym.getMonthValue()));
            vo.setEndTime(String.format("%04d-%02d-%02d 23:59:59",
                    ym.getYear(), ym.getMonthValue(), ym.lengthOfMonth()));
            return;
        }
        if (timeKey.length() >= 8) {
            String day = timeKey.substring(0, 8);
            vo.setStartTime(toDateTime(day, "00:00:00"));
            vo.setEndTime(toDateTime(day, "23:59:59"));
        }
    }
    private static String toDateTime(String yyyyMMdd, String time) {
        return yyyyMMdd.substring(0, 4) + "-"
                + yyyyMMdd.substring(4, 6) + "-"
                + yyyyMMdd.substring(6, 8) + " " + time;
    }
    private static boolean hasText(String value) {
        return value != null && !value.isBlank();
    }
    /**
@@ -219,7 +293,10 @@
    private static List<StatisticEleRecordVo> sorted(Map<String, StatisticEleRecordVo> map) {
        return map.values().stream()
                .peek(vo -> normalizeConsumptions(vo))
                .peek(vo -> {
                    normalizeConsumptions(vo);
                    fillTimeRangeIfEmpty(vo);
                })
                .sorted(Comparator.comparing(StatisticEleRecordVo::getTimeKey))
                .collect(Collectors.toList());
    }