Browse Source

文件导出

wangchangsheng 3 years ago
parent
commit
facfd6330a

+ 27 - 63
src/main/java/com/gyee/frame/controller/export/GoldenController.java

@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.File;
 import java.util.*;
 
 @RestController
@@ -29,6 +30,7 @@ public class GoldenController {
     @Autowired
     FileUtils fileUtils;
 
+
     /**
      * 查询所有的场站
      *
@@ -71,63 +73,20 @@ public class GoldenController {
     /**
      * 通过模板导出所有风机数据
      *
-     * @param id       模板ID
-     * @param startTs  开始时间
-     * @param endTs    结束时间
-     * @param interval 时间间隔
+     * @param startTs 开始时间
+     * @param endTs   结束时间
      * @return
      */
     @GetMapping("/history/all")
     public AjaxResult getHistoryAll(
             @RequestParam(value = "station") String station,
-            @RequestParam(value = "templateId") Integer id,
             @RequestParam(value = "startTs") Long startTs,
-            @RequestParam(value = "endTs") Long endTs,
-            @RequestParam(value = "interval", required = false) Optional<Integer> interval) {
+            @RequestParam(value = "endTs") Long endTs) {
 
-        int val = interval.isPresent() ? interval.get() : 1800;
-        Map<String, List<Object>> map = goldenService.getHistoryDataAll(station, id, startTs, endTs, val);
-        return AjaxResult.successData(AjaxStatus.success.code, map);
+        boolean tag = goldenService.getHistoryDataAll(station, startTs, endTs);
+        return AjaxResult.successData(AjaxStatus.success.code, tag);
     }
 
-//    /**
-//     * @param request
-//     * @return
-//     */
-//    @GetMapping("/downloadFile")
-//    public ResponseEntity<Resource> downloadFile(HttpServletRequest request,
-//                                                 @RequestParam(value = "station") String station,
-//                                                 @RequestParam(value = "templateId") String templateId,
-//                                                 @RequestParam(value = "month") String month,
-//                                                 @RequestParam(value = "interval") int interval) throws Exception {
-//
-//        String path = fileUtils.getFilePath(station, templateId, month, interval);
-//        if ("1".equals(templateId)){
-//            ZipUtils.toZip(fileUtils.getFilePathNoEx(path), path, true);
-//        }
-//        // Load file as Resource
-//        Resource resource = fileUtils.loadFileAsResource(path);
-//
-//        // Try to determine file's content type
-//        String contentType = null;
-//        try {
-//            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
-//        } catch (IOException ex) {
-//
-//        }
-//
-//        // Fallback to the default content type if type could not be determined
-//        if (contentType == null) {
-//            contentType = "application/octet-stream";
-//        }
-//
-//        return ResponseEntity.ok()
-//                .contentType(MediaType.parseMediaType(contentType))
-//                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
-//                .body(resource);
-//    }
-
-
 
     /**
      * @param request
@@ -135,24 +94,29 @@ public class GoldenController {
      */
     @GetMapping("/downloadFile")
     public AjaxResult downloadFile(HttpServletRequest request,
-                                                 @RequestParam(value = "station") String station,
-                                                 @RequestParam(value = "templateId") String templateId,
-                                                 @RequestParam(value = "month") String month,
-                                                 @RequestParam(value = "interval") int interval) throws Exception {
-        String path = fileUtils.getFilePath(station, templateId, month, interval);
-        if ("1".equals(templateId)){
-            ZipUtils.toZip(fileUtils.getFilePathNoEx(path), path, true);
+                                   @RequestParam(value = "station") String[] stations,
+                                   @RequestParam(value = "templateId") String templateId,
+                                   @RequestParam(value = "month") String[] month,
+                                   @RequestParam(value = "interval") int[] interval) throws Exception {
+
+        String zipName = "";
+        List<File> srcFiles = new ArrayList<>();
+        for (int i = 0; i < stations.length; i++) {
+            String path = fileUtils.getFilePath(stations[i], templateId, month[i], interval[i]);
+            if ("1".equals(templateId)) {
+                ZipUtils.toZip(fileUtils.getFilePathNoEx(path), path, true);
+                zipName = month[0] + "_单机数据.zip";
+            } else {
+                zipName = month[0] + "_测风塔数据.zip";
+            }
+            srcFiles.add(new File(path));
         }
+        ZipUtils.toZip(srcFiles, fileUtils.getFilePath2("path", month[0], interval[0]), zipName);
         Map map = new HashMap();
-        map.put("path",fileUtils.getFilePath2(station, templateId, month, interval));
-
-        if ("1".equals(templateId)) {
-            map.put("name",station+".zip");
-        } else if ("2".equals(templateId)) {
-            map.put("name",station+".xls");
-        }
+        map.put("path", fileUtils.getFilePath2("url", month[0], interval[0]));
+        map.put("name", zipName);
 
-        AjaxResult result =  AjaxResult.successData(200,map);
+        AjaxResult result = AjaxResult.successData(200, map);
         return result;
     }
 

+ 22 - 18
src/main/java/com/gyee/frame/service/export/GoldenService.java

@@ -84,28 +84,32 @@ public class GoldenService {
      * 查询所有风机
      *
      * @param station
-     * @param templateId
      * @param startTs
      * @param endTs
-     * @param interval
      * @return
      */
-    public Map<String, List<Object>> getHistoryDataAll(String station, int templateId, long startTs, long endTs, int interval) {
-        switch (templateId) {
-            case 1:
-                getTemplateHistory1(station, null,startTs, endTs, interval);
-                return null;
-            case 2:
-                return getTemplateHistory2(station,null, startTs, endTs, interval);
-            default:
-                throw new QiNiuException(QiNiuErrorEnum.TEMPLATE_NO_SUPPORT);
+    public boolean getHistoryDataAll(String station,long startTs, long endTs) {
+
+        List<Integer>  val  =  exportConfig.getExportval();
+        WindpowerstationExample example = new WindpowerstationExample();
+        example.createCriteria().andIdLike("%"+station+"%");
+        List<Windpowerstation> lists = windpowerstationService.selectByExample(example);
+        for(int i :val){
+            for (Windpowerstation st: lists) {
+                getTemplateHistory1(st.getCode(),st.getAddress(),startTs,endTs,i*60);
+                getTemplateHistory2(st.getCode(),st.getAddress(),startTs,endTs,i*60);
+            }
         }
+        return true;
+
+
+
     }
 
 
 
     @Scheduled(cron = "0 0 1 * * ?")
-//    @Scheduled(cron = "0/5 * * * * *")
+//    @Scheduled(cron = "0/1 * * * * *")
     public void getHistoryData() {
 
         Calendar c = Calendar.getInstance();
@@ -119,8 +123,8 @@ public class GoldenService {
         WindpowerstationExample example = new WindpowerstationExample();
         example.createCriteria().andIdLike("%_FDC%");
         List<Windpowerstation> lists = windpowerstationService.selectByExample(example);
-        beginOfDate = 1633017600000L;
-        endOfDate = 1635696000000L;
+        beginOfDate = 1635696000000L;
+        endOfDate = 1637596800000L;
         for(int i :val){
             for (Windpowerstation station: lists) {
                 getTemplateHistory1(station.getCode(),station.getAddress(),beginOfDate,endOfDate,i*60);
@@ -166,7 +170,7 @@ public class GoldenService {
                 for (Map.Entry<String, Map<String, List<Object>>> entry : wtMap.entrySet()) {
                     map.put(entry.getKey(), entry.getValue());
                     List<String> title = exportConfig.getTemplate1title();
-                    ExcelExportUtils.exelcExportTem(entry.getValue(), entry.getKey(), title, exportConfig.getExportpath() + "\\excel\\" + startTss.substring(0, 7) + "\\" + val + "\\" + address + "单机\\", entry.getKey());
+                    ExcelExportUtils.exelcExportTem(entry.getValue(), entry.getKey(), title, exportConfig.getExportpath() + "\\" + startTss.substring(0, 7) + "\\" + val + "分钟\\" + address + "单机\\", entry.getKey());
                 }
             }
         } catch (Exception e) {
@@ -262,7 +266,7 @@ public class GoldenService {
             Map<String, List<Object>> swdlVos = new HashMap<>();
             swdlVos.put("F1", data.stream().map(p -> dateFormat.format(p.get("RECORDDATE"))).collect(Collectors.toList()));
             swdlVos.put("F2", data.stream().map(p -> Double.valueOf(p.get("SWDL").toString())).collect(Collectors.toList()));
-            ExcelExportUtils.exelcExportTem(swdlVos, "日上网电量", swdltitle, exportConfig.getExportpath() + "\\excel\\" + startTss.substring(0, 7) + "//" + val + "//", address+"测风塔");
+            ExcelExportUtils.exelcExportTem(swdlVos, "日上网电量", swdltitle, exportConfig.getExportpath() + "\\" + startTss.substring(0, 7) + "\\" + val + "分钟\\", address+"测风塔");
             map.put("swdl", list);
 
             // 上网功率
@@ -275,14 +279,14 @@ public class GoldenService {
             swglVos.put("F1", data2.stream().map(p -> DateUtils.format(p.getTs(), DateUtils.DATE_TIME_PATTERN)).collect(Collectors.toList()));
             swglVos.put("F2", data2.stream().map(TsPointData::getDoubleValue).collect(Collectors.toList()));
             List<String> swgltitle = exportConfig.getTemplate2swgltitle();
-            ExcelExportUtils.exelcExportTem(swglVos, "上网功率", swgltitle, exportConfig.getExportpath() + "\\excel\\" + startTss.substring(0, 7) + "//" + val + "//", address+"测风塔");
+            ExcelExportUtils.exelcExportTem(swglVos, "上网功率", swgltitle, exportConfig.getExportpath() + "\\" + startTss.substring(0, 7) + "\\" + val + "分钟\\", address+"测风塔");
             map.put("swgl", list2);
 
             // 测风塔数据
             String point3 = exportConfig.getTemplate2cft().get(station);
             Map<String, List<Object>> cftlVos = getTemplateHistory2(startTs, endTs, interval, point3);
             List<String> cftltitle = exportConfig.getTemplate2cfttitle();
-            ExcelExportUtils.exelcExportTem(cftlVos, "测风塔数据", cftltitle, exportConfig.getExportpath() + "\\excel\\" + startTss.substring(0, 7) + "//" + val + "//", address+"测风塔");
+            ExcelExportUtils.exelcExportTem(cftlVos, "测风塔数据", cftltitle, exportConfig.getExportpath() + "\\" + startTss.substring(0, 7) + "\\" + val + "分钟\\", address+"测风塔");
         } catch (Exception e) {
             log.error("error", e);
             throw new QiNiuException(QiNiuErrorEnum.ERROR_DATA);

+ 32 - 11
src/main/java/com/gyee/frame/util/excel/FileUtils.java

@@ -1,6 +1,10 @@
 package com.gyee.frame.util.excel;
 
+import com.gyee.frame.common.conf.ExportConfig;
 import com.gyee.frame.common.exception.QiNiuException;
+import com.gyee.frame.model.auto.Windpowerstation;
+import com.gyee.frame.model.auto.WindpowerstationExample;
+import com.gyee.frame.service.WindpowerstationService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.UrlResource;
@@ -9,11 +13,22 @@ import org.springframework.stereotype.Service;
 import java.net.MalformedURLException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.List;
 
 @Service
 public class FileUtils {
 
 
+
+
+    @Autowired
+    private ExportConfig exportConfig;
+
+
+    @Autowired
+    private WindpowerstationService windpowerstationService;
+
+
     private final Path fileStorageLocation; // 文件在本地存储的地址
 
 
@@ -56,15 +71,17 @@ public class FileUtils {
         StringBuilder sb = new StringBuilder();
         try {
             String val = Integer.valueOf(interval / 60) + "";
-//            String uploadDir = System.getProperty("user.dir");// ResourceUtils.getURL("classpath:").getPath()+"static\\excel";
-            String uploadDir = "D:\\java\\apache-tomcat-9.0.50\\webapps\\excel";
+            WindpowerstationExample example = new WindpowerstationExample();
+            example.createCriteria().andIdLike("%"+station+"%");
+            List<Windpowerstation> lists = windpowerstationService.selectByExample(example);
+            String uploadDir =  exportConfig.getExportpath();
             sb.append(uploadDir).append("\\");
             sb.append(month).append("\\");
-            sb.append(val).append("\\");
+            sb.append(val).append("分钟\\");
             if ("1".equals(templateId)){
-                sb.append(station).append(".zip");
+                sb.append(lists.get(0).getAddress()).append("单机").append(".zip");
             }else if ("2".equals(templateId)){
-                sb.append(station).append(".xls");
+                sb.append(lists.get(0).getAddress()).append("测风塔").append(".xls");
             } else {
                 throw new QiNiuException("请求错误");
             }
@@ -77,18 +94,22 @@ public class FileUtils {
 
     /**
      * 获取文件路径
-     * @param station
-     * @param templateId
+     * @param tag
      * @param month
      * @param interval
      * @return
      */
-    public String getFilePath2(String station,String templateId,String month,int interval) {
+    public String getFilePath2(String tag,String month,int interval) {
         String val = Integer.valueOf(interval / 60) + "";
-        StringBuilder sb = new StringBuilder("http:\\192.168.10.15:8080").append("\\");
-        sb.append("excel").append("\\");
+        StringBuilder sb = new StringBuilder();
+        if ("path".equals(tag)){
+            sb.append(exportConfig.getExportpath()).append("\\");
+        }else {
+            sb.append("http:\\10.155.32.4:8087").append("\\excel\\");
+        }
+
         sb.append(month).append("\\");
-        sb.append(val);
+        sb.append(val).append("分钟");
         return sb.toString();
     }
 

+ 21 - 4
src/main/java/com/gyee/frame/util/excel/ZipUtils.java

@@ -66,13 +66,21 @@ public class ZipUtils {
      * 压缩成ZIP 方法2
      *
      * @param srcFiles 需要压缩的文件列表
-     * @param out      压缩文件输出流
+     * @param path     压缩文件地址
+     * @param zipName  压缩文件名
      * @throws RuntimeException 压缩失败会抛出运行时异常
      */
-    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
+    public static void toZip(List<File> srcFiles, String path, String zipName) throws RuntimeException {
         long start = System.currentTimeMillis();
+
+        FileOutputStream out = null;
         ZipOutputStream zos = null;
         try {
+            File file = new File(path+"\\"+zipName);
+            if (file.exists()) {
+                file.delete();
+            }
+            out = new FileOutputStream(file);
             zos = new ZipOutputStream(out);
             for (File srcFile : srcFiles) {
                 byte[] buf = new byte[BUFFER_SIZE];
@@ -98,10 +106,21 @@ public class ZipUtils {
                     e.printStackTrace();
                 }
             }
+
+            if (out != null) {
+                try {
+                    out.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
         }
     }
 
 
+
+
     /**
      * 递归压缩方法
      *
@@ -152,6 +171,4 @@ public class ZipUtils {
         }
 
     }
-
-
 }

+ 6 - 5
src/main/resources/application.yml

@@ -26,8 +26,8 @@ gyee:
   #漂亮得拖动验证码 默认false普通验证码、true滚动验证码
   rollVerification: true
   #实时数据库Url
-  baseurl: http://49.4.50.80:8011/ts
-  #baseurl: http://10.155.32.4:8011/ts
+  #baseurl: http://49.4.50.80:8011/ts
+  baseurl: http://10.155.32.4:8011/ts
   #API访问ip
   #swaggerip: 49.4.50.80:8082
   swaggerip: 10.155.32.4:8082
@@ -91,8 +91,8 @@ spring :
     date-format: yyyy-MM-dd HH:mm:ss
   redis:
     database: 1
-    host: 123.60.213.70
-#   host: 10.155.32.4
+#    host: 123.60.213.70
+    host: 10.155.32.4
     password: gdnxfd123
     pool:
       maxTotal: 20
@@ -205,6 +205,7 @@ exportcode:
     SBQ_FDC: SBQFCJSFW.NX_GD_SBQF_XX_XX_XXX_XXX_CI0106,SBQFGL.NX_GD_SBQF_YC_P1_L1_001_FGLD70,SBQFGL.NX_GD_SBQF_YC_P1_L1_001_FGLTEM,SBQFGL.NX_GD_SBQF_YC_P1_L1_001_FGLPRE,SBQFGL.NX_GD_SBQF_YC_P1_L1_001_FGLHUM
     XS_FDC: XSFGL.NX_GD_XSF_YC_P1_L1_001_FGLS30,XSFGL.NX_GD_XSF_YC_P1_L1_001_FGLD70,XSFGL.NX_GD_XSF_YC_P1_L1_001_FGLTEM,XSFGL.NX_GD_XSF_YC_P1_L1_001_FGLPRE,XSFGL.NX_GD_XSF_YC_P1_L1_001_FGLHUM
   template2cfttitle: 数据时间(格式:2019-05-01 13:10:00), 轮毂高度平均风速(m/s), 轮毂高度平均风向(°), 温度(℃), 压强(hPa), 湿度(%)
-  exportpath: D:\java\apache-tomcat-9.0.50\webapps #D:\\gis\apache-tomcat-8.0.36\\webapps\\zhfx
+  exportpath: D:\\gis\apache-tomcat-8.0.36\\webapps\\zhfx\\excel
+  #exportpath: D:\\java\\apache-tomcat-9.0.50\\webapps\\excel
   exportval: 5, 10, 15