From fbc07ac629403dec83aafc99a46172e30d79bfad Mon Sep 17 00:00:00 2001 From: zcxlsm Date: Fri, 29 Aug 2025 15:50:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(property):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E8=83=BD=E6=BA=90=E8=B6=8B=E5=8A=BF=E6=8E=A5=E5=8F=A3=20-?= =?UTF-8?q?=E5=88=86=E5=88=AB=E8=BF=94=E5=9B=9E=E4=BB=8A=E6=97=A5=E5=92=8C?= =?UTF-8?q?=E6=98=A8=E6=97=A5=E3=80=81=E6=9C=AC=E6=9C=88=E5=92=8C=E4=B8=8A?= =?UTF-8?q?=E6=9C=88=E3=80=81=E4=BB=8A=E5=B9=B4=E5=92=8C=E5=8E=BB=E5=B9=B4?= =?UTF-8?q?=E7=9A=84=E8=B6=8B=E5=8A=BF=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TbMeterRecordServiceImpl.java | 98 ++++++++++++------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java index 2c206a81..de1e099e 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java @@ -238,50 +238,80 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { public Map getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year) { Map resultMap = new HashMap<>(); - // hour - List> 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 todayMap = trendHourData(floorId, meterId, meterType, day); + // yesterdayHour + Map yesterdayMap = trendHourData(floorId, meterId, meterType, yesterday); + Map 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> 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 nowMonthMap = trendDayData(floorId, meterId, meterType, monthArr[0], monthArr[1]); + // lastMonth + Map lastMonthMap = trendDayData(floorId, meterId, meterType, monthArr[0], lastMonth); + Map 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 nowYearMap = trendMonthData(floorId, meterId, meterType, year); + // lastYear + Map lastYearMap = trendMonthData(floorId, meterId, meterType, lastYear); - // month - List> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year); Map 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 trendHourData(String floorId, String meterId, Long meterType, String day) { + Map hourMap = new HashMap<>(); + List> hourList = baseMapper.getHourTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, day); + List 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 trendDayData(String floorId, String meterId, Long meterType, String year, String month) { + Map dayMap = new HashMap<>(); + List> dayList = baseMapper.getDayTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year, month); + List 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 trendMonthData(String floorId, String meterId, Long meterType, String year) { + Map resultMap = new HashMap<>(); + List> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year); + List 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; + } }