Explorar o código

调整上传试卷接口中内部调用方法

4228306 %!s(int64=5) %!d(string=hai) anos
pai
achega
22d5439deb

+ 5 - 0
src/main/java/com/yaoxiang/diagnosis/controller/TestController.java

@@ -41,6 +41,11 @@ public class TestController {
 
     private static final Logger logger = LoggerFactory.getLogger(TestController.class);
 
+    @GetMapping("/")
+    public String index(){
+        return "hello";
+    }
+
     @PreAuthorize("hasRole('ROLE_USER')")
     @RequestMapping(value = "/test", method = RequestMethod.GET)
     public String test() {

+ 1 - 1
src/main/java/com/yaoxiang/diagnosis/service/CommitService.java

@@ -138,7 +138,7 @@ public class CommitService {
 
         String jsonAns = ObjectUtil.object2Json(answers);
         paperCommit.setJsonAns(jsonAns);
-        paperCommitRepo.save(paperCommit);
+//        paperCommitRepo.save(paperCommit);
         logger.info("uid={},name={},pid={} commit paper success", paperCommit.getUid(), paperCommit.getName(), paperCommit.getPid());
         try {
             paperCountService.updateAvgCommitTime(paper);

+ 74 - 65
src/main/java/com/yaoxiang/diagnosis/service/PaperService.java

@@ -253,6 +253,7 @@ public class PaperService {
         return addWithQuestion(paper);
 //        return Result.ok(paper);
     }
+
     @Transactional(rollbackFor = Exception.class)
     public Result uploadPaperTest(PaperVo paperVo, Long templateId, String url, byte[] data) {
         Paper paper = init(paperVo, url);
@@ -283,62 +284,6 @@ public class PaperService {
         return Result.ok(paper);
     }
 
-    private Map<String, List<XWPFParagraph>> initSections(Paper paper, String sectionPattern, String questionPattern, String picturePattern, List<XWPFParagraph> paragraphs) {
-        //保证顺序
-        Map<String, List<XWPFParagraph>> sections = new LinkedHashMap<>();
-        String tag = "";
-        boolean betweenSectionAndQuestion = false;
-        logger.info("sectionPattern is {},questionPattern is {}",sectionPattern,questionPattern);
-        StringBuilder material = new StringBuilder();
-        int section = 0;
-        List<Section> sectionList = new ArrayList<>();
-        logger.info("initSections start");
-        boolean hasFormula = false;
-        for (XWPFParagraph paragraph : paragraphs) {
-            String text = paragraph.getText();
-            //此处应判断下是否有图片、文字或者公式
-            if (WordUtil.notContent(paragraph, text)) {
-                continue;
-            }
-            //下一个section
-            if (text.matches(sectionPattern)) {
-                logger.info("match section: text is {} sectionPattern is {}", text, sectionPattern);
-                Matcher matcher = Pattern.compile("\\d+").matcher(text);
-                //获取到section编号
-                if (matcher.find()) {
-                    section = Integer.parseInt(matcher.group());
-                }
-                //1 2 3
-                tag = String.valueOf(section);
-                betweenSectionAndQuestion = true;
-                continue;
-            }
-            if (betweenSectionAndQuestion && text.matches(questionPattern)) {
-                Section s = initSection(section, material.toString());
-                sectionList.add(s);
-                //清空背景材料
-                material = new StringBuilder();
-                betweenSectionAndQuestion = false;
-            }
-            //Session后面可能有背景材料,背景材料里面可能有图片和公式,这个放后面来处理
-            if (betweenSectionAndQuestion) {
-                //解析背景材料的图片和公式
-                wordService.parsePicture(paragraph, picturePattern);
-                hasFormula = wordService.parseFormula(paragraph);
-                material.append("<p>").append(paragraph.getText()).append("</p>");
-            }
-            //此处可能是第一段
-            if (StringUtils.isNotBlank(tag)) {
-                List<XWPFParagraph> list = sections.getOrDefault(tag, new ArrayList<>());
-                list.add(paragraph);
-                sections.put(tag, list);
-            }
-        }
-        paper.setSections(sectionList);
-        logger.info("initSections end,sections num is {}", sectionList.size());
-        return sections;
-    }
-
     private void parsePaper(Paper paper, PaperTemplate template, XWPFDocument document) {
         String tagPattern = template.getSection();
         String questionPattern = template.getQuestion();
@@ -346,10 +291,9 @@ public class PaperService {
         List<XWPFParagraph> paragraphs = document.getParagraphs();
         Assert.notEmpty(paragraphs, "未检测到段落,请检查上传的文档");
         String name = paragraphs.get(0).getText();
-        //TODO check name of paper
         parsePaperName(paper, name, template);
         //获取模块,根据模块,给段落分组
-        Map<String, List<XWPFParagraph>> sections = initSections(paper, tagPattern, questionPattern, picturePattern, paragraphs);
+        Map<String, List<XWPFParagraph>> sections = initSections(paper, template, paragraphs);
         Assert.notEmpty(sections, "未检测到模块,请检查上传的文档");
 //        List<String> topics = paperTemplateService.getTopic(template);
         Map<String, String> topic = paperTemplateService.getMapTopic(template);
@@ -368,9 +312,9 @@ public class PaperService {
 //            Section current = ss.stream().filter(section -> s.contains(section.getNumber().toString())).findFirst().orElse(null);
             Section current = sectionMap.get(s);
             //根据题目标签,给题目分组
-            Map<String, List<XWPFParagraph>> questions = initQuestions(current, questionPattern, ps);
+            Map<String, List<XWPFParagraph>> questions = initQuestions(current, template, ps);
             current.setStartNumber(currentNumber);
-            List<Question> list = parseQuestions(questions, current, template, topic, abilities);
+            List<Question> list = parseQuestions(questions, current, template, topic, paper.getUseAbility(),abilities);
             current.setNums(list.size());
             currentNumber = currentNumber + list.size();
             qs.put(s, list);
@@ -400,11 +344,68 @@ public class PaperService {
             sb.append(g).append("年级").append(first ? "上学期" : "下学期");
         }
         //取最后一个字为卷,一般为A、B卷
-        sb.append(name.substring(name.length() -1)).append("卷");
+        sb.append(name.substring(name.length() - 1)).append("卷");
         paper.setName(sb.toString());
     }
 
-    private List<Question> parseQuestions(Map<String, List<XWPFParagraph>> questions, Section section, PaperTemplate template, Map<String, String> topic, List<String> abilities) {
+    private Map<String, List<XWPFParagraph>> initSections(Paper paper, PaperTemplate template, List<XWPFParagraph> paragraphs) {
+        //保证顺序
+        Map<String, List<XWPFParagraph>> sections = new LinkedHashMap<>();
+        String tag = "";
+        boolean betweenSectionAndQuestion = false;
+        logger.info("sectionPattern is {},questionPattern is {}", template.getSection(), template.getQuestion());
+        StringBuilder material = new StringBuilder();
+        int section = 0;
+        List<Section> sectionList = new ArrayList<>();
+        logger.info("initSections start");
+        boolean hasFormula = false;
+        for (XWPFParagraph paragraph : paragraphs) {
+            String text = paragraph.getText();
+            //此处应判断下是否有图片、文字或者公式
+            if (WordUtil.notContent(paragraph, text)) {
+                continue;
+            }
+            //下一个section
+            if (text.matches(template.getSection())) {
+                logger.info("match section: text is {} sectionPattern is {}", text, template.getSection());
+                Matcher matcher = Pattern.compile("\\d+").matcher(text);
+                //获取到section编号
+                if (matcher.find()) {
+                    section = Integer.parseInt(matcher.group());
+                }
+                //1 2 3
+                tag = String.valueOf(section);
+                betweenSectionAndQuestion = true;
+                continue;
+            }
+            if (betweenSectionAndQuestion && text.matches(template.getQuestion())) {
+                Section s = initSection(section, material.toString());
+                sectionList.add(s);
+                //清空背景材料
+                material = new StringBuilder();
+                betweenSectionAndQuestion = false;
+            }
+            //Session后面可能有背景材料,背景材料里面可能有图片和公式,这个放后面来处理
+            if (betweenSectionAndQuestion) {
+                //解析背景材料的图片和公式
+                wordService.parseSubScript(paragraph);
+                wordService.parsePicture(paragraph, template.getPicturePattern());
+                hasFormula = wordService.parseFormula(paragraph);
+                material.append("<p>").append(paragraph.getText()).append("</p>");
+            }
+            //此处可能是第一段
+            if (StringUtils.isNotBlank(tag)) {
+                List<XWPFParagraph> list = sections.getOrDefault(tag, new ArrayList<>());
+                list.add(paragraph);
+                sections.put(tag, list);
+            }
+        }
+        paper.setSections(sectionList);
+        logger.info("initSections end,sections num is {}", sectionList.size());
+        return sections;
+    }
+
+    private List<Question> parseQuestions(Map<String, List<XWPFParagraph>> questions, Section section, PaperTemplate template, Map<String, String> topic, Boolean useAbility,List<String> abilities) {
         List<Question> list = new ArrayList<>();
         //问题合集
         Pattern pattern = Pattern.compile("\\d+");
@@ -412,7 +413,7 @@ public class PaperService {
             Matcher matcher = pattern.matcher(k);
             if (k.matches(template.getQuestion()) && matcher.find()) {
                 String code = matcher.group();
-                Question question = questionService.initQuestion(section.getNumber(), section.getStartNumber(), Integer.valueOf(code));
+                Question question = questionService.initQuestion(section.getNumber(), section.getStartNumber(), Integer.valueOf(code),useAbility);
                 wordService.parseQuestion(question, v, topic, template, abilities);
                 list.add(question);
             }
@@ -420,7 +421,7 @@ public class PaperService {
         return list;
     }
 
-    private Map<String, List<XWPFParagraph>> initQuestions(Section section, String questionPattern, List<XWPFParagraph> ps) {
+    private Map<String, List<XWPFParagraph>> initQuestions(Section section, PaperTemplate paperTemplate, List<XWPFParagraph> ps) {
         Map<String, List<XWPFParagraph>> questions = new ConcurrentHashMap<>();
         String tag = "";
         for (XWPFParagraph paragraph : ps) {
@@ -428,9 +429,17 @@ public class PaperService {
             if (WordUtil.notContent(paragraph, text)) {
                 continue;
             }
+            //识别上下标
+            wordService.parseSubScript(paragraph);
+            //识别图片
+            wordService.parsePicture(paragraph, paperTemplate.getPicturePattern());
+            //识别公式
+            wordService.parseFormula(paragraph);
+            //重新获取text
+            text = paragraph.getText();
             //下一个question
             // questionPattern ^#[A-Z]#(.*)
-            if (text.matches(questionPattern)) {
+            if (text.matches(paperTemplate.getQuestion())) {
                 tag = text;
             }
             logger.info("initQuestions,current tag is {},current text is {}", tag, text);

+ 4 - 1
src/main/java/com/yaoxiang/diagnosis/service/QuestionService.java

@@ -131,11 +131,14 @@ public class QuestionService {
         return question;
     }
 
-    public Question initQuestion(Integer section,Integer startNumber, Integer number) {
+    public Question initQuestion(Integer section,Integer startNumber, Integer number,Boolean useAbility) {
         Question question = new Question();
         question.setCreatetime(new Date());
         question.setScore(1D);
         question.setNumber(number);
+        if(useAbility == null || !useAbility){
+            question.setJsonScore("{\"A0\":0}");
+        }
         //更改code
         question.setCode(number - startNumber);
         question.setDuration(60);

+ 25 - 3
src/main/java/com/yaoxiang/diagnosis/word/WordService.java

@@ -93,9 +93,7 @@ public class WordService {
         StringBuilder content = new StringBuilder();
         boolean hasFormula = false;
         for (XWPFParagraph p : ps) {
-            parsePicture(p, picturePattern);
-            //TODO 图片丢失
-            hasFormula = parseFormula(p);
+
             String text = p.getText();
             //清除掉#Small#
             text = text.replaceAll(picturePattern, "");
@@ -419,4 +417,28 @@ public class WordService {
 
     }
 
+    public void parseSubScript(XWPFParagraph paragraph) {
+        Map<Integer, String> insert = new HashMap<>();
+        List<XWPFRun> runs = paragraph.getRuns();
+        for (int i = 0; i < runs.size(); i++) {
+            switch (runs.get(i).getSubscript()) {
+                case BASELINE:
+                    break;
+                case SUBSCRIPT:
+                    insert.put(i, "<sub>");
+                    insert.put(i + 1, "</sub>");
+                    break;
+                case SUPERSCRIPT:
+                    insert.put(i, "<sup>");
+                    insert.put(i + 1, "</sup>");
+                    break;
+            }
+        }
+        int i = 0;
+        for (Map.Entry<Integer, String> entry : insert.entrySet()) {
+            XWPFRun run = paragraph.insertNewRun(entry.getKey() + i++);
+            run.setText(entry.getValue());
+        }
+    }
+
 }