|
@@ -10,6 +10,7 @@ import org.jeecg.modules.deviceLesm.mapper.DeviceInformationMapper;
|
|
|
import org.jeecg.modules.deviceLesm.mapper.DeviceRegionMapper;
|
|
|
import org.jeecg.modules.deviceLesm.service.IDeviceRegionService;
|
|
|
import org.jeecg.modules.gatherData.entity.FpgGatherData;
|
|
|
+import org.jeecg.modules.gatherData.entity.FpgTotalData;
|
|
|
import org.jeecg.modules.gatherData.mapper.FpgGatherDataMapper;
|
|
|
import org.jeecg.modules.systemConfig.peaksAndValleysTimeConfig.entity.PeaksAndValleysTimeConfig;
|
|
|
import org.jeecg.modules.systemConfig.peaksAndValleysTimeConfig.service.IPeaksAndValleysTimeConfigService;
|
|
@@ -24,6 +25,7 @@ import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -44,11 +46,21 @@ public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, Dev
|
|
|
@Autowired
|
|
|
private DeviceInformationMapper deviceInformationMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DeviceRegionMapper deviceRegionMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public List<Map<String, Object>> powerStatistics() throws ParseException {
|
|
|
|
|
|
List<Map<String, Object>> arrayList = new ArrayList<>();
|
|
|
|
|
|
+ List<DeviceRegion> list = deviceRegionMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
+
|
|
|
+ // 将 id 和 regionTitle 转为 key-value 的 Map
|
|
|
+ Map<String, String> idToRegionTitleMap = list.stream()
|
|
|
+ .collect(Collectors.toMap(DeviceRegion::getId, DeviceRegion::getRegionTitle));
|
|
|
+
|
|
|
+
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
// 获取当天的开始和结束时间
|
|
@@ -61,74 +73,62 @@ public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, Dev
|
|
|
Date startOfDay = sdf.parse(startOfDayString);
|
|
|
Date endOfDay = sdf.parse(endOfDayString);
|
|
|
|
|
|
- LambdaQueryWrapper<DeviceRegion> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(DeviceRegion::getStatus, "1")
|
|
|
- .notIn(DeviceRegion::getRegionTitle, "储运中心", "能管中心", "炼钢厂");
|
|
|
- List<DeviceRegion> deviceRegions = baseMapper.selectList(wrapper);
|
|
|
+ List<String> regionTitles = Arrays.asList("轧钢厂", "炼铁厂");
|
|
|
|
|
|
- Map<String, Object> powerMap = new HashMap<>();
|
|
|
|
|
|
- powerMap.put("公司实时总功率", null);
|
|
|
- powerMap.put("公司累计总功率", null);
|
|
|
+ Map<String, Object> powerMap = new HashMap<>();
|
|
|
|
|
|
- // 初始化公司实时总功率和累计总功率的变量
|
|
|
- BigDecimal companyRealTimeTotalPower = BigDecimal.ZERO;
|
|
|
- BigDecimal companyAccumulatedTotalPower = BigDecimal.ZERO;
|
|
|
+ // 初始化公司实时总功率和累计总功率的原子引用
|
|
|
+ final AtomicReference<BigDecimal> companyRealTimeTotalPower = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
+ final AtomicReference<BigDecimal> companyAccumulatedTotalPower = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
// 初始化公司尖、峰、平、谷时段总功率的变量
|
|
|
- BigDecimal companyTopsPower = BigDecimal.ZERO;
|
|
|
- BigDecimal companyPeaksPower = BigDecimal.ZERO;
|
|
|
- BigDecimal companyFlatPower = BigDecimal.ZERO;
|
|
|
- BigDecimal companyValleysPower = BigDecimal.ZERO;
|
|
|
+ final AtomicReference<BigDecimal> companyTopsPower = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
+ final AtomicReference<BigDecimal> companyPeaksPower = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
+ final AtomicReference<BigDecimal> companyFlatPower = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
+ final AtomicReference<BigDecimal> companyValleysPower = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
|
|
|
- for (DeviceRegion deviceRegion : deviceRegions) {
|
|
|
+ List<FpgTotalData> fpgTotalDataList = fpgGatherDataMapper.getFpgTotalDataList(regionTitles, startOfDayString, endOfDayString);
|
|
|
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "实时总功率", null);
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "累计总功率", null);
|
|
|
+ // 按 regionTitle 分组
|
|
|
+ Map<String, List<FpgTotalData>> groupedByRegion = fpgTotalDataList.stream()
|
|
|
+ .collect(Collectors.groupingBy(FpgTotalData::getDeviceRegionId));
|
|
|
|
|
|
- // 初始化 LambdaQueryWrapper
|
|
|
- LambdaQueryWrapper<FpgGatherData> fpgGatherDataQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- fpgGatherDataQueryWrapper
|
|
|
- .eq(FpgGatherData::getDeviceRegionId, deviceRegion.getId())
|
|
|
- .ge(FpgGatherData::getCreateTime, startOfDay)
|
|
|
- .le(FpgGatherData::getCreateTime, endOfDay)
|
|
|
- .orderByDesc(FpgGatherData::getCreateTime);
|
|
|
-
|
|
|
- List<FpgGatherData> resultList = fpgGatherDataMapper.selectList(fpgGatherDataQueryWrapper);
|
|
|
|
|
|
+ // 输出每个区域的分组数据
|
|
|
+ groupedByRegion.forEach((region, dataList) -> {
|
|
|
// 累加 RunCurrent 并设置值到 powerMap
|
|
|
BigDecimal realTimeActivePower = BigDecimal.ZERO;
|
|
|
BigDecimal totalActivePower = BigDecimal.ZERO;
|
|
|
-
|
|
|
- if (resultList != null && !resultList.isEmpty()) {
|
|
|
- // 按 deviceInformationId 分组并取每组最新数据(按 createTime 降序)
|
|
|
- Map<String, FpgGatherData> latestDataByDevice = resultList.stream()
|
|
|
- .collect(Collectors.groupingBy(
|
|
|
- FpgGatherData::getDeviceInformationId,
|
|
|
- Collectors.collectingAndThen(
|
|
|
- Collectors.maxBy(Comparator.comparing(FpgGatherData::getCreateTime)),
|
|
|
- Optional::get
|
|
|
- )
|
|
|
- ));
|
|
|
- // 累加 RunCurrent,判空处理
|
|
|
- totalActivePower = resultList.stream()
|
|
|
- .map(FpgGatherData::getActivePower)
|
|
|
- .filter(Objects::nonNull) // 过滤掉 null 值
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
-
|
|
|
- // 累加每组最新数据的 RunCurrent
|
|
|
- realTimeActivePower = latestDataByDevice.values().stream()
|
|
|
- .map(FpgGatherData::getActivePower)
|
|
|
- .filter(Objects::nonNull) // 过滤掉 null 值
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- }
|
|
|
+ Map<String, FpgTotalData> latestDataByDevice = dataList.stream()
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
+ FpgTotalData::getDeviceInformationId,
|
|
|
+ Collectors.collectingAndThen(
|
|
|
+ Collectors.maxBy(Comparator.comparing(FpgTotalData::getCreateTime)),
|
|
|
+ Optional::get
|
|
|
+ )
|
|
|
+ ));
|
|
|
+ // 累加每组最新数据的 RunCurrent
|
|
|
+ realTimeActivePower = latestDataByDevice.values().stream()
|
|
|
+ .map(FpgTotalData::getActivePower)
|
|
|
+ .filter(Objects::nonNull) // 过滤掉 null 值
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ // 累加 RunCurrent,判空处理
|
|
|
+ totalActivePower = dataList.stream()
|
|
|
+ .map(FpgTotalData::getActivePower)
|
|
|
+ .filter(Objects::nonNull) // 过滤掉 null 值
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
// 更新区域实时和累计功率到 powerMap
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "实时总功率", realTimeActivePower);
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "累计总功率", totalActivePower);
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "实时总功率", realTimeActivePower.setScale(2, RoundingMode.HALF_UP));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "累计总功率", totalActivePower.setScale(2, RoundingMode.HALF_UP));
|
|
|
|
|
|
- // 累加到公司实时和累计功率
|
|
|
- companyRealTimeTotalPower = companyRealTimeTotalPower.add(realTimeActivePower);
|
|
|
- companyAccumulatedTotalPower = companyAccumulatedTotalPower.add(totalActivePower);
|
|
|
+ // 使用 AtomicReference 对公司实时和累计功率进行原子操作累加
|
|
|
+ // 获取当前值,执行加法并更新
|
|
|
+ BigDecimal finalRealTimeActivePower = realTimeActivePower;
|
|
|
+ companyRealTimeTotalPower.updateAndGet(currentValue -> currentValue.add(finalRealTimeActivePower));
|
|
|
+ BigDecimal finalTotalActivePower = totalActivePower;
|
|
|
+ companyAccumulatedTotalPower.updateAndGet(currentValue -> currentValue.add(finalTotalActivePower));
|
|
|
|
|
|
|
|
|
Date todayDate = new Date();
|
|
@@ -137,7 +137,6 @@ public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, Dev
|
|
|
calendar.setTime(todayDate);
|
|
|
calendar.add(Calendar.DAY_OF_YEAR, -0);
|
|
|
|
|
|
- // 获取前一天的日期
|
|
|
Date yesterdayDate = calendar.getTime();
|
|
|
String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(yesterdayDate);
|
|
|
List<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
|
|
@@ -159,7 +158,7 @@ public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, Dev
|
|
|
// 转换为字符串
|
|
|
String formattedStartTime = start_time.format(formatter);
|
|
|
|
|
|
- Map peaksAndValleysTimeMap = fpgGatherDataMapper.getFpgTheLeftSideOneData(deviceRegion.getId(), formattedStartTime, endDate);
|
|
|
+ Map peaksAndValleysTimeMap = fpgGatherDataMapper.getFpgTheLeftSideOneData(region, formattedStartTime, endDate);
|
|
|
if (peaksAndValleysTimeMap != null) {
|
|
|
if ("tops".equals(peaksAndValleysTime.getType())) { // 尖值处理
|
|
|
BigDecimal topsAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
@@ -180,7 +179,7 @@ public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, Dev
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- map.put("name", deviceRegion.getRegionTitle());
|
|
|
+ map.put("name", idToRegionTitleMap.get(region));
|
|
|
map.put("tops", String.format("%.2f", mapSum.get("topsSum")));
|
|
|
map.put("peaks", String.format("%.2f", mapSum.get("peaksSum")));
|
|
|
map.put("flat", String.format("%.2f", mapSum.get("flatSum")));
|
|
@@ -192,212 +191,189 @@ public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, Dev
|
|
|
BigDecimal regionFlatPower = new BigDecimal(map.get("flat"));
|
|
|
BigDecimal regionValleysPower = new BigDecimal(map.get("valleys"));
|
|
|
|
|
|
- companyTopsPower = companyTopsPower.add(regionTopsPower);
|
|
|
- companyPeaksPower = companyPeaksPower.add(regionPeaksPower);
|
|
|
- companyFlatPower = companyFlatPower.add(regionFlatPower);
|
|
|
- companyValleysPower = companyValleysPower.add(regionValleysPower);
|
|
|
-
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "尖时段总功率", map.get("tops"));
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "峰时段总功率", map.get("peaks"));
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "平时段总功率", map.get("flat"));
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "谷时段总功率", map.get("valleys"));
|
|
|
-
|
|
|
-
|
|
|
- // 获取各时段总功率
|
|
|
- BigDecimal topsPower1 = map.containsKey("tops") && map.get("tops") != null ?
|
|
|
- new BigDecimal(String.valueOf(map.get("tops"))) : BigDecimal.ZERO;
|
|
|
-
|
|
|
- BigDecimal peaksPower1 = map.containsKey("peaks") && map.get("peaks") != null ?
|
|
|
- new BigDecimal(String.valueOf(map.get("peaks"))) : BigDecimal.ZERO;
|
|
|
+ companyTopsPower.updateAndGet(currentValue -> currentValue.add(regionTopsPower));
|
|
|
+ companyPeaksPower.updateAndGet(currentValue -> currentValue.add(regionPeaksPower));
|
|
|
+ companyFlatPower.updateAndGet(currentValue -> currentValue.add(regionFlatPower));
|
|
|
+ companyValleysPower.updateAndGet(currentValue -> currentValue.add(regionValleysPower));
|
|
|
|
|
|
- BigDecimal flatPower1 = map.containsKey("flat") && map.get("flat") != null ?
|
|
|
- new BigDecimal(String.valueOf(map.get("flat"))) : BigDecimal.ZERO;
|
|
|
-
|
|
|
- BigDecimal valleysPower1 = map.containsKey("valleys") && map.get("valleys") != null ?
|
|
|
- new BigDecimal(String.valueOf(map.get("valleys"))) : BigDecimal.ZERO;
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "尖时段总功率", map.get("tops"));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "峰时段总功率", map.get("peaks"));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "平时段总功率", map.get("flat"));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "谷时段总功率", map.get("valleys"));
|
|
|
|
|
|
// 计算总功率
|
|
|
- BigDecimal totalPower1 = topsPower1.add(peaksPower1).add(flatPower1).add(valleysPower1);
|
|
|
+ BigDecimal totalPower1 = regionTopsPower.add(regionPeaksPower).add(regionFlatPower).add(regionValleysPower);
|
|
|
|
|
|
// 避免除以零的情况
|
|
|
if (totalPower1.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
// 计算占比
|
|
|
- BigDecimal topsRatio = topsPower1.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
- BigDecimal peaksRatio = peaksPower1.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
- BigDecimal flatRatio = flatPower1.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
- BigDecimal valleysRatio = valleysPower1.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal topsRatio = regionTopsPower.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal peaksRatio = regionPeaksPower.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal flatRatio = regionFlatPower.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal valleysRatio = regionValleysPower.divide(totalPower1, 4, RoundingMode.HALF_UP);
|
|
|
|
|
|
// 更新到 powerMap,转换为百分比格式
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "尖时段总功率占比", formatPercentage(topsRatio));
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "峰时段总功率占比", formatPercentage(peaksRatio));
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "平时段总功率占比", formatPercentage(flatRatio));
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "谷时段总功率占比", formatPercentage(valleysRatio));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "尖时段总功率占比", formatPercentage(topsRatio));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "峰时段总功率占比", formatPercentage(peaksRatio));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "平时段总功率占比", formatPercentage(flatRatio));
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "谷时段总功率占比", formatPercentage(valleysRatio));
|
|
|
} else {
|
|
|
// 如果总功率为 0,所有占比设为 0%
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "尖时段总功率占比", "0%");
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "峰时段总功率占比", "0%");
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "平时段总功率占比", "0%");
|
|
|
- powerMap.put(deviceRegion.getRegionTitle() + "谷时段总功率占比", "0%");
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "尖时段总功率占比", "0%");
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "峰时段总功率占比", "0%");
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "平时段总功率占比", "0%");
|
|
|
+ powerMap.put(idToRegionTitleMap.get(region) + "谷时段总功率占比", "0%");
|
|
|
}
|
|
|
|
|
|
- List<String> stringList = new ArrayList<>();
|
|
|
- stringList.add("1");
|
|
|
- stringList.add("2");
|
|
|
- stringList.add("3");
|
|
|
- stringList.add("4");
|
|
|
- stringList.add("5");
|
|
|
-
|
|
|
- // 遍历每个 deviceInformation,根据 deviceType 分组查询
|
|
|
- for (String type : stringList) {
|
|
|
-
|
|
|
- Map<String, String> map1 = new HashMap<>();
|
|
|
- Map<String, Object> mapSum1 = new HashMap<>();
|
|
|
- mapSum1.put("topsSum", 0.00);
|
|
|
- mapSum1.put("peaksSum", 0.00);
|
|
|
- mapSum1.put("flatSum", 0.00);
|
|
|
- mapSum1.put("valleysSum", 0.00);
|
|
|
- String deviceType = type;
|
|
|
-
|
|
|
- if (null != deviceType) {
|
|
|
- LambdaQueryWrapper<DeviceInformation> deviceInformationWrapper = new LambdaQueryWrapper<>();
|
|
|
- deviceInformationWrapper.eq(DeviceInformation::getDeviceType, deviceType);
|
|
|
- List<DeviceInformation> deviceInformationList = deviceInformationMapper.selectList(deviceInformationWrapper);
|
|
|
-
|
|
|
- List<String> deviceIds = deviceInformationList.stream()
|
|
|
- .map(DeviceInformation::getId) // 获取每个 DeviceInformation 的 id 属性
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- if (!CollectionUtils.isEmpty(deviceIds)) {
|
|
|
- for (PeaksAndValleysTimeConfig peaksAndValleysTimeConfig : peaksAndValleysTimeConfiglist) {
|
|
|
- String startDate = dayDate + " " + peaksAndValleysTimeConfig.getStartTime();
|
|
|
- String endDate = dayDate + " " + peaksAndValleysTimeConfig.getEndTime();
|
|
|
-
|
|
|
- LocalDateTime start_time = adjustCrossDay(LocalDateTime.parse(startDate, formatter), LocalDateTime.parse(endDate, formatter));
|
|
|
-
|
|
|
- // 转换为字符串
|
|
|
- String formattedStartTime = start_time.format(formatter);
|
|
|
-
|
|
|
- Map peaksAndValleysTimeMap = fpgGatherDataMapper.getDeviceTypeData(deviceIds, formattedStartTime, endDate);
|
|
|
- if (peaksAndValleysTimeMap != null) {
|
|
|
- if ("tops".equals(peaksAndValleysTimeConfig.getType())) { // 尖值处理
|
|
|
- BigDecimal topsAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
- BigDecimal topsSum = new BigDecimal(String.valueOf(mapSum1.get("topsSum")));
|
|
|
- mapSum1.put("topsSum", topsSum.add(topsAmount));
|
|
|
- } else if ("peaks".equals(peaksAndValleysTimeConfig.getType())) { // 峰值处理
|
|
|
- BigDecimal peaksAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
- BigDecimal peaksSum = new BigDecimal(String.valueOf(mapSum1.get("peaksSum")));
|
|
|
- mapSum1.put("peaksSum", peaksSum.add(peaksAmount));
|
|
|
- } else if ("flat".equals(peaksAndValleysTimeConfig.getType())) { // 平值处理
|
|
|
- BigDecimal flatAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
- BigDecimal flatSum = new BigDecimal(String.valueOf(mapSum1.get("flatSum")));
|
|
|
- mapSum1.put("flatSum", flatSum.add(flatAmount));
|
|
|
- } else if ("valleys".equals(peaksAndValleysTimeConfig.getType())) { // 谷值处理
|
|
|
- BigDecimal valleysAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
- BigDecimal valleysSum = new BigDecimal(String.valueOf(mapSum1.get("valleysSum")));
|
|
|
- mapSum1.put("valleysSum", valleysSum.add(valleysAmount));
|
|
|
- }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ List<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
|
|
|
+
|
|
|
+ Date todayDate = new Date();
|
|
|
+ // 使用 Calendar 获取当天日期
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(todayDate);
|
|
|
+ calendar.add(Calendar.DAY_OF_YEAR, -0);
|
|
|
+
|
|
|
+ Date yesterdayDate = calendar.getTime();
|
|
|
+ String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(yesterdayDate);
|
|
|
+ List<String> stringList = new ArrayList<>();
|
|
|
+ stringList.add("1");
|
|
|
+ stringList.add("2");
|
|
|
+ stringList.add("3");
|
|
|
+ stringList.add("4");
|
|
|
+ stringList.add("5");
|
|
|
+
|
|
|
+ // 遍历每个 deviceInformation,根据 deviceType 分组查询
|
|
|
+ for (String type : stringList) {
|
|
|
+
|
|
|
+ Map<String, String> map1 = new HashMap<>();
|
|
|
+ Map<String, Object> mapSum1 = new HashMap<>();
|
|
|
+ mapSum1.put("topsSum", 0.00);
|
|
|
+ mapSum1.put("peaksSum", 0.00);
|
|
|
+ mapSum1.put("flatSum", 0.00);
|
|
|
+ mapSum1.put("valleysSum", 0.00);
|
|
|
+ String deviceType = type;
|
|
|
+
|
|
|
+ if (null != deviceType) {
|
|
|
+ LambdaQueryWrapper<DeviceInformation> deviceInformationWrapper = new LambdaQueryWrapper<>();
|
|
|
+ deviceInformationWrapper.eq(DeviceInformation::getDeviceType, deviceType);
|
|
|
+ List<DeviceInformation> deviceInformationList = deviceInformationMapper.selectList(deviceInformationWrapper);
|
|
|
+
|
|
|
+ List<String> deviceIds = deviceInformationList.stream()
|
|
|
+ .map(DeviceInformation::getId) // 获取每个 DeviceInformation 的 id 属性
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(deviceIds)) {
|
|
|
+ for (PeaksAndValleysTimeConfig peaksAndValleysTimeConfig : peaksAndValleysTimeConfiglist) {
|
|
|
+ String startDate = dayDate + " " + peaksAndValleysTimeConfig.getStartTime();
|
|
|
+ String endDate = dayDate + " " + peaksAndValleysTimeConfig.getEndTime();
|
|
|
+
|
|
|
+ LocalDateTime start_time = adjustCrossDay(LocalDateTime.parse(startDate, formatter), LocalDateTime.parse(endDate, formatter));
|
|
|
+
|
|
|
+ // 转换为字符串
|
|
|
+ String formattedStartTime = start_time.format(formatter);
|
|
|
+
|
|
|
+ Map peaksAndValleysTimeMap = fpgGatherDataMapper.getDeviceTypeData(deviceIds, formattedStartTime, endDate);
|
|
|
+ if (peaksAndValleysTimeMap != null) {
|
|
|
+ if ("tops".equals(peaksAndValleysTimeConfig.getType())) { // 尖值处理
|
|
|
+ BigDecimal topsAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal topsSum = new BigDecimal(String.valueOf(mapSum1.get("topsSum")));
|
|
|
+ mapSum1.put("topsSum", topsSum.add(topsAmount));
|
|
|
+ } else if ("peaks".equals(peaksAndValleysTimeConfig.getType())) { // 峰值处理
|
|
|
+ BigDecimal peaksAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal peaksSum = new BigDecimal(String.valueOf(mapSum1.get("peaksSum")));
|
|
|
+ mapSum1.put("peaksSum", peaksSum.add(peaksAmount));
|
|
|
+ } else if ("flat".equals(peaksAndValleysTimeConfig.getType())) { // 平值处理
|
|
|
+ BigDecimal flatAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal flatSum = new BigDecimal(String.valueOf(mapSum1.get("flatSum")));
|
|
|
+ mapSum1.put("flatSum", flatSum.add(flatAmount));
|
|
|
+ } else if ("valleys".equals(peaksAndValleysTimeConfig.getType())) { // 谷值处理
|
|
|
+ BigDecimal valleysAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal valleysSum = new BigDecimal(String.valueOf(mapSum1.get("valleysSum")));
|
|
|
+ mapSum1.put("valleysSum", valleysSum.add(valleysAmount));
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- BigDecimal totalActivePower2 = BigDecimal.ZERO; // 假设需要累加 activePower
|
|
|
-
|
|
|
- for (String deviceId : deviceIds) {
|
|
|
- // 构造查询条件
|
|
|
- LambdaQueryWrapper<FpgGatherData> fpgGatherQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- fpgGatherQueryWrapper
|
|
|
- .eq(FpgGatherData::getDeviceInformationId, deviceId) // 针对当前 deviceId 查询
|
|
|
- .ge(FpgGatherData::getCreateTime, startOfDay) // 创建时间大于等于开始时间
|
|
|
- .le(FpgGatherData::getCreateTime, endOfDay) // 创建时间小于等于结束时间
|
|
|
- .orderByDesc(FpgGatherData::getCreateTime) // 按时间降序排列
|
|
|
- .last("limit 1"); // 只取最新的一条数据
|
|
|
-
|
|
|
- // 查询数据
|
|
|
- List<FpgGatherData> singleList = fpgGatherDataMapper.selectList(fpgGatherQueryWrapper);
|
|
|
-
|
|
|
- // 如果查询结果非空,进行累加
|
|
|
- if (singleList != null && !singleList.isEmpty()) {
|
|
|
- FpgGatherData data = singleList.get(0); // 最新的一条数据
|
|
|
- totalActivePower2 = totalActivePower2.add(data.getActivePower() == null ? BigDecimal.ZERO : data.getActivePower());
|
|
|
- }
|
|
|
+ BigDecimal totalActivePower2 = BigDecimal.ZERO; // 假设需要累加 activePower
|
|
|
+
|
|
|
+ for (String deviceId : deviceIds) {
|
|
|
+ // 构造查询条件
|
|
|
+ LambdaQueryWrapper<FpgGatherData> fpgGatherQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ fpgGatherQueryWrapper
|
|
|
+ .eq(FpgGatherData::getDeviceInformationId, deviceId) // 针对当前 deviceId 查询
|
|
|
+ .ge(FpgGatherData::getCreateTime, startOfDay) // 创建时间大于等于开始时间
|
|
|
+ .le(FpgGatherData::getCreateTime, endOfDay) // 创建时间小于等于结束时间
|
|
|
+ .orderByDesc(FpgGatherData::getCreateTime) // 按时间降序排列
|
|
|
+ .last("limit 1"); // 只取最新的一条数据
|
|
|
+
|
|
|
+ // 查询数据
|
|
|
+ List<FpgGatherData> singleList = fpgGatherDataMapper.selectList(fpgGatherQueryWrapper);
|
|
|
+
|
|
|
+ // 如果查询结果非空,进行累加
|
|
|
+ if (singleList != null && !singleList.isEmpty()) {
|
|
|
+ FpgGatherData data = singleList.get(0); // 最新的一条数据
|
|
|
+ totalActivePower2 = totalActivePower2.add(data.getActivePower() == null ? BigDecimal.ZERO : data.getActivePower());
|
|
|
}
|
|
|
- // 对累计总功率处理,去除无效小数
|
|
|
- powerMap.put(mapDeviceType(deviceType) + "实时总功率", totalActivePower2.stripTrailingZeros().toPlainString());
|
|
|
+ }
|
|
|
+ // 对累计总功率处理,去除无效小数
|
|
|
+ powerMap.put(mapDeviceType(deviceType) + "实时总功率", totalActivePower2.stripTrailingZeros().toPlainString());
|
|
|
|
|
|
|
|
|
- }
|
|
|
}
|
|
|
- map1.put("name", mapDeviceType(deviceType));
|
|
|
- map1.put("tops", String.format("%.2f", mapSum1.get("topsSum")));
|
|
|
- map1.put("peaks", String.format("%.2f", mapSum1.get("peaksSum")));
|
|
|
- map1.put("flat", String.format("%.2f", mapSum1.get("flatSum")));
|
|
|
- map1.put("valleys", String.format("%.2f", mapSum1.get("valleysSum")));
|
|
|
-
|
|
|
- // 假设 powerMap 里已经包含了各个时段的总功率
|
|
|
- BigDecimal totalPower = BigDecimal.ZERO;
|
|
|
-
|
|
|
- BigDecimal topsPower = new BigDecimal(String.valueOf(map1.get("tops")));
|
|
|
- BigDecimal peaksPower = new BigDecimal(String.valueOf(map1.get("peaks")));
|
|
|
- BigDecimal flatPower = new BigDecimal(String.valueOf(map1.get("flat")));
|
|
|
- BigDecimal valleysPower = new BigDecimal(String.valueOf(map1.get("valleys")));
|
|
|
-
|
|
|
- // 计算总功率
|
|
|
- totalPower = totalPower.add(topsPower).add(peaksPower).add(flatPower).add(valleysPower);
|
|
|
-
|
|
|
- // 计算每个时段的占比
|
|
|
- BigDecimal topsRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? topsPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
- BigDecimal peaksRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? peaksPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
- BigDecimal flatRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? flatPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
- BigDecimal valleysRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? valleysPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
-
|
|
|
- // 将占比放入 powerMap
|
|
|
- // 格式化为百分比并去除无效小数
|
|
|
- powerMap.put(mapDeviceType(deviceType) + "尖时段总功率占比", formatPercentage(topsRatio));
|
|
|
- powerMap.put(mapDeviceType(deviceType) + "峰时段总功率占比", formatPercentage(peaksRatio));
|
|
|
- powerMap.put(mapDeviceType(deviceType) + "平时段总功率占比", formatPercentage(flatRatio));
|
|
|
- powerMap.put(mapDeviceType(deviceType) + "谷时段总功率占比", formatPercentage(valleysRatio));
|
|
|
-
|
|
|
- // 对累计总功率处理,去除无效小数
|
|
|
- powerMap.put(mapDeviceType(deviceType) + "累计总功率", totalPower.stripTrailingZeros().toPlainString());
|
|
|
-
|
|
|
}
|
|
|
+ map1.put("name", mapDeviceType(deviceType));
|
|
|
+ map1.put("tops", String.format("%.2f", mapSum1.get("topsSum")));
|
|
|
+ map1.put("peaks", String.format("%.2f", mapSum1.get("peaksSum")));
|
|
|
+ map1.put("flat", String.format("%.2f", mapSum1.get("flatSum")));
|
|
|
+ map1.put("valleys", String.format("%.2f", mapSum1.get("valleysSum")));
|
|
|
|
|
|
- }
|
|
|
+ // 假设 powerMap 里已经包含了各个时段的总功率
|
|
|
+ BigDecimal totalPower = BigDecimal.ZERO;
|
|
|
|
|
|
- // 更新公司实时和累计功率到 powerMap
|
|
|
- powerMap.put("公司实时总功率", companyRealTimeTotalPower);
|
|
|
- powerMap.put("公司累计总功率", companyAccumulatedTotalPower);
|
|
|
- // 更新公司的尖、峰、平、谷时段总功率到 powerMap
|
|
|
- powerMap.put("公司尖时段总功率", companyTopsPower);
|
|
|
- powerMap.put("公司峰时段总功率", companyPeaksPower);
|
|
|
- powerMap.put("公司平时段总功率", companyFlatPower);
|
|
|
- powerMap.put("公司谷时段总功率", companyValleysPower);
|
|
|
+ BigDecimal topsPower = new BigDecimal(String.valueOf(map1.get("tops")));
|
|
|
+ BigDecimal peaksPower = new BigDecimal(String.valueOf(map1.get("peaks")));
|
|
|
+ BigDecimal flatPower = new BigDecimal(String.valueOf(map1.get("flat")));
|
|
|
+ BigDecimal valleysPower = new BigDecimal(String.valueOf(map1.get("valleys")));
|
|
|
|
|
|
-// // 假设 powerMap 里已经包含了公司相关的总功率
|
|
|
-// BigDecimal companyTotalPower = companyAccumulatedTotalPower != null ? companyAccumulatedTotalPower : BigDecimal.ZERO; // 公司累计总功率
|
|
|
+ // 计算总功率
|
|
|
+ totalPower = totalPower.add(topsPower).add(peaksPower).add(flatPower).add(valleysPower);
|
|
|
|
|
|
- BigDecimal companyTotalPower = BigDecimal.ZERO;
|
|
|
+ // 计算每个时段的占比
|
|
|
+ BigDecimal topsRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? topsPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal peaksRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? peaksPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal flatRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? flatPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal valleysRatio = totalPower.compareTo(BigDecimal.ZERO) != 0 ? valleysPower.divide(totalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
|
|
|
- // 获取各时段总功率,若值为 null,使用 BigDecimal.ZERO
|
|
|
- BigDecimal companyTopsPower1 = powerMap.containsKey("公司尖时段总功率") && powerMap.get("公司尖时段总功率") != null ?
|
|
|
- new BigDecimal(String.valueOf(powerMap.get("公司尖时段总功率"))) : BigDecimal.ZERO;
|
|
|
+ // 将占比放入 powerMap
|
|
|
+ // 格式化为百分比并去除无效小数
|
|
|
+ powerMap.put(mapDeviceType(deviceType) + "尖时段总功率占比", formatPercentage(topsRatio));
|
|
|
+ powerMap.put(mapDeviceType(deviceType) + "峰时段总功率占比", formatPercentage(peaksRatio));
|
|
|
+ powerMap.put(mapDeviceType(deviceType) + "平时段总功率占比", formatPercentage(flatRatio));
|
|
|
+ powerMap.put(mapDeviceType(deviceType) + "谷时段总功率占比", formatPercentage(valleysRatio));
|
|
|
|
|
|
- BigDecimal companyPeaksPower1 = powerMap.containsKey("公司峰时段总功率") && powerMap.get("公司峰时段总功率") != null ?
|
|
|
- new BigDecimal(String.valueOf(powerMap.get("公司峰时段总功率"))) : BigDecimal.ZERO;
|
|
|
+ // 对累计总功率处理,去除无效小数
|
|
|
+ powerMap.put(mapDeviceType(deviceType) + "累计总功率", totalPower.stripTrailingZeros().toPlainString());
|
|
|
|
|
|
- BigDecimal companyFlatPower1 = powerMap.containsKey("公司平时段总功率") && powerMap.get("公司平时段总功率") != null ?
|
|
|
- new BigDecimal(String.valueOf(powerMap.get("公司平时段总功率"))) : BigDecimal.ZERO;
|
|
|
+ }
|
|
|
|
|
|
- BigDecimal companyValleysPower1 = powerMap.containsKey("公司谷时段总功率") && powerMap.get("公司谷时段总功率") != null ?
|
|
|
- new BigDecimal(String.valueOf(powerMap.get("公司谷时段总功率"))) : BigDecimal.ZERO;
|
|
|
|
|
|
- // 计算总功率
|
|
|
- companyTotalPower = companyTotalPower.add(companyTopsPower1).add(companyPeaksPower1).add(companyFlatPower1).add(companyValleysPower1);
|
|
|
+ // 更新公司实时和累计功率到 powerMap
|
|
|
+ powerMap.put("公司实时总功率", companyRealTimeTotalPower.get().setScale(2, RoundingMode.HALF_UP));
|
|
|
+ powerMap.put("公司累计总功率", companyAccumulatedTotalPower.get().setScale(2, RoundingMode.HALF_UP));
|
|
|
+ // 更新公司的尖、峰、平、谷时段总功率到 powerMap
|
|
|
+ powerMap.put("公司尖时段总功率", companyTopsPower.get().setScale(2, RoundingMode.HALF_UP));
|
|
|
+ powerMap.put("公司峰时段总功率", companyPeaksPower.get().setScale(2, RoundingMode.HALF_UP));
|
|
|
+ powerMap.put("公司平时段总功率", companyFlatPower.get().setScale(2, RoundingMode.HALF_UP));
|
|
|
+ powerMap.put("公司谷时段总功率", companyValleysPower.get().setScale(2, RoundingMode.HALF_UP));
|
|
|
|
|
|
// 计算占比:每个时段的总功率 / 公司累计总功率
|
|
|
- BigDecimal topsRatio = companyTotalPower.compareTo(BigDecimal.ZERO) != 0 ? companyTopsPower1.divide(companyTotalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
- BigDecimal peaksRatio = companyTotalPower.compareTo(BigDecimal.ZERO) != 0 ? companyPeaksPower1.divide(companyTotalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
- BigDecimal flatRatio = companyTotalPower.compareTo(BigDecimal.ZERO) != 0 ? companyFlatPower1.divide(companyTotalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
- BigDecimal valleysRatio = companyTotalPower.compareTo(BigDecimal.ZERO) != 0 ? companyValleysPower1.divide(companyTotalPower, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal topsRatio = companyAccumulatedTotalPower.get().compareTo(BigDecimal.ZERO) != 0 ? companyTopsPower.get().divide(companyAccumulatedTotalPower.get(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal peaksRatio = companyAccumulatedTotalPower.get().compareTo(BigDecimal.ZERO) != 0 ? companyPeaksPower.get().divide(companyAccumulatedTotalPower.get(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal flatRatio = companyAccumulatedTotalPower.get().compareTo(BigDecimal.ZERO) != 0 ? companyFlatPower.get().divide(companyAccumulatedTotalPower.get(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ BigDecimal valleysRatio = companyAccumulatedTotalPower.get().compareTo(BigDecimal.ZERO) != 0 ? companyValleysPower.get().divide(companyAccumulatedTotalPower.get(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
|
|
|
// 将占比放入 powerMap,转换为百分比格式
|
|
|
powerMap.put("公司尖时段总功率占比", formatPercentage(topsRatio));
|