|
|
@@ -2,8 +2,13 @@ package com.ufo.project.forecast.shortterm.power.controller;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
@@ -13,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.ufo.base.common.utils.DateUtils;
|
|
|
+import com.ufo.base.common.utils.TimeUtils;
|
|
|
import com.ufo.base.framework.aspectj.lang.annotation.Log;
|
|
|
import com.ufo.base.framework.aspectj.lang.enums.BusinessType;
|
|
|
import com.ufo.base.framework.web.controller.BaseController;
|
|
|
@@ -82,20 +88,53 @@ public class ShortTermPowerForecastingController extends BaseController
|
|
|
long now = System.currentTimeMillis() / 1000L;
|
|
|
long end = now + (long) forecastHours * 3600L;
|
|
|
List<DataShortTermPowerForecastIssued> issuedList = dataShortTermPowerForecastIssuedService
|
|
|
- .selectByTimeRange(stationId.toString(), now, end);
|
|
|
- if (issuedList == null)
|
|
|
+ .selectByTimeRange(stationId.toString(), TimeUtils.gmt8ToUtc(now), TimeUtils.gmt8ToUtc(end));
|
|
|
+ if (issuedList == null || issuedList.isEmpty())
|
|
|
{
|
|
|
return result;
|
|
|
}
|
|
|
- for (DataShortTermPowerForecastIssued issued : issuedList)
|
|
|
+
|
|
|
+ // 按 forecast_time 分组,统计每个时间点的平均功率
|
|
|
+ Map<Long, List<DataShortTermPowerForecastIssued>> groupedByTime = issuedList.stream()
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
+ DataShortTermPowerForecastIssued::getForecastTime,
|
|
|
+ LinkedHashMap::new,
|
|
|
+ Collectors.toList()));
|
|
|
+
|
|
|
+ for (Map.Entry<Long, List<DataShortTermPowerForecastIssued>> entry : groupedByTime.entrySet())
|
|
|
{
|
|
|
+ List<DataShortTermPowerForecastIssued> items = entry.getValue();
|
|
|
+ if (items.isEmpty())
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal sum = BigDecimal.ZERO;
|
|
|
+ int validCount = 0;
|
|
|
+ for (DataShortTermPowerForecastIssued issued : items)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (issued.getPower() != null && !issued.getPower().isEmpty())
|
|
|
+ {
|
|
|
+ sum = sum.add(new BigDecimal(issued.getPower()));
|
|
|
+ validCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (NumberFormatException ignored)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
ShortTermPowerForecastData data = new ShortTermPowerForecastData();
|
|
|
- data.setTime(ExcelForecastFileUtil.formatTime(String.valueOf(issued.getStartTime())));
|
|
|
- try
|
|
|
+ data.setTime(TimeUtils.utcToGmt8Format(entry.getKey()));
|
|
|
+ // 计算平均值,保留3位小数(四舍五入)
|
|
|
+ if (validCount > 0)
|
|
|
{
|
|
|
- data.setPower(Double.parseDouble(issued.getPower()));
|
|
|
+ BigDecimal avg = sum.divide(BigDecimal.valueOf(validCount), 3, RoundingMode.HALF_UP);
|
|
|
+ data.setPower(avg.doubleValue());
|
|
|
}
|
|
|
- catch (NumberFormatException e)
|
|
|
+ else
|
|
|
{
|
|
|
data.setPower(0.0);
|
|
|
}
|
|
|
@@ -103,6 +142,7 @@ public class ShortTermPowerForecastingController extends BaseController
|
|
|
data.setCurtailmentPower(0.0);
|
|
|
result.add(data);
|
|
|
}
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
}
|