|
|
@@ -0,0 +1,105 @@
|
|
|
+package cn.reghao.tnb.content.app.vod.service;
|
|
|
+
|
|
|
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.tnb.content.app.ContentApplication;
|
|
|
+import cn.reghao.tnb.content.app.exam.db.mapper.QuestionMapper;
|
|
|
+import cn.reghao.tnb.content.app.exam.db.mapper.QuestionOptionMapper;
|
|
|
+import cn.reghao.tnb.content.app.exam.model.constant.QuestionType;
|
|
|
+import cn.reghao.tnb.content.app.exam.model.po.Question;
|
|
|
+import cn.reghao.tnb.content.app.exam.model.po.QuestionOption;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
+import lombok.Setter;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.ActiveProfiles;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2025-08-05 21:28:11
|
|
|
+ */
|
|
|
+@ActiveProfiles("dev")
|
|
|
+@SpringBootTest(classes = ContentApplication.class)
|
|
|
+public class ExamTest {
|
|
|
+ @Autowired
|
|
|
+ QuestionMapper questionMapper;
|
|
|
+ @Autowired
|
|
|
+ QuestionOptionMapper questionOptionMapper;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void questionTest() {
|
|
|
+ String filePath = "/home/reghao/Downloads/xxqg.json";
|
|
|
+ JsonObject jsonObject = JsonConverter.jsonFileToObject(new File(filePath), JsonObject.class);
|
|
|
+ List<AhaQuestion> questionList = new ArrayList<>();
|
|
|
+ jsonObject.entrySet().forEach(entry -> {
|
|
|
+ AhaQuestion question1 = new AhaQuestion();
|
|
|
+ JsonElement jsonElement = entry.getValue();
|
|
|
+ String correctAnswer = jsonElement.getAsString();
|
|
|
+ question1.setCorrectAnswer(correctAnswer);
|
|
|
+
|
|
|
+ String key = entry.getKey();
|
|
|
+ String[] arr = key.split("\\|");
|
|
|
+ for (int i = 1; i < arr.length; i++) {
|
|
|
+ question1.getAnswers().add(arr[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ String question = arr[0];
|
|
|
+ if (question.contains("来源:")) {
|
|
|
+ String[] arr1 = question.split("来源:");
|
|
|
+ question1.setTitle(arr1[0]);
|
|
|
+ question1.setSource("来源:" + arr1[1]);
|
|
|
+ } else {
|
|
|
+ question1.setTitle(question);
|
|
|
+ }
|
|
|
+ questionList.add(question1);
|
|
|
+ });
|
|
|
+
|
|
|
+ List<Question> list = new ArrayList<>();
|
|
|
+ for (int i = 0; i < questionList.size(); i++) {
|
|
|
+ AhaQuestion ahaQuestion = questionList.get(i);
|
|
|
+ int questionId = i+1;
|
|
|
+ String content = ahaQuestion.getTitle();
|
|
|
+ String extra = ahaQuestion.getSource();
|
|
|
+ String correctAnswer = ahaQuestion.getCorrectAnswer();
|
|
|
+
|
|
|
+ int questionType = QuestionType.QUESTION1.getCode();
|
|
|
+ int size = ahaQuestion.getAnswers().size();
|
|
|
+ if (size == 2 && (correctAnswer.equals("正确") || correctAnswer.equals("错误"))) {
|
|
|
+ questionType = QuestionType.QUESTION4.getCode();
|
|
|
+ }
|
|
|
+
|
|
|
+ Question question = new Question(questionId, content, questionType, extra);
|
|
|
+ List<String> answers = ahaQuestion.getAnswers();
|
|
|
+ List<QuestionOption> questionOptionList = new ArrayList<>();
|
|
|
+ for (int j = 0; j < answers.size(); j++) {
|
|
|
+ String answer = answers.get(j);
|
|
|
+ boolean correct = answer.equalsIgnoreCase(correctAnswer);
|
|
|
+ int pos = j+1;
|
|
|
+
|
|
|
+ QuestionOption questionOption = new QuestionOption(questionId, answer, pos, correct);
|
|
|
+ questionOptionList.add(questionOption);
|
|
|
+ }
|
|
|
+ question.setQuestionOptions(questionOptionList);
|
|
|
+
|
|
|
+ questionMapper.save(question);
|
|
|
+ questionOptionMapper.saveAll(questionOptionList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @NoArgsConstructor
|
|
|
+ @Setter
|
|
|
+ @Getter
|
|
|
+ class AhaQuestion {
|
|
|
+ private String title;
|
|
|
+ private List<String> answers = new ArrayList<>();
|
|
|
+ private String correctAnswer;
|
|
|
+ private String source;
|
|
|
+ }
|
|
|
+}
|