ソースを参照

解析云端嘉越DQ文件

xusl 1 年間 前
コミット
69902bac16

+ 9 - 1
ipp-ap/src/const/crud/parsingLog.js

@@ -24,11 +24,19 @@ export const tableOption = {
       "label": "文件名称",
       "prop": "fileName"
     }
+    // , {
+    //   "type": "input",
+    //   "label": "解析时间",
+    //   "prop": "parsingTime"
+    // }
     , {
-      "type": "input",
+      "type": "datetime",
       "label": "解析时间",
+      "format": 'yyyy-MM-dd HH:mm:ss',
+      "valueFormat": 'yyyy-MM-dd HH:mm:ss',
       "prop": "parsingTime"
     }
+
     , {
       "type": "input",
       "label": "文件类型",

+ 1 - 0
ipp-idp/src/main/java/com/jiayue/ipp/idp/job/ParsingJob.java

@@ -51,6 +51,7 @@ public class ParsingJob {
 //                        downloadService.v3DataFilesDownload(parsingChannel);
                         break;
                     case "E3":
+                        // 解析自己家的云端预测文件
                         downloadService.parsing(parsingChannel);
                         break;
                     default:

+ 141 - 30
ipp-idp/src/main/java/com/jiayue/ipp/idp/service/an/DownloadService.java

@@ -1,21 +1,37 @@
 package com.jiayue.ipp.idp.service.an;
 
+import cn.hutool.core.date.DateUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
+import com.jiayue.ipfcst.common.core.util.DateTimeUtil;
+import com.jiayue.ipp.common.data.entity.ForecastPowerShortTerm;
+import com.jiayue.ipp.common.data.entity.ForecastPowerShortTermHis;
 import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
 import com.jiayue.ipp.common.data.entity.an.ParsingLog;
 import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
 import com.jiayue.ipp.idp.dto.ParsingConstant;
+import com.jiayue.ipp.idp.service.ForecastPowerShortTermHisService;
+import com.jiayue.ipp.idp.service.ForecastPowerShortTermService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.time.DateFormatUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * ftp通信业务层
@@ -35,6 +51,10 @@ public class DownloadService {
 
     private final ParsingLogService parsingLogService;
 
+    private final ForecastPowerShortTermHisService forecastPowerShortTermHisService;
+
+    private final ForecastPowerShortTermService forecastPowerShortTermService;
+
     /**
      * 解析
      *
@@ -56,6 +76,7 @@ public class DownloadService {
             String path = parsingUrl.getUrl() + File.separator + "new";
 
             File dirFile = new File(path);
+
             if (!dirFile.exists()) {
                 dirFile.mkdirs();
             }
@@ -71,47 +92,137 @@ public class DownloadService {
 
                 if (!fileName.contains(dayStr)) {
                     file.delete();
-                    log.warn(fileName + "不是当天的文件,删除!");
+                    log.warn(fileName + "云端文件不是当天的文件,删除!");
                     break;
                 }
 
-                //定义解析的类型,默认为错误(未知),成功后为文件类型,也会作为存储目录名
-                String type = parsingFileService.parsingFile(file, parsingUrl);
-
-                if (!type.startsWith(ParsingConstant.FAIL)) {
-
+                // 如果是短期文件
+                if (file.getName().startsWith("DQ")) {
                     try {
-                        File file1 = new File(success + File.separator + fileName);
-                        if(file1.exists()){
-                            file1.delete();
+                        // 解析短期文件
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        List<ForecastPowerShortTerm> forecastPowerShortTermList = new ArrayList<>();
+                        List<ForecastPowerShortTermHis> forecastPowerShortTermHisList = new ArrayList<>();
+                        // 当文件未被使用时,进行解析上报
+                        if (file.renameTo(file)) {
+                            InputStreamReader read = null;
+                            BufferedReader bufferedReader = null;
+                            String stringLine;
+                            ForecastPowerShortTerm stf;
+                            try {
+                                read = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
+                                bufferedReader = new BufferedReader(read);
+                                Long systemTime = System.currentTimeMillis();
+                                // 循环解析文件
+                                while ((stringLine = bufferedReader.readLine()) != null) {
+                                    String[] string_arr = stringLine.split("\t");
+                                    if (string_arr.length == 4 && string_arr[0].startsWith("#")) {
+                                        if (StringUtils.isNotEmpty(string_arr[2])) {
+                                            // 实时表
+                                            stf = new ForecastPowerShortTerm();
+                                            stf.setFpValue(new BigDecimal(string_arr[3]));
+                                            stf.setForecastTime(sdf.parse(string_arr[2]));
+                                            stf.setGenDate(new Date());
+                                            stf.setStationCode(parsingUrl.getStationCode());
+                                            stf.setForecastManufactor("SYJY");
+                                            forecastPowerShortTermList.add(stf);
+
+                                            // 历史表
+                                            //过滤当天的数据
+                                            long secondDayTime = com.jiayue.ipp.idp.util.DateMomentUtil.getDayStartTime(systemTime)+1000*60*60*24;
+                                            if (sdf.parse(string_arr[2]).getTime() >= secondDayTime) {
+                                                ForecastPowerShortTermHis forecastPowerShortTermHis = new ForecastPowerShortTermHis();
+                                                forecastPowerShortTermHis.setGenDate(new Date());
+                                                forecastPowerShortTermHis.setStationCode(parsingUrl.getStationCode());
+                                                forecastPowerShortTermHis.setAbleValue(new BigDecimal(string_arr[3]));
+                                                forecastPowerShortTermHis.setForecastTime(sdf.parse(string_arr[2]));
+                                                forecastPowerShortTermHis.setForecastHowLongAgo(DateMomentUtil.getDaysBetweenTwoDate(systemTime,sdf.parse(string_arr[2]).getTime()));
+                                                forecastPowerShortTermHis.setForecastManufactor("SYJY");
+                                                forecastPowerShortTermHisList.add(forecastPowerShortTermHis);
+                                            }
+                                        }
+                                    }
+                                }
+
+                                if (forecastPowerShortTermList.size()>0 && forecastPowerShortTermHisList.size()>0){
+                                    // 保存短期实时
+                                    Date startTime = forecastPowerShortTermList.get(0).getForecastTime();
+                                    Date endTime = forecastPowerShortTermList.get(forecastPowerShortTermList.size() - 1).getForecastTime();
+                                    LambdaQueryWrapper<ForecastPowerShortTerm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+                                    lambdaQueryWrapper.eq(ForecastPowerShortTerm::getStationCode, parsingUrl.getStationCode());
+                                    lambdaQueryWrapper.eq(ForecastPowerShortTerm::getForecastManufactor, "SYJY");
+                                    lambdaQueryWrapper.between(ForecastPowerShortTerm::getForecastTime, startTime, endTime);
+                                    forecastPowerShortTermService.remove(lambdaQueryWrapper);
+                                    forecastPowerShortTermService.saveBatch(forecastPowerShortTermList);
+
+                                    // 保存短期历史
+                                    LambdaQueryWrapper<ForecastPowerShortTermHis> forecastPowerShortTermHisLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                                    forecastPowerShortTermHisLambdaQueryWrapper.eq(ForecastPowerShortTermHis::getStationCode, parsingUrl.getStationCode());
+                                    forecastPowerShortTermHisLambdaQueryWrapper.eq(ForecastPowerShortTermHis::getForecastManufactor, "SYJY");
+                                    forecastPowerShortTermHisLambdaQueryWrapper.eq(ForecastPowerShortTermHis::getGenDate, DateUtil.format(new Date(),"yyyy-MM-dd"));
+                                    forecastPowerShortTermHisService.remove(forecastPowerShortTermHisLambdaQueryWrapper);
+                                    forecastPowerShortTermHisService.saveBatch(forecastPowerShortTermHisList);
+                                    log.info("嘉越云端文件解析成功!");
+                                    try {
+                                        File file1 = new File(success + File.separator + fileName);
+                                        if(file1.exists()){
+                                            file1.delete();
+                                        }
+                                        FileUtils.moveFile(file, new File(success + File.separator + fileName));
+                                    } catch (IOException e) {
+                                        e.printStackTrace();
+                                    }
+                                }
+                            } catch (Exception e) {
+                                log.error("嘉越云端文件解析失败:", e);
+                                try {
+                                    File file1 = new File(failDir + File.separator + fileName);
+                                    if(file1.exists()){
+                                        file1.delete();
+                                    }
+                                    FileUtils.moveFile(file, new File(failDir + File.separator + fileName));
+                                } catch (IOException ex) {
+                                    log.error(file.getName() + "嘉越云端文件解析失败1", ex);
+                                }
+                            } finally {
+                                close(bufferedReader, read);
+                            }
                         }
-                        FileUtils.moveFile(file, new File(success + File.separator + fileName));
-                    } catch (IOException e) {
-                        e.printStackTrace();
-                    }
-                    parsingLog.setParsingFileStatus("1");
-                    parsingLog.setFileType(type);
-                } else {
-                    try {
-                        File file1 = new File(failDir + File.separator + fileName);
-                        if(file1.exists()){
-                            file1.delete();
+                    } catch (Exception e) {
+                        log.error("解析嘉越云端DQ文件失败", e);
+                        try {
+                            File file1 = new File(failDir + File.separator + fileName);
+                            if(file1.exists()){
+                                file1.delete();
+                            }
+                            FileUtils.moveFile(file, new File(failDir + File.separator + fileName));
+                        } catch (IOException ex) {
+                            log.error(file.getName() + "嘉越云端文件解析失败1", ex);
                         }
-                        FileUtils.moveFile(file, new File(failDir + File.separator + fileName));
-                    } catch (IOException e) {
-                        e.printStackTrace();
                     }
-                    parsingLog.setParsingFileStatus("0");
-                    //失败情况下会返回失败信息
-                    parsingLog.setParsingDescribe(type);
                 }
-                parsingLogService.save(parsingLog);
             }
         }
-
-
-        log.info("解析执行完成", parsingChannel.getChannelName());
+        log.info("解析嘉越云端文件执行完成", parsingChannel.getChannelName());
 
     }
 
+    /**
+     * 关闭文件流
+     *
+     * @param bufferedReader 字符数据
+     * @param read           字节流
+     */
+    private void close(BufferedReader bufferedReader, InputStreamReader read) {
+        try {
+            if (bufferedReader != null) {
+                bufferedReader.close();
+            }
+            if (read != null) {
+                read.close();
+            }
+        } catch (IOException e) {
+            log.error("关闭文件流失败:", e);
+        }
+    }
 }