Kaynağa Gözat

试卷预览改为可配置模板

xiatian 5 yıl önce
ebeveyn
işleme
dfd84593ba

+ 73 - 25
examcloud-core-questions-api-provider/src/main/java/cn/com/qmth/examcloud/core/questions/api/controller/PaperController.java

@@ -5,6 +5,7 @@ import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.questions.base.BeanCopierUtil;
 import cn.com.qmth.examcloud.core.questions.base.SpringContextUtils;
 import cn.com.qmth.examcloud.core.questions.base.StringSimilarityUtils;
+import cn.com.qmth.examcloud.core.questions.base.enums.ExportTemplateType;
 import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
 import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
 import cn.com.qmth.examcloud.core.questions.dao.ExportServiceManageRepo;
@@ -23,6 +24,8 @@ import cn.com.qmth.examcloud.core.questions.service.config.SysProperty;
 import cn.com.qmth.examcloud.core.questions.service.export.ExportPaperAbstractService;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.web.support.Naked;
+import freemarker.template.TemplateException;
+
 import com.google.gson.Gson;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
@@ -37,6 +40,8 @@ import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
+import java.io.StringWriter;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -50,7 +55,7 @@ import java.util.stream.Stream;
 @Controller
 @RequestMapping("${api_cqb}/")
 public class PaperController extends ControllerSupport {
-
+    private final static String BASE_PAGE="base_page";
     @Autowired
     PaperService paperService;
 
@@ -598,19 +603,40 @@ public class PaperController extends ControllerSupport {
         ExportPaperAbstractService exportPaperService = SpringContextUtils.getBean(ExportPaperAbstractService.class, manager.getExportServiceName());
         PaperExp previewPaper = exportPaperService.previewPaperForPDF(paper);
 
-        model.addAttribute("htmlList", PreviewPaperHandler.convertPaperHtml(previewPaper));
-        model.addAttribute("orgName", manager.getOrgName());
-        model.addAttribute("examName", examName);
-        model.addAttribute("courseNo", previewPaper.getCourseNo());
-        model.addAttribute("courseName", previewPaper.getCourseName());
-        model.addAttribute("courseLevel", previewPaper.getCourseLevel());
+//        model.addAttribute("htmlList", PreviewPaperHandler.convertPaperHtml(previewPaper));
+//        model.addAttribute("orgName", manager.getOrgName());
+//        model.addAttribute("examName", examName);
+//        model.addAttribute("courseNo", previewPaper.getCourseNo());
+//        model.addAttribute("courseName", previewPaper.getCourseName());
+//        model.addAttribute("courseLevel", previewPaper.getCourseLevel());
 
         //目前只“山东大学”和“陕师大”配了试卷模板
-        if ("sddxExportPaperService".equals(manager.getExportServiceName())
-                || "sxsfExportPaperService".equals(manager.getExportServiceName())) {
-            return String.format("%s_paper", manager.getExportServiceName());
-        } else {
-            return "default_paper";
+//        if ("sddxExportPaperService".equals(manager.getExportServiceName())
+//                || "sxsfExportPaperService".equals(manager.getExportServiceName())) {
+//            return String.format("%s_paper", manager.getExportServiceName());
+//        } else {
+//            return "default_paper";
+//        }
+        StringWriter result = new StringWriter();
+        try {
+            Map<String, Object> map = new HashMap<String, Object>();
+            map.put("htmlList", PreviewPaperHandler.convertPaperHtml(previewPaper));
+            map.put("orgName", manager.getOrgName());
+            map.put("examName", examName);
+            map.put("courseNo", previewPaper.getCourseNo());
+            map.put("courseName", previewPaper.getCourseName());
+            map.put("courseLevel", previewPaper.getCourseLevel());
+            exportPaperService.getTemplate(Long.valueOf(paper.getOrgId()), ExportTemplateType.PAPER_VIEW).process(map, result);
+            String content=result.toString();
+            model.addAttribute("pageContent", content);
+            return BASE_PAGE;
+        } catch (NumberFormatException | TemplateException | IOException e) {
+            throw new StatusException("500", "解析模板出错!");
+        }finally {
+            try {
+                result.close();
+            } catch (IOException e) {
+            }
         }
     }
 
@@ -631,19 +657,41 @@ public class PaperController extends ControllerSupport {
         ExportPaperAbstractService exportPaperService = SpringContextUtils.getBean(ExportPaperAbstractService.class, manager.getExportServiceName());
         PaperExp previewPaper = exportPaperService.previewPaperForPDF(paper);
 
-        model.addAttribute("htmlList", PreviewPaperHandler.convertPaperAnswerHtml(previewPaper));
-        model.addAttribute("orgName", manager.getOrgName());
-        model.addAttribute("examName", examName);
-        model.addAttribute("courseNo", previewPaper.getCourseNo());
-        model.addAttribute("courseName", previewPaper.getCourseName());
-        model.addAttribute("courseLevel", previewPaper.getCourseLevel());
-
-        //目前只“山东大学”和“陕师大”配了试卷模板
-        if ("sddxExportPaperService".equals(manager.getExportServiceName())
-                || "sxsfExportPaperService".equals(manager.getExportServiceName())) {
-            return String.format("%s_answer", manager.getExportServiceName());
-        } else {
-            return "default_answer";
+//        model.addAttribute("htmlList", PreviewPaperHandler.convertPaperAnswerHtml(previewPaper));
+//        model.addAttribute("orgName", manager.getOrgName());
+//        model.addAttribute("examName", examName);
+//        model.addAttribute("courseNo", previewPaper.getCourseNo());
+//        model.addAttribute("courseName", previewPaper.getCourseName());
+//        model.addAttribute("courseLevel", previewPaper.getCourseLevel());
+//
+//        //目前只“山东大学”和“陕师大”配了试卷模板
+//        if ("sddxExportPaperService".equals(manager.getExportServiceName())
+//                || "sxsfExportPaperService".equals(manager.getExportServiceName())) {
+//            return String.format("%s_answer", manager.getExportServiceName());
+//        } else {
+//            return "default_answer";
+//        }
+        
+        StringWriter result = new StringWriter();
+        try {
+            Map<String, Object> map = new HashMap<String, Object>();
+            map.put("htmlList", PreviewPaperHandler.convertPaperAnswerHtml(previewPaper));
+            map.put("orgName", manager.getOrgName());
+            map.put("examName", examName);
+            map.put("courseNo", previewPaper.getCourseNo());
+            map.put("courseName", previewPaper.getCourseName());
+            map.put("courseLevel", previewPaper.getCourseLevel());
+            exportPaperService.getTemplate(Long.valueOf(paper.getOrgId()), ExportTemplateType.ANWSER_VIEW).process(map, result);
+            String content=result.toString();
+            model.addAttribute("pageContent", content);
+            return BASE_PAGE;
+        } catch (NumberFormatException | TemplateException | IOException e) {
+            throw new StatusException("500", "解析模板出错!");
+        }finally {
+            try {
+                result.close();
+            } catch (IOException e) {
+            }
         }
     }
 

+ 1 - 0
examcloud-core-questions-starter/src/main/resources/templates/base_page.ftl

@@ -0,0 +1 @@
+${pageContent}