Explorar o código

新增下载接口

chenhongyan1989 hai 9 meses
pai
achega
a93013dffb

+ 10 - 0
energy-manage-service/src/main/java/com/energy/manage/service/controller/analysis/AnalysisController.java

@@ -108,4 +108,14 @@ public class AnalysisController extends BaseServiceController {
     {
         return success(analysisService.queryAnalysisTypeConfig());
     }
+
+    @UserLoginToken
+    @ApiOperation(value = "文件下载")
+    @PostMapping("/downloadFile")
+    public ResultResp downloadFile(@RequestParam(value = "fieldCode") String fieldCode,
+                                   @RequestParam("batchCode") String batchCode,
+                                   @RequestParam("analysisTypeCode") String analysisTypeCode)
+    {
+        return success(analysisService.downloadFile(fieldCode,batchCode,analysisTypeCode));
+    }
 }

+ 10 - 0
energy-manage-service/src/main/java/com/energy/manage/service/service/analysis/AnalysisService.java

@@ -95,5 +95,15 @@ public interface AnalysisService {
     JSONArray queryAnalysisTypeConfig();
 
 
+    /**
+     * 文件现在
+     * @param batchCode
+     * @param fieldCode
+     * @param analysisTypeCode
+     * @return
+     */
+    int downloadFile(String fieldCode, String batchCode, String analysisTypeCode);
+
+
 
 }

+ 29 - 7
energy-manage-service/src/main/java/com/energy/manage/service/service/analysis/impl/AnalysisServiceImpl.java

@@ -22,6 +22,7 @@ import com.energy.manage.service.mappers.windenginegroup.WindEngineGroupMapper;
 import com.energy.manage.service.mappers.windfieldbatch.WindFieldBatchMapper;
 import com.energy.manage.service.property.analysis.AlgorithmProperties;
 import com.energy.manage.service.service.analysis.AnalysisService;
+import com.energy.manage.service.util.BatchDownloadUtil;
 import com.energy.manage.service.util.BeanMapUtils;
 import com.energy.manage.service.util.NetUtils;
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -36,6 +37,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.net.URISyntaxException;
@@ -269,6 +271,20 @@ public class AnalysisServiceImpl implements AnalysisService {
         return JSONArray.parseArray(analysisTypeConfig.getAnalysisTypeConfig());
     }
 
+    /**
+     * 文件现在
+     *
+     * @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;
+    }
+
 
     /**
      * 获取分析类型
@@ -359,10 +375,12 @@ public class AnalysisServiceImpl implements AnalysisService {
      * @return
      */
     private String replacePath(String fileAddr) {
+        //删除自动、手动 文件夹名称
         for(String replaceStr : algorithmProperties.getAnalysisFileReplaceStr()){
             fileAddr = fileAddr.replace(replaceStr, AnalysisConstants.PATH_SEPARATOR);
         }
-        return fileAddr;
+        //删除文件后缀名
+        return fileAddr.substring(0, fileAddr.lastIndexOf('.'));
     }
 
     /**
@@ -673,16 +691,20 @@ public class AnalysisServiceImpl implements AnalysisService {
 //        AnalysisGeneralFileVo a2 = new AnalysisGeneralFileVo();
 //        a2.setFileAddr("http://123/412/89.html");
 //        a2.setCreateTime(new Date());
+//        a2.setBatchCode("456");
 //        generalFileVos.add(a2);
-//        a2.setBatchCode("453");
+//        Thread.sleep(1000);
+//        AnalysisGeneralFileVo a3 = new AnalysisGeneralFileVo();
+//        a3.setFileAddr("http://123/412/89.json");
+//        a3.setCreateTime(new Date());
+//        a3.setBatchCode("789");
+//        generalFileVos.add(a3);
 //        generalFileVos.sort(Comparator.comparing(AnalysisGeneralFileVo::getCreateTime,Comparator.reverseOrder()));
 //        //删除地址相同的文件
-//        List<AnalysisGeneralFileVo> returnList =  generalFileVos.stream().filter(distinctByKey(AnalysisGeneralFileVo::getFileAddr)).collect(Collectors.toList());
+//        List<AnalysisGeneralFileVo> returnList =  generalFileVos.stream().filter(distinctByKey(item -> replacePath(item.getFileAddr()))).collect(Collectors.toList());
+//
 //        returnList.forEach(System.out::println);
-//        List<String> list = new ArrayList<>();
-//        list.add("1");
-//        list.add(0,"2");
-//        System.out.println(list);
+
 
     }
 

+ 2 - 6
energy-manage-service/src/main/java/com/energy/manage/service/test/TestProperties.java

@@ -1,6 +1,5 @@
 package com.energy.manage.service.test;
 
-import com.alibaba.nacos.api.config.annotation.NacosValue;
 import lombok.Data;
 import lombok.experimental.Accessors;
 import org.springframework.beans.factory.annotation.Value;
@@ -19,15 +18,12 @@ import org.springframework.stereotype.Component;
 public class TestProperties {
 
 
-    private static String downloadUrl;
+    public static String downloadUrl;
 
 
-    @Value("${download.url}")
+    @Value("${test.download.url:123}")
     private void setDownloadUrl(String downloadUrl){
         TestProperties.downloadUrl = downloadUrl;
     }
 
-    public static String getdownloadUrl(){
-        return downloadUrl;
-    }
 }

+ 20 - 17
energy-manage-service/src/main/java/com/energy/manage/service/util/BatchDownloadUtil.java

@@ -1,8 +1,6 @@
 package com.energy.manage.service.util;
 
 
-import cn.hutool.core.io.FileUtil;
-import com.energy.manage.service.config.MinIoClientConfig;
 import io.minio.GetObjectArgs;
 import io.minio.ListObjectsArgs;
 import io.minio.MinioClient;
@@ -11,6 +9,7 @@ import io.minio.messages.Item;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import java.io.File;
@@ -23,13 +22,10 @@ import java.io.InputStream;
 @Component
 public class BatchDownloadUtil {
 
-    @Autowired
-    private MinioClient minioClient;
 
-    private static final String LOCAL_DIR = "/Users/shiyue/Downloads/zn/minio-data/";
+    private static MinioClient minioClient;
 
-//    private static final String BUCKET_NAME = "wof085500002";
-//    private static final String ROOT_PATH = "WOF085500002-WOB00002/";
+    private static String localDir;
 
     /**
      * 下载minio中一个桶下de资源
@@ -37,7 +33,7 @@ public class BatchDownloadUtil {
      * @param resoucePath 桶下资源路径(批次编号/)
      * @return
      */
-    public String backResourceAddress(String bucketName,String resoucePath){
+    public static String backResourceAddress(String bucketName,String resoucePath){
         ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(bucketName).prefix(resoucePath).build();
         try {
             return findAndSaveFile(listObjectsArgs,bucketName );
@@ -47,28 +43,26 @@ public class BatchDownloadUtil {
         return null;
     }
 
-    private String findAndSaveFile(ListObjectsArgs listObjectsArgs,String bucketName) throws Exception {
+    private static String findAndSaveFile(ListObjectsArgs listObjectsArgs,String bucketName) throws Exception {
 
-        File ifile = new File(LOCAL_DIR + bucketName);
+        File ifile = new File(localDir + bucketName);
         ifile.mkdirs();
 
         Iterable<Result<Item>> results = minioClient.listObjects(listObjectsArgs);
         if(results == null){
             ifile.delete();
-            System.out.println("Iterable is null");
             return null;
         }
         for (Result<Item> result : results) {
             Item item = result.get();
             if (item.isDir()) {
-                System.out.println("文件夹:" + item.objectName());
                 ListObjectsArgs args = ListObjectsArgs.builder().bucket(bucketName).prefix(item.objectName()).build();
                 findAndSaveFile(args,bucketName);
             } else {
                 GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(item.objectName()).build();
                 String objectName = item.objectName();
                 // Create a local file with the same name as the object
-                File file = new File(LOCAL_DIR +File.separator + bucketName+ File.separator + objectName);
+                File file = new File(localDir +File.separator + bucketName+ File.separator + objectName);
                 // Create parent directories if needed
                 file.getParentFile().mkdirs();
                 // Get the object as an input stream
@@ -83,8 +77,17 @@ public class BatchDownloadUtil {
         return ifile.getPath();
     }
 
-    //    public void main(String[] args) throws Exception {
-//        ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(BUCKET_NAME).prefix(ROOT_PATH).build();
-//        findAndSaveFile(listObjectsArgs);
-//    }
+
+    @Autowired
+    public void setMinioClient(MinioClient minioClient){
+        BatchDownloadUtil.minioClient = minioClient;
+    }
+
+
+    @Value("${file.download.dir:/Users/baiyahui/Downloads/zn/minio-data/}")
+    public void setLocalDir(String localDir) {
+        BatchDownloadUtil.localDir = localDir;
+    }
+
+
 }