From 9bcefe6dd1c1634c48f5eb1a7e6fb13c8a2ac8ce Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期三, 02 四月 2025 00:20:04 +0800
Subject: [PATCH] 如果小于天粒度,则从下个时间粒度开始

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java |   63 ++++++++++++++++++++++++-------
 1 files changed, 49 insertions(+), 14 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java
index 16447f5..d8548a3 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java
@@ -1,16 +1,19 @@
 package com.iailab.module.model.mdk.predict;
 
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.iailab.module.model.common.enums.CommonDict;
 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
+import com.iailab.module.model.mcs.pre.enums.PredGranularityEnum;
 import com.iailab.module.model.mcs.pre.service.MmItemResultService;
+import com.iailab.module.model.mcs.sche.service.StAdjustResultService;
 import com.iailab.module.model.mdk.factory.ItemEntityFactory;
 import com.iailab.module.model.mdk.vo.DataValueVO;
 import com.iailab.module.model.mdk.vo.PredictResultVO;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+import java.math.BigDecimal;
 import java.util.*;
 
 @Service
@@ -21,6 +24,9 @@
     @Autowired
     private ItemEntityFactory itemEntityFactory;
 
+    @Autowired
+    private StAdjustResultService stAdjustResultService;
+
     /**
      * convertToPredictData
      *
@@ -29,19 +35,18 @@
      */
     public Map<String, List<DataValueVO>> convertToPredictData(PredictResultVO predictResult) {
         Map<String, List<DataValueVO>> resultMap = new HashMap<>();
-//        List<MmItemOutputEntity> itemOutPutList = itemEntityFactory.getOutPutByItemId(predictResult.getPredictId());
-//
-//        if (!CollectionUtils.isEmpty(predictResult.getPredictList())) {
-//            resultMap.put(itemOutPutList.get(0).getId(), predictResult.getPredictList());
-//            return resultMap;
-//        }
-        Map<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs();
-        HashMap<String,List<DataValueVO>> predictLists = new HashMap<>();
-        for (Map.Entry<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) {
+        Map<MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs();
+        HashMap<String, List<DataValueVO>> predictLists = new HashMap<>();
+        for (Map.Entry<MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) {
             Integer rows = entry.getValue().length;
             List<DataValueVO> predictDataList = new ArrayList<>();
             Calendar calendar = Calendar.getInstance();
             calendar.setTime(predictResult.getPredictTime());
+
+            if (predictResult.getGranularity() < PredGranularityEnum.D1.getCode()) {
+                // 如果小于天粒度,则从下个时间粒度开始
+                calendar.add(Calendar.SECOND, predictResult.getGranularity());
+            }
             for (Integer i = 0; i < rows; i++) {
                 DataValueVO predictData = new DataValueVO();
                 predictData.setDataTime(calendar.getTime());
@@ -52,6 +57,23 @@
             }
             resultMap.put(entry.getKey().getId(), predictDataList);
             predictLists.put(entry.getKey().getResultstr(), predictDataList);
+
+            //处理累计计算
+            if (entry.getKey().getIscumulant() == 1) {
+                resultMap.put(entry.getKey().getId() + CommonDict.CUMULANT_SUFFIX, new ArrayList<DataValueVO>() {{
+                    DataValueVO predictData = new DataValueVO();
+                    // 时间 预测时间+预测长度*粒度
+                    Calendar calendar = Calendar.getInstance();
+                    calendar.setTime(predictResult.getPredictTime());
+                    calendar.add(Calendar.SECOND, predictResult.getGranularity() * rows);
+                    predictData.setDataTime(calendar.getTime());
+                    //值 所有值相加/除数
+                    BigDecimal sum = BigDecimal.valueOf(Arrays.stream(entry.getValue()).sum());
+                    BigDecimal divisor = BigDecimal.valueOf(entry.getKey().getCumuldivisor());
+                    predictData.setDataValue(sum.divide(divisor, 2, BigDecimal.ROUND_HALF_UP).doubleValue());
+                    add(predictData);
+                }});
+            }
         }
         predictResult.setPredictLists(predictLists);
         return resultMap;
@@ -62,19 +84,20 @@
         if (!CollectionUtils.isEmpty(predictResult.getPredictList())) {
             return predictLists;
         }
-        Map<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs();
-        for (Map.Entry<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) {
+        Map<MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs();
+        for (Map.Entry<MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) {
             Integer rows = entry.getValue().length;
             List<DataValueVO> predictDataList = new ArrayList<>();
             Calendar calendar = Calendar.getInstance();
             calendar.setTime(predictResult.getPredictTime());
             for (Integer i = 0; i < rows; i++) {
+                // 从下个时间粒度开始
+                calendar.add(Calendar.SECOND, predictResult.getGranularity());
+
                 DataValueVO predictData = new DataValueVO();
                 predictData.setDataTime(calendar.getTime());
                 predictData.setDataValue(Double.valueOf(entry.getValue()[i]));
                 predictDataList.add(predictData);
-
-                calendar.add(Calendar.SECOND, predictResult.getGranularity());
             }
             predictLists.put(entry.getKey().getResultstr(), predictDataList);
         }
@@ -92,6 +115,18 @@
         mmItemResultService.savePredictValue(resultMap, predictResult.getLt(), "n", predictResult.getPredictTime());
     }
 
+    /**
+     * savePredictAdjustResult
+     *
+     * @param predictResult
+     * @param configId
+     */
+    @DSTransactional
+    public void savePredictAdjustResult(PredictResultVO predictResult, String adjustValue, String scheduleModelId, String configId) {
+        Map<String, List<DataValueVO>> resultMap = convertToPredictData(predictResult);
+        stAdjustResultService.saveResult(resultMap, predictResult.getPredictTime(), adjustValue, scheduleModelId,configId);
+    }
+
     public List<DataValueVO> getPredictValueByItemNo(String itemNo, Date start, Date end) {
         String itemId = itemEntityFactory.getItemByItemNo(itemNo).getId();
         List<MmItemOutputEntity> outputList = itemEntityFactory.getOutPutByItemId(itemId);

--
Gitblit v1.9.3