Jelajahi Sumber

[101903] 上报处理器接口/工厂/抽象基类

taosj 2 minggu lalu
induk
melakukan
2994f745c8

+ 91 - 0
forecast-backend-server/src/main/java/com/ufo/project/forecast/processor/AbstractForecastProcessor.java

@@ -0,0 +1,91 @@
+package com.ufo.project.forecast.processor;
+
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.ufo.project.config.domain.ConfigStation;
+import com.ufo.project.config.service.IConfigStationService;
+import com.ufo.project.forecast.fileupload.domain.DataPowerForecastReportBatch;
+import com.ufo.project.forecast.fileupload.service.IDataPowerForecastReportBatchService;
+
+/**
+ * 功率预测上报处理器抽象基类(模板方法:窗口计算 + 批次行遍历,单场站异常隔离)
+ *
+ * @author ufo
+ * @date 2026-10-19
+ */
+public abstract class AbstractForecastProcessor implements ForecastProcessor
+{
+    protected static final ZoneId ZONE_SH = ZoneId.of("Asia/Shanghai");
+
+    protected static final int INSERT_CHUNK = 500;
+
+    protected static final Logger log = LoggerFactory.getLogger(AbstractForecastProcessor.class);
+
+    @Autowired
+    protected IDataPowerForecastReportBatchService reportBatchService;
+
+    @Autowired
+    protected IConfigStationService configStationService;
+
+    @Override
+    public void initData(int day, String time)
+    {
+        LocalDate d = LocalDate.parse(String.valueOf(day), DateTimeFormatter.ofPattern("yyyyMMdd"));
+        long windowStart = d.atStartOfDay().atZone(ZONE_SH).toEpochSecond();
+        long windowEnd = d.atTime(LocalTime.parse(time)).atZone(ZONE_SH).toEpochSecond();
+        List<DataPowerForecastReportBatch> batches = reportBatchService.selectByTypeDayTime(getType(), day, time);
+        if (batches == null || batches.isEmpty())
+        {
+            log.info("[PowerForecastReportBatch] 无上报批次可处理, type={}, day={}, time={}", getType(), day, time);
+            return;
+        }
+        for (DataPowerForecastReportBatch batch : batches)
+        {
+            try
+            {
+                initStation(batch, windowStart, windowEnd);
+            }
+            catch (Exception e)
+            {
+                log.error("[PowerForecastReportBatch] 单场站初始化失败, type={}, stationCode={}, batchNo={}",
+                        getType(), batch.getStationCode(), batch.getBatchNo(), e);
+            }
+        }
+    }
+
+    /**
+     * 初始化单场站上报数据(窗口内源表取数合并写入 report 表)
+     *
+     * @param batch 上报批次行
+     * @param windowStart 窗口起点(unix秒, 不含)
+     * @param windowEnd 窗口终点(unix秒, 含)
+     */
+    protected abstract void initStation(DataPowerForecastReportBatch batch, long windowStart, long windowEnd);
+
+    /**
+     * 按场站编码解析 entityId
+     *
+     * @param stationCode 场站编码
+     * @return entityId, 场站查无时返回 null
+     */
+    protected String resolveEntityId(String stationCode)
+    {
+        ConfigStation q = new ConfigStation();
+        q.setStationCode(stationCode);
+        List<ConfigStation> list = configStationService.selectConfigStationList(q);
+        if (list == null || list.isEmpty())
+        {
+            log.error("[PowerForecastReportBatch] 未找到场站, stationCode={}", stationCode);
+            return null;
+        }
+        return String.valueOf(list.get(0).getId());
+    }
+}

+ 25 - 0
forecast-backend-server/src/main/java/com/ufo/project/forecast/processor/ForecastProcessor.java

@@ -0,0 +1,25 @@
+package com.ufo.project.forecast.processor;
+
+/**
+ * 功率预测上报处理器接口
+ *
+ * @author ufo
+ * @date 2026-10-19
+ */
+public interface ForecastProcessor
+{
+    /**
+     * 获取上报类型
+     *
+     * @return 上报类型(short短期, ultra超短期)
+     */
+    String getType();
+
+    /**
+     * 初始化指定上报日/上报时间的上报数据(源表取数写入 report 表)
+     *
+     * @param day 上报日(yyyyMMdd, 北京时间)
+     * @param time 上报时间(HH:mm:ss, 北京时间)
+     */
+    void initData(int day, String time);
+}

+ 37 - 0
forecast-backend-server/src/main/java/com/ufo/project/forecast/processor/ForecastProcessorFactory.java

@@ -0,0 +1,37 @@
+package com.ufo.project.forecast.processor;
+
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 功率预测上报处理器工厂
+ *
+ * @author ufo
+ * @date 2026-10-19
+ */
+@Component
+public class ForecastProcessorFactory
+{
+    @Autowired
+    private List<ForecastProcessor> processors;
+
+    /**
+     * 按类型获取上报处理器
+     *
+     * @param type 上报类型(short短期, ultra超短期)
+     * @return 上报处理器
+     */
+    public ForecastProcessor getInstant(String type)
+    {
+        for (ForecastProcessor processor : processors)
+        {
+            if (processor.getType().equals(type))
+            {
+                return processor;
+            }
+        }
+        throw new RuntimeException("未找到 type=" + type + " 的上报处理器");
+    }
+}