|
@@ -6,16 +6,14 @@ import com.zksy.common.exception.CommonException;
|
|
|
import com.zksy.infrared.domain.InfraredReadingMeter;
|
|
import com.zksy.infrared.domain.InfraredReadingMeter;
|
|
|
import com.zksy.infrared.domain.vo.MeterDayDataVo;
|
|
import com.zksy.infrared.domain.vo.MeterDayDataVo;
|
|
|
import com.zksy.infrared.domain.vo.MeterMonthDataVo;
|
|
import com.zksy.infrared.domain.vo.MeterMonthDataVo;
|
|
|
|
|
+import com.zksy.infrared.domain.vo.MeterQuarterDataVo;
|
|
|
import com.zksy.infrared.domain.vo.MeterYearDataVo;
|
|
import com.zksy.infrared.domain.vo.MeterYearDataVo;
|
|
|
import com.zksy.infrared.service.InfraredReadingMeterService;
|
|
import com.zksy.infrared.service.InfraredReadingMeterService;
|
|
|
import com.zksy.infrared.mapper.InfraredReadingMeterMapper;
|
|
import com.zksy.infrared.mapper.InfraredReadingMeterMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
-import java.time.LocalDate;
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
-import java.time.LocalTime;
|
|
|
|
|
-import java.time.ZoneId;
|
|
|
|
|
|
|
+import java.time.*;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
@@ -32,6 +30,8 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
private static final List<BigDecimal> DEFAULT_MONTH_LIST = Collections.nCopies(12, BigDecimal.ZERO);
|
|
private static final List<BigDecimal> DEFAULT_MONTH_LIST = Collections.nCopies(12, BigDecimal.ZERO);
|
|
|
// 工具方法:生成默认日数据列表
|
|
// 工具方法:生成默认日数据列表
|
|
|
private static final List<BigDecimal> DEFAULT_DAY_LIST = Collections.nCopies(31, BigDecimal.ZERO);
|
|
private static final List<BigDecimal> DEFAULT_DAY_LIST = Collections.nCopies(31, BigDecimal.ZERO);
|
|
|
|
|
+ // 工具方法:生成默认季数据列表
|
|
|
|
|
+ private static final List<BigDecimal> DEFAULT_QUARTER_LIST = Collections.nCopies(3, BigDecimal.ZERO);
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public MeterYearDataVo selectByYear(Integer year, String meterNumber) {
|
|
public MeterYearDataVo selectByYear(Integer year, String meterNumber) {
|
|
@@ -187,4 +187,188 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
|
|
|
|
|
return new MeterDayDataVo(lastValue.subtract(firstValue));
|
|
return new MeterDayDataVo(lastValue.subtract(firstValue));
|
|
|
}
|
|
}
|
|
|
|
|
+ //TODO 按照年统计电表
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public MeterYearDataVo selectByCurrentYearData() {
|
|
|
|
|
+ //首先获取当前年份
|
|
|
|
|
+ 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()
|
|
|
|
|
+ .toInstant()
|
|
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
|
|
+ .toLocalDate()
|
|
|
|
|
+ .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());
|
|
|
|
|
+ BigDecimal end = BigDecimal.valueOf(meterList.get(meterList.size()-1).getElectricEnergy());
|
|
|
|
|
+ monthTotal=monthTotal.add(end.subtract(start));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ monthDataList.set(month-1,monthTotal);
|
|
|
|
|
+ }
|
|
|
|
|
+ //计算年用电量
|
|
|
|
|
+ BigDecimal totalElectricity = monthDataList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
+
|
|
|
|
|
+ return new MeterYearDataVo(totalElectricity,monthDataList);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //TODO 根据当前年月统计电表数据
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public MeterMonthDataVo selectByCurrentYearselectByCurrentYearAndCurrentMonthWithDayData() {
|
|
|
|
|
+ //首先获取当前的日期
|
|
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
|
|
+ int year = now.getYear();
|
|
|
|
|
+ int month = now.getMonthValue();
|
|
|
|
|
+
|
|
|
|
|
+ LocalDate firstDayOfMonth = LocalDate.of(year, month, 1);
|
|
|
|
|
+ LocalDate lastDayOfMonth = firstDayOfMonth.withDayOfMonth(firstDayOfMonth.lengthOfMonth());
|
|
|
|
|
+
|
|
|
|
|
+ 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(BigDecimal.ZERO,DEFAULT_DAY_LIST);
|
|
|
|
|
+ }
|
|
|
|
|
+ //对查询结果按照日期和电表编号分组
|
|
|
|
|
+ Map<Integer,Map<String,List<InfraredReadingMeter>>> groupedByDayAndMeter=list.stream()
|
|
|
|
|
+ .collect(Collectors.groupingBy(item->item.getCreateTime()
|
|
|
|
|
+ .toInstant()
|
|
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
|
|
+ .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());
|
|
|
|
|
+ BigDecimal end = BigDecimal.valueOf(meterList.get(meterList.size() - 1).getElectricEnergy());
|
|
|
|
|
+ dayTotal=dayTotal.add(end.subtract(start));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ 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) // 获取最后一条
|
|
|
|
|
+ .orElse(null);
|
|
|
|
|
+
|
|
|
|
|
+ if (firstRecord != null && lastRecord != null) {
|
|
|
|
|
+ BigDecimal startEnergy = BigDecimal.valueOf(firstRecord.getElectricEnergy());
|
|
|
|
|
+ BigDecimal endEnergy = BigDecimal.valueOf(lastRecord.getElectricEnergy());
|
|
|
|
|
+ totalElectricity = totalElectricity.add(endEnergy.subtract(startEnergy));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new MeterMonthDataVo(totalElectricity,dayDataList);
|
|
|
|
|
+ }
|
|
|
|
|
+ //TODO 根据当前季度统计电表数据
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public MeterQuarterDataVo selectByCurrentQuarterData() {
|
|
|
|
|
+ //首先获取当前的日期
|
|
|
|
|
+ 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;
|
|
|
|
|
+
|
|
|
|
|
+ LocalDate startDate = LocalDate.of(year, startMonth, 1);
|
|
|
|
|
+ LocalDate endDate = LocalDate.of(year, endMonth, YearMonth.of(year, endMonth).lengthOfMonth());
|
|
|
|
|
+
|
|
|
|
|
+ LocalDateTime startDateTime = startDate.atStartOfDay();
|
|
|
|
|
+ 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(BigDecimal.ZERO,DEFAULT_QUARTER_LIST);
|
|
|
|
|
+ }
|
|
|
|
|
+ //将结果集根据年份和电表编号进行分组
|
|
|
|
|
+ Map<Integer, Map<String, List<InfraredReadingMeter>>> groupedByMonthAndMeter =
|
|
|
|
|
+ list.stream().collect(Collectors.groupingBy(
|
|
|
|
|
+ item -> item.getCreateTime().toInstant()
|
|
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
|
|
+ .toLocalDate()
|
|
|
|
|
+ .getMonthValue(),
|
|
|
|
|
+ Collectors.groupingBy(InfraredReadingMeter::getElectricNumber)
|
|
|
|
|
+ ));
|
|
|
|
|
+ List<BigDecimal> monthDataList=new ArrayList<>(Collections.nCopies(3, BigDecimal.ZERO));
|
|
|
|
|
+ BigDecimal totalQuarterElectricity = BigDecimal.ZERO;
|
|
|
|
|
+ for(int m=startMonth;m<=endMonth;m++){
|
|
|
|
|
+ Map<String, List<InfraredReadingMeter>> meterData = groupedByMonthAndMeter.getOrDefault(m, Collections.emptyMap());
|
|
|
|
|
+ BigDecimal monthTotal = BigDecimal.ZERO;
|
|
|
|
|
+ for(List<InfraredReadingMeter> meterList:meterData.values()){
|
|
|
|
|
+ if(meterList.size()>=2){
|
|
|
|
|
+ BigDecimal start = BigDecimal.valueOf(meterList.get(0).getElectricEnergy());
|
|
|
|
|
+ BigDecimal end = BigDecimal.valueOf(meterList.get(meterList.size() - 1).getElectricEnergy());
|
|
|
|
|
+ monthTotal=monthTotal.add(end.subtract(start));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ monthDataList.set(m-startMonth,monthTotal);
|
|
|
|
|
+ totalQuarterElectricity = totalQuarterElectricity.add(monthTotal);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new MeterQuarterDataVo(totalQuarterElectricity,monthDataList);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|