|
@@ -3,13 +3,16 @@ package com.zksy.infrared.service.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zksy.common.exception.CommonException;
|
|
import com.zksy.common.exception.CommonException;
|
|
|
|
|
+import com.zksy.infrared.domain.CompanyElectric;
|
|
|
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.MeterQuarterDataVo;
|
|
|
import com.zksy.infrared.domain.vo.MeterYearDataVo;
|
|
import com.zksy.infrared.domain.vo.MeterYearDataVo;
|
|
|
|
|
+import com.zksy.infrared.service.CompanyElectricService;
|
|
|
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.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
@@ -33,6 +36,8 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
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, new BigDecimal("0.00"));
|
|
private static final List<BigDecimal> DEFAULT_QUARTER_LIST = Collections.nCopies(3, new BigDecimal("0.00"));
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private CompanyElectricService companyElectricService;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public MeterYearDataVo selectByYear(Integer year, String meterNumber) {
|
|
public MeterYearDataVo selectByYear(Integer year, String meterNumber) {
|
|
@@ -400,4 +405,90 @@ public class InfraredReadingMeterServiceImpl extends ServiceImpl<InfraredReading
|
|
|
|
|
|
|
|
return new MeterQuarterDataVo(totalQuarterElectricity,monthDataList);
|
|
return new MeterQuarterDataVo(totalQuarterElectricity,monthDataList);
|
|
|
}
|
|
}
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Map<String, Object>> getDailyAndTotalElectricityAsMap() {
|
|
|
|
|
+ // 确定当日时间范围
|
|
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
|
|
+ Date startOfDay = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
|
|
+ Date endOfDay = Date.from(today.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
|
|
+
|
|
|
|
|
+ // 获取所有有效的电表号(非空且有总电量记录)
|
|
|
|
|
+ LambdaQueryWrapper<InfraredReadingMeter> allMeterQuery = new LambdaQueryWrapper<>();
|
|
|
|
|
+ allMeterQuery.isNotNull(InfraredReadingMeter::getElectricNumber)
|
|
|
|
|
+ .isNotNull(InfraredReadingMeter::getElectricEnergy)
|
|
|
|
|
+ .groupBy(InfraredReadingMeter::getElectricNumber); // 去重获取所有电表号
|
|
|
|
|
+ List<String> allValidMeterNumbers = list(allMeterQuery).stream()
|
|
|
|
|
+ .map(InfraredReadingMeter::getElectricNumber)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .filter(num -> !num.trim().isEmpty())
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ // 查询当日所有有效记录(用于计算当日用电量)
|
|
|
|
|
+ LambdaQueryWrapper<InfraredReadingMeter> todayQuery = new LambdaQueryWrapper<>();
|
|
|
|
|
+ todayQuery.between(InfraredReadingMeter::getCreateTime, startOfDay, endOfDay)
|
|
|
|
|
+ .isNotNull(InfraredReadingMeter::getElectricNumber)
|
|
|
|
|
+ .isNotNull(InfraredReadingMeter::getElectricEnergy)
|
|
|
|
|
+ .orderByAsc(InfraredReadingMeter::getCreateTime);
|
|
|
|
|
+ List<InfraredReadingMeter> todayRecords = list(todayQuery);
|
|
|
|
|
+
|
|
|
|
|
+ // 按电表号分组存储当日记录
|
|
|
|
|
+ Map<String, List<InfraredReadingMeter>> todayRecordsByMeter = todayRecords.stream()
|
|
|
|
|
+ .collect(Collectors.groupingBy(InfraredReadingMeter::getElectricNumber));
|
|
|
|
|
+
|
|
|
|
|
+ // 查询所有电表的最新总电量记录
|
|
|
|
|
+ LambdaQueryWrapper<InfraredReadingMeter> latestQuery = new LambdaQueryWrapper<>();
|
|
|
|
|
+ latestQuery.isNotNull(InfraredReadingMeter::getElectricNumber)
|
|
|
|
|
+ .isNotNull(InfraredReadingMeter::getElectricEnergy);
|
|
|
|
|
+ List<InfraredReadingMeter> allRecords = list(latestQuery);
|
|
|
|
|
+
|
|
|
|
|
+ // 按电表号分组,取最新记录(创建时间最晚)
|
|
|
|
|
+ Map<String, InfraredReadingMeter> latestRecordsByMeter = allRecords.stream()
|
|
|
|
|
+ .collect(Collectors.groupingBy(InfraredReadingMeter::getElectricNumber,
|
|
|
|
|
+ Collectors.collectingAndThen(
|
|
|
|
|
+ Collectors.maxBy(Comparator.comparing(InfraredReadingMeter::getCreateTime)),
|
|
|
|
|
+ Optional::get)));
|
|
|
|
|
+
|
|
|
|
|
+ // 组装结果(保留两位小数)
|
|
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
|
|
+ for (String electricNumber : allValidMeterNumbers) {
|
|
|
|
|
+ Map<String, Object> meterMap = new HashMap<>(4);
|
|
|
|
|
+ meterMap.put("electricNumber", electricNumber);
|
|
|
|
|
+
|
|
|
|
|
+ LambdaQueryWrapper<CompanyElectric> companyElectricQuery = new LambdaQueryWrapper<>();
|
|
|
|
|
+ companyElectricQuery.eq(CompanyElectric::getElectricNumber, electricNumber);
|
|
|
|
|
+ CompanyElectric companyElectric = companyElectricService.getOne(companyElectricQuery);
|
|
|
|
|
+ meterMap.put("companyName", companyElectric == null ? "" : companyElectric.getCompanyName());
|
|
|
|
|
+
|
|
|
|
|
+ // 获取该电表的最新总电量(保留两位小数)
|
|
|
|
|
+ InfraredReadingMeter latestRecord = latestRecordsByMeter.get(electricNumber);
|
|
|
|
|
+ if (latestRecord == null || latestRecord.getElectricEnergy() == null) {
|
|
|
|
|
+ continue; // 排除总电量为空的电表
|
|
|
|
|
+ }
|
|
|
|
|
+ BigDecimal totalElectricity = BigDecimal.valueOf(latestRecord.getElectricEnergy())
|
|
|
|
|
+ .setScale(2, RoundingMode.HALF_UP); // 四舍五入保留两位小数
|
|
|
|
|
+ meterMap.put("totalElectricity", totalElectricity);
|
|
|
|
|
+
|
|
|
|
|
+ // 计算当日用电量(无数据则为0,保留两位小数)
|
|
|
|
|
+ List<InfraredReadingMeter> todayMeterRecords = todayRecordsByMeter.getOrDefault(electricNumber, Collections.emptyList());
|
|
|
|
|
+ BigDecimal dailyElectricity;
|
|
|
|
|
+ if (todayMeterRecords.size() >= 2) {
|
|
|
|
|
+ InfraredReadingMeter first = todayMeterRecords.get(0);
|
|
|
|
|
+ InfraredReadingMeter last = todayMeterRecords.get(todayMeterRecords.size() - 1);
|
|
|
|
|
+ if (first.getElectricEnergy() != null && last.getElectricEnergy() != null) {
|
|
|
|
|
+ double diff = last.getElectricEnergy() - first.getElectricEnergy();
|
|
|
|
|
+ dailyElectricity = BigDecimal.valueOf(diff)
|
|
|
|
|
+ .setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ dailyElectricity = BigDecimal.ZERO.setScale(2); // 数据异常时设为0.00
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ dailyElectricity = BigDecimal.ZERO.setScale(2); // 当日无数据或记录不足,设为0.00
|
|
|
|
|
+ }
|
|
|
|
|
+ meterMap.put("dailyElectricity", dailyElectricity);
|
|
|
|
|
+
|
|
|
|
|
+ result.add(meterMap);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|