Browse Source

生成下发文件

xusl 10 tháng trước cách đây
mục cha
commit
f7141ebd05

+ 10 - 0
ipp-idp/pom.xml

@@ -31,6 +31,16 @@
 
     <dependencies>
         <dependency>
+            <groupId>org.apache.velocity</groupId>
+            <artifactId>velocity</artifactId>
+            <version>1.7</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>${hutool.version}</version>
+        </dependency>
+        <dependency>
             <groupId>com.syjy</groupId>
             <artifactId>calculation-spring-boot-starter</artifactId>
             <version>0.0.32</version>

+ 25 - 5
ipp-idp/src/main/java/com/jiayue/ipp/idp/IppIdpApplication.java

@@ -1,13 +1,14 @@
 package com.jiayue.ipp.idp;
 
 
-import com.baomidou.mybatisplus.core.config.GlobalConfig;
-import org.mybatis.spring.annotation.MapperScans;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.app.VelocityEngine;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.context.annotation.Bean;
 
+import java.util.Properties;
+
 @SpringBootApplication
 public class IppIdpApplication {
 
@@ -15,6 +16,25 @@ public class IppIdpApplication {
 		SpringApplication.run(IppIdpApplication.class, args);
 	}
 
-
-
+	/**
+	 * 上报文件模板引擎
+	 *
+	 * @return 返回模板引擎
+	 */
+	@Bean
+	public VelocityEngine velocityEngine() {
+		VelocityEngine ve = new VelocityEngine();
+		Properties properties = new Properties();
+		//设置velocity资源加载方式为file
+		properties.setProperty("resource.loader", "file");
+		//设置velocity资源加载方式为file时的处理类
+		properties
+				.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
+		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
+		properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
+		properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
+		properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
+		ve.init(properties);
+		return ve;
+	}
 }

+ 157 - 0
ipp-idp/src/main/java/com/jiayue/ipp/idp/job/SendJyDqData.java

@@ -0,0 +1,157 @@
+package com.jiayue.ipp.idp.job;
+
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.io.resource.ResourceUtil;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.extra.template.Template;
+import cn.hutool.extra.template.TemplateConfig;
+import cn.hutool.extra.template.TemplateEngine;
+import cn.hutool.extra.template.TemplateUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
+import com.jiayue.ipp.common.data.entity.ForecastPowerShortTerm;
+import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
+import com.jiayue.ipp.idp.dto.ForecastData;
+import com.jiayue.ipp.idp.service.ForecastPowerShortTermService;
+import com.jiayue.ipp.idp.service.an.ParsingChannelService;
+import com.jiayue.ipp.idp.util.FileUtil;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.time.DateFormatUtils;
+import org.apache.commons.lang.time.DateUtils;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.*;
+
+/**
+ * 下发嘉越短期数据
+ *
+ * @author jy
+ * @since 2024/07/01
+ */
+@Service
+@Slf4j
+public class SendJyDqData {
+
+    @Value("${dqForecastDays}")
+    private Integer dqForecastDays;
+
+    @Autowired
+    ParsingChannelService parsingChannelService;
+    @Autowired
+    ForecastPowerShortTermService forecastPowerShortTermService;
+    @Autowired
+    VelocityEngine velocityEngine;
+
+    @Scheduled(cron = "0 0/1 * * * *")
+    public void sendJyDqData() {
+        log.info("下发嘉越DQ数据开始");
+        // 获取ftp通道
+        LambdaQueryWrapper<ParsingChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+        lambdaQueryWrapper.eq(ParsingChannel::getAnChannelType, "E1");
+        lambdaQueryWrapper.eq(ParsingChannel::getUseStatus, "E1");
+        List<ParsingChannel> parsingChannels = parsingChannelService.list(lambdaQueryWrapper);
+        String dateDir = DateFormatUtils.format(new Date(), "yyyyMMdd");
+        //遍历解析路径,对文件进行解析
+        for (ParsingChannel parsingChannel : parsingChannels) {
+            try{
+                String distPath = FileUtil.getSendJyDataPath()+File.separator+parsingChannel.getUsername()+File.separator+"download"+File.separator+dateDir;
+                File dirFile = new File(distPath);
+                if (!dirFile.exists()) {
+                    dirFile.mkdirs();
+                }
+                // 生成短期文件
+                String stationCode = parsingChannel.getUsername().split("-")[0];
+                // 获取短期实时数据,开始时间
+                Long startTime = DateMomentUtil.getDayStartTime(DateUtils.addDays(new Date(), 1).getTime());
+                // 结束时间(开始加24小时再减去1秒)
+                Long endTime = DateMomentUtil.getDayLastTime(DateUtils.addDays(new Date(), dqForecastDays).getTime());
+
+                // 查询短期预测功率
+                LambdaQueryWrapper<ForecastPowerShortTerm> queryWrapper = new LambdaQueryWrapper<>();
+                queryWrapper.eq(ForecastPowerShortTerm::getForecastManufactor, "SYJY");
+                queryWrapper.eq(ForecastPowerShortTerm::getStationCode, stationCode);
+                queryWrapper.between(ForecastPowerShortTerm::getForecastTime, new Date(startTime), new Date(endTime));
+
+                List<ForecastPowerShortTerm> forecastPowerShortTermList = this.forecastPowerShortTermService.list(queryWrapper);
+                System.out.println(stationCode+"获取实时短期数据多少条:"+forecastPowerShortTermList.size());
+                List<ForecastData> vList = new ArrayList<>();
+                for (ForecastPowerShortTerm forecastPowerShortTerm:forecastPowerShortTermList){
+                    ForecastData forecastData = new ForecastData();
+                    forecastData.setTime(DateFormatUtils.format(forecastPowerShortTerm.getForecastTime(), "yyyy-MM-dd HH:mm:ss"));
+                    forecastData.setPower(forecastPowerShortTerm.getFpValue().toString());
+                    vList.add(forecastData);
+                }
+
+                VelocityContext velocityContext = new VelocityContext();
+                velocityContext.put("vList", vList);
+                // 系统当前日期
+                velocityContext.put("currentTime", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
+                StringWriter writer = new StringWriter();
+                org.apache.velocity.Template template = this.velocityEngine.getTemplate(FileUtil.getResourceBasePath() + "/templates/send_dq.vm");
+                template.merge(velocityContext, writer);
+                String vmFileName = "send_"+stationCode+"_dq_"+dateDir+".WPD";
+                createSendDqFile(writer,new File(distPath + File.separator+vmFileName));
+
+
+//                Map bindingMap = new HashMap();
+//                bindingMap.put("vList", forecastPowerShortTermHisList);
+//                String content = IoUtil.readUtf8(ResourceUtil.getStream("templates"+File.separator +"send_dq.vm"));
+//                Template templateDq = getTemplate(content);
+//                //渲染模型数据
+//                String vmStr = templateDq.render(bindingMap);
+//
+//                cn.hutool.core.io.FileUtil.writeString(vmStr, distPath + File.separator+vmFileName, CharsetUtil.UTF_8);
+                log.info(stationCode+"下发嘉越DQ数据成功!");
+            }
+            catch (Exception e){
+                e.printStackTrace();
+            }
+        }
+        log.info("下发嘉越DQ数据结束");
+
+    }
+    public static Template getTemplate(String templateStr) {
+        //构造模板引擎
+        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig());
+        Template template = engine.getTemplate(templateStr);
+        return template;
+    }
+
+    /**
+     * 生成上报文件
+     *
+     * @param writer
+     * @param file
+     */
+    protected void createSendDqFile(StringWriter writer, File file) {
+        FileOutputStream os = null;
+        try {
+            os = new FileOutputStream(file);
+            // 采用UTF-8字符集
+            os.write(writer.toString().getBytes("UTF-8"));
+            os.flush();
+
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    log.error("文件生成关闭流失败", e);
+                }
+            }
+        }
+    }
+}

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

@@ -92,7 +92,7 @@ public class DownloadService {
 
                 if (!fileName.contains(dayStr)) {
                     file.delete();
-                    log.warn(fileName + "云端文件不是当天的文件,删除!");
+                    log.warn(parsingUrl.getStationCode()+"下"+fileName + "云端文件不是当天的文件,删除!");
                     break;
                 }
 
@@ -162,7 +162,7 @@ public class DownloadService {
                                     forecastPowerShortTermHisLambdaQueryWrapper.eq(ForecastPowerShortTermHis::getGenDate, DateUtil.format(new Date(),"yyyy-MM-dd"));
                                     forecastPowerShortTermHisService.remove(forecastPowerShortTermHisLambdaQueryWrapper);
                                     forecastPowerShortTermHisService.saveBatch(forecastPowerShortTermHisList);
-                                    log.info("嘉越云端文件解析成功!");
+                                    log.info(parsingUrl.getStationCode()+"下"+fileName + "嘉越云端文件解析成功!");
                                     try {
                                         File file1 = new File(success + File.separator + fileName);
                                         if(file1.exists()){
@@ -182,14 +182,14 @@ public class DownloadService {
                                     }
                                     FileUtils.moveFile(file, new File(failDir + File.separator + fileName));
                                 } catch (IOException ex) {
-                                    log.error(file.getName() + "嘉越云端文件解析失败1", ex);
+                                    log.error(parsingUrl.getStationCode()+"下"+fileName + "嘉越云端文件解析失败1", ex);
                                 }
                             } finally {
                                 close(bufferedReader, read);
                             }
                         }
                     } catch (Exception e) {
-                        log.error("解析嘉越云端DQ文件失败", e);
+                        log.error(parsingUrl.getStationCode()+"下"+fileName + "解析嘉越云端DQ文件失败", e);
                         try {
                             File file1 = new File(failDir + File.separator + fileName);
                             if(file1.exists()){
@@ -197,13 +197,13 @@ public class DownloadService {
                             }
                             FileUtils.moveFile(file, new File(failDir + File.separator + fileName));
                         } catch (IOException ex) {
-                            log.error(file.getName() + "嘉越云端文件解析失败1", ex);
+                            log.error(parsingUrl.getStationCode()+"下"+fileName + "嘉越云端文件解析失败1", ex);
                         }
                     }
                 }
             }
         }
-        log.info("解析嘉越云端文件执行完成", parsingChannel.getChannelName());
+        log.info("解析嘉越云端文件定时执行完成");
 
     }
 

+ 3 - 1
ipp-idp/src/main/java/com/jiayue/ipp/idp/service/an/FTPService.java

@@ -13,6 +13,7 @@ import com.jiayue.ipp.idp.util.FileUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.time.DateFormatUtils;
 import org.apache.commons.net.ftp.FTPFile;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -97,11 +98,12 @@ public class FTPService {
                 List<ParsingUrl> ftpParsingUrlList = ftpParsingUrls.stream().filter(s -> s.getCId().equals(ftpParsingChannel.getId()) && s.getUrlStatus().equals("1")).collect(Collectors.toList());
 
                 try {
+                    String dateDir = DateFormatUtils.format(new Date(), "yyyyMMdd");
                     //遍历解析路径,对文件进行解析
                     for (ParsingUrl ftpParsingUrl : ftpParsingUrlList) {
                         String url = ftpParsingUrl.getUrl();
                         // 从ftp下载到本地parsing目录里
-                        String path = FileUtil.getParsingPath() + File.separator + ftpParsingUrl.getStationCode() + File.separator + ftpParsingUrl.getForecastManufactor();
+                        String path = FileUtil.getParsingPath() + File.separator + ftpParsingUrl.getStationCode() + File.separator + ftpParsingUrl.getForecastManufactor()+File.separator +dateDir;
                         File dirFile = new File(path);
                         if (!dirFile.exists()) {
                             dirFile.mkdirs();

+ 5 - 17
ipp-idp/src/main/java/com/jiayue/ipp/idp/util/FileUtil.java

@@ -74,24 +74,12 @@ public class FileUtil {
         return createParsingDir("parsing");
     }
 
+    public static String getSendJyDataPath() {
+        return createParsingDir("ftp");
+    }
+
     private static String createParsingDir(String dir) {
-        String path = "";
-        if (System.getProperties().getProperty("file.separator").equals("\\")) {
-            path = new File(getResourceBasePath()).getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + dir;
-            try {
-                path = URLDecoder.decode(path, "UTF-8");
-            } catch (UnsupportedEncodingException e) {
-                e.printStackTrace();
-            }
-            File file = new File(path);
-            if (!file.exists()) {
-                boolean b = file.mkdirs();
-                if (!b)
-                    log.error("目录创建失败" + path);
-            }
-        } else {
-            path = "/home/syjy/ipp/" + dir;
-        }
+        String path = "/home/syjy/ipp/" + dir;
         return path;
     }
 

+ 7 - 0
ipp-idp/src/main/resources/templates/send_dq.vm

@@ -0,0 +1,7 @@
+//${currentTime}
+<forecastdata>
+@id	预测时间 预测值
+#foreach( $dq in $vList )
+#${velocityCount}	${dq.time} 	${dq.power}
+#end
+</forecastdata>