From 21ff1b4f2ef67975eef303bff66ef60388243cf1 Mon Sep 17 00:00:00 2001 From: qirong <2032486488@qq.com> Date: 星期三, 05 七月 2023 16:42:03 +0800 Subject: [PATCH] 随机试卷修改和统计 --- src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java | 182 +++++++++++++++++++++++++++++++++++++++------ 1 files changed, 156 insertions(+), 26 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 b98b439..4028398 100644 --- a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java +++ b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java @@ -1,5 +1,6 @@ package com.mindskip.xzs.service.impl; +import com.mindskip.xzs.configuration.spring.exception.QuestionException; import com.mindskip.xzs.domain.*; import com.mindskip.xzs.domain.TextContent; import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum; @@ -18,6 +19,7 @@ import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM; +import com.mindskip.xzs.viewmodel.admin.exam.QuestionTypeVM; import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM; import com.mindskip.xzs.viewmodel.student.dashboard.PaperFilter; import com.mindskip.xzs.viewmodel.student.dashboard.PaperInfo; @@ -84,21 +86,22 @@ @Override @Transactional - public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) { + public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) throws QuestionException { ActionEnum actionEnum = (examPaperEditRequestVM.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; Date now = new Date(); List<ExamPaperTitleItemObject> frameTextContentList = new ArrayList<>(); List<ExamPaperTitleItemVM> titleItemsVM = new ArrayList<>(); //闅忔満璇曞嵎 - if (examPaperEditRequestVM.getSubjectSource() != null) { + if (examPaperEditRequestVM.getQuestionTypeVMS().size() != 0) { // Map<Integer, Integer> questionList = questionService.getAll().stream().collect(Collectors.toMap(Question::getId, Question::getScore)); - randomQuestion(examPaperEditRequestVM, frameTextContentList, titleItemsVM); +// randomQuestion(examPaperEditRequestVM, frameTextContentList, titleItemsVM); + randomQuestionType(examPaperEditRequestVM, frameTextContentList, titleItemsVM); } - if (titleItemsVM.size() < 0) { + if (titleItemsVM.size() == 0) { titleItemsVM = examPaperEditRequestVM.getTitleItems(); } - if (frameTextContentList.size() < 0) { + if (frameTextContentList.size() == 0) { frameTextContentList = frameTextContentFromVM(titleItemsVM); } @@ -145,14 +148,18 @@ .map(q -> q.getId())) .collect(Collectors.toList()); List<Question> questions = questionMapper.selectByIds(questionIds); + Integer order = 0; + Set<Integer> generatedNumbers = new HashSet<>(); + Random random = new Random(); List<ExamPaperTitleItemVM> examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> { ExamPaperTitleItemVM tTitleVM = modelMapper.map(t, ExamPaperTitleItemVM.class); List<QuestionEditRequestVM> questionItemsVM = t.getQuestionItems().stream().map(i -> { - Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get(); - QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question); - questionEditRequestVM.setItemOrder(i.getItemOrder()); - return questionEditRequestVM; - }).collect(Collectors.toList()); + Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get(); + QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question); + questionEditRequestVM.setItemOrder(getRandomNumber(t.getQuestionItems().size() - 1, generatedNumbers, random)); + return questionEditRequestVM; + }).sorted(Comparator.comparing(QuestionEditRequestVM::getItemOrder)) + .collect(Collectors.toList()); tTitleVM.setQuestionItems(questionItemsVM); return tTitleVM; }).collect(Collectors.toList()); @@ -199,6 +206,11 @@ return examPaperMapper.removeByIds(ids); } + @Override + public List<ExamPaper> gets(Integer[] ids) { + return examPaperMapper.gets(ids); + } + private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List<ExamPaperTitleItemVM> titleItemsVM) { // Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVM.getSubjectId()); Integer questionCount = titleItemsVM.stream() @@ -234,6 +246,9 @@ } private void addExamPaperDepartment(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) { + if (examPaperEditRequestVM.getDepartmentIds().length == 0) { + return; + } List<ExamPaperDepartment> list = Arrays.asList(examPaperEditRequestVM.getDepartmentIds()).stream().map(e -> { ExamPaperDepartment examPaperDepartment = new ExamPaperDepartment(); examPaperDepartment.setExamPaperId(examPaper.getId()); @@ -256,13 +271,12 @@ } /** - * * @param result * @param scoresMap * @param quantity * @param totalScore */ - public static void selectRandomScores(Map<Integer, Integer> result, Map<Integer, Integer> scoresMap, int quantity, int totalScore) { + public static void selectRandomScores(Map<Integer, Integer> result, Map<Integer, Integer> scoresMap, int quantity, int totalScore) throws QuestionException { // Map<String, Integer> selectedScoresMap = new HashMap<>(); if (quantity <= 0 || totalScore <= 0 || scoresMap.isEmpty()) { @@ -270,13 +284,16 @@ } List<Integer> keys = new ArrayList<>(scoresMap.keySet()); - List<Integer> values = scoresMap.values().stream().map(e->e=e/10).collect(Collectors.toList()); + List<Integer> values = scoresMap.values().stream().map(e -> e = e / 10).collect(Collectors.toList()); Random random = new Random(); int remainingQuantity = quantity; int remainingScore = totalScore; for (int i = 0; i < quantity - 1; i++) { + if (i >= scoresMap.size()) { + throw new QuestionException(); + } int index = random.nextInt(values.size()); int score = values.get(index); @@ -292,7 +309,7 @@ if (!values.isEmpty()) { int index = random.nextInt(values.size()); int score = values.get(index); - result.put(keys.get(index), remainingScore); // 鏈�鍚庝竴涓垎鏁颁负鍓╀綑鐨勫垎鏁� + result.put(keys.get(index), score); // 鏈�鍚庝竴涓垎鏁颁负鍓╀綑鐨勫垎鏁� } } @@ -304,27 +321,29 @@ * @param frameTextContentList * @param titleItemsVM title */ - private void randomQuestion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) { + private void randomQuestion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException { Map<Integer, Integer> subjectSource = examPaperEditRequestVM.getSubjectSource(); //鎬绘暟閲� Integer sum = subjectSource.values().stream().mapToInt(Integer::intValue).sum(); - Integer avg = 0; + Integer count = 0; + Integer mark = 0; ExamPaperTitleItemObject examPaperTitleItemObject = new ExamPaperTitleItemObject(); - examPaperTitleItemObject.setName("娴嬭瘯001"); + examPaperTitleItemObject.setName(examPaperEditRequestVM.getTitleItems().get(0).getName()); ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM(); //titleItemsVM - examPaperTitleItemVM.setName("娴嬭瘯001"); + examPaperTitleItemVM.setName(examPaperEditRequestVM.getTitleItems().get(0).getName()); List<ExamPaperQuestionItemObject> examPaperQuestionItemObjectList = new ArrayList<>(); List<QuestionEditRequestVM> questionItems = new ArrayList<>(); + Integer order = 0; for (Integer key : subjectSource.keySet()) { // key锛氭暟閲� //璇ユ爣绛惧搴旂殑鍒嗘暟 澶氶�夛細3 鍒ゆ柇锛�3 鍗曢�夛細4 - Integer source = 20 * subjectSource.get(key) / sum; + Integer source = 100 * subjectSource.get(key) / sum - mark; // 澶氶�夊拰鍒ゆ柇鏁伴噺 - Integer multipleNumber = subjectSource.get(key) * 3 / 10; + Integer multipleNumber = (subjectSource.get(key) + count) * 3 / 10; //澶氶�夊拰鍒ゆ柇鍒嗘暟 Integer multipleSource = source * 3 / 10; List<Integer> questions = questionSubjectService.getSubject(key) @@ -336,26 +355,27 @@ 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); + + selectRandomScores(multiple, multipleMap, multipleNumber < multipleMap.size() ? multipleNumber : multipleMap.size(), 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); + selectRandomScores(multiple, judgmentMap, multipleNumber < judgmentMap.size() ? multipleNumber : judgmentMap.size(), multipleSource); //鍗曢�夋暟閲� Integer radioNumber = subjectSource.get(key) - multiple.size(); //鍗曢�夊垎鏁� - Integer radioSource = source - multiple.values().stream().mapToInt(Integer::intValue).sum();; + Integer radioSource = source - multiple.values().stream().mapToInt(Integer::intValue).sum(); //鍗曢�� 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); - Integer order = 0; - titleItemsVM.add(examPaperTitleItemVM); + selectRandomScores(multiple, radioMap, radioNumber < radioMap.size() ? radioNumber : radioMap.size(), radioSource); + +// titleItemsVM.add(examPaperTitleItemVM); for (Integer k : multiple.keySet()) { ExamPaperQuestionItemObject examPaperQuestionItemObject = new ExamPaperQuestionItemObject(); examPaperQuestionItemObject.setId(k); @@ -371,11 +391,121 @@ //titleItemsVM examPaperTitleItemVM.setQuestionItems(questionItems); + //鏁伴噺 鍖归厤 + count = multiple.size() != subjectSource.get(key) ? subjectSource.get(key) - multiple.size() : 0; + //鍒嗘暟鍖归厤 + mark = source != multiple.values().stream().mapToInt(Integer::intValue).sum() ? multiple.values().stream().mapToInt(Integer::intValue).sum() - source : 0; + } + frameTextContentList.add(examPaperTitleItemObject); + titleItemsVM.add(examPaperTitleItemVM); + + if (count != 0 || mark != 0) { + throw new QuestionException(); + } + + } + + /** + * 闅忔満璇曞嵎 + * + * @param examPaperEditRequestVM 浼犲弬 + * @param frameTextContentList + * @param titleItemsVM title + */ + private void randomQuestionType(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException { + //鍗曢�� + Integer singleChoice = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getSingleChoice).sum(); + //澶氶�� + Integer multipleChoice = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getMultipleChoice).sum(); + //鍒ゆ柇 + Integer judgment = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getTrueFalse).sum(); + if ((singleChoice * 4 + multipleChoice * 5 + judgment * 2) != 100) { + throw new QuestionException(); + } + + ExamPaperTitleItemObject examPaperTitleItemObject = new ExamPaperTitleItemObject(); + examPaperTitleItemObject.setName(examPaperEditRequestVM.getTitleItems().get(0).getName()); + + ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM(); + //titleItemsVM + examPaperTitleItemVM.setName(examPaperEditRequestVM.getTitleItems().get(0).getName()); + + List<ExamPaperQuestionItemObject> examPaperQuestionItemObjectList = new ArrayList<>(); + + List<QuestionEditRequestVM> questionItems = new ArrayList<>(); + + List<QuestionTypeVM> questionTypeVMList = examPaperEditRequestVM.getQuestionTypeVMS(); + Integer order = 0; + for (QuestionTypeVM questionTypeVM : questionTypeVMList) { + + List<Integer> questions = questionSubjectService.getSubject(questionTypeVM.getSubjectId()) + .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)); + Integer multipleSource = questionTypeVM.getMultipleChoice() * 5; + selectRandomScores(multiple, multipleMap, questionTypeVM.getMultipleChoice(), multipleSource); + + + //鍒ゆ柇 + Map<Integer, Integer> judgmentMap = list.stream() + .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.TrueFalse.getCode())) + .collect(Collectors.toMap(Question::getId, Question::getScore)); + Integer trueFalse = questionTypeVM.getTrueFalse() * 2; + selectRandomScores(multiple, judgmentMap, questionTypeVM.getTrueFalse(), trueFalse); + + //鍗曢�夊垎鏁� + Integer radioSource = questionTypeVM.getSingleChoice() * 4; + //鍗曢�� + Map<Integer, Integer> radioMap = list.stream() + .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.SingleChoice.getCode())) + .collect(Collectors.toMap(Question::getId, Question::getScore)); + selectRandomScores(multiple, radioMap, questionTypeVM.getSingleChoice(), radioSource); + +// titleItemsVM.add(examPaperTitleItemVM); + for (Integer k : multiple.keySet()) { + ExamPaperQuestionItemObject examPaperQuestionItemObject = new ExamPaperQuestionItemObject(); + examPaperQuestionItemObject.setId(k); + examPaperQuestionItemObject.setItemOrder(order++); + examPaperQuestionItemObjectList.add(examPaperQuestionItemObject); + //titleItemsVM + QuestionEditRequestVM questionEditRequestVM = new QuestionEditRequestVM(); + questionEditRequestVM.setScore(multiple.get(k).toString()); + questionItems.add(questionEditRequestVM); + } + + examPaperTitleItemObject.setQuestionItems(examPaperQuestionItemObjectList); + + //titleItemsVM + examPaperTitleItemVM.setQuestionItems(questionItems); } frameTextContentList.add(examPaperTitleItemObject); titleItemsVM.add(examPaperTitleItemVM); } + public Integer getRandomNumber(Integer a, Set<Integer> generatedNumbers, Random random) { + if (a <= 0) { + throw new IllegalArgumentException("a蹇呴』涓烘鏁存暟"); + } + + int range = a + 1; + + if (generatedNumbers.size() >= range) { + generatedNumbers.clear(); // 娓呯┖宸茬敓鎴愮殑鏁板瓧闆嗗悎 + } + + int randomNumber; + do { + randomNumber = random.nextInt(range); + } while (generatedNumbers.contains(randomNumber)); + + generatedNumbers.add(randomNumber); + return randomNumber; + } } -- Gitblit v1.8.0