Pārlūkot izejas kodu

增加数据迁移功能

欧阳劲驰 3 nedēļas atpakaļ
vecāks
revīzija
405f82fe30

+ 8 - 0
pom.xml

@@ -334,6 +334,14 @@
 				</configuration>
 			</plugin>
 
+            <!--maven-surefire-->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <skipTests>true</skipTests>
+                </configuration>
+            </plugin>
 		</plugins>
 		<resources>
 			<resource>

+ 48 - 0
src/main/java/com/shkpr/service/aimodelpower/bizmgr/DataMigrateBizMgr.java

@@ -0,0 +1,48 @@
+package com.shkpr.service.aimodelpower.bizmgr;
+
+
+import com.shkpr.service.aimodelpower.commproperties.MigrateProperties;
+import com.shkpr.service.aimodelpower.components.DataMigrator;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * 数据迁移管理
+ *
+ * @author 欧阳劲驰
+ * @serial 1.0.4
+ */
+@Component
+public class DataMigrateBizMgr {
+    final
+    MigrateProperties migrateProperties;
+    final
+    ThreadPoolTaskExecutor taskScheduler;
+    final
+    DataMigrator dataMigrator;
+
+    public DataMigrateBizMgr(MigrateProperties migrateProperties
+            , @Qualifier("taskExecutor") ThreadPoolTaskExecutor taskScheduler, DataMigrator dataMigrator) {
+        this.migrateProperties = migrateProperties;
+        this.taskScheduler = taskScheduler;
+        this.dataMigrator = dataMigrator;
+    }
+
+    /**
+     * 初始化
+     */
+    @PostConstruct
+    public void init() {
+        if (CollectionUtils.isEmpty(migrateProperties.getOrgNames())) return;
+        //根据配置迁移
+        taskScheduler.execute(() ->
+                migrateProperties.getOrgNames().forEach(orgName ->
+                        dataMigrator.migrationWaterVolume((int) migrateProperties.getPrevious().toHours(), orgName)
+                )
+        );
+    }
+}

+ 6 - 6
src/main/java/com/shkpr/service/aimodelpower/bizmgr/PredictBizMgr.java

@@ -33,9 +33,9 @@ public class PredictBizMgr {
     public void init() {
         taskScheduler.execute(() -> {
             //预测水量
-            modelPredictor.predictDayWater(2);
-            modelPredictor.predictHourWater(2);
-            modelPredictor.predictMinuteWater(2);
+            modelPredictor.predictDayWater(0, 2, null);
+            modelPredictor.predictHourWater(0, 2, null);
+            modelPredictor.predictMinuteWater(0, 2, null);
             //预测小时泵,同步小时泵
             modelPredictor.predictHourPump(2);
             modelPredictor.syncHourPump(2);
@@ -48,7 +48,7 @@ public class PredictBizMgr {
     @Scheduled(cron = "0 */10 * * * *")
     public void minuteTask() {
         //预测分钟水量
-        taskScheduler.execute(() -> modelPredictor.predictMinuteWater(2));
+        taskScheduler.execute(() -> modelPredictor.predictMinuteWater(0, 2, null));
     }
 
     /**
@@ -58,7 +58,7 @@ public class PredictBizMgr {
     public void hourTask() {
         taskScheduler.execute(() -> {
             //预测小时水量
-            modelPredictor.predictHourWater(2);
+            modelPredictor.predictHourWater(0, 2, null);
             //预测小时泵,同步小时泵
             modelPredictor.predictHourPump(2);
             modelPredictor.syncHourPump(2);
@@ -72,7 +72,7 @@ public class PredictBizMgr {
     public void dayTask() {
         taskScheduler.execute(() -> {
             //预测日水量
-            modelPredictor.predictDayWater(2);
+            modelPredictor.predictDayWater(0, 2, null);
         });
     }
 

+ 10 - 10
src/main/java/com/shkpr/service/aimodelpower/bizmgr/WaterVolumeBizMgr.java

@@ -39,12 +39,12 @@ public class WaterVolumeBizMgr {
     public void init() {
         //采集4天数据
         if (collectProperties.getBusinessCollected()) {
-            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessMinuteData(4 * 24));
-            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessHourData(4 * 24));
+            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessMinuteData(4 * 24, null));
+            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessHourData(4 * 24, null));
         }
-        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceMinuteData(4 * 24));
-        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceHourData(4 * 24));
-        taskScheduler.execute(() -> waterVolumeCollector.collectRawData(4 * 24));
+        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceMinuteData(4 * 24, null));
+        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceHourData(4 * 24, null));
+        taskScheduler.execute(() -> waterVolumeCollector.collectRawData(4 * 24, null));
     }
 
     /**
@@ -54,9 +54,9 @@ public class WaterVolumeBizMgr {
     public void minuteTask() {
         //采集营业所1小时的分钟数据
         if (collectProperties.getBusinessCollected())
-            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessMinuteData(1));
+            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessMinuteData(1, null));
         //采集1小时的分钟数据
-        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceMinuteData(1));
+        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceMinuteData(1, null));
     }
 
     /**
@@ -66,9 +66,9 @@ public class WaterVolumeBizMgr {
     public void hourTask() {
         //采集营业所2小时的小时数据
         if (collectProperties.getBusinessCollected())
-            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessHourData(2));
+            taskScheduler.execute(() -> waterVolumeCollector.collectBusinessHourData(2, null));
         //采集2小时的小时数据
-        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceHourData(2));
+        taskScheduler.execute(() -> waterVolumeCollector.collectDifferenceHourData(2, null));
     }
 
     /**
@@ -77,6 +77,6 @@ public class WaterVolumeBizMgr {
     @Scheduled(cron = "0 5 0 * * ?")
     public void dayTask() {
         //采集次日的原始数据
-        taskScheduler.execute(() -> waterVolumeCollector.collectRawData(24));
+        taskScheduler.execute(() -> waterVolumeCollector.collectRawData(24, null));
     }
 }

+ 33 - 0
src/main/java/com/shkpr/service/aimodelpower/commproperties/MigrateProperties.java

@@ -0,0 +1,33 @@
+package com.shkpr.service.aimodelpower.commproperties;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 迁移属性
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.4
+ */
+@ConfigurationProperties("migrate")
+@Configuration
+@Getter
+@Setter
+@ToString
+public class MigrateProperties {
+    /**
+     * 回溯区间
+     */
+    private Duration previous = Duration.ofDays(30);
+    /**
+     * 迁移组织名称
+     */
+    private List<String> orgNames = new ArrayList<>();
+}

+ 97 - 0
src/main/java/com/shkpr/service/aimodelpower/components/DataMigrator.java

@@ -0,0 +1,97 @@
+package com.shkpr.service.aimodelpower.components;
+
+import com.global.base.log.LogLevelFlag;
+import com.global.base.log.LogPrintMgr;
+import com.shkpr.service.aimodelpower.commproperties.CollectProperties;
+import com.shkpr.service.aimodelpower.constants.LogFlagBusiType;
+import com.shkpr.service.aimodelpower.dbdao.services.intef.WaterVolumePredictService;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+/**
+ *
+ * 数据迁移器
+ *
+ * @author 欧阳劲驰
+ * @serial 1.0.4
+ */
+@Component
+public class DataMigrator {
+    /**
+     * log
+     */
+    private final static String mStrClassName = "DataMigrator";
+    private final static String mBizType = LogFlagBusiType.BUSI_DATA_COLLECT.toStrValue();
+    final
+    CollectProperties collectProperties;
+    final
+    WaterVolumeCollector waterVolumeCollector;
+    final
+    ModelPredictor modelPredictor;
+    final
+    WaterVolumePredictService waterVolumePredictService;
+
+    public DataMigrator(CollectProperties collectProperties, WaterVolumeCollector waterVolumeCollector, ModelPredictor modelPredictor, WaterVolumePredictService waterVolumePredictService) {
+        this.collectProperties = collectProperties;
+        this.waterVolumeCollector = waterVolumeCollector;
+        this.modelPredictor = modelPredictor;
+        this.waterVolumePredictService = waterVolumePredictService;
+    }
+
+    /**
+     * 迁移水量
+     *
+     * @param previousHours 往前回溯小时数
+     */
+    public void migrationWaterVolume(Integer previousHours, String orgName) {
+        if (StringUtils.isBlank(orgName)) return;
+
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                , String.format("开始执行迁移水量数据 组织名称:%s", orgName));
+        long begin = System.currentTimeMillis();
+
+        //采集分钟数据
+        waterVolumeCollector.collectDifferenceMinuteData(previousHours, orgName);
+        if (collectProperties.getBusinessCollected())
+            waterVolumeCollector.collectBusinessMinuteData(previousHours, orgName);
+        //采集小时数据
+        waterVolumeCollector.collectDifferenceHourData(previousHours, orgName);
+        if (collectProperties.getBusinessCollected())
+            waterVolumeCollector.collectBusinessHourData(previousHours, orgName);
+        //采集原始数据
+        waterVolumeCollector.collectRawData(previousHours, orgName);
+
+        //迁移预测库
+        Boolean predictMigrated = waterVolumePredictService.migrateData(orgName);
+        if (!predictMigrated) {
+            long end = System.currentTimeMillis();
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                    , String.format(
+                            "执行迁移水量数据失败,组织名称:%s,用时(毫秒):%d",
+                            orgName
+                            , (end - begin)
+                    )
+            );
+
+            return;
+        }
+
+        //训练模型
+        modelPredictor.trainDayWater();
+        modelPredictor.trainHourWater();
+
+        //预测数据
+        modelPredictor.predictMinuteWater(30, 2, orgName);
+        modelPredictor.predictDayWater(30, 2, orgName);
+        modelPredictor.predictMinuteWater(30, 2, orgName);
+
+        long end = System.currentTimeMillis();
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                , String.format(
+                        "结束执行迁移水量数据,组织名称:%s,用时(毫秒):%d",
+                        orgName
+                        , (end - begin)
+                )
+        );
+    }
+}

+ 29 - 16
src/main/java/com/shkpr/service/aimodelpower/components/ModelPredictor.java

@@ -9,7 +9,7 @@ import com.shkpr.service.aimodelpower.commtools.ModelPredictorUtil;
 import com.shkpr.service.aimodelpower.constants.LogFlagBusiType;
 import com.shkpr.service.aimodelpower.constants.ModelPredictorTaskStatus;
 import com.shkpr.service.aimodelpower.dbdao.services.intef.OrgConfigService;
-import com.shkpr.service.aimodelpower.dbdao.services.intef.WaterVolumePredictHourService;
+import com.shkpr.service.aimodelpower.dbdao.services.intef.WaterVolumePredictService;
 import com.shkpr.service.aimodelpower.dto.ModelPredictorResult;
 import com.shkpr.service.aimodelpower.dto.ModelPredictorTask;
 import com.shkpr.service.aimodelpower.dto.OrgConfig;
@@ -24,6 +24,7 @@ import org.springframework.stereotype.Component;
 
 import java.time.LocalDate;
 import java.util.List;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
@@ -73,29 +74,31 @@ public class ModelPredictor {
     final
     OrgConfigService orgConfigService;
     final
-    WaterVolumePredictHourService waterVolumePredictHourService;
+    WaterVolumePredictService waterVolumePredictService;
     final
     AsyncRequestUtil asyncRequestUtil;
 
     public ModelPredictor(@Qualifier("taskExecutor") ThreadPoolTaskExecutor taskScheduler, ObjectMapper objectMapper
             , PredictProperties predictProperties, OrgConfigService orgConfigService
-            , WaterVolumePredictHourService waterVolumePredictHourService, AsyncRequestUtil asyncRequestUtil) {
+            , WaterVolumePredictService waterVolumePredictService, AsyncRequestUtil asyncRequestUtil) {
         this.taskScheduler = taskScheduler;
         this.objectMapper = objectMapper;
         this.predictProperties = predictProperties;
         this.orgConfigService = orgConfigService;
-        this.waterVolumePredictHourService = waterVolumePredictHourService;
+        this.waterVolumePredictService = waterVolumePredictService;
         this.asyncRequestUtil = asyncRequestUtil;
     }
 
     /**
      * 预测日水量
      *
-     * @param nextDays 往后预测天数
+     * @param previousDays 往前回溯天数
+     * @param nextDays     往后预测天数
+     * @param orgName      组织名称
      */
-    public void predictDayWater(int nextDays) {
+    public void predictDayWater(int previousDays, int nextDays, String orgName) {
         //计算开始和结束日期
-        LocalDate startTime = LocalDate.now();
+        LocalDate startTime = LocalDate.now().minusDays(previousDays);
         LocalDate endTime = LocalDate.now().plusDays(nextDays);
 
         //关闭的组织名称
@@ -108,6 +111,8 @@ public class ModelPredictor {
 
         //遍历组织配置
         orgConfigs.forEach(orgConfig -> {
+            if (orgName != null && !Objects.equals(orgConfig.getOrgName(), orgName)) return;
+
             long begin = System.currentTimeMillis();
 
             //请求参数
@@ -149,11 +154,13 @@ public class ModelPredictor {
     /**
      * 预测小时水量
      *
-     * @param nextDays 往后预测天数
+     * @param previousDays 往前回溯天数
+     * @param nextDays     往后预测天数
+     * @param orgName      组织名称
      */
-    public void predictHourWater(int nextDays) {
+    public void predictHourWater(int previousDays, int nextDays, String orgName) {
         //计算开始日期
-        LocalDate startTime = LocalDate.now();
+        LocalDate startTime = LocalDate.now().minusDays(previousDays);
 
         //关闭的组织名称
         List<String> closeOrgNames = predictProperties.getCloseOrgNames();
@@ -165,6 +172,8 @@ public class ModelPredictor {
 
         //遍历组织配置
         for (OrgConfig orgConfig : orgConfigs) {
+            if (orgName != null && !Objects.equals(orgConfig.getOrgName(), orgName)) continue;
+
             //遍历预测天数
             for (int i = 0; i <= nextDays; i++) {
                 long begin = System.currentTimeMillis();
@@ -212,11 +221,13 @@ public class ModelPredictor {
     /**
      * 预测分钟水量
      *
-     * @param nextDays 往后预测天数
+     * @param previousDays 往前回溯天数
+     * @param nextDays     往后预测天数
+     * @param orgName      组织名称
      */
-    public void predictMinuteWater(int nextDays) {
+    public void predictMinuteWater(int previousDays, int nextDays, String orgName) {
         //计算开始和结束日期
-        LocalDate startTime = LocalDate.now();
+        LocalDate startTime = LocalDate.now().minusDays(previousDays);
         LocalDate endTime = LocalDate.now().plusDays(nextDays);
 
         //关闭的组织名称
@@ -229,18 +240,20 @@ public class ModelPredictor {
 
         //遍历组织配置
         for (OrgConfig orgConfig : orgConfigs) {
+            if (orgName != null && !Objects.equals(orgConfig.getOrgName(), orgName)) continue;
+
             LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
                     , String.format("开始执行预测分钟水量 组织名称:%s,开始日期:%s,结束日期:%s",
                             orgConfig.getOrgName(), startTime, endTime));
             long begin = System.currentTimeMillis();
 
             //读取小时预测数据
-            List<WaterVolumePredictHour> hours = waterVolumePredictHourService.findHour(startTime, endTime, orgConfig.getOrgId());
+            List<WaterVolumePredictHour> hours = waterVolumePredictService.findHour(startTime, endTime, orgConfig.getOrgId());
             //计算分钟预测数据
             List<WaterVolumePredictHour> minutes = ModelPredictorUtil.calcMinuteWater(hours);
 
             //执行批量合并
-            Boolean upserted = waterVolumePredictHourService.upsertAllMinute(minutes);
+            Boolean upserted = waterVolumePredictService.upsertAllMinute(minutes);
 
             long end = System.currentTimeMillis();
             LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
@@ -437,7 +450,7 @@ public class ModelPredictor {
         long begin = System.currentTimeMillis();
 
         //执行同步
-        Boolean synced = waterVolumePredictHourService.syncStatusAndEnergy(startTime, endTime);
+        Boolean synced = waterVolumePredictService.syncStatusAndEnergy(startTime, endTime);
 
         long end = System.currentTimeMillis();
         LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName

+ 16 - 5
src/main/java/com/shkpr/service/aimodelpower/components/WaterVolumeCollector.java

@@ -25,6 +25,7 @@ import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
@@ -86,8 +87,9 @@ public class WaterVolumeCollector {
      * 采集营业所小时数据
      *
      * @param previousHours 往前回溯小时数
+     * @param orgName       组织名称
      */
-    public void collectBusinessHourData(int previousHours) {
+    public void collectBusinessHourData(int previousHours, String orgName) {
         //自供标签配置
         Map<String, List<String>> selfTagsMap = collectProperties.getSupplySelfTagsMap();
         //供入标签配置
@@ -101,6 +103,7 @@ public class WaterVolumeCollector {
 
         //遍历自供标签
         for (CollectProperties.CollectTagAttr collectTag : collectProperties.getSupplySelfTags()) {
+            if (orgName != null && !Objects.equals(collectTag.getName(), orgName)) continue;
             taskScheduler.execute(() -> {
                 //采集标签
                 List<String> selfTags = selfTagsMap.get(collectTag.getName());
@@ -169,8 +172,9 @@ public class WaterVolumeCollector {
      * 采集营业所分钟数据
      *
      * @param previousHours 往前回溯小时数
+     * @param orgName       组织名称
      */
-    public void collectBusinessMinuteData(int previousHours) {
+    public void collectBusinessMinuteData(int previousHours, String orgName) {
         //自供标签配置
         Map<String, List<String>> selfTagsMap = collectProperties.getSupplySelfTagsMap();
         //供入标签配置
@@ -188,6 +192,7 @@ public class WaterVolumeCollector {
 
         //遍历自供标签
         for (CollectProperties.CollectTagAttr collectTag : collectProperties.getSupplySelfTags()) {
+            if (orgName != null && !Objects.equals(collectTag.getName(), orgName)) continue;
             taskScheduler.execute(() -> {
                 //采集标签
                 List<String> selfTags = selfTagsMap.get(collectTag.getName());
@@ -260,8 +265,9 @@ public class WaterVolumeCollector {
      * 采集差值小时数据
      *
      * @param previousHours 往前回溯小时数
+     * @param orgName       组织名称
      */
-    public void collectDifferenceHourData(int previousHours) {
+    public void collectDifferenceHourData(int previousHours, String orgName) {
         //计算开始和结束时间,开始时间整点回溯小时,结束时间整点
         final LocalDateTime startTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS).minusHours(previousHours);
         final LocalDateTime endTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
@@ -278,6 +284,7 @@ public class WaterVolumeCollector {
 
         //遍历采集配置
         for (Map.Entry<String, List<WaterVolumeCollectionConfig>> config : configs.entrySet()) {
+            if (orgName != null && !Objects.equals(config.getKey(), orgName)) continue;
             taskScheduler.execute(() -> {
                 //采集标签
                 List<String> tags = config.getValue().stream()
@@ -350,8 +357,9 @@ public class WaterVolumeCollector {
      * 采集差值分钟数据
      *
      * @param previousHours 往前回溯小时数
+     * @param orgName       组织名称
      */
-    public void collectDifferenceMinuteData(int previousHours) {
+    public void collectDifferenceMinuteData(int previousHours, String orgName) {
         //计算开始和结束时间
         final LocalDateTime now = LocalDateTime.now();
         final int nowMinute = now.getMinute();
@@ -372,6 +380,7 @@ public class WaterVolumeCollector {
 
         //遍历采集配置
         for (Map.Entry<String, List<WaterVolumeCollectionConfig>> config : configs.entrySet()) {
+            if (orgName != null && !Objects.equals(config.getKey(), orgName)) continue;
             taskScheduler.execute(() -> {
                 //采集标签
                 List<String> tags = config.getValue().stream()
@@ -449,8 +458,9 @@ public class WaterVolumeCollector {
      * 采集原始数据
      *
      * @param previousHours 往前回溯小时数
+     * @param orgName       组织名称
      */
-    public void collectRawData(int previousHours) {
+    public void collectRawData(int previousHours, String orgName) {
         //计算开始和结束时间,开始时间整日回溯小时
         final LocalDateTime startTime = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusHours(previousHours);
         final LocalDateTime endTime = LocalDateTime.now();
@@ -467,6 +477,7 @@ public class WaterVolumeCollector {
 
         //遍历采集配置
         for (Map.Entry<String, List<WaterVolumeCollectionConfig>> config : configs.entrySet()) {
+            if (orgName != null && !Objects.equals(config.getKey(), orgName)) continue;
             taskScheduler.execute(() -> {
                 //采集标签
                 List<String> tags = config.getValue().stream()

+ 15 - 1
src/main/java/com/shkpr/service/aimodelpower/dbdao/mapperwatervolume/WaterVolumePredictHourMapper.java

@@ -12,7 +12,7 @@ import org.apache.ibatis.cursor.Cursor;
  * @serial 1.0.4
  */
 @Mapper
-public interface WaterVolumePredictHourMapper {
+public interface WaterVolumePredictMapper {
     /**
      * 合并操作分钟数据
      *
@@ -31,6 +31,20 @@ public interface WaterVolumePredictHourMapper {
     int syncStatusAndEnergyByTime(@Param("beginDate") String beginDate, @Param("endDate") String endDate);
 
     /**
+     * 迁移日数据
+     * @param orgName 组织名称
+     * @return 迁移量
+     */
+    int migrateDay(@Param("orgName") String orgName);
+
+    /**
+     * 迁移小时数据
+     * @param orgName 组织名称
+     * @return 迁移量
+     */
+    int migrateHour(@Param("orgName") String orgName);
+
+    /**
      * 根据时间和组织id查询小时数据
      *
      * @param beginDate 开始时间

+ 22 - 9
src/main/java/com/shkpr/service/aimodelpower/dbdao/services/WaterVolumePredictHourServiceImpl.java

@@ -3,8 +3,8 @@ package com.shkpr.service.aimodelpower.dbdao.services;
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
 import com.shkpr.service.aimodelpower.constants.LogFlagBusiType;
-import com.shkpr.service.aimodelpower.dbdao.mapperwatervolume.WaterVolumePredictHourMapper;
-import com.shkpr.service.aimodelpower.dbdao.services.intef.WaterVolumePredictHourService;
+import com.shkpr.service.aimodelpower.dbdao.mapperwatervolume.WaterVolumePredictMapper;
+import com.shkpr.service.aimodelpower.dbdao.services.intef.WaterVolumePredictService;
 import com.shkpr.service.aimodelpower.dto.WaterVolumePredictHour;
 import org.apache.ibatis.cursor.Cursor;
 import org.apache.ibatis.session.ExecutorType;
@@ -28,7 +28,7 @@ import java.util.stream.Collectors;
  * @serial 1.0.4
  */
 @Service
-public class WaterVolumePredictHourServiceImpl implements WaterVolumePredictHourService {
+public class WaterVolumePredictServiceImpl implements WaterVolumePredictService {
     /**
      * log
      */
@@ -38,11 +38,11 @@ public class WaterVolumePredictHourServiceImpl implements WaterVolumePredictHour
     final
     SqlSessionFactory childSqlSessionFactory;
     final
-    WaterVolumePredictHourMapper waterVolumePredictHourMapper;
+    WaterVolumePredictMapper waterVolumePredictMapper;
 
-    public WaterVolumePredictHourServiceImpl(@Qualifier("childSqlSessionFactory") SqlSessionFactory childSqlSessionFactory, WaterVolumePredictHourMapper waterVolumePredictHourMapper) {
+    public WaterVolumePredictServiceImpl(@Qualifier("childSqlSessionFactory") SqlSessionFactory childSqlSessionFactory, WaterVolumePredictMapper waterVolumePredictMapper) {
         this.childSqlSessionFactory = childSqlSessionFactory;
-        this.waterVolumePredictHourMapper = waterVolumePredictHourMapper;
+        this.waterVolumePredictMapper = waterVolumePredictMapper;
     }
 
     /**
@@ -69,7 +69,7 @@ public class WaterVolumePredictHourServiceImpl implements WaterVolumePredictHour
                 conn.setAutoCommit(false);
 
                 //从session获取mapper
-                WaterVolumePredictHourMapper mapper = sqlSession.getMapper(WaterVolumePredictHourMapper.class);
+                WaterVolumePredictMapper mapper = sqlSession.getMapper(WaterVolumePredictMapper.class);
 
                 //批量合并
                 records.forEach(mapper::upsertMinute);
@@ -113,7 +113,20 @@ public class WaterVolumePredictHourServiceImpl implements WaterVolumePredictHour
      */
     @Override
     public Boolean syncStatusAndEnergy(LocalDate beginDate, LocalDate endDate) {
-        return waterVolumePredictHourMapper.syncStatusAndEnergyByTime(beginDate.toString(), endDate.toString()) > 0;
+        return waterVolumePredictMapper.syncStatusAndEnergyByTime(beginDate.toString(), endDate.toString()) > 0;
+    }
+
+    /**
+     * 迁移数据
+     *
+     * @param orgName 组织名称
+     * @return 迁移状态
+     */
+    @Override
+    public Boolean migrateData(String orgName) {
+        int dayMigrated = waterVolumePredictMapper.migrateDay(orgName);
+        int hourMigrated = waterVolumePredictMapper.migrateHour(orgName);
+        return dayMigrated + hourMigrated > 0;
     }
 
     /**
@@ -128,7 +141,7 @@ public class WaterVolumePredictHourServiceImpl implements WaterVolumePredictHour
     public List<WaterVolumePredictHour> findHour(LocalDate beginDate, LocalDate endDate, String orgId) {
         //获取session和mapper
         try (SqlSession session = childSqlSessionFactory.openSession()) {
-            WaterVolumePredictHourMapper mapper = session.getMapper(WaterVolumePredictHourMapper.class);
+            WaterVolumePredictMapper mapper = session.getMapper(WaterVolumePredictMapper.class);
 
             //数据集合
             List<WaterVolumePredictHour> dates = new ArrayList<>();

+ 12 - 4
src/main/java/com/shkpr/service/aimodelpower/dbdao/services/intef/WaterVolumePredictHourService.java

@@ -6,12 +6,12 @@ import java.time.LocalDate;
 import java.util.List;
 
 /**
- * 水量预测小时service实现
+ * 水量预测service实现
  *
  * @author 欧阳劲驰
  * @serial 1.0.4
  */
-public interface WaterVolumePredictHourService {
+public interface WaterVolumePredictService {
     /**
      * 批量合并操作分钟数据
      *
@@ -21,15 +21,23 @@ public interface WaterVolumePredictHourService {
     Boolean upsertAllMinute(List<WaterVolumePredictHour> records);
 
     /**
-     * 根据时间同步状态和电量
+     * 同步状态和电量
      *
      * @param beginDate 开始时间
      * @param endDate   结束时间
-     * @return 同步数量
+     * @return 同步状态
      */
     Boolean syncStatusAndEnergy(LocalDate beginDate, LocalDate endDate);
 
     /**
+     * 迁移数据
+     *
+     * @param orgName 组织名称
+     * @return 迁移状态
+     */
+    Boolean migrateData(String orgName);
+
+    /**
      * 查询小时数据
      *
      * @param beginDate 开始时间

+ 2 - 0
src/main/resources/application.properties

@@ -210,3 +210,5 @@ predict.connect-timeout=30000
 predict.read-timeout=30000
 #关闭组织:井口水厂,丰收坝水厂,沙坪坝水厂,北碚红工水厂,大溪沟水厂
 predict.close-org-names=\u4e95\u53e3\u6c34\u5382,\u4e30\u6536\u575d\u6c34\u5382,\u6c99\u576a\u575d\u6c34\u5382,\u5317\u789a\u7ea2\u5de5\u6c34\u5382,\u5927\u6eaa\u6c9f\u6c34\u5382
+#====================迁移配置====================
+migrate.previous=P365D

+ 35 - 1
src/main/resources/mapper/WaterVolumePredictHourMapper.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="com.shkpr.service.aimodelpower.dbdao.mapperwatervolume.WaterVolumePredictHourMapper">
+<mapper namespace="com.shkpr.service.aimodelpower.dbdao.mapperwatervolume.WaterVolumePredictMapper">
     <insert id="upsertMinute">
         INSERT INTO tb_m_hourwater_new ("Date",
                                         "Hour",
@@ -70,4 +70,38 @@
           and "Date"::date &gt;= #{beginDate,jdbcType=VARCHAR}::date
           and "Date"::date &lt; #{endDate,jdbcType=VARCHAR}::date
     </select>
+
+    <insert id="migrateDay">
+        insert into tb_m_water("Month", "Week", "Date", "LastActualWaterSupply", "ActualWaterSupply", "isAbnormal",
+                               "isForecast", "LastModifyTime", "orgId")
+        select date_part('month', to_date(d.day, 'yyyy-mm-dd'))::int  as "Month",
+               DATE_PART('isodow', to_date(d.day, 'yyyy-mm-dd'))::int as "Week",
+               to_date(d.day, 'yyyy-mm-dd')                           as "Date",
+               LAG(d.value) over (order by d.day)                     as "LastActualWaterSupply",
+               value                                                  as "ActualWaterSupply",
+               0                                                      as "isAbnormal",
+               0                                                      as "isForecast",
+               now()                                                  as "LastModifyTime",
+               d.org_id                                               as "orgId"
+        from (select to_char(to_date(wcr.time, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd') as day,
+                     sum(wcr.value::numeric)                                           as value,
+                     woc.org_id                                                        as org_id
+              from water_collecation_record wcr
+                       left join water_org_config woc on wcr.org_name = woc.org_name
+              where wcr.org_name = #{orgName,jdbcType=VARCHAR}
+              group by wcr.org_name, to_char(to_date(wcr.time, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd')) d
+        ORDER BY "Date";
+    </insert>
+
+    <insert id="migrateHour">
+        insert into tb_m_hourwater("Date", "Hour", "HourActualWaterSupply", "orgId")
+        selecT wcra.time::date     as "Date",
+               wcra.time::time     as "Hour",
+               wcra.value::numeric as "HourActualWaterSupply",
+               woc.org_id          as "orgId"
+        from water_collecation_record_all wcra
+                 left join water_org_config woc on wcra.org_name = woc.org_name
+        where wcra.org_name = #{orgName,jdbcType=VARCHAR}
+        order by wcra.time;
+    </insert>
 </mapper>