From 983a2d36e9d3ca4ff2da89bbaca687fc55e92610 Mon Sep 17 00:00:00 2001 From: qirong <2032486488@qq.com> Date: 星期二, 20 六月 2023 09:22:54 +0800 Subject: [PATCH] 随机试卷 --- src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 100 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java index cce5acb..249fa99 100644 --- a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java +++ b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java @@ -3,6 +3,7 @@ import com.mindskip.xzs.domain.*; import com.mindskip.xzs.domain.TextContent; import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum; +import com.mindskip.xzs.domain.enums.QuestionTypeEnum; import com.mindskip.xzs.domain.exam.ExamPaperQuestionItemObject; import com.mindskip.xzs.domain.exam.ExamPaperTitleItemObject; import com.mindskip.xzs.domain.other.KeyValue; @@ -31,9 +32,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Arrays; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -48,9 +47,10 @@ private final SubjectService subjectService; private final ExamPaperDepartmentService examPaperDepartmentService; private final ExamPaperSubjectService examPaperSubjectService; + private final QuestionSubjectService questionSubjectService; @Autowired - public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService) { + public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService) { super(examPaperMapper); this.examPaperMapper = examPaperMapper; this.questionMapper = questionMapper; @@ -59,6 +59,7 @@ this.subjectService = subjectService; this.examPaperDepartmentService = examPaperDepartmentService; this.examPaperSubjectService = examPaperSubjectService; + this.questionSubjectService = questionSubjectService; } @@ -86,6 +87,59 @@ public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) { ActionEnum actionEnum = (examPaperEditRequestVM.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; Date now = new Date(); + if (examPaperEditRequestVM.getSubjectSource() != null) { +// Map<Integer, Integer> questionList = questionService.getAll().stream().collect(Collectors.toMap(Question::getId, Question::getScore)); + Map<Integer, Integer> subjectSource = examPaperEditRequestVM.getSubjectSource(); + //鎬绘暟閲� + Integer sum = subjectSource.values().stream().mapToInt(Integer::intValue).sum(); + Map<Integer, Integer> map = new HashMap<>(); + Integer avg = 0; + for (Integer key : subjectSource.keySet()) { // key锛氭暟閲� + //璇ユ爣绛惧搴旂殑鍒嗘暟 澶氶�夛細3 鍒ゆ柇锛�3 鍗曢�夛細4 + Integer source = examPaperEditRequestVM.getAggregateSource() * subjectSource.get(key) / sum; + // 澶氶�夊拰鍒ゆ柇鏁伴噺 + Integer multipleNumber = key * 3 / 10; + //澶氶�夊拰鍒ゆ柇鍒嗘暟 + Integer multipleSource = source * 3 / 10; + List<Integer> questions = questionSubjectService.getQuestion(key) + .stream().map(QuestionSubject::getQuestionId).collect(Collectors.toList()); + List<Question> list = questionService.selectByIds(questions); + + Map<Integer, Integer> multiple = new HashMap<>(); + //澶氶�� + Map<Integer, Integer> multipleMap = list.stream() + .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.MultipleChoice.getCode())) + .collect(Collectors.toMap(Question::getId, Question::getScore)); + selectRandomScores(multiple, multipleMap, multipleNumber, multipleSource); + + //鍒ゆ柇 + Map<Integer, Integer> judgmentMap = list.stream() + .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.TrueFalse.getCode())) + .collect(Collectors.toMap(Question::getId, Question::getScore)); + selectRandomScores(multiple, judgmentMap, multipleNumber, multipleSource); + + //鍗曢�夋暟閲� + Integer radioNumber = key - (2 * multipleNumber); + //鍗曢�夊垎鏁� + Integer radioSource = source - (2 * multipleSource); + //鍗曢�� + Map<Integer, Integer> radioMap = list.stream() + .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.SingleChoice.getCode())) + .collect(Collectors.toMap(Question::getId, Question::getScore)); + selectRandomScores(multiple, radioMap, radioNumber, radioSource); + ExamPaperTitleItemObject examPaperTitleItemObject = new ExamPaperTitleItemObject(); + examPaperTitleItemObject.setName("娴嬭瘯001"); + Integer order = 0; + List<ExamPaperQuestionItemObject> examPaperQuestionItemObjectList = new ArrayList<>(); + for (Integer k : multiple.keySet()) { + ExamPaperQuestionItemObject examPaperQuestionItemObject = new ExamPaperQuestionItemObject(); + examPaperQuestionItemObject.setId(k); + examPaperQuestionItemObject.setItemOrder(order++); + examPaperQuestionItemObjectList.add(examPaperQuestionItemObject); + } + examPaperTitleItemObject.setQuestionItems(examPaperQuestionItemObjectList); + } + } List<ExamPaperTitleItemVM> titleItemsVM = examPaperEditRequestVM.getTitleItems(); List<ExamPaperTitleItemObject> frameTextContentList = frameTextContentFromVM(titleItemsVM); String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList); @@ -114,8 +168,8 @@ examPaperDepartmentService.removeByExamPaperId(examPaper.getId()); examPaperSubjectService.removeByExamPaperId(examPaper.getId()); } - addExamPaperDepartment(examPaperEditRequestVM,examPaper); - addExamPaperSubject(examPaperEditRequestVM,examPaper); + addExamPaperDepartment(examPaperEditRequestVM, examPaper); + addExamPaperSubject(examPaperEditRequestVM, examPaper); return examPaper; } @@ -219,8 +273,8 @@ }).collect(Collectors.toList()); } - private void addExamPaperDepartment(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper){ - List<ExamPaperDepartment> list = Arrays.asList(examPaperEditRequestVM.getDepartmentIds()).stream().map(e->{ + private void addExamPaperDepartment(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) { + List<ExamPaperDepartment> list = Arrays.asList(examPaperEditRequestVM.getDepartmentIds()).stream().map(e -> { ExamPaperDepartment examPaperDepartment = new ExamPaperDepartment(); examPaperDepartment.setExamPaperId(examPaper.getId()); examPaperDepartment.setDepartmentId(e); @@ -230,8 +284,8 @@ examPaperDepartmentService.saves(list); } - private void addExamPaperSubject(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper){ - List<ExamPaperSubject> subjectList = Arrays.asList(examPaperEditRequestVM.getSubjectId()).stream().map(e->{ + private void addExamPaperSubject(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) { + List<ExamPaperSubject> subjectList = Arrays.asList(examPaperEditRequestVM.getSubjectId()).stream().map(e -> { ExamPaperSubject examPaperSubject = new ExamPaperSubject(); examPaperSubject.setSubjectId(e); examPaperSubject.setExamPaperId(examPaper.getId()); @@ -240,4 +294,40 @@ }).collect(Collectors.toList()); examPaperSubjectService.saves(subjectList); } + + public static void selectRandomScores(Map<Integer, Integer> result, Map<Integer, Integer> scoresMap, int quantity, int totalScore) { +// Map<Integer, Integer> selectedScoresMap = new HashMap<>(); + + if (quantity <= 0 || totalScore <= 0 || scoresMap.isEmpty()) { + return; // 杩斿洖绌篗ap + } + + List<Integer> keys = new ArrayList<>(scoresMap.keySet()); + List<Integer> values = new ArrayList<>(scoresMap.values()); + + Random random = new Random(); + int remainingQuantity = quantity; + int remainingScore = totalScore; + + for (int i = 0; i < quantity - 1; i++) { + int index = random.nextInt(values.size()); + int score = values.get(index); + + if (score <= remainingScore) { + result.put(keys.get(index), score); + remainingQuantity--; + remainingScore -= score; + values.remove(index); + keys.remove(index); + } + } + + if (!values.isEmpty()) { + int index = random.nextInt(values.size()); + int score = values.get(index); + result.put(keys.get(index), remainingScore); // 鏈�鍚庝竴涓垎鏁颁负鍓╀綑鐨勫垎鏁� + } + + } + } -- Gitblit v1.8.0