Explorar o código

修复套题小题分的bug

宋悦 %!s(int64=7) %!d(string=hai) anos
pai
achega
c30535c17f

+ 90 - 0
cqb-comm-utils/src/main/java/com/qmth/cqb/utils/CommonUtils.java

@@ -15,6 +15,7 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
+import org.apache.commons.lang3.StringEscapeUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.dom4j.Attribute;
 import org.dom4j.Document;
@@ -275,6 +276,95 @@ public final class CommonUtils {
         BigDecimal formatNumber = new BigDecimal(number);
         return formatNumber.setScale(2, RoundingMode.HALF_UP).doubleValue();
     }
+
+    public static String repairHtmlStr(String htmlStr)throws Exception{
+        htmlStr = htmlStr.trim();
+        if(htmlStr.toLowerCase().contains("<!doctype html ")){
+            int index1 = htmlStr.toLowerCase().indexOf("<!doctype html ");
+            int index2 = htmlStr.indexOf('>',index1 + 1);
+            htmlStr = htmlStr.substring(0, index1) + htmlStr.substring(index2 + 1);
+        }
+        while(htmlStr.toLowerCase().contains("<br ")){
+            int index1 = htmlStr.toLowerCase().indexOf("<br ");
+            int index2 = htmlStr.toLowerCase().indexOf(">",index1 + 1);
+            htmlStr = htmlStr.substring(0, index1) + "<br/>" + htmlStr.substring(index2 + 1);
+        }
+        while(htmlStr.toLowerCase().endsWith("<br>") || htmlStr.toLowerCase().endsWith("<br/>")){
+            if(htmlStr.toLowerCase().endsWith("<br>")){
+                htmlStr = htmlStr.substring(0, htmlStr.length()-"<br>".length());
+            }else if(htmlStr.toLowerCase().endsWith("<br/>")){
+                htmlStr = htmlStr.substring(0, htmlStr.length()-"<br/>".length());
+            }
+        }
+        htmlStr = htmlStr.replace("<br>", "<br/>").replace("<BR>", "<br/>");
+
+        {//补全META标签
+            int imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ");
+            while(imgIndex > 0){
+                int flag = htmlStr.indexOf(">", imgIndex);
+                if(htmlStr.charAt(flag - 1) != '/'){
+                    htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
+                }
+                imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ",flag);
+            }
+        }
+
+        {//补全img标签
+            int imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ");
+            while(imgIndex > 0){
+                int flag = htmlStr.indexOf(">", imgIndex);
+                if(htmlStr.charAt(flag - 1) != '/'){
+                    htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
+                }
+                imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ",flag);
+            }
+        }
+        //添加body标签
+        if(!htmlStr.toLowerCase().contains("<body")){
+            htmlStr = "<body>"+htmlStr+"</body>";
+        }
+        return new String(htmlStr.getBytes("UTF-8"));
+    }
+
+    /**
+     * 从指定的位置开始查找第一个匹配正则表达式的字符串的位置
+     * @param str
+     * @param regex 正则表达式
+     * @param fromIndex 指定的起始位置
+     * @return
+     */
+    public static int indexOfRegex(String str,String regex,int fromIndex){
+        int index = indexOfRegex(str.substring(fromIndex),regex);
+        if(index < 0){
+            return -1;
+        }
+        return fromIndex + index;
+    }
+
+    /**
+     * 查找第一个匹配正则表达式的字符串的位置
+     * @param str
+     * @param regex 正则表达式
+     * @return
+     */
+    public static int indexOfRegex(String str,String regex){
+        Pattern p = Pattern.compile(regex);
+        Matcher m = p.matcher(str);
+        if(m.find()){
+            return m.start();
+        }else{
+            return -1;
+        }
+    }
+
+    public static String formatHtml(String htmlStr)throws Exception{
+        if(StringUtils.isEmpty(htmlStr)){
+            return "";
+        }
+        htmlStr = repairHtmlStr(htmlStr);
+        htmlStr = StringEscapeUtils.unescapeHtml4(htmlStr);
+        return htmlStr;
+    }
     
 
     public static void main(String[] args) {

+ 12 - 0
cqb-paper/src/main/java/com/qmth/cqb/paper/service/impl/PaperServiceImpl.java

@@ -785,6 +785,18 @@ public class PaperServiceImpl implements PaperService{
         for (Question ques : questions) {
             PaperDetailUnit pdu = new PaperDetailUnit(paper,paperDetail,ques);
             pdu.setNumber(paperDetailUnit.getNumber());//设置为大题中最大的number
+            pdu.setScore(paperDetailUnit.getScore());
+            //处理套题
+            if(pdu.getQuestionType() == QuesStructType.NESTED_ANSWER_QUESTION){
+                List<Question> subQuestions = ques.getSubQuestions();
+                List<Double> subScoreList = new ArrayList<>();
+                if(subQuestions != null && subQuestions.size() > 0){
+                    for(Question subQuestion:subQuestions){
+                        subScoreList.add(CommonUtils.formatDouble(pdu.getScore()/subQuestions.size()));
+                    }
+                }
+                pdu.setSubScoreListNew(subScoreList);
+            }
             saveUnits.add(pdu);
         }
         paperDetailUnitRepo.save(saveUnits);

+ 5 - 5
cqb-question-resource/src/main/java/com/qmth/cqb/question/service/impl/QuesServiceImpl.java

@@ -329,15 +329,15 @@ public class QuesServiceImpl implements QuesService{
     }
 
     public void updateQuesWordUnit(WordprocessingMLPackage wordMLPackage, Question question) throws Exception {
-        String quesBody = StringEscapeUtils.unescapeHtml4(question.getQuesBody());
-        String quesAnswer = StringEscapeUtils.unescapeHtml4(question.getQuesAnswer());
+        String quesBody = question.getQuesBody();
+        String quesAnswer = question.getQuesAnswer();
         if(!StringUtils.isEmpty(quesBody)){
-            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, quesBody));
+            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(quesBody)));
         }
         if(!StringUtils.isEmpty(quesAnswer)
                 && (question.getQuestionType() == QuesStructType.FILL_BLANK_QUESTION
                 || question.getQuestionType() == QuesStructType.TEXT_ANSWER_QUESTION)){
-            question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, quesAnswer));
+            question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(quesAnswer)));
         }
         if(!StringUtils.isEmpty(quesAnswer)&& 
         		(question.getQuestionType() == QuesStructType.SINGLE_ANSWER_QUESTION
@@ -348,7 +348,7 @@ public class QuesServiceImpl implements QuesService{
         if (quesOptions != null && quesOptions.size() > 0) {
             for (QuesOption quesOption : quesOptions) {
                 quesOption.setOptionBodyWord(DocxProcessUtil.html2Docx(wordMLPackage,
-                        StringEscapeUtils.unescapeHtml4(quesOption.getOptionBody())));
+                        CommonUtils.formatHtml(quesOption.getOptionBody())));
             }
         }
         byte [] pkgByte = DocxProcessUtil.getPkgByte(wordMLPackage);