| | |
| | | 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; |
| | |
| | | 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; |
| | | |
| | |
| | | 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; |
| | |
| | | this.subjectService = subjectService; |
| | | this.examPaperDepartmentService = examPaperDepartmentService; |
| | | this.examPaperSubjectService = examPaperSubjectService; |
| | | this.questionSubjectService = questionSubjectService; |
| | | } |
| | | |
| | | |
| | |
| | | public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) { |
| | | ActionEnum actionEnum = (examPaperEditRequestVM.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; |
| | | Date now = new Date(); |
| | | List<ExamPaperTitleItemVM> titleItemsVM = examPaperEditRequestVM.getTitleItems(); |
| | | List<ExamPaperTitleItemObject> frameTextContentList = frameTextContentFromVM(titleItemsVM); |
| | | |
| | | List<ExamPaperTitleItemObject> frameTextContentList = new ArrayList<>(); |
| | | List<ExamPaperTitleItemVM> titleItemsVM = new ArrayList<>(); |
| | | //随机试卷 |
| | | if (examPaperEditRequestVM.getSubjectSource() != null) { |
| | | // Map<Integer, Integer> questionList = questionService.getAll().stream().collect(Collectors.toMap(Question::getId, Question::getScore)); |
| | | randomQuestion(examPaperEditRequestVM, frameTextContentList, titleItemsVM); |
| | | } |
| | | if (titleItemsVM.size() < 0) { |
| | | titleItemsVM = examPaperEditRequestVM.getTitleItems(); |
| | | } |
| | | if (frameTextContentList.size() < 0) { |
| | | frameTextContentList = frameTextContentFromVM(titleItemsVM); |
| | | } |
| | | |
| | | String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList); |
| | | |
| | | ExamPaper examPaper; |
| | |
| | | examPaperDepartmentService.removeByExamPaperId(examPaper.getId()); |
| | | examPaperSubjectService.removeByExamPaperId(examPaper.getId()); |
| | | } |
| | | addExamPaperDepartment(examPaperEditRequestVM,examPaper); |
| | | addExamPaperSubject(examPaperEditRequestVM,examPaper); |
| | | addExamPaperDepartment(examPaperEditRequestVM, examPaper); |
| | | addExamPaperSubject(examPaperEditRequestVM, examPaper); |
| | | return examPaper; |
| | | } |
| | | |
| | |
| | | }).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); |
| | |
| | | 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()); |
| | |
| | | }).collect(Collectors.toList()); |
| | | examPaperSubjectService.saves(subjectList); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param result |
| | | * @param scoresMap |
| | | * @param quantity |
| | | * @param totalScore |
| | | */ |
| | | public static void selectRandomScores(Map<Integer, Integer> result, Map<Integer, Integer> scoresMap, int quantity, int totalScore) { |
| | | // Map<String, Integer> selectedScoresMap = new HashMap<>(); |
| | | |
| | | if (quantity <= 0 || totalScore <= 0 || scoresMap.isEmpty()) { |
| | | return; // 返回空Map |
| | | } |
| | | |
| | | List<Integer> keys = new ArrayList<>(scoresMap.keySet()); |
| | | 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++) { |
| | | 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); // 最后一个分数为剩余的分数 |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 随机试卷 |
| | | * |
| | | * @param examPaperEditRequestVM 传参 |
| | | * @param frameTextContentList |
| | | * @param titleItemsVM title |
| | | */ |
| | | private void randomQuestion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) { |
| | | Map<Integer, Integer> subjectSource = examPaperEditRequestVM.getSubjectSource(); |
| | | //总数量 |
| | | Integer sum = subjectSource.values().stream().mapToInt(Integer::intValue).sum(); |
| | | Integer avg = 0; |
| | | |
| | | ExamPaperTitleItemObject examPaperTitleItemObject = new ExamPaperTitleItemObject(); |
| | | examPaperTitleItemObject.setName("测试001"); |
| | | |
| | | ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM(); |
| | | //titleItemsVM |
| | | examPaperTitleItemVM.setName("测试001"); |
| | | |
| | | List<ExamPaperQuestionItemObject> examPaperQuestionItemObjectList = new ArrayList<>(); |
| | | |
| | | List<QuestionEditRequestVM> questionItems = new ArrayList<>(); |
| | | for (Integer key : subjectSource.keySet()) { // key:数量 |
| | | //该标签对应的分数 多选:3 判断:3 单选:4 |
| | | Integer source = 20 * subjectSource.get(key) / sum; |
| | | // 多选和判断数量 |
| | | Integer multipleNumber = subjectSource.get(key) * 3 / 10; |
| | | //多选和判断分数 |
| | | Integer multipleSource = source * 3 / 10; |
| | | List<Integer> questions = questionSubjectService.getSubject(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 = subjectSource.get(key) - multiple.size(); |
| | | //单选分数 |
| | | 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); |
| | | 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); |
| | | |
| | | } |
| | | |
| | | } |