Browse Source

feat:导出

Eureka 10 months ago
parent
commit
b98eb90270

+ 92 - 1
taais-modules/taais-biz/src/main/java/com/taais/biz/controller/DataController.java

@@ -1,12 +1,15 @@
 package com.taais.biz.controller;
 
 import cn.dev33.satoken.annotation.SaCheckPermission;
+import cn.hutool.core.util.IdUtil;
 import com.taais.biz.domain.Data;
 import com.taais.biz.domain.bo.DataBo;
 import com.taais.biz.domain.dto.DataAmplifyDto;
+import com.taais.biz.domain.vo.DataExcelVo;
 import com.taais.biz.domain.vo.DataSelectVo;
 import com.taais.biz.domain.vo.DataVo;
 import com.taais.biz.service.IDataService;
+import com.taais.common.core.config.TaaisConfig;
 import com.taais.common.core.core.domain.CommonResult;
 import com.taais.common.core.core.page.PageResult;
 import com.taais.common.excel.utils.ExcelUtil;
@@ -16,8 +19,12 @@ import com.taais.common.web.annotation.RepeatSubmit;
 import com.taais.common.web.core.BaseController;
 import com.taais.system.config.ServerConfig;
 import jakarta.annotation.Resource;
+import jakarta.servlet.ServletOutputStream;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -25,8 +32,16 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 /**
  * 数据管理Controller
@@ -73,8 +88,84 @@ public class DataController extends BaseController {
     @Log(title = "数据管理", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
     public void export(HttpServletResponse response, DataBo dataBo) {
+        // 查询数据列表
         List<DataVo> list = dataService.selectList(dataBo);
-        ExcelUtil.exportExcel(list, "数据管理", DataVo.class, response);
+
+        // 创建一个临时目录用于存放文件
+        String tempDirPath = System.getProperty("java.io.tmpdir") + "/export_" + System.currentTimeMillis();
+        File tempDir = new File(tempDirPath);
+        if (!tempDir.exists()) {
+            log.info("创建临时目录:{}", tempDir.mkdirs());
+        }
+        String profile = TaaisConfig.getProfile();
+        // 拷贝文件到临时目录
+        for (DataVo dataVo : list) {
+            File srcFile = new File(profile + dataVo.getUrl());
+            File destFile = new File(tempDir, srcFile.getName());
+            try {
+                if (srcFile.exists()) {
+                    FileUtils.copyFile(srcFile, destFile);
+                } else {
+                    log.error("图片源文件不存在:{}", srcFile.getName());
+                }
+                if (!StringUtils.isEmpty(dataVo.getLabelurl())) {
+                    File labelurlFile = new File(profile + dataVo.getLabelurl());
+                    File labelurlDestFile = new File(tempDir, labelurlFile.getName());
+                    if (labelurlFile.exists()) {
+                        FileUtils.copyFile(labelurlFile, labelurlDestFile);
+                    } else {
+                        log.error("标注源文件不存在:{}", labelurlFile.getName());
+                    }
+                }
+            } catch (IOException e) {
+                throw new RuntimeException("文件拷贝失败:" + srcFile.getName(), e);
+            }
+        }
+
+        List<DataExcelVo> dataExcelVoList = new ArrayList<>();
+        if (!list.isEmpty()) {
+            dataExcelVoList = list.stream().map(info -> DataExcelVo.builder().id(info.getId()).name(info.getName())
+                            .dataType(info.getDataType()).fileType(info.getFileType()).objectType(info.getObjectType())
+                            .objectSubtype(info.getObjectSubtype()).batchNum(info.getBatchNum()).scene(info.getScene())
+                            .dataSource(info.getDataSource()).gatherTime(info.getGatherTime()).gatherSpot(info.getGatherSpot())
+                            .increment(info.getIncrement()).build())
+                    .toList();
+        }
+        // 生成 Excel 文件并保存到临时目录
+        File excelFile = new File(tempDir, "数据管理.xlsx");
+        try (FileOutputStream fos = new FileOutputStream(excelFile)) {
+            ExcelUtil.exportExcel(dataExcelVoList, "数据管理", DataExcelVo.class, fos);
+        } catch (IOException e) {
+            throw new RuntimeException("生成Excel文件失败", e);
+        }
+        // 将临时目录打包为ZIP文件
+        File zipFile = new File(tempDir.getParent(), "export_" + System.currentTimeMillis() + ".zip");
+        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
+            for (File file : Objects.requireNonNull(tempDir.listFiles())) {
+                zos.putNextEntry(new ZipEntry(file.getName()));
+                try (FileInputStream fis = new FileInputStream(file)) {
+                    IOUtils.copy(fis, zos);
+                }
+                zos.closeEntry();
+            }
+        } catch (IOException e) {
+            throw new RuntimeException("打包ZIP文件失败", e);
+        }
+
+        // 设置响应头,返回ZIP文件给客户端
+        response.setContentType("application/zip");
+        response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
+
+        try (ServletOutputStream os = response.getOutputStream()) {
+            FileUtils.copyFile(zipFile, os);
+            os.flush();
+        } catch (IOException e) {
+            throw new RuntimeException("返回ZIP文件失败", e);
+        } finally {
+            // 删除临时文件
+            FileUtils.deleteQuietly(tempDir);
+            FileUtils.deleteQuietly(zipFile);
+        }
     }
 
     /**

+ 74 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/domain/vo/DataExcelVo.java

@@ -0,0 +1,74 @@
+package com.taais.biz.domain.vo;
+
+import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.taais.biz.domain.Data;
+import io.github.linpeilie.annotations.AutoMapper;
+import lombok.*;
+import lombok.experimental.Accessors;
+
+import java.util.Date;
+
+/**
+ * @author king
+ * @date 2024年08月23日 上午9:43
+ */
+@Getter
+@Setter
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+@Accessors(chain = true)
+@ExcelIgnoreUnannotated
+@AutoMapper(target = Data.class)
+public class DataExcelVo {
+
+    @ExcelProperty(value = "编号")
+    private Long id;
+
+    /** 名称 */
+    @ExcelProperty(value = "名称")
+    private String name;
+
+    /** 数据类型 */
+    @ExcelProperty(value = "数据类型")
+    private String dataType;
+
+    /** 文件类型 */
+    @ExcelProperty(value = "文件类型")
+    private String fileType;
+
+    /** 目标类型 */
+    @ExcelProperty(value = "目标类型")
+    private String objectType;
+
+    /** 目标子类型 */
+    @ExcelProperty(value = "目标子类型")
+    private String objectSubtype;
+
+    /** 批次号 */
+    @ExcelProperty(value = "批次号")
+    private String batchNum;
+
+    /** 场景 */
+    @ExcelProperty(value = "场景")
+    private String scene;
+
+    /** 数据源 */
+    @ExcelProperty(value = "数据源")
+    private String dataSource;
+
+    /** 采集时间 */
+    @ExcelProperty(value = "采集时间")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date gatherTime;
+
+    /** 采集地点 */
+    @ExcelProperty(value = "采集地点")
+    private String gatherSpot;
+
+    /** 扩增方式 */
+    @ExcelProperty(value = "扩增方式")
+    private String increment;
+}