|
|
|
|
@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@ -197,4 +198,87 @@ public class EmsReportServiceImpl implements IEmsReportService {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 能源同比分析接口
|
|
|
|
|
* @param hashMap
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Map<String, Object> yoyAnalysisReport(Map hashMap) {
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
if (!hashMap.containsKey("beginCollectTime") || !hashMap.containsKey("endCollectTime") || !hashMap.containsKey("workUnitCode")) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
String beginCollectTime = String.valueOf(hashMap.get("beginCollectTime"));
|
|
|
|
|
String endCollectTime = String.valueOf(hashMap.get("endCollectTime"));
|
|
|
|
|
String dateType = String.valueOf(hashMap.get("dateType"));
|
|
|
|
|
//时间范围
|
|
|
|
|
List<String> timeIntervalList = getTimeIntervals(beginCollectTime, endCollectTime, dateType);
|
|
|
|
|
List<EnergyStatisticalReport> statisticalReportList = this.energyStatisticalReportList(hashMap);
|
|
|
|
|
//同比增长率=(本期数值−去年同期数值)/去年同期数值×100%
|
|
|
|
|
String beginYearDateTime = DateUtils.getLastYearDateTime(beginCollectTime);
|
|
|
|
|
String endYearDateTime = DateUtils.getLastYearDateTime(endCollectTime);
|
|
|
|
|
hashMap.put("beginCollectTime", beginYearDateTime);
|
|
|
|
|
hashMap.put("endCollectTime", endYearDateTime);
|
|
|
|
|
List<EnergyStatisticalReport> lastDateReportList = this.energyStatisticalReportList(hashMap);
|
|
|
|
|
|
|
|
|
|
List<BigDecimal> thisDataList = new ArrayList<>();
|
|
|
|
|
List<BigDecimal> contrastDataList = new ArrayList<>();
|
|
|
|
|
List<BigDecimal> rateList = new ArrayList<>();
|
|
|
|
|
for (String time : timeIntervalList) {
|
|
|
|
|
BigDecimal nowExpend = new BigDecimal(0);
|
|
|
|
|
BigDecimal lastInitExpend = new BigDecimal(0);
|
|
|
|
|
BigDecimal rate = new BigDecimal(0);
|
|
|
|
|
for (EnergyStatisticalReport report : statisticalReportList) {
|
|
|
|
|
BigDecimal expend = report.getExpend();
|
|
|
|
|
if (time.equals(report.getBeginTime())){
|
|
|
|
|
nowExpend = nowExpend.add(expend);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (EnergyStatisticalReport report : lastDateReportList) {
|
|
|
|
|
BigDecimal expend = report.getExpend();
|
|
|
|
|
if (time.equals(report.getBeginTime())){
|
|
|
|
|
lastInitExpend = lastInitExpend.add(expend);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
thisDataList.add(nowExpend);
|
|
|
|
|
contrastDataList.add(lastInitExpend);
|
|
|
|
|
BigDecimal decimal = calculateYearOverYearGrowth(nowExpend, lastInitExpend, 2, RoundingMode.HALF_UP);
|
|
|
|
|
rateList.add(decimal);
|
|
|
|
|
}
|
|
|
|
|
result.put("time", timeIntervalList);
|
|
|
|
|
result.put("thisData", thisDataList);
|
|
|
|
|
result.put("contrastData", contrastDataList);
|
|
|
|
|
result.put("rate", rateList);
|
|
|
|
|
BigDecimal thisSum = thisDataList.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
BigDecimal contrastSum = contrastDataList.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
result.put("thisSum", thisSum);
|
|
|
|
|
result.put("contrastSum", contrastSum);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算同比增长率(百分比)
|
|
|
|
|
* @param currentValue 本期数值
|
|
|
|
|
* @param lastYearValue 去年同期数值
|
|
|
|
|
* @param scale 结果保留小数位数
|
|
|
|
|
* @param roundingMode 舍入模式(默认四舍五入)
|
|
|
|
|
* @return 增长率百分比(如 15.25 表示 15.25%)
|
|
|
|
|
* @throws IllegalArgumentException 如果去年同期数值为0
|
|
|
|
|
*/
|
|
|
|
|
public BigDecimal calculateYearOverYearGrowth( BigDecimal currentValue, BigDecimal lastYearValue, int scale, RoundingMode roundingMode) {
|
|
|
|
|
// 验证参数
|
|
|
|
|
if (lastYearValue.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
|
|
return new BigDecimal(0);
|
|
|
|
|
}
|
|
|
|
|
// 计算差值:本期 - 去年同期
|
|
|
|
|
BigDecimal difference = currentValue.subtract(lastYearValue);
|
|
|
|
|
// 计算比率:(差值 / 去年同期值)
|
|
|
|
|
BigDecimal ratio = difference.divide(lastYearValue, scale + 2, roundingMode);
|
|
|
|
|
// 转换为百分比:× 100
|
|
|
|
|
return ratio.multiply(new BigDecimal("100")).setScale(scale, roundingMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|