|
@@ -1,5 +1,7 @@
|
|
|
package com.energy.manage.service.service.analysis.impl;
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.energy.manage.common.po.analysis.AnalysisOptionRecordsPo;
|
|
|
import com.energy.manage.common.reponse.ConstVar;
|
|
@@ -10,14 +12,12 @@ import com.energy.manage.service.constant.analysis.AnalysisConstants;
|
|
|
import com.energy.manage.service.domain.dto.analysis.AnalysisDto;
|
|
|
import com.energy.manage.service.domain.dto.analysis.AnalysisResultDto;
|
|
|
import com.energy.manage.service.domain.vo.analysis.*;
|
|
|
-import com.energy.manage.service.domain.vo.datatransfer.DataTransferDataTimeVo;
|
|
|
import com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupShortVo;
|
|
|
import com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupVo;
|
|
|
import com.energy.manage.service.domain.vo.windfieldbatch.WindFieldBatchVo;
|
|
|
import com.energy.manage.service.filter.LoginUser;
|
|
|
import com.energy.manage.service.filter.LoginUserContextHolder;
|
|
|
import com.energy.manage.service.mappers.analysis.*;
|
|
|
-import com.energy.manage.service.mappers.datatransfer.DataTransferMapper;
|
|
|
import com.energy.manage.service.mappers.windenginegroup.WindEngineGroupMapper;
|
|
|
import com.energy.manage.service.mappers.windfieldbatch.WindFieldBatchMapper;
|
|
|
import com.energy.manage.service.property.analysis.AlgorithmProperties;
|
|
@@ -30,21 +30,30 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import io.minio.GetObjectArgs;
|
|
|
+import io.minio.ListObjectsArgs;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.Result;
|
|
|
+import io.minio.messages.Item;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URISyntaxException;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
-import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.function.Predicate;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -90,6 +99,12 @@ public class AnalysisServiceImpl implements AnalysisService {
|
|
|
@Autowired
|
|
|
private AnalysisTypeConfig analysisTypeConfig;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BatchDownloadUtil batchDownloadUtil;
|
|
|
+
|
|
|
|
|
|
private static AnalysisOptionRecordsMapper optionRecordsMapper;
|
|
|
|
|
@@ -109,6 +124,7 @@ public class AnalysisServiceImpl implements AnalysisService {
|
|
|
|
|
|
private static ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 分析结果列表
|
|
|
* @param analysisResultDto
|
|
@@ -272,17 +288,184 @@ public class AnalysisServiceImpl implements AnalysisService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 文件现在
|
|
|
- *
|
|
|
+ * 全部文件下载查询接口
|
|
|
* @param fieldCode
|
|
|
* @param batchCode
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public int downloadFile(String fieldCode, String batchCode, String analysisTypeCode) {
|
|
|
- String resourceAddress = batchCode + File.separator + analysisTypeCode + File.separator;
|
|
|
- String downloadUrl = BatchDownloadUtil.backResourceAddress(fieldCode,resourceAddress);
|
|
|
- return 0;
|
|
|
+ public List<String> queryDownloadFile(String fieldCode, String batchCode){
|
|
|
+ String resourcePath = createSourceFilePath(batchCode,null);
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(StringUtils.lowerCase(fieldCode)).prefix(resourcePath).build());
|
|
|
+ List<String> returnList = new ArrayList<>();
|
|
|
+ for(Result<Item> result : results){
|
|
|
+ returnList.add(getAnalysisTypeCode(result));
|
|
|
+ }
|
|
|
+ return returnList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取分析类型
|
|
|
+ * @param item
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getAnalysisTypeCode(Result<Item> item){
|
|
|
+ String analysisTypeCode = null;
|
|
|
+ try {
|
|
|
+ analysisTypeCode = item.get().objectName().split(File.separator)[1];
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("analysisTypeCode = {} 查询文件异常", analysisTypeCode, e);
|
|
|
+ }
|
|
|
+ return analysisTypeCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件下载
|
|
|
+ * @param fieldCode 风场code
|
|
|
+ * @param batchCode 批次code
|
|
|
+ * @param analysisTypeCode 分析类型code
|
|
|
+ * @param response 响应
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResultResp downloadFile(String fieldCode, String batchCode, String analysisTypeCode, HttpServletResponse response) {
|
|
|
+ List<InputStream> inputStreams = new ArrayList<>();
|
|
|
+ List<String> zipFilePaths = new ArrayList<>();
|
|
|
+ try{
|
|
|
+ String resourcePath = createSourceFilePath(batchCode,analysisTypeCode);
|
|
|
+ setStreamAndPath(fieldCode, resourcePath, zipFilePaths, inputStreams);
|
|
|
+ log.info("fieldCode = {} ,batchCode = {} ,analysisTypeCode = {} 文件数量 = {}",fieldCode,batchCode,analysisTypeCode,zipFilePaths.size());
|
|
|
+ String[] pathsArray = zipFilePaths.stream().toArray(String[] :: new);
|
|
|
+ InputStream[] inputStreamsArray = inputStreams.stream().toArray(InputStream[] :: new);
|
|
|
+ response.reset();
|
|
|
+ String zipName = batchCode + analysisTypeCode + AnalysisConstants.ZIP_SUFFIX;
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipName, "UTF-8"));
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ log.info("fieldCode = {} ,batchCode = {} ,analysisTypeCode = {} 响应头部设置完毕,准备压缩下载",fieldCode,batchCode,analysisTypeCode);
|
|
|
+ ZipUtil.zip(response.getOutputStream(), pathsArray, inputStreamsArray);
|
|
|
+ log.info("fieldCode = {} ,batchCode = {} ,analysisTypeCode = {} 压缩下载完毕 !!!",fieldCode,batchCode,analysisTypeCode);
|
|
|
+ return ResultResp.SUCCESS();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("fileCode = {} batchCode = {} analysisTypeCode = {} 文件下载失败",fieldCode,batchCode,analysisTypeCode,e);
|
|
|
+ return ResultResp.FAIL("文件下载失败,请重试或联系管理员");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 文件下载
|
|
|
+// * @param fieldCode
|
|
|
+// * @param batchCode
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// @Override
|
|
|
+// public void downloadFile(String fieldCode, String batchCode, String analysisTypeCode, HttpServletResponse response) {
|
|
|
+// //下载文件到本地
|
|
|
+// String downloadUrl = BatchDownloadUtil.backResourceAddress(StringUtils.lowerCase(fieldCode),createSourceFilePath(batchCode,analysisTypeCode));
|
|
|
+// if(StringUtils.isBlank(downloadUrl)){
|
|
|
+// log.info("fieldCode = {} ,batchCode = {} ,analysisTypeCode = {} 文件不存在",fieldCode,batchCode,analysisTypeCode);
|
|
|
+// return ;
|
|
|
+// }
|
|
|
+// File file = ZipUtil.zip(downloadUrl, createZipPath(batchCode),true);
|
|
|
+// //异步删除临时文件与zip文件
|
|
|
+// deleteFileDir(downloadUrl);
|
|
|
+// downloadZip(file,response);
|
|
|
+// //异步删除临时文件与zip文件
|
|
|
+// deleteFileDir(createDeleteZipPath());
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置文件流与文件路径
|
|
|
+ * @param bucketName
|
|
|
+ * @param resoucePath
|
|
|
+ * @param zipFilePaths
|
|
|
+ * @param inputStreams
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private void setStreamAndPath (String bucketName,
|
|
|
+ String resoucePath,
|
|
|
+ List<String> zipFilePaths,
|
|
|
+ List<InputStream> inputStreams) throws Exception {
|
|
|
+ bucketName = StringUtils.lowerCase(bucketName);
|
|
|
+ //递归获取文件列表(只有文件不含文件夹)
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(resoucePath).recursive(true).build());
|
|
|
+ int count = 0;
|
|
|
+ for(Result<Item> result : results){
|
|
|
+ Item item = result.get();
|
|
|
+ String objectName = item.objectName();
|
|
|
+ log.info("当前操作文件名称 {} ", resoucePath, objectName);
|
|
|
+ GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(objectName).build();
|
|
|
+ String filePath = objectName.replace(resoucePath, resoucePath.replaceFirst(File.separator,"_"));
|
|
|
+ zipFilePaths.add(filePath);
|
|
|
+ log.info("当前放入文件-{} ", resoucePath, objectName);
|
|
|
+ inputStreams.add(minioClient.getObject(getObjectArgs));
|
|
|
+ log.info("文件-{} 放入下载列表成功,当前放入文件数量 = {}",objectName,++count);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成源文件路径
|
|
|
+ * @param batchCode 批次编号
|
|
|
+ * @param analysisTypeCode 分析类型code
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String createSourceFilePath(String batchCode,String analysisTypeCode){
|
|
|
+ String resourcePath = batchCode + AnalysisConstants.PATH_SEPARATOR ;
|
|
|
+ if(StringUtils.isNotBlank(analysisTypeCode)){
|
|
|
+ resourcePath += analysisTypeCode + AnalysisConstants.PATH_SEPARATOR;
|
|
|
+ }
|
|
|
+ return resourcePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建压缩文件路径
|
|
|
+ * @param batchCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String createZipPath(String batchCode){
|
|
|
+ return batchDownloadUtil.localDir + AnalysisConstants.ZIP_DIR
|
|
|
+ + AnalysisConstants.PATH_SEPARATOR + batchCode
|
|
|
+ + AnalysisConstants.ZIP_SUFFIX;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String createDeleteZipPath(){
|
|
|
+ return batchDownloadUtil.localDir + AnalysisConstants.PATH_SEPARATOR + AnalysisConstants.ZIP_DIR;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载zip文件
|
|
|
+ * @param file
|
|
|
+ * @param response
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private void downloadZip(File file,HttpServletResponse response){
|
|
|
+ try{
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ byte[] bytes = FileUtil.readBytes(file.getAbsolutePath());
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ os.write(bytes);
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ }catch (IOException ioe){
|
|
|
+ log.error("fileName = {} 文件下载失败",file.getName(),ioe);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步删除文件
|
|
|
+ * @param filePath
|
|
|
+ */
|
|
|
+ private void deleteFileDir(String filePath){
|
|
|
+ File file = new File(filePath);
|
|
|
+ if(file.exists()){
|
|
|
+ CompletableFuture.supplyAsync(() -> FileUtil.del(file))
|
|
|
+ .thenAccept(result -> log.info("filePath = {} 删除结果result = :{} " , filePath, result));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -705,7 +888,6 @@ public class AnalysisServiceImpl implements AnalysisService {
|
|
|
//
|
|
|
// returnList.forEach(System.out::println);
|
|
|
|
|
|
-
|
|
|
}
|
|
|
|
|
|
}
|