|
|
@@ -191,22 +191,21 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
@Override
|
|
|
public MeterYearDataVo selectByCurrentYearData() {
|
|
|
log.warn("按照年统计电表");
|
|
|
- //首先获取当前年份
|
|
|
int currentYear = LocalDate.now().getYear();
|
|
|
|
|
|
LocalDate firstDayOfYear = LocalDate.of(currentYear, 1, 1);
|
|
|
LocalDate lastDayOfYear = LocalDate.of(currentYear, 12, 31);
|
|
|
- //获取当前年份的开始日期和结束日期
|
|
|
+
|
|
|
LocalDateTime startOfDay = firstDayOfYear.atStartOfDay();
|
|
|
LocalDateTime endOfDay = lastDayOfYear.atTime(LocalTime.MAX);
|
|
|
|
|
|
- //构建查询条件,查询在时间范围内的数据,按照升序排序
|
|
|
+
|
|
|
LambdaQueryWrapper<InfraredReadingMeter> wrapper = new LambdaQueryWrapper<InfraredReadingMeter>()
|
|
|
.between(InfraredReadingMeter::getCreateTime, startOfDay, endOfDay)
|
|
|
.orderByAsc(InfraredReadingMeter::getCreateTime);
|
|
|
- //根据查询条件返回结果
|
|
|
+
|
|
|
List<InfraredReadingMeter> list = this.list(wrapper);
|
|
|
- //根据电表编号和月份对结果集进行分组
|
|
|
+
|
|
|
Map<Integer, Map<String, List<InfraredReadingMeter>>> groupedByMonthAndMeter =
|
|
|
list.stream().collect(Collectors.groupingBy(
|
|
|
item -> item.getCreateTime()
|
|
|
@@ -216,14 +215,14 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
.getMonthValue(),
|
|
|
Collectors.groupingBy(InfraredReadingMeter::getElectricNumber)
|
|
|
));
|
|
|
- //每月用电量
|
|
|
+
|
|
|
List<BigDecimal> monthDataList = new ArrayList<>(Collections.nCopies(12, BigDecimal.ZERO));
|
|
|
for(int month=1;month<=12;month++){
|
|
|
- //获取当前月份的数据
|
|
|
+
|
|
|
Map<String, List<InfraredReadingMeter>> meterData=groupedByMonthAndMeter.getOrDefault(month,Collections.emptyMap());
|
|
|
|
|
|
BigDecimal monthTotal=BigDecimal.ZERO;
|
|
|
- //将每个电表每月的数据累加
|
|
|
+
|
|
|
for(List<InfraredReadingMeter> meterList:meterData.values()){
|
|
|
if(meterList.size()>=2){
|
|
|
BigDecimal start = BigDecimal.valueOf(meterList.get(0).getElectricEnergy());
|
|
|
@@ -231,12 +230,12 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
monthTotal=monthTotal.add(end.subtract(start));
|
|
|
}
|
|
|
}
|
|
|
- monthTotal = monthTotal.setScale(2, RoundingMode.HALF_UP); //精度
|
|
|
+ monthTotal = monthTotal.setScale(2, RoundingMode.HALF_UP);
|
|
|
monthDataList.set(month-1,monthTotal);
|
|
|
}
|
|
|
- //计算年用电量
|
|
|
+
|
|
|
BigDecimal totalElectricity = monthDataList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- //年总量处理
|
|
|
+
|
|
|
totalElectricity = totalElectricity.setScale(2, RoundingMode.HALF_UP);
|
|
|
return new MeterYearDataVo(totalElectricity,monthDataList);
|
|
|
|
|
|
@@ -245,8 +244,8 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
@Override
|
|
|
public MeterMonthDataVo selectByCurrentYearselectByCurrentYearAndCurrentMonthWithDayData() {
|
|
|
final List<BigDecimal> defaultDayList = Collections.nCopies(31, new BigDecimal("0.00"));
|
|
|
- log.warn("根据当前年月统计电表数据");
|
|
|
- //首先获取当前的日期
|
|
|
+
|
|
|
+
|
|
|
LocalDate now = LocalDate.now();
|
|
|
int year = now.getYear();
|
|
|
int month = now.getMonthValue();
|
|
|
@@ -257,17 +256,17 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
LocalDateTime startOfDay = firstDayOfMonth.atStartOfDay();
|
|
|
LocalDateTime endOfDay = lastDayOfMonth.atTime(LocalTime.MAX);
|
|
|
|
|
|
- //构造查询条件
|
|
|
+
|
|
|
LambdaQueryWrapper<InfraredReadingMeter> wrapper = new LambdaQueryWrapper<>();
|
|
|
wrapper.between(InfraredReadingMeter::getCreateTime, startOfDay, endOfDay)
|
|
|
.orderByAsc(InfraredReadingMeter::getCreateTime);
|
|
|
- //根据查询条件获取查询结果
|
|
|
+
|
|
|
List<InfraredReadingMeter> list = this.list(wrapper);
|
|
|
|
|
|
if(list==null||list.size()<2){
|
|
|
return new MeterMonthDataVo(new BigDecimal("0.00"),defaultDayList);
|
|
|
}
|
|
|
- //对查询结果按照日期和电表编号分组
|
|
|
+
|
|
|
Map<Integer,Map<String,List<InfraredReadingMeter>>> groupedByDayAndMeter=list.stream()
|
|
|
.collect(Collectors.groupingBy(item->item.getCreateTime()
|
|
|
.toInstant()
|
|
|
@@ -275,14 +274,14 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
.toLocalDate()
|
|
|
.getDayOfMonth(),
|
|
|
Collectors.groupingBy(InfraredReadingMeter::getElectricNumber)));
|
|
|
- //构建返回的结果集
|
|
|
+
|
|
|
List<BigDecimal> dayDataList=new ArrayList<>(Collections.nCopies(firstDayOfMonth.lengthOfMonth(),BigDecimal.ZERO));
|
|
|
for(int day=1;day<=firstDayOfMonth.lengthOfMonth();day++){
|
|
|
- //获取到天数的各个电表的用电量
|
|
|
+
|
|
|
Map<String, List<InfraredReadingMeter>> meterData = groupedByDayAndMeter.getOrDefault(day, Collections.emptyMap());
|
|
|
|
|
|
BigDecimal dayTotal=BigDecimal.ZERO;
|
|
|
- //遍历结果集,即各个电表的各天数据
|
|
|
+
|
|
|
for(List<InfraredReadingMeter> meterList:meterData.values()){
|
|
|
if(meterList.size()>=2){
|
|
|
BigDecimal start = BigDecimal.valueOf(meterList.get(0).getElectricEnergy());
|
|
|
@@ -290,26 +289,26 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
dayTotal=dayTotal.add(end.subtract(start));
|
|
|
}
|
|
|
}
|
|
|
- dayTotal = dayTotal.setScale(2, RoundingMode.HALF_UP); // 添加这行
|
|
|
+ dayTotal = dayTotal.setScale(2, RoundingMode.HALF_UP);
|
|
|
dayDataList.set(day-1,dayTotal);
|
|
|
}
|
|
|
- // 按电表分组,计算每个电表当月最后一天最后一条数据减去当月第一天第一条数据,再累加
|
|
|
+
|
|
|
Map<String, List<InfraredReadingMeter>> groupedByMeter = list.stream()
|
|
|
.collect(Collectors.groupingBy(InfraredReadingMeter::getElectricNumber));
|
|
|
|
|
|
BigDecimal totalElectricity = BigDecimal.ZERO;
|
|
|
|
|
|
for (List<InfraredReadingMeter> meterList : groupedByMeter.values()) {
|
|
|
- // 找到第一天第一条数据
|
|
|
+
|
|
|
InfraredReadingMeter firstRecord = meterList.stream()
|
|
|
.filter(r -> r.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().equals(firstDayOfMonth))
|
|
|
.findFirst()
|
|
|
.orElse(null);
|
|
|
|
|
|
- // 找到最后一天最后一条数据
|
|
|
+
|
|
|
InfraredReadingMeter lastRecord = meterList.stream()
|
|
|
.filter(r -> r.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().equals(lastDayOfMonth))
|
|
|
- .reduce((first, second) -> second) // 获取最后一条
|
|
|
+ .reduce((first, second) -> second)
|
|
|
.orElse(null);
|
|
|
|
|
|
if (firstRecord != null && lastRecord != null) {
|
|
|
@@ -318,19 +317,18 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
totalElectricity = totalElectricity.add(endEnergy.subtract(startEnergy));
|
|
|
}
|
|
|
}
|
|
|
- // 月总量处理
|
|
|
+
|
|
|
totalElectricity = totalElectricity.setScale(2, RoundingMode.HALF_UP);
|
|
|
return new MeterMonthDataVo(totalElectricity,dayDataList);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public MeterQuarterDataVo selectByCurrentQuarterData() {
|
|
|
- log.warn("根据当前季度统计电表数据");
|
|
|
- //首先获取当前的日期
|
|
|
+
|
|
|
LocalDate now = LocalDate.now();
|
|
|
int year = now.getYear();
|
|
|
int month = now.getMonthValue();
|
|
|
- //计算当前季度
|
|
|
+
|
|
|
int currentQuarter = (month - 1) / 3 + 1;
|
|
|
int startMonth = (currentQuarter - 1) * 3 + 1;
|
|
|
int endMonth = startMonth + 2;
|
|
|
@@ -342,16 +340,16 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
LocalDateTime endDateTime = endDate.atTime(LocalTime.MAX);
|
|
|
|
|
|
LambdaQueryWrapper<InfraredReadingMeter> wrapper = new LambdaQueryWrapper<>();
|
|
|
- //构建查询条件
|
|
|
+
|
|
|
wrapper.between(InfraredReadingMeter::getCreateTime,startDateTime,endDateTime)
|
|
|
.orderByAsc(InfraredReadingMeter::getCreateTime);
|
|
|
- //获取查询结果
|
|
|
+
|
|
|
List<InfraredReadingMeter> list = this.list(wrapper);
|
|
|
|
|
|
if(list==null||list.size()<2){
|
|
|
return new MeterQuarterDataVo(new BigDecimal("0.00"),DEFAULT_QUARTER_LIST);
|
|
|
}
|
|
|
- //将结果集根据年份和电表编号进行分组
|
|
|
+
|
|
|
Map<Integer, Map<String, List<InfraredReadingMeter>>> groupedByMonthAndMeter =
|
|
|
list.stream().collect(Collectors.groupingBy(
|
|
|
item -> item.getCreateTime().toInstant()
|