deason hace 6 años
padre
commit
68b677a194

+ 9 - 2
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/Constants.java

@@ -34,7 +34,14 @@ public interface Constants {
      * 权限错误
      */
     String PRT_CODE_403 = "PRT-000403";
-    
-    
+
+    /* 导出文件默认名称 */
+    String PAPER_DOC_NAME = "试卷.docx";
+    String ANSWER_DOC_NAME = "答案.docx";
+    String PAPER_PDF_NAME = "试卷.pdf";
+    String ANSWER_PDF_NAME = "答案.pdf";
+    String STRUCT_ZIP_NAME = "试卷结构.zip";
+    String OBJECTIVE_EXCEL_NAME = "客观题.xlsx";
+    String SUBJECTIVE_EXCEL_NAME = "主观题.xlsx";
 
 }

+ 92 - 1
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/utils/FileUtils.java

@@ -7,16 +7,28 @@
 
 package cn.com.qmth.examcloud.core.print.common.utils;
 
-import java.io.File;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.nio.charset.Charset;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.UUID;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 /**
  * @author: fengdesheng
  * @since: 2018/11/2
  */
 public class FileUtils {
+    private static Logger log = LoggerFactory.getLogger(FileUtils.class);
+
+    public static String randomUUID() {
+        return UUID.randomUUID().toString().replace("-", "");
+    }
 
     /**
      * 生成日期目录
@@ -72,4 +84,83 @@ public class FileUtils {
         return true;
     }
 
+    /**
+     * 文件压缩
+     *
+     * @param target  目录或文件
+     * @param zipFile 压缩后的ZIP文件
+     */
+    public static boolean doZip(File target, File zipFile) {
+        if (target == null || !target.exists()) {
+            log.error("目录或文件不能为空!");
+            return false;
+        }
+        if (zipFile == null) {
+            log.error("待压缩的文件不能为空!");
+            return false;
+        }
+        ZipOutputStream zipOutStream = null;
+        OutputStream outStream = null;
+        try {
+            if (!zipFile.exists()) {
+                zipFile.createNewFile();
+            }
+            outStream = new FileOutputStream(zipFile);
+            zipOutStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));
+            if (target.isDirectory()) {
+                File[] files = target.listFiles();
+                if (files.length == 0) {
+                    log.error("文件夹内未找到任何文件!");
+                    return false;
+                }
+                for (File file : files) {
+                    doZip(zipOutStream, file, null);
+                }
+            } else {
+                doZip(zipOutStream, target, null);
+            }
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        } finally {
+            if (zipOutStream != null) {
+                IOUtils.closeQuietly(zipOutStream);
+            }
+            if (outStream != null) {
+                IOUtils.closeQuietly(outStream);
+            }
+        }
+        return true;
+    }
+
+    private static void doZip(ZipOutputStream zipOutStream, File target, String parentDir) throws IOException {
+        //log.info("Zip:" + parentDir);
+        if (parentDir == null) {
+            parentDir = "";
+        }
+        if (!"".equals(parentDir) && !parentDir.endsWith(File.separator)) {
+            parentDir += File.separator;
+        }
+        if (target.isDirectory()) {
+            File[] files = target.listFiles();
+            if (files.length > 0) {
+                for (File file : files) {
+                    doZip(zipOutStream, file, parentDir + target.getName());
+                }
+            } else {
+                zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
+                zipOutStream.closeEntry();
+            }
+        } else {
+            InputStream is = new FileInputStream(target);
+            zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
+            int len = 0;
+            byte[] bytes = new byte[1024];
+            while ((len = is.read(bytes)) > 0) {
+                zipOutStream.write(bytes, 0, len);
+            }
+            IOUtils.closeQuietly(is);
+            zipOutStream.closeEntry();
+        }
+    }
+
 }

+ 68 - 0
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/bean/coursepaper/ExportFileInfo.java

@@ -0,0 +1,68 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-11-20 17:02:16.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.core.print.service.bean.coursepaper;
+
+import cn.com.qmth.examcloud.commons.web.cloud.api.JsonSerializable;
+import javafx.util.Pair;
+
+/**
+ * @author: fengdesheng
+ * @since: 2018/11/20
+ */
+public class ExportFileInfo implements JsonSerializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 试卷Word文件 <名称,地址>
+     */
+    private Pair<String, String> paperWord;
+    /**
+     * 答案Word文件 <名称,地址>
+     */
+    private Pair<String, String> answerWord;
+    /**
+     * 试卷PDF文件 <名称,地址>
+     */
+    private Pair<String, String> paperPdf;
+    /**
+     * 答案PDF文件 <名称,地址>
+     */
+    private Pair<String, String> answerPdf;
+
+    public Pair<String, String> getPaperWord() {
+        return paperWord;
+    }
+
+    public void setPaperWord(Pair<String, String> paperWord) {
+        this.paperWord = paperWord;
+    }
+
+    public Pair<String, String> getAnswerWord() {
+        return answerWord;
+    }
+
+    public void setAnswerWord(Pair<String, String> answerWord) {
+        this.answerWord = answerWord;
+    }
+
+    public Pair<String, String> getPaperPdf() {
+        return paperPdf;
+    }
+
+    public void setPaperPdf(Pair<String, String> paperPdf) {
+        this.paperPdf = paperPdf;
+    }
+
+    public Pair<String, String> getAnswerPdf() {
+        return answerPdf;
+    }
+
+    public void setAnswerPdf(Pair<String, String> answerPdf) {
+        this.answerPdf = answerPdf;
+    }
+
+}

+ 52 - 1
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CoursePaperServiceImpl.java

@@ -11,14 +11,18 @@ import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
+import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import cn.com.qmth.examcloud.core.print.entity.CoursePaper;
 import cn.com.qmth.examcloud.core.print.entity.CourseStatistic;
+import cn.com.qmth.examcloud.core.print.entity.ObjectiveQuestionStructure;
+import cn.com.qmth.examcloud.core.print.entity.SubjectiveQuestionStructure;
 import cn.com.qmth.examcloud.core.print.enums.PaperStatus;
 import cn.com.qmth.examcloud.core.print.repository.CoursePaperRepository;
 import cn.com.qmth.examcloud.core.print.repository.CourseStatisticRepository;
 import cn.com.qmth.examcloud.core.print.service.CoursePaperService;
 import cn.com.qmth.examcloud.core.print.service.QuestionStructureService;
 import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.*;
+import javafx.util.Pair;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,7 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.io.File;
 import java.util.*;
 
-import static cn.com.qmth.examcloud.core.print.common.Constants.PRT_CODE_500;
+import static cn.com.qmth.examcloud.core.print.common.Constants.*;
 
 /**
  * @author: fengdesheng
@@ -252,6 +256,53 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         Check.isNull(req.getOrgId(), "学校ID不能为空!");
         Check.isNull(req.getExamId(), "考试ID不能为空!");
         Check.isNull(req.required(), "至少选择一种导出内容!");
+
+        SearchBuilder searches = new SearchBuilder();
+        searches.eq("orgId", req.getOrgId());
+        searches.eq("examId", req.getExamId());
+        searches.eq("paperStatus", PaperStatus.已有.getIndex());
+        Specification<CourseStatistic> spec = SpecUtils.buildSearchers(CourseStatistic.class, searches.build());
+        List<CourseStatistic> statistics = courseStatisticRepository.findAll(spec);
+        if (statistics == null || statistics.isEmpty()) {
+            log.warn(String.format("No paper to export! orgId = %s examId = %s", req.getOrgId(), req.getExamId()));
+            return null;
+        }
+
+        //封装待导出的文件列表
+        List<ExportFileInfo> exportFiles = new ArrayList<>();
+        //封装试卷结构-客观题数据
+        List<ObjectiveQuestionStructure> objectiveStructures = new ArrayList<>();
+        //封装试卷结构-主观题数据
+        List<SubjectiveQuestionStructure> subjectiveStructures = new ArrayList<>();
+        for (CourseStatistic statistic : statistics) {
+            CoursePaper paper = statistic.getCoursePaper();
+            if (paper == null) {
+                continue;
+            }
+            //命名规则:试卷名_课程代码_试卷类型_试卷或答案
+            final String title = String.format("%s_%s_%s_", paper.getPaperName(), paper.getCourseCode(), statistic.getPaperType());
+            ExportFileInfo info = new ExportFileInfo();
+            if (req.getNeedPaper()) {
+                info.setPaperWord(new Pair<>(title + PAPER_DOC_NAME, paper.getPaperWordUrl()));
+                info.setPaperPdf(new Pair<>(title + PAPER_PDF_NAME, paper.getPaperPdfUrl()));
+            }
+            if (req.getNeedAnswer()) {
+                info.setAnswerWord(new Pair<>(title + ANSWER_DOC_NAME, paper.getAnswerWordUrl()));
+                info.setAnswerPdf(new Pair<>(title + ANSWER_PDF_NAME, paper.getAnswerPdfUrl()));
+            }
+            if (req.getNeedStruct()) {
+                //todo
+                //objectiveStructures.add(...)
+                //subjectiveStructures.add(...)
+            }
+            exportFiles.add(info);
+        }
+
+        //文件根目录
+        final String rootDir = ExportFileInfo.class.getClassLoader().getResource("").getPath() + "/files";
+        final String tempPath = rootDir + "/" + FileUtils.randomUUID();
+        FileUtils.makeDirs(tempPath);
+
         //todo
         return null;
     }