Pārlūkot izejas kodu

[0924] 增加中期功率预测report/export接口

taosj 1 mēnesi atpakaļ
vecāks
revīzija
5c33eff275

+ 100 - 0
forecast-backend-server/src/main/java/com/ufo/project/forecast/mediumterm/power/controller/DataMediumTermPowerForecastIssuedController.java

@@ -1,7 +1,15 @@
 package com.ufo.project.forecast.mediumterm.power.controller;
 
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.net.URLEncoder;
+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.apache.commons.lang3.StringUtils;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -11,10 +19,15 @@ import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+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.project.forecast.mediumterm.power.domain.DataMediumTermPowerForecastIssued;
+import com.ufo.project.forecast.mediumterm.power.domain.MediumTermPowerForecastData;
 import com.ufo.project.forecast.mediumterm.power.service.IDataMediumTermPowerForecastIssuedService;
 import com.ufo.base.framework.web.controller.BaseController;
 import com.ufo.base.framework.web.domain.AjaxResult;
@@ -109,4 +122,91 @@ public class DataMediumTermPowerForecastIssuedController extends BaseController
     {
         return toAjax(dataMediumTermPowerForecastIssuedService.deleteDataMediumTermPowerForecastIssuedByIds(Ids));
     }
+
+    /**
+     * 获取中期功率预测数据
+     *
+     * @param stationId 场站id
+     * @param forecastHours 预测时间(小时, 中期最大240=10天)
+     * @return time/power/theoretical_power/curtailment_power 数据列表
+     */
+    @PreAuthorize("@ss.hasPermi('electricity:forecast:mediumtermpower')")
+    @GetMapping("/report")
+    public AjaxResult report(@RequestParam("stationId") Long stationId,
+            @RequestParam(value = "forecastHours", defaultValue = "24") Integer forecastHours)
+    {
+        return success(queryForecastData(stationId, forecastHours));
+    }
+
+    /**
+     * 导出中期功率预测数据
+     */
+    @PreAuthorize("@ss.hasPermi('electricity:forecast:mediumtermpower')")
+    @Log(title = "中期功率预测", businessType = BusinessType.EXPORT)
+    @GetMapping("/export/excel")
+    public void exportForecast(HttpServletResponse response,
+            @RequestParam("stationId") Long stationId,
+            @RequestParam(value = "forecastHours", defaultValue = "24") Integer forecastHours) throws IOException
+    {
+        List<MediumTermPowerForecastData> data = queryForecastData(stationId, forecastHours);
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        String fileName = URLEncoder.encode("中期预测-" + DateUtils.getDate() + ".xlsx", "UTF-8").replaceAll("\\+", "%20");
+        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName);
+        EasyExcel.write(response.getOutputStream(), MediumTermPowerForecastData.class)
+                .sheet("中期功率预测")
+                .doWrite(data);
+    }
+
+    /**
+     * 查询场站指定时间范围内的中期功率下发数据,按 forecast_time 分组取功率最大值
+     */
+    private List<MediumTermPowerForecastData> queryForecastData(Long stationId, Integer forecastHours)
+    {
+        List<MediumTermPowerForecastData> result = new ArrayList<>();
+        long now = System.currentTimeMillis() / 1000;
+        long end = now + (long) forecastHours * 3600L;
+        List<DataMediumTermPowerForecastIssued> issuedList = dataMediumTermPowerForecastIssuedService
+                .selectByTimeRange(stationId.toString(), now, end);
+        if (issuedList == null || issuedList.isEmpty())
+        {
+            return result;
+        }
+
+        // 按 forecast_time 分组,统计每个时间点的最大预测功率
+        Map<Long, List<DataMediumTermPowerForecastIssued>> groupedByTime = issuedList.stream()
+                .collect(Collectors.groupingBy(
+                        DataMediumTermPowerForecastIssued::getForecastTime,
+                        LinkedHashMap::new,
+                        Collectors.toList()));
+
+        for (Map.Entry<Long, List<DataMediumTermPowerForecastIssued>> entry : groupedByTime.entrySet())
+        {
+            List<DataMediumTermPowerForecastIssued> items = entry.getValue();
+            if (items.isEmpty())
+            {
+                continue;
+            }
+
+            BigDecimal power = BigDecimal.ZERO;
+            for (DataMediumTermPowerForecastIssued issued : items)
+            {
+                if (StringUtils.isEmpty(issued.getPower())) continue;
+                BigDecimal itemPower = new BigDecimal(issued.getPower());
+                if (itemPower.compareTo(power) > 0)
+                {
+                    power = itemPower;
+                }
+            }
+
+            MediumTermPowerForecastData data = new MediumTermPowerForecastData()
+                    .setTime(TimeUtils.utcToGmt8Format(entry.getKey()))
+                    .setPower(power.doubleValue());
+            data.setTheoreticalPower(0.0);
+            data.setCurtailmentPower(0.0);
+            result.add(data);
+        }
+
+        return result;
+    }
 }

+ 34 - 0
forecast-backend-server/src/main/java/com/ufo/project/forecast/mediumterm/power/domain/MediumTermPowerForecastData.java

@@ -0,0 +1,34 @@
+package com.ufo.project.forecast.mediumterm.power.domain;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * 中期功率预测业务数据
+ *
+ * @author ufo
+ */
+@Data
+@Accessors(chain = true)
+public class MediumTermPowerForecastData
+{
+    /** 时间(yyyy-MM-dd HH:mm) */
+    @ExcelProperty("预测时间")
+    private String time;
+
+    /** 预报发电功率(kw) */
+    @ExcelProperty("预测功率")
+    private Double power;
+
+    /** 无故障、无限电下的理论功率(kw) */
+    @ExcelProperty("理论功率")
+    @JsonProperty("theoretical_power")
+    private Double theoreticalPower;
+
+    /** 因调度指令限制的功率(KW) */
+    @ExcelProperty("限制功率")
+    @JsonProperty("curtailment_power")
+    private Double curtailmentPower;
+}