|
|
@@ -3,6 +3,7 @@ package com.es.demo;
|
|
|
import com.es.demo.back.ChoiceQuestion;
|
|
|
import com.es.demo.back.TQRepository;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
@@ -11,6 +12,7 @@ import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
|
|
import org.springframework.data.elasticsearch.core.query.GetQuery;
|
|
|
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
|
|
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
|
|
|
+import org.springframework.util.Assert;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import sun.security.util.Length;
|
|
|
|
|
|
@@ -30,11 +32,12 @@ import java.util.stream.Stream;
|
|
|
@RequestMapping("/rest")
|
|
|
public class WebRestfulController {
|
|
|
private ElasticsearchOperations elasticsearchOperations;
|
|
|
- private final TQRepository tqRepository;
|
|
|
@Autowired
|
|
|
- public WebRestfulController(ElasticsearchOperations elasticsearchOperations, TQRepository tqRepository) {
|
|
|
+ @Qualifier("TQRepositoryImp")
|
|
|
+ private TQRepository tqRepository;
|
|
|
+ @Autowired
|
|
|
+ public WebRestfulController(ElasticsearchOperations elasticsearchOperations) {
|
|
|
this.elasticsearchOperations = elasticsearchOperations;
|
|
|
- this.tqRepository = tqRepository;
|
|
|
}
|
|
|
|
|
|
@PostMapping("/tq")
|
|
|
@@ -67,16 +70,31 @@ public class WebRestfulController {
|
|
|
public String saveAll(@RequestParam String chapeter,@RequestBody String content) {
|
|
|
try {
|
|
|
Map<Integer,ChoiceQuestion> questions=new LinkedHashMap<>();
|
|
|
+ final int answerStartPos = content.lastIndexOf("## 参考答案与解析");
|
|
|
+ final int choiceStartPos = content.lastIndexOf("## 单项选择题");
|
|
|
+ final int completionStartPos = content.lastIndexOf("## 填空题");
|
|
|
+ final int shortAnswerStartPos = content.lastIndexOf("## 简答题");
|
|
|
|
|
|
- SetQuestion(chapeter, content, questions);
|
|
|
+ int choiceStartPosEnd=content.length();
|
|
|
+ if(answerStartPos!=-1){choiceStartPosEnd=answerStartPos;}
|
|
|
+ if(shortAnswerStartPos!=-1){choiceStartPosEnd=shortAnswerStartPos;}
|
|
|
+ if(completionStartPos!=-1){choiceStartPosEnd=completionStartPos;}
|
|
|
|
|
|
- final int answerStartPos = content.lastIndexOf("## 参考答案与解析");
|
|
|
- content=content.substring(answerStartPos);
|
|
|
- SetAnswer(chapeter, content, questions);
|
|
|
+ String choiceQuestionContent=content.substring(choiceStartPos,choiceStartPosEnd);
|
|
|
+// return choiceQuestionContent;
|
|
|
+ Assert.isTrue(choiceQuestionContent.length()>8,"不存在题目!");
|
|
|
+ SetQuestion(chapeter, choiceQuestionContent, questions);
|
|
|
+
|
|
|
+
|
|
|
+ String answerContent=content.substring(answerStartPos);
|
|
|
+ SetAnswer(chapeter, answerContent, questions);
|
|
|
|
|
|
System.out.println(questions.toString());
|
|
|
questions.forEach((k,v)->{
|
|
|
- saveChoiceQuestion(v);
|
|
|
+ if(v.questionIsLegal()){
|
|
|
+ saveChoiceQuestion(v);
|
|
|
+ }
|
|
|
+
|
|
|
});
|
|
|
return String.valueOf(questions.size());
|
|
|
}catch (Exception e){
|
|
|
@@ -85,12 +103,14 @@ public class WebRestfulController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private void SetQuestion(String chapeter,String content, Map<Integer, ChoiceQuestion> questions) {
|
|
|
int num=1;
|
|
|
String type="choice";
|
|
|
while (true){
|
|
|
String startTag="\n"+num+".";
|
|
|
- int startPos=content.indexOf(startTag);
|
|
|
+ String startTagOr="\n"+num+"\\.";
|
|
|
+ int startPos= content.contains(startTag) ?content.indexOf(startTag):content.indexOf(startTagOr);
|
|
|
if(startPos==-1){ break;}
|
|
|
content = content.substring(startPos);
|
|
|
int endPos=getEndPos(num,content);
|
|
|
@@ -137,7 +157,9 @@ public class WebRestfulController {
|
|
|
private void SetAnswer(String chapeter,String content, Map<Integer, ChoiceQuestion> questions) {
|
|
|
int num=1;
|
|
|
while (true){
|
|
|
- int startPos=content.indexOf("\n"+num+".");
|
|
|
+ String startTag="\n"+num+".";
|
|
|
+ String startTagOr="\n"+num+"\\.";
|
|
|
+ int startPos= content.contains(startTag) ?content.indexOf(startTag):content.indexOf(startTagOr);
|
|
|
if(startPos==-1){ break;}
|
|
|
content = content.substring(startPos);
|
|
|
int endPos=getEndPos(num,content);
|
|
|
@@ -146,14 +168,23 @@ public class WebRestfulController {
|
|
|
int answerStart=solve.indexOf("【答案】");
|
|
|
int keyPointsStart=solve.indexOf("【考点】");
|
|
|
int analysisStart=solve.indexOf("【解析】");
|
|
|
- questions.get(id).initAnswer( solve.substring(answerStart,keyPointsStart).trim(),
|
|
|
- solve.substring(keyPointsStart,analysisStart).trim(),
|
|
|
- solve.substring(analysisStart,solve.length()).trim());
|
|
|
+ Assert.isTrue(answerStart>0,startTag+" answerStart!=-1");
|
|
|
+ Assert.isTrue(keyPointsStart>0,startTag+" keyPointsStart!=-1");
|
|
|
+ Assert.isTrue(analysisStart>0,startTag+" analysisStart!=-1");
|
|
|
+ final ChoiceQuestion choiceQuestion = questions.get(id);
|
|
|
+ if(choiceQuestion!=null) {
|
|
|
+ choiceQuestion.initAnswer(solve.substring(answerStart, keyPointsStart).trim(),
|
|
|
+ solve.substring(keyPointsStart, analysisStart).trim(),
|
|
|
+ solve.substring(analysisStart, solve.length()).trim());
|
|
|
+ }
|
|
|
num++;
|
|
|
}
|
|
|
}
|
|
|
private int getEndPos(int num, String content){
|
|
|
- int nextQ=content.indexOf("\n"+(num+1)+".");
|
|
|
+
|
|
|
+ String startTag="\n"+(num+1)+".";
|
|
|
+ String startTagOr="\n"+(num+1)+"\\.";
|
|
|
+ int nextQ= content.contains(startTag) ?content.indexOf(startTag):content.indexOf(startTagOr);
|
|
|
int nextC=content.indexOf("\n## ");
|
|
|
if(nextQ==-1&&nextC==-1){
|
|
|
return content.length();
|