deason 6 years ago
parent
commit
ef62d28ded

+ 4 - 0
examcloud-core-print-common/pom.xml

@@ -109,5 +109,9 @@
                 </exclusion>
                 </exclusion>
             </exclusions>
             </exclusions>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+        </dependency>
     </dependencies>
     </dependencies>
 </project>
 </project>

+ 21 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/upyun/UpYunClient.java

@@ -7,6 +7,7 @@
 
 
 package cn.com.qmth.examcloud.core.print.common.upyun;
 package cn.com.qmth.examcloud.core.print.common.upyun;
 
 
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.Constants;
 import cn.com.qmth.examcloud.core.print.common.Constants;
 import cn.com.qmth.examcloud.core.print.common.Result;
 import cn.com.qmth.examcloud.core.print.common.Result;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
@@ -20,6 +21,8 @@ import org.springframework.stereotype.Component;
 
 
 import java.io.File;
 import java.io.File;
 
 
+import static cn.com.qmth.examcloud.core.print.common.Constants.PRT_CODE_400;
+
 /**
 /**
  * @author: fengdesheng
  * @author: fengdesheng
  * @since: 2018/11/1
  * @since: 2018/11/1
@@ -30,6 +33,24 @@ public class UpYunClient {
     @Autowired
     @Autowired
     private UpYunProperty upYunProperty;
     private UpYunProperty upYunProperty;
 
 
+    public String upload(File file) {
+        Check.isEmpty(file, "上传的文件不能为空!");
+        if (!file.exists() || !file.isFile()) {
+            throw new StatusException(PRT_CODE_400, "上传的文件不正确!");
+        }
+        try {
+            final String newFileName = FileUtils.newFileName(file.getName());
+            final String newFilePath = upYunProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
+            this.getInstance().writeFile(newFilePath, file, true);
+
+            //成功后,返回文件访问地址
+            return upYunProperty.getFileUrl() + newFilePath;
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            return null;
+        }
+    }
+
     public Result upload(byte[] bytes, String originalFilename) {
     public Result upload(byte[] bytes, String originalFilename) {
         Check.isEmpty(bytes, "上传的文件不能为空!");
         Check.isEmpty(bytes, "上传的文件不能为空!");
         Check.isBlank(originalFilename, "上传的文件名不正确!");
         Check.isBlank(originalFilename, "上传的文件名不正确!");

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

@@ -41,17 +41,31 @@ public class FileUtils {
         return "/" + new SimpleDateFormat(pattern).format(new Date()) + "/";
         return "/" + new SimpleDateFormat(pattern).format(new Date()) + "/";
     }
     }
 
 
+    /**
+     * 获取文件后缀名(包含".")
+     */
+    public static String getFileSuffix(String filePath) {
+        if (filePath == null) {
+            return "";
+        }
+        int index = filePath.lastIndexOf(".");
+        if (index > 0 && index < filePath.length() - 1) {
+            return filePath.substring(index).toLowerCase();
+        }
+        return "";
+    }
+
     /**
     /**
      * 生成新的文件名(带后缀)
      * 生成新的文件名(带后缀)
      * PS: uuid.xyz
      * PS: uuid.xyz
      */
      */
     public static String newFileName(String fileName) {
     public static String newFileName(String fileName) {
         String uuid = UUID.randomUUID().toString().replaceAll("-", "");
         String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-        if (fileName != null && !"".equals(fileName)) {
+        if (fileName != null) {
             int index = fileName.lastIndexOf(".");
             int index = fileName.lastIndexOf(".");
             if (index > 0 && index < fileName.length() - 1) {
             if (index > 0 && index < fileName.length() - 1) {
                 //获取原来文件的后缀名
                 //获取原来文件的后缀名
-                return uuid + fileName.substring(index);
+                return uuid + fileName.substring(index).toLowerCase();
             }
             }
         }
         }
         return uuid;
         return uuid;
@@ -61,12 +75,12 @@ public class FileUtils {
      * 获取文件名(包含后缀名)
      * 获取文件名(包含后缀名)
      */
      */
     public static String getFileName(String filePath) {
     public static String getFileName(String filePath) {
-        if (filePath == null || "".equals(filePath)) {
+        if (filePath == null) {
             return "";
             return "";
         }
         }
         filePath = filePath.replace("\\", "/");
         filePath = filePath.replace("\\", "/");
         int index = filePath.lastIndexOf("/");
         int index = filePath.lastIndexOf("/");
-        if (index > -1) {
+        if (index >= 0) {
             return filePath.substring(index + 1);
             return filePath.substring(index + 1);
         }
         }
         return filePath;
         return filePath;

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

@@ -11,6 +11,7 @@ import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.Constants;
 import cn.com.qmth.examcloud.core.print.common.Constants;
 import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 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.jpa.SpecUtils;
+import cn.com.qmth.examcloud.core.print.common.upyun.UpYunClient;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.common.utils.ExcelUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.ExcelUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
@@ -24,6 +25,7 @@ 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.CoursePaperService;
 import cn.com.qmth.examcloud.core.print.service.QuestionStructureService;
 import cn.com.qmth.examcloud.core.print.service.QuestionStructureService;
 import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.*;
 import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.*;
+import com.itextpdf.text.pdf.PdfReader;
 import javafx.util.Pair;
 import javafx.util.Pair;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
@@ -35,6 +37,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
 import java.io.File;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.IOException;
 import java.util.*;
 import java.util.*;
 
 
@@ -48,6 +51,8 @@ import static cn.com.qmth.examcloud.core.print.common.Constants.*;
 public class CoursePaperServiceImpl implements CoursePaperService {
 public class CoursePaperServiceImpl implements CoursePaperService {
     private static final Logger log = LoggerFactory.getLogger(CoursePaperServiceImpl.class);
     private static final Logger log = LoggerFactory.getLogger(CoursePaperServiceImpl.class);
     @Autowired
     @Autowired
+    private UpYunClient upYunClient;
+    @Autowired
     private CoursePaperRepository coursePaperRepository;
     private CoursePaperRepository coursePaperRepository;
     @Autowired
     @Autowired
     private CourseStatisticRepository courseStatisticRepository;
     private CourseStatisticRepository courseStatisticRepository;
@@ -96,28 +101,48 @@ public class CoursePaperServiceImpl implements CoursePaperService {
 //        Check.isBlank(coursePaper.getAnswerHtmlUrl(), "答案页面地址不能为空!");
 //        Check.isBlank(coursePaper.getAnswerHtmlUrl(), "答案页面地址不能为空!");
 //        Check.isNull(coursePaper.getPaperP(), "试卷页数不能为空!");
 //        Check.isNull(coursePaper.getPaperP(), "试卷页数不能为空!");
 
 
-        //试卷预览和答案预览地址
+        //试卷地址和答案地址
         String paperHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/pdf/").concat(coursePaper.getPaperId());
         String paperHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/pdf/").concat(coursePaper.getPaperId());
         String answerHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/answer/pdf/").concat(coursePaper.getPaperId());
         String answerHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/answer/pdf/").concat(coursePaper.getPaperId());
 
 
+        //转换PDF文件
+        String rootDir = Constants.rootFileDir();
+        FileUtils.makeDirs(rootDir);
+        String paperPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
+        String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
+        String paperPdfUrl = upYunClient.upload(new File(paperPdfPath));
+        String answerPdfUrl = upYunClient.upload(new File(answerPdfPath));
+
+        //获取PDF页数
+        int paperP = 0;
+        try (FileInputStream fis = new FileInputStream(paperPdfPath);) {
+            PdfReader pdfReader = new PdfReader(fis);
+            paperP = pdfReader.getNumberOfPages();
+        } catch (IOException e) {
+            log.error(e.getMessage());
+        }
+
         CoursePaper oldCoursePaper = coursePaperRepository.findByExamIdAndPaperId(coursePaper.getExamId(), coursePaper.getPaperId());
         CoursePaper oldCoursePaper = coursePaperRepository.findByExamIdAndPaperId(coursePaper.getExamId(), coursePaper.getPaperId());
         if (oldCoursePaper != null) {
         if (oldCoursePaper != null) {
             //存在则修改
             //存在则修改
             oldCoursePaper.setPaperName(coursePaper.getPaperName());
             oldCoursePaper.setPaperName(coursePaper.getPaperName());
             oldCoursePaper.setPaperHtmlUrl(paperHtmlUrl);
             oldCoursePaper.setPaperHtmlUrl(paperHtmlUrl);
             oldCoursePaper.setAnswerHtmlUrl(answerHtmlUrl);
             oldCoursePaper.setAnswerHtmlUrl(answerHtmlUrl);
-            oldCoursePaper.setPaperPdfUrl(coursePaper.getPaperPdfUrl());//todo
-            oldCoursePaper.setAnswerPdfUrl(coursePaper.getAnswerPdfUrl());//todo
-            oldCoursePaper.setPaperP(coursePaper.getPaperP());//todo
+            oldCoursePaper.setPaperPdfUrl(paperPdfUrl);
+            oldCoursePaper.setAnswerPdfUrl(answerPdfUrl);
+            oldCoursePaper.setPaperP(paperP);
             coursePaperRepository.save(oldCoursePaper);
             coursePaperRepository.save(oldCoursePaper);
-
-            //更新试卷试题结构
-            questionStructureService.savePaperQuestionStructure(coursePaper.getExamId(), coursePaper.getPaperId());
-            return;
+        } else {
+            //否则新增
+            coursePaper.setPaperHtmlUrl(paperHtmlUrl);
+            coursePaper.setAnswerHtmlUrl(answerHtmlUrl);
+            coursePaper.setPaperPdfUrl(paperPdfUrl);
+            coursePaper.setAnswerPdfUrl(answerPdfUrl);
+            coursePaper.setPaperP(paperP);
+            coursePaperRepository.save(coursePaper);
         }
         }
-        //否则新增
-        coursePaperRepository.save(coursePaper);
-        //保存试卷试题结构
+
+        //保存或更新试卷试题结构
         questionStructureService.savePaperQuestionStructure(coursePaper.getExamId(), coursePaper.getPaperId());
         questionStructureService.savePaperQuestionStructure(coursePaper.getExamId(), coursePaper.getPaperId());
 
 
         //如果当前考试课程"只有一种试卷类型"且"未分配试卷",则默认分配该试卷
         //如果当前考试课程"只有一种试卷类型"且"未分配试卷",则默认分配该试卷
@@ -130,6 +155,7 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         if (statistics == null || statistics.isEmpty()) {
         if (statistics == null || statistics.isEmpty()) {
             return;
             return;
         }
         }
+
         for (CourseStatistic statistic : statistics) {
         for (CourseStatistic statistic : statistics) {
             if (PaperStatus.已有.getIndex() == statistic.getPaperStatus()) {
             if (PaperStatus.已有.getIndex() == statistic.getPaperStatus()) {
                 //跳过已分配的
                 //跳过已分配的