|
@@ -1,19 +1,431 @@
|
|
|
package org.jeecg.modules.deviceLesm.service.impl;
|
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.jeecg.modules.deviceLesm.entity.DeviceInformation;
|
|
|
import org.jeecg.modules.deviceLesm.entity.DeviceRegion;
|
|
|
+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.mapper.FpgGatherDataMapper;
|
|
|
+import org.jeecg.modules.systemConfig.peaksAndValleysTimeConfig.entity.PeaksAndValleysTimeConfig;
|
|
|
+import org.jeecg.modules.systemConfig.peaksAndValleysTimeConfig.service.IPeaksAndValleysTimeConfigService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static net.sf.jsqlparser.parser.feature.Feature.limit;
|
|
|
+
|
|
|
/**
|
|
|
* @Description: 区域管理信息表
|
|
|
* @Author: jeecg-boot
|
|
|
- * @Date: 2024-09-19
|
|
|
+ * @Date: 2024-09-19
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
@Service
|
|
|
public class DeviceRegionServiceImpl extends ServiceImpl<DeviceRegionMapper, DeviceRegion> implements IDeviceRegionService {
|
|
|
|
|
|
-}
|
|
|
+ @Autowired
|
|
|
+ private IPeaksAndValleysTimeConfigService peaksAndValleysTimeConfigService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FpgGatherDataMapper fpgGatherDataMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceInformationMapper deviceInformationMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> powerStatistics() throws ParseException {
|
|
|
+
|
|
|
+ List<Map<String, Object>> arrayList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 获取前一天的开始和结束时间
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ String startOfDayString = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00:00:00";
|
|
|
+ String endOfDayString = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 23:59:59";
|
|
|
+
|
|
|
+ // 转换为 Date 类型
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ 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);
|
|
|
+
|
|
|
+ Map<String, Object> powerMap = new HashMap<>();
|
|
|
+
|
|
|
+ powerMap.put("公司实时总功率", null);
|
|
|
+ powerMap.put("公司累计总功率", null);
|
|
|
+
|
|
|
+ // 初始化公司实时总功率和累计总功率的变量
|
|
|
+ BigDecimal companyRealTimeTotalPower = BigDecimal.ZERO;
|
|
|
+ BigDecimal companyAccumulatedTotalPower = BigDecimal.ZERO;
|
|
|
+ // 初始化公司尖、峰、平、谷时段总功率的变量
|
|
|
+ BigDecimal companyTopsPower = BigDecimal.ZERO;
|
|
|
+ BigDecimal companyPeaksPower = BigDecimal.ZERO;
|
|
|
+ BigDecimal companyFlatPower = BigDecimal.ZERO;
|
|
|
+ BigDecimal companyValleysPower = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ for (DeviceRegion deviceRegion : deviceRegions) {
|
|
|
+
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "实时总功率", null);
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "累计总功率", null);
|
|
|
+
|
|
|
+ // 初始化 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);
|
|
|
+
|
|
|
+ // 累加 RunCurrent 并设置值到 powerMap
|
|
|
+ BigDecimal realTimeRunCurrent = BigDecimal.ZERO;
|
|
|
+ BigDecimal totalRunCurrent = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ if (resultList != null && !resultList.isEmpty()) {
|
|
|
+ // 累加 RunCurrent,判空处理
|
|
|
+ totalRunCurrent = resultList.stream()
|
|
|
+ .map(FpgGatherData::getRunCurrent)
|
|
|
+ .filter(Objects::nonNull) // 过滤掉 null 值
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ // 获取第一个数据的 RunCurrent(最新值,按降序排列后第一个),判空处理
|
|
|
+ realTimeRunCurrent = resultList.get(0).getRunCurrent();
|
|
|
+ if (realTimeRunCurrent == null) {
|
|
|
+ realTimeRunCurrent = BigDecimal.ZERO; // 如果为 null,设置为零
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新区域实时和累计功率到 powerMap
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "实时总功率", realTimeRunCurrent);
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "累计总功率", totalRunCurrent);
|
|
|
+
|
|
|
+ // 累加到公司实时和累计功率
|
|
|
+ companyRealTimeTotalPower = companyRealTimeTotalPower.add(realTimeRunCurrent);
|
|
|
+ companyAccumulatedTotalPower = companyAccumulatedTotalPower.add(totalRunCurrent);
|
|
|
+
|
|
|
+
|
|
|
+ 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<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
|
|
|
+
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ Map<String, Object> mapSum = new HashMap<>();
|
|
|
+ mapSum.put("topsSum", 0.00);
|
|
|
+ mapSum.put("peaksSum", 0.00);
|
|
|
+ mapSum.put("flatSum", 0.00);
|
|
|
+ mapSum.put("valleysSum", 0.00);
|
|
|
+
|
|
|
+ // 峰平谷时段处理
|
|
|
+ peaksAndValleysTimeConfiglist.forEach(peaksAndValleysTime -> {
|
|
|
+ String startDate = dayDate + " " + peaksAndValleysTime.getStartTime();
|
|
|
+ String endDate = dayDate + " " + peaksAndValleysTime.getEndTime();
|
|
|
+ Map peaksAndValleysTimeMap = fpgGatherDataMapper.getFpgTheLeftSideOneData(deviceRegion.getId(), startDate, endDate);
|
|
|
+ if (peaksAndValleysTimeMap != null) {
|
|
|
+ if ("tops".equals(peaksAndValleysTime.getType())) { // 尖值处理
|
|
|
+ BigDecimal topsAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal topsSum = new BigDecimal(String.valueOf(mapSum.get("topsSum")));
|
|
|
+ mapSum.put("topsSum", topsSum.add(topsAmount));
|
|
|
+ } else if ("peaks".equals(peaksAndValleysTime.getType())) { // 峰值处理
|
|
|
+ BigDecimal peaksAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal peaksSum = new BigDecimal(String.valueOf(mapSum.get("peaksSum")));
|
|
|
+ mapSum.put("peaksSum", peaksSum.add(peaksAmount));
|
|
|
+ } else if ("flat".equals(peaksAndValleysTime.getType())) { // 平值处理
|
|
|
+ BigDecimal flatAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal flatSum = new BigDecimal(String.valueOf(mapSum.get("flatSum")));
|
|
|
+ mapSum.put("flatSum", flatSum.add(flatAmount));
|
|
|
+ } else if ("valleys".equals(peaksAndValleysTime.getType())) { // 谷值处理
|
|
|
+ BigDecimal valleysAmount = new BigDecimal(String.valueOf(peaksAndValleysTimeMap.get("active_power")));
|
|
|
+ BigDecimal valleysSum = new BigDecimal(String.valueOf(mapSum.get("valleysSum")));
|
|
|
+ mapSum.put("valleysSum", valleysSum.add(valleysAmount));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ map.put("name", deviceRegion.getRegionTitle());
|
|
|
+ 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")));
|
|
|
+ map.put("valleys", String.format("%.2f", mapSum.get("valleysSum")));
|
|
|
+
|
|
|
+ // 将区域的尖、峰、平、谷时段功率转换为 BigDecimal 并累加到公司的变量
|
|
|
+ BigDecimal regionTopsPower = new BigDecimal(map.get("tops"));
|
|
|
+ BigDecimal regionPeaksPower = new BigDecimal(map.get("peaks"));
|
|
|
+ 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;
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ // 计算总功率
|
|
|
+ BigDecimal totalPower1 = topsPower1.add(peaksPower1).add(flatPower1).add(valleysPower1);
|
|
|
+
|
|
|
+ // 避免除以零的情况
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // 更新到 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));
|
|
|
+ } else {
|
|
|
+ // 如果总功率为 0,所有占比设为 0%
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "尖时段总功率占比", "0%");
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "峰时段总功率占比", "0%");
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "平时段总功率占比", "0%");
|
|
|
+ powerMap.put(deviceRegion.getRegionTitle() + "谷时段总功率占比", "0%");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// deviceInformationWrapper.eq(DeviceInformation::getDeviceRegionId, deviceRegion.getId())
|
|
|
+// .orderByAsc(DeviceInformation::getDeviceType);
|
|
|
+// ;
|
|
|
+// List<DeviceInformation> deviceInformations = deviceInformationMapper.selectList(deviceInformationWrapper);
|
|
|
+
|
|
|
+ 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();
|
|
|
+ Map peaksAndValleysTimeMap = fpgGatherDataMapper.getDeviceTypeData(deviceIds, startDate, 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));
|
|
|
+ }
|
|
|
+ }
|
|
|
+// // 检查 peaksAndValleysTimeMapLast 是否包含 "active_power" 且值不为 null 或空
|
|
|
+// if (peaksAndValleysTimeMapLast != null && peaksAndValleysTimeMapLast.containsKey("active_power")
|
|
|
+// && peaksAndValleysTimeMapLast.get("active_power") != null) {
|
|
|
+// powerMap.put(mapDeviceType(deviceType) + "累计总功率", peaksAndValleysTimeMapLast.get("active_power"));
|
|
|
+// } else {
|
|
|
+// powerMap.put(mapDeviceType(deviceType) + "累计总功率", "0"); // 如果值为空,设置为默认值
|
|
|
+// }
|
|
|
+//
|
|
|
+// powerMap.put(mapDeviceType(deviceType) + "累计总功率", peaksAndValleysTimeMapLast.get("active_power"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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());
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新公司实时和累计功率到 powerMap
|
|
|
+ powerMap.put("公司实时总功率", companyRealTimeTotalPower);
|
|
|
+ powerMap.put("公司累计总功率", companyAccumulatedTotalPower);
|
|
|
+ // 更新公司的尖、峰、平、谷时段总功率到 powerMap
|
|
|
+ powerMap.put("公司尖时段总功率", companyTopsPower);
|
|
|
+ powerMap.put("公司峰时段总功率", companyPeaksPower);
|
|
|
+ powerMap.put("公司平时段总功率", companyFlatPower);
|
|
|
+ powerMap.put("公司谷时段总功率", companyValleysPower);
|
|
|
+
|
|
|
+
|
|
|
+// // 假设 powerMap 里已经包含了公司相关的总功率
|
|
|
+// BigDecimal companyTotalPower = companyAccumulatedTotalPower != null ? companyAccumulatedTotalPower : BigDecimal.ZERO; // 公司累计总功率
|
|
|
+
|
|
|
+ BigDecimal companyTotalPower = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ // 获取各时段总功率,若值为 null,使用 BigDecimal.ZERO
|
|
|
+ BigDecimal companyTopsPower1 = powerMap.containsKey("公司尖时段总功率") && powerMap.get("公司尖时段总功率") != null ?
|
|
|
+ new BigDecimal(String.valueOf(powerMap.get("公司尖时段总功率"))) : BigDecimal.ZERO;
|
|
|
+
|
|
|
+ BigDecimal companyPeaksPower1 = powerMap.containsKey("公司峰时段总功率") && powerMap.get("公司峰时段总功率") != null ?
|
|
|
+ new BigDecimal(String.valueOf(powerMap.get("公司峰时段总功率"))) : BigDecimal.ZERO;
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // 计算占比:每个时段的总功率 / 公司累计总功率
|
|
|
+ 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;
|
|
|
+
|
|
|
+ // 将占比放入 powerMap,转换为百分比格式
|
|
|
+ powerMap.put("公司尖时段总功率占比", formatPercentage(topsRatio));
|
|
|
+ powerMap.put("公司峰时段总功率占比", formatPercentage(peaksRatio));
|
|
|
+ powerMap.put("公司平时段总功率占比", formatPercentage(flatRatio));
|
|
|
+ powerMap.put("公司谷时段总功率占比", formatPercentage(valleysRatio));
|
|
|
+
|
|
|
+
|
|
|
+ arrayList.add(powerMap);
|
|
|
+
|
|
|
+ return arrayList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String mapDeviceType(String deviceType) {
|
|
|
+ // 定义映射关系
|
|
|
+ Map<String, String> deviceTypeMap = new HashMap<>();
|
|
|
+ deviceTypeMap.put("1", "棒一");
|
|
|
+ deviceTypeMap.put("2", "棒二");
|
|
|
+ deviceTypeMap.put("3", "棒三");
|
|
|
+ deviceTypeMap.put("4", "高线");
|
|
|
+ deviceTypeMap.put("5", "板带");
|
|
|
+
|
|
|
+ // 返回映射值,如果找不到对应的 key,则返回默认值
|
|
|
+ return deviceTypeMap.getOrDefault(deviceType, "未知类型");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化百分比,保留最多两位小数,去除无效零
|
|
|
+ */
|
|
|
+ private String formatPercentage(BigDecimal ratio) {
|
|
|
+ BigDecimal percentage = ratio.multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
|
|
|
+ return percentage.stripTrailingZeros().toPlainString() + "%";
|
|
|
+ }
|
|
|
+}
|