Browse Source

结果导出

xiatian 9 months ago
parent
commit
bc638d55ea

+ 44 - 16
src/main/java/cn/com/qmth/scancentral/service/impl/SubjectServiceImpl.java

@@ -18,6 +18,8 @@ import java.util.concurrent.TimeUnit;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -62,6 +64,8 @@ import cn.com.qmth.scancentral.vo.subject.TaskIdVo;
 @Service
 public class SubjectServiceImpl extends MppServiceImpl<SubjectDao, SubjectEntity> implements SubjectService {
 
+    private static final Logger log = LoggerFactory.getLogger(SubjectService.class);
+
     private static ExecutorService exec;
 
     @Autowired
@@ -295,6 +299,7 @@ public class SubjectServiceImpl extends MppServiceImpl<SubjectDao, SubjectEntity
             throw new StatusException("没有考生信息");
         }
         DataExportTaskVo vo = new DataExportTaskVo();
+        vo.setTotalCount(scount);
         vo.setExamId(examId);
         vo.setSubjectCode(subjectCode);
         vo.setSuccess(true);
@@ -309,33 +314,56 @@ public class SubjectServiceImpl extends MppServiceImpl<SubjectDao, SubjectEntity
 
     @Override
     public void answerDataExportDispose(DataExportTaskVo vo) {
-        File temDir = new File("temp/" + FastUUID.get() + "/");
-        temDir.mkdirs();
-        ExportCetMarkingQueryVo req = new ExportCetMarkingQueryVo();
-        req.setExamId(vo.getExamId());
-        req.setPageSize(100000);
-        int pageNumber = 0;
-        req.setPageNumber(pageNumber);
-        Set<String> examNumbers = new HashSet<>();
-        Map<String, MarkSiteEntity> cms = readCetMarking(vo.getExamId());
-        while (true) {
-            req.setPageNumber(++pageNumber);
-            List<ExportCetVo> list = studentService.exportCetData(req);
-            if (CollectionUtils.isEmpty(list)) {
-                break;
+        File[] files = null;
+        try {
+            File temDir = new File("temp/" + FastUUID.get() + "/");
+            temDir.mkdirs();
+            ExportCetMarkingQueryVo req = new ExportCetMarkingQueryVo();
+            req.setExamId(vo.getExamId());
+            req.setPageSize(100000);
+            int pageNumber = 0;
+            req.setPageNumber(pageNumber);
+            Set<String> examNumbers = new HashSet<>();
+            Map<String, MarkSiteEntity> cms = readCetMarking(vo.getExamId());
+            while (true) {
+                req.setPageNumber(++pageNumber);
+                List<ExportCetVo> list = studentService.exportCetData(req);
+                if (CollectionUtils.isEmpty(list)) {
+                    break;
+                }
+                exportAnswer(examNumbers, temDir, list, cms, vo);
+            }
+            files = temDir.listFiles();
+            if (files.length > 0) {
+                File zip = new File(temDir.getAbsolutePath() + "/answer-data.zip");
+                FileUtil.dozip(zip, files);
+                vo.setFilePath(zip.getAbsolutePath());
+            }
+        } catch (StatusException e) {
+            vo.setSuccess(false);
+            vo.setErrMsg(e.getMessage());
+        } catch (Exception e) {
+            vo.setSuccess(false);
+            vo.setErrMsg("系统异常");
+            log.error("导出扫描答案DBF异常", e);
+        } finally {
+            if (files != null && files.length > 0) {
+                for (File file : files) {
+                    FileUtil.deleteDirectory(file);
+                }
             }
-            exportAnswer(examNumbers, temDir, list, cms);
         }
     }
 
     private void exportAnswer(Set<String> examNumbers, File temDir, List<ExportCetVo> list,
-            Map<String, MarkSiteEntity> cms) {
+            Map<String, MarkSiteEntity> cms, DataExportTaskVo vo) {
         Map<String, List<String>> subjects = new HashMap<>();
         for (ExportCetVo data : list) {
             String key = data.getSubjectCode() + "-" + data.getExamNumber();
             if (examNumbers.contains(key)) {
                 continue;
             }
+            vo.setProgressCount(vo.getProgressCount() + 1);
             examNumbers.add(key);
             List<String> line = new ArrayList<>();
             line.add(data.getExamNumber());

+ 75 - 5
src/main/java/cn/com/qmth/scancentral/util/FileUtil.java

@@ -1,14 +1,27 @@
 package cn.com.qmth.scancentral.util;
 
-import com.google.common.io.Files;
-import com.qmth.boot.core.exception.StatusException;
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.*;
-import java.nio.charset.StandardCharsets;
-import java.util.List;
+import com.google.common.io.Files;
+import com.qmth.boot.core.exception.StatusException;
 
 public class FileUtil {
 
@@ -126,4 +139,61 @@ public class FileUtil {
         }
     }
 
+    public static void dozip(File target, File[] files) {
+        // 压缩后的 zip 文件名
+        if (target == null || !target.exists()) {
+            throw new RuntimeException("目录或文件不能为空!");
+        }
+        ZipOutputStream zipOut = null;
+        try {
+            // 创建 ZipOutputStream 对象
+            FileOutputStream fos = new FileOutputStream(target);
+            zipOut = new ZipOutputStream(fos, Charset.forName("UTF-8"));
+
+            // 循环每个文件并将其添加到压缩包
+            for (File vo : files) {
+                FileInputStream in = new FileInputStream(vo);
+                try {
+                    ZipEntry zipEntry = new ZipEntry(vo.getName());
+                    zipOut.putNextEntry(zipEntry);
+                    // 将文件内容写入 ZipOutputStream
+                    byte[] bytes = new byte[1024];
+                    int length;
+                    while ((length = in.read(bytes)) >= 0) {
+                        zipOut.write(bytes, 0, length);
+                    }
+                } finally {
+                    // 关闭当前文件的输入流
+                    in.close();
+                }
+            }
+
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                if (zipOut != null) {
+                    zipOut.close();
+                }
+            } catch (IOException e) {
+            }
+        }
+    }
+
+    public static void deleteDirectory(File dirFile) {
+        if (!dirFile.exists()) {
+            return;
+        }
+        if (dirFile.isFile()) {
+            dirFile.delete();
+        } else {
+            File[] files = dirFile.listFiles();
+            if (files != null) {
+                for (int i = 0; i < files.length; i++) {
+                    deleteDirectory(files[i]);
+                }
+            }
+            dirFile.delete();
+        }
+    }
 }

+ 20 - 0
src/main/java/cn/com/qmth/scancentral/vo/subject/DataExportTaskVo.java

@@ -18,6 +18,10 @@ public class DataExportTaskVo {
 
     private String fileName;
 
+    private int totalCount;
+
+    private int progressCount;
+
     public String getTaskId() {
         return taskId;
     }
@@ -82,4 +86,20 @@ public class DataExportTaskVo {
         this.fileName = fileName;
     }
 
+    public int getTotalCount() {
+        return totalCount;
+    }
+
+    public void setTotalCount(int totalCount) {
+        this.totalCount = totalCount;
+    }
+
+    public int getProgressCount() {
+        return progressCount;
+    }
+
+    public void setProgressCount(int progressCount) {
+        this.progressCount = progressCount;
+    }
+
 }