|
@@ -0,0 +1,147 @@
|
|
|
+package com.jiayue.cppclient.job;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.jiayue.cppclient.util.Md5KeyForCppUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 下载文件定时
|
|
|
+ *
|
|
|
+ * @author jy
|
|
|
+ * @since 2024/12/10
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class DownloadFileJob {
|
|
|
+
|
|
|
+ @Value("${client.stationCodes}")
|
|
|
+ String stationCodes;
|
|
|
+ @Value("${client.cloudAddress}")
|
|
|
+ String cloudAddress;
|
|
|
+ @Value("${client.tempFilePath}")
|
|
|
+ String tempFilePath;
|
|
|
+ @Value("${client.fxglPath}")
|
|
|
+ String fxglPath;
|
|
|
+
|
|
|
+ @Scheduled(cron="${client.job.downloadCron}")
|
|
|
+ public void downloadFile() {
|
|
|
+ // 遍历场站编号
|
|
|
+ String[] stationCode = stationCodes.split(",");
|
|
|
+ // 当日日期yyyy-MM-dd
|
|
|
+ String forecastDay = DateUtil.format(new Date(),"yyyyMMdd");
|
|
|
+ for (int i=0;i<stationCode.length;i++){
|
|
|
+ Map<String,String> dataMap = null;
|
|
|
+ // 查询云端预测文件
|
|
|
+ try {
|
|
|
+ dataMap = queryToDownfilesFromCloud(stationCode[i],forecastDay);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(stationCode[i]+"获取云端预测文件失败",e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dataMap!=null){
|
|
|
+ // 遍历文件下载地址Map
|
|
|
+ for (String fileName:dataMap.keySet()){
|
|
|
+ try {
|
|
|
+ String downUrl = dataMap.get(fileName);
|
|
|
+ // 下载成功后回传更新云端文件状态,同时将临时文件移动道反向隔离路径
|
|
|
+ Long size = downloadFileByCloud(fileName, downUrl);
|
|
|
+ if (size>0){
|
|
|
+ callBackFileDownloadStatusToCloud(stationCode[i], fileName,"down","是");
|
|
|
+ // 将临时文件移动道反向隔离路径
|
|
|
+ File fxglPathDir = new File(fxglPath);
|
|
|
+ if (!fxglPathDir.exists() && !fxglPathDir.isDirectory()){
|
|
|
+ // 创建目录
|
|
|
+ fxglPathDir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ FileUtil.move(new File(tempFilePath + File.separatorChar + fileName),new File(fxglPath),true);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ callBackFileDownloadStatusToCloud(stationCode[i], fileName,"down","否");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e){
|
|
|
+ log.error(stationCode[i]+"下载云端预测文件失败",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求云端,获取今日未下载文件列表
|
|
|
+ *
|
|
|
+ * @return Map<String,String>: 文件名,文件下载地址
|
|
|
+ */
|
|
|
+ private Map<String,String> queryToDownfilesFromCloud(String stationCode,String forecastDay) throws Exception {
|
|
|
+ String signStr = Md5KeyForCppUtil.downloadFileMd5Key(stationCode,forecastDay);
|
|
|
+ Map postParms = new HashMap<>(3);
|
|
|
+ postParms.put("stationCode", stationCode);
|
|
|
+ postParms.put("forecastDay", forecastDay);
|
|
|
+ postParms.put("signStr",signStr);
|
|
|
+ String body = HttpUtil.post(cloudAddress+"/getCppForecastFileInfo", postParms, 10000);
|
|
|
+ JSONObject json = JSONUtil.parseObj(body);
|
|
|
+ String code = json.get("code").toString();
|
|
|
+ String data = json.get("data").toString();
|
|
|
+ if ("0".equals(code)) {
|
|
|
+ Map dataMap = JSONUtil.toBean(data, Map.class);
|
|
|
+ if (MapUtil.isNotEmpty(dataMap)){
|
|
|
+ return dataMap;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从minio下载文件
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * @param fileDownloadUrl
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Long downloadFileByCloud(String fileName, String fileDownloadUrl) throws Exception{
|
|
|
+ Long size = 0L;
|
|
|
+ size = HttpUtil.downloadFile(fileDownloadUrl, new File(tempFilePath + File.separatorChar
|
|
|
+ + fileName), 10000);
|
|
|
+ if (size > 0) {
|
|
|
+ log.info("云端下载文件成功:{}", fileName);
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ log.info("云端下载文件失败,回传失败状态:{}", fileName);
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回传云端文件状态
|
|
|
+ *
|
|
|
+ * @param stationCode
|
|
|
+ * @param forecastFileName
|
|
|
+ * @param type
|
|
|
+ * @param status
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void callBackFileDownloadStatusToCloud(String stationCode, String forecastFileName,String type,String status) throws Exception{
|
|
|
+ String callbackSignStr = Md5KeyForCppUtil.callbackFileMd5Key(stationCode, forecastFileName,type,status);
|
|
|
+ Map postParms = new HashMap<>(5);
|
|
|
+ postParms.put("stationCode", stationCode);
|
|
|
+ postParms.put("forecastFileName", forecastFileName);
|
|
|
+ postParms.put("type", type);
|
|
|
+ postParms.put("status", status);
|
|
|
+ postParms.put("signStr", callbackSignStr);
|
|
|
+ String body = HttpUtil.post(cloudAddress + "/updateCppFileStatus", postParms, 10000);
|
|
|
+ log.info("文件下载成功,回传云端文件下载状态响应报文");
|
|
|
+ }
|
|
|
+}
|