refactor(property): 重构能源趋势接口

-分别返回今日和昨日、本月和上月、今年和去年的趋势数据
This commit is contained in:
2025-08-29 15:50:00 +08:00
parent 710379c1a4
commit fbc07ac629

View File

@@ -238,50 +238,80 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService {
public Map<String, Object> getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year) {
Map<String, Object> resultMap = new HashMap<>();
// hour
List<Map<String, Object>> hourList = baseMapper.getHourTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, day);
String yesterday = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(day), -1), "yyyy-MM-dd");
// todayHour
Map<String, Object> todayMap = trendHourData(floorId, meterId, meterType, day);
// yesterdayHour
Map<String, Object> yesterdayMap = trendHourData(floorId, meterId, meterType, yesterday);
Map<String, Object> hourMap = new HashMap<>();
String[] hourCategories = hourList.stream()
.map(map -> map.get("hour").toString())
.toArray(String[]::new);
BigDecimal[] hourData = hourList.stream()
.map(map -> new BigDecimal(map.get("total_consumption").toString()))
.toArray(BigDecimal[]::new);
hourMap.put("categories", hourCategories);
hourMap.put("data", hourData);
hourMap.put("total" , hourList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue());
hourMap.put("today", todayMap);
hourMap.put("yesterday", yesterdayMap);
// day
String[] monthArr = month.split("-");
List<Map<String, Object>> dayList = baseMapper.getDayTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, monthArr[0], monthArr[1]);
String lastMonth = Integer.parseInt(monthArr[1]) - 1 + "";
// nowMonth
Map<String, Object> nowMonthMap = trendDayData(floorId, meterId, meterType, monthArr[0], monthArr[1]);
// lastMonth
Map<String, Object> lastMonthMap = trendDayData(floorId, meterId, meterType, monthArr[0], lastMonth);
Map<String, Object> dayMap = new HashMap<>();
String[] dayCategories = dayList.stream()
.map(map -> map.get("day").toString())
.toArray(String[]::new);
BigDecimal[] dayData = dayList.stream()
.map(map -> new BigDecimal(map.get("total_consumption").toString()))
.toArray(BigDecimal[]::new);
dayMap.put("categories", dayCategories);
dayMap.put("data", dayData);
dayMap.put("total", dayList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue());
dayMap.put("nowMonth", nowMonthMap);
dayMap.put("lastMonth", lastMonthMap);
String lastYear = Integer.parseInt(year) - 1 + "";
// nowYear
Map<String, Object> nowYearMap = trendMonthData(floorId, meterId, meterType, year);
// lastYear
Map<String, Object> lastYearMap = trendMonthData(floorId, meterId, meterType, lastYear);
// month
List<Map<String, Object>> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year);
Map<String, Object> monthMap = new HashMap<>();
String[] monthCategories = monthList.stream()
.map(map -> map.get("month").toString())
.toArray(String[]::new);
BigDecimal[] monthData = monthList.stream()
.map(map -> new BigDecimal(map.get("total_consumption").toString()))
.toArray(BigDecimal[]::new);
monthMap.put("categories", monthCategories);
monthMap.put("data", monthData);
monthMap.put("total", monthList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue());
monthMap.put("nowYear", nowYearMap);
monthMap.put("lastYear", lastYearMap);
resultMap.put("hour", hourMap);
resultMap.put("day", dayMap);
resultMap.put("month", monthMap);
return resultMap;
}
private Map<String, Object> trendHourData(String floorId, String meterId, Long meterType, String day) {
Map<String, Object> hourMap = new HashMap<>();
List<Map<String, Object>> hourList = baseMapper.getHourTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, day);
List<String[]> hourData = new ArrayList<>();
hourList.forEach(item -> {
hourData.add(new String[]{item.get("hour").toString(), item.get("total_consumption").toString()});
});
Float total = hourList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue();
hourMap.put("total", total);
hourMap.put("data", hourData);
return hourMap;
}
private Map<String, Object> trendDayData(String floorId, String meterId, Long meterType, String year, String month) {
Map<String, Object> dayMap = new HashMap<>();
List<Map<String, Object>> dayList = baseMapper.getDayTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year, month);
List<String[]> dayData = new ArrayList<>();
dayList.forEach(item -> {
dayData.add(new String[]{item.get("day").toString(), item.get("total_consumption").toString()});
});
Float total = dayList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue();
dayMap.put("total", total);
dayMap.put("data", dayData);
return dayMap;
}
private Map<String, Object> trendMonthData(String floorId, String meterId, Long meterType, String year) {
Map<String, Object> resultMap = new HashMap<>();
List<Map<String, Object>> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year);
List<String[]> monthData = new ArrayList<>();
monthList.forEach(item -> {
monthData.add(new String[]{item.get("month").toString(), item.get("total_consumption").toString()});
});
Float total = monthList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue();
resultMap.put("total", total);
resultMap.put("data", monthData);
return resultMap;
}
}