From 30383e16e21601ddf244b37a467b4e965d7a0dfb Mon Sep 17 00:00:00 2001 From: zcxlsm Date: Sun, 14 Sep 2025 21:45:56 +0800 Subject: [PATCH] refactor(Property): 1 --- .../xcx/XResidentPersonController.java | 3 +- .../impl/CostHouseChargeServiceImpl.java | 9 + .../TbMeterRecordServiceImpl.java | 156 +++++++++++++----- 3 files changed, 124 insertions(+), 44 deletions(-) diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/xcx/XResidentPersonController.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/xcx/XResidentPersonController.java index 08163dac..89c177c6 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/xcx/XResidentPersonController.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/xcx/XResidentPersonController.java @@ -81,10 +81,9 @@ public class XResidentPersonController extends BaseController { /** * 新增入驻员工 */ - @SaCheckPermission("xcx:person:add") - @Log(title = "入驻员工", businessType = BusinessType.INSERT) @RepeatSubmit() @PostMapping() + @Log(title = "入驻员工", businessType = BusinessType.INSERT) public R add(@Validated(AddGroup.class) @RequestBody ResidentPersonBo bo) { return toAjax(residentPersonService.insertByBo(bo)); } diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/CostHouseChargeServiceImpl.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/CostHouseChargeServiceImpl.java index 3c8d0e3e..e5e6e491 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/CostHouseChargeServiceImpl.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/CostHouseChargeServiceImpl.java @@ -214,6 +214,8 @@ public class CostHouseChargeServiceImpl implements ICostHouseChargeService { } //类型为5,则为水费 if (Objects.equals(remoteDictDataVo.getDictValue(), "5")) { + + } //类型为6,则为电费 if (Objects.equals(remoteDictDataVo.getDictValue(), "6")) { @@ -245,6 +247,13 @@ public class CostHouseChargeServiceImpl implements ICostHouseChargeService { return flag; } + /** + * 水电气费计算 + */ + private void meterCharge(){ + + } + /** * 房屋退费 * 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 f82f2fa4..162962d1 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 @@ -27,9 +27,11 @@ import org.dromara.property.service.smartDevicesService.ITbMeterRecordService; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; +import java.time.YearMonth; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * 抄记录Service业务层处理 @@ -267,50 +269,75 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { public Map getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year) { Map resultMap = new HashMap<>(); - 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); + if (!StrUtil.isBlank(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<>(); - hourMap.put("today", todayMap); - hourMap.put("yesterday", yesterdayMap); + Map hourMap = new HashMap<>(); + hourMap.put("today", todayMap); + hourMap.put("yesterday", yesterdayMap); + resultMap.put("hour", hourMap); + } + if (!StrUtil.isBlank(month)) { + String[] monthArr = month.split("-"); + 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); - String[] monthArr = month.split("-"); - 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<>(); + dayMap.put("nowMonth", nowMonthMap); + dayMap.put("lastMonth", lastMonthMap); + resultMap.put("day", dayMap); + } - Map dayMap = new HashMap<>(); - dayMap.put("nowMonth", nowMonthMap); - dayMap.put("lastMonth", lastMonthMap); + if (!StrUtil.isBlank(year)) { + String lastYear = Integer.parseInt(year) - 1 + ""; + // nowYear + Map nowYearMap = trendMonthData(floorId, meterId, meterType, year); + // lastYear + Map lastYearMap = trendMonthData(floorId, meterId, meterType, lastYear); - String lastYear = Integer.parseInt(year) - 1 + ""; - // nowYear - Map nowYearMap = trendMonthData(floorId, meterId, meterType, year); - // lastYear - Map lastYearMap = trendMonthData(floorId, meterId, meterType, lastYear); - - Map monthMap = new HashMap<>(); - monthMap.put("nowYear", nowYearMap); - monthMap.put("lastYear", lastYearMap); - - resultMap.put("hour", hourMap); - resultMap.put("day", dayMap); - resultMap.put("month", monthMap); + Map monthMap = new HashMap<>(); + monthMap.put("nowYear", nowYearMap); + monthMap.put("lastYear", lastYearMap); + 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(); + List> sqlList = baseMapper.getHourTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, day); + + // 创建小时--能耗的映射,缺失的小时自动补0 + List hourData = IntStream.rangeClosed(0, 23) + .mapToObj(hour -> { + // 查找对应小时的数据 + Optional> hourResult = sqlList.stream() + .filter(item -> Integer.parseInt(item.get("hour").toString().split(":")[0]) == hour) + .findFirst(); + + // 存在则使用实际值,否则使用0 + String consumption = hourResult + .map(item -> item.get("total_consumption").toString()) + .orElse(BigDecimal.ZERO.toString()); + + return new String[]{hour + ":00", consumption}; + }).collect(Collectors.toList()); + + // 计算总能耗(直接使用sqlList中的数据求和,避免二次计算) + Float total = sqlList.stream() + .map(item -> new BigDecimal(item.get("total_consumption").toString())) + .reduce(BigDecimal::add) + .orElse(BigDecimal.ZERO) + .floatValue(); + hourMap.put("total", total); hourMap.put("data", hourData); return hourMap; @@ -318,10 +345,33 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { 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(); + List> sqlList = baseMapper.getDayTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year, month); + + YearMonth yearMonth = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month)); + // 创建月份--能耗的映射,缺失的月份自动补0 + List dayData = IntStream.rangeClosed(1, yearMonth.lengthOfMonth()) + .mapToObj(day -> { + // 查找对应月份的数据 + Optional> dayResult = sqlList.stream() + .filter(item -> Integer.parseInt(item.get("day").toString()) == day) + .findFirst(); + + // 存在则使用实际值,否则使用0 + String consumption = dayResult + .map(item -> item.get("total_consumption").toString()) + .orElse(BigDecimal.ZERO.toString()); + + return new String[]{String.valueOf(day), consumption}; + }) + .collect(Collectors.toList()); + + + // 计算总能耗(直接使用sqlList中的数据求和,避免二次计算) + Float total = sqlList.stream() + .map(item -> new BigDecimal(item.get("total_consumption").toString())) + .reduce(BigDecimal::add) + .orElse(BigDecimal.ZERO) + .floatValue(); dayMap.put("total", total); dayMap.put("data", dayData); return dayMap; @@ -329,10 +379,32 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { 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(); + List> sqlList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year); + + // 创建月份--能耗的映射,缺失的月份自动补0 + List monthData = IntStream.rangeClosed(1, 12) + .mapToObj(month -> { + // 查找对应月份的数据 + Optional> monthResult = sqlList.stream() + .filter(item -> Integer.parseInt(item.get("month").toString()) == month) + .findFirst(); + + // 存在则使用实际值,否则使用0 + String consumption = monthResult + .map(item -> item.get("total_consumption").toString()) + .orElse(BigDecimal.ZERO.toString()); + + return new String[]{String.valueOf(month), consumption}; + }) + .collect(Collectors.toList()); + + // 计算总能耗(直接使用sqlList中的数据求和,避免二次计算) + Float total = sqlList.stream() + .map(item -> new BigDecimal(item.get("total_consumption").toString())) + .reduce(BigDecimal::add) + .orElse(BigDecimal.ZERO) + .floatValue(); + resultMap.put("total", total); resultMap.put("data", monthData); return resultMap;