Browse Source

fix appendScoreToQuestionWordBody

deason 6 years ago
parent
commit
3cccc70484

+ 72 - 41
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/export/ExportPaperAbstractService.java

@@ -1,6 +1,7 @@
 package cn.com.qmth.examcloud.core.questions.service.export;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
 import cn.com.qmth.examcloud.core.questions.base.FileDisposeUtil;
 import cn.com.qmth.examcloud.core.questions.base.Model;
@@ -27,6 +28,8 @@ import org.docx4j.XmlUtils;
 import org.docx4j.jaxb.Context;
 import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
 import org.docx4j.wml.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.domain.Example;
@@ -51,6 +54,7 @@ import java.util.stream.Collectors;
  * @description 导出、上传文件service 父类
  */
 public abstract class ExportPaperAbstractService {
+    private static final Logger log = LoggerFactory.getLogger(ExportPaperAbstractService.class);
 
     @Autowired
     protected PaperRepo paperRepo;
@@ -801,38 +805,6 @@ public abstract class ExportPaperAbstractService {
         option.setOptionBodyWord(pWordMl.toString());
     }
 
-    /**
-     * 在Word题干上设置分数
-     */
-    public String appendScoreToWordQuestionBody(String quesBodyWordMl, double score) throws Exception {
-        String totalScore = BigDecimal.valueOf(score).stripTrailingZeros().toPlainString();
-        String scores = CommonUtils.clearZeroPoint(totalScore);
-        String tmpStr = DocxProcessUtil.BODY_HEADER + quesBodyWordMl + DocxProcessUtil.BODY_TAIL;
-        Body body = (Body) XmlUtils.unmarshalString(tmpStr, Context.jc, Body.class);
-        List<Object> pList = body.getContent();
-        int index = 0;
-        for (Object pObj : pList) {
-            if (index == pList.size() - 1) {
-                P p = (P) pObj;
-                List<Object> pContent = p.getContent();
-                R run = new R();
-                Text text = new Text();
-                text.setValue("(" + scores + "分)");
-                run.getContent().add(text);
-                pContent.add(run);
-
-            }
-            index++;
-        }
-        StringBuffer pWordMl = new StringBuffer();
-        for (Object pObj : pList) {
-            if (pObj instanceof P) {
-                pWordMl.append(DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj)));
-            }
-        }
-        return pWordMl.toString();
-    }
-
     /**
      * 设置大题标题
      */
@@ -1463,7 +1435,7 @@ public abstract class ExportPaperAbstractService {
         sortPaperDetailUnits(exportPaper.getPaperDetails());
 
         //如果每个小题分数不一样,题干后面添加分数
-        setQuestionScoreForHtml(exportPaper.getPaperDetails());
+        setQuestionScore(exportPaper.getPaperDetails(), true);
 
         //处理Word的题干选项
         setQuestionNumberForWord(exportPaper.getPaperDetails());
@@ -1483,7 +1455,7 @@ public abstract class ExportPaperAbstractService {
         sortPaperDetailUnits(previewPaper.getPaperDetails());
 
         //如果每个小题分数不一致,题干后面添加分数;一致时则不用加
-        setQuestionScoreForHtml(previewPaper.getPaperDetails());
+        setQuestionScore(previewPaper.getPaperDetails(), false);
 
         //处理Html的题干选项
         setQuestionNumberForHtml(previewPaper.getPaperDetails());
@@ -1552,9 +1524,58 @@ public abstract class ExportPaperAbstractService {
     }
 
     /**
-     * 题干上设置分数内容
+     * 题干上设置分数内容(Word)
      */
-    public String appendScoreToQuestionBodyHtml(String quesBodyHtml, double score) {
+    public String appendScoreToQuestionWordBody(String wordStr, double score) {
+        String totalScore = BigDecimal.valueOf(score).stripTrailingZeros().toPlainString();
+        String scores = CommonUtils.clearZeroPoint(totalScore);
+
+        String tmpStr = DocxProcessUtil.BODY_HEADER + wordStr + DocxProcessUtil.BODY_TAIL;
+        Body body;
+        try {
+            body = (Body) XmlUtils.unmarshalString(tmpStr, Context.jc, Body.class);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw new StatusException("500", "Word解析异常!");
+        }
+
+        List<Object> pList = body.getContent();
+        int index = 0;
+        for (Object pObj : pList) {
+            if (index == pList.size() - 1) {
+                List<Object> pContent = ((P) pObj).getContent();
+
+                R run = new R();
+                Text text = new Text();
+                text.setValue("(" + scores + "分)");
+                run.getContent().add(text);
+                pContent.add(run);
+            }
+            index++;
+        }
+
+        StringBuffer pWord = new StringBuffer();
+        for (Object pObj : pList) {
+            if (pObj instanceof P) {
+                String value;
+                try {
+                    value = DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj));
+                } catch (Exception e) {
+                    log.error(e.getMessage());
+                    throw new StatusException("500", "Word解析异常!");
+                }
+
+                pWord.append(value);
+            }
+        }
+
+        return pWord.toString();
+    }
+
+    /**
+     * 题干上设置分数内容(Html)
+     */
+    public String appendScoreToQuestionHtmlBody(String quesBodyHtml, double score) {
         quesBodyHtml = quesBodyHtml.replaceAll("<p>", "").replaceAll("</p>", "");
         quesBodyHtml = quesBodyHtml.replaceAll("<span>", "").replaceAll("</span>", "");
         return "<p>" + quesBodyHtml + "(" + score + "分)</p>";
@@ -1563,7 +1584,7 @@ public abstract class ExportPaperAbstractService {
     /**
      * 如果每个小题分数不一致,题干后面添加分数;一致时则不用加
      */
-    public void setQuestionScoreForHtml(List<PaperDetailExp> details) {
+    public void setQuestionScore(List<PaperDetailExp> details, boolean isWord) {
         if (CollectionUtils.isEmpty(details)) {
             return;
         }
@@ -1580,13 +1601,23 @@ public abstract class ExportPaperAbstractService {
                     for (int i = 0; i < subQuestions.size(); i++) {
                         Question subQuestion = subQuestions.get(i);
                         double subScore = unit.getSubScoreList().get(i);
-                        String bodyHtml = appendScoreToQuestionBodyHtml(subQuestion.getQuesBody(), subScore);
-                        subQuestion.setQuesBody(bodyHtml);
+                        if (isWord) {
+                            String questionBodyWord = appendScoreToQuestionWordBody(subQuestion.getQuesBodyWord(), subScore);
+                            subQuestion.setQuesBodyWord(questionBodyWord);
+                        } else {
+                            String bodyHtml = appendScoreToQuestionHtmlBody(subQuestion.getQuesBody(), subScore);
+                            subQuestion.setQuesBody(bodyHtml);
+                        }
                     }
                 } else {
                     Question question = unit.getQuestion();
-                    String bodyHtml = appendScoreToQuestionBodyHtml(question.getQuesBody(), unit.getScore());
-                    question.setQuesBody(bodyHtml);
+                    if (isWord) {
+                        String questionBodyWord = appendScoreToQuestionWordBody(question.getQuesBodyWord(), unit.getScore());
+                        question.setQuesBodyWord(questionBodyWord);
+                    } else {
+                        String bodyHtml = appendScoreToQuestionHtmlBody(question.getQuesBody(), unit.getScore());
+                        question.setQuesBody(bodyHtml);
+                    }
                 }
             }
         }