package com.ots.project.exam.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.ots.common.enums.QuestionTypeEnum; import com.ots.project.exam.domain.ExamPaperAnswer; import com.ots.project.exam.domain.ExamPaperQuestionCustomerAnswer; import com.ots.project.exam.domain.ExamUser; import com.ots.project.exam.domain.TExamPaper; import com.ots.project.exam.domain.TQuestion; import com.ots.project.exam.dto.ExamPaperAnswerInfo; import com.ots.project.exam.dto.ExamPaperQuestionItemObject; import com.ots.project.exam.dto.ExamPaperTitleItemObject; import com.ots.project.exam.dto.QuestionPart; import com.ots.project.exam.dto.SubmitReport; import com.ots.project.exam.mapper.ExamPaperAnswerMapper; import com.ots.project.exam.mapper.TExamPaperMapper; import com.ots.project.exam.mapper.TQuestionMapper; import com.ots.project.exam.service.ExamPaperAnswerService; import com.ots.project.exam.service.ITTextContentService; import com.ots.project.exam.viewmodel.ExamPaperAnswerPageVM; import com.ots.project.exam.viewmodel.ExamPaperSubmitItemVM; import com.ots.project.exam.viewmodel.ExamPaperSubmitVM; import com.ots.project.tool.exam.ExamUtil; import com.ots.project.tool.exam.JsonUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @Service public class ExamPaperAnswerServiceImpl implements ExamPaperAnswerService { private static final Logger log = LoggerFactory.getLogger(ExamPaperAnswerServiceImpl.class); @Autowired private TExamPaperMapper examPaperMapper; @Autowired private ExamPaperAnswerMapper examPaperAnswerMapper; @Autowired private ITTextContentService textContentService; @Autowired private TQuestionMapper questionMapper; @Override public ExamPaperAnswerInfo calculateExamPaperAnswer(ExamPaperSubmitVM examPaperSubmitVM, ExamUser user) { ExamPaperAnswerInfo examPaperAnswerInfo = new ExamPaperAnswerInfo(); Date now = new Date(); TExamPaper examPaper = examPaperMapper.selectTExamPaperById(examPaperSubmitVM.getId().longValue()); String frameTextContent = textContentService.selectTTextContentById(examPaper.getFrameTextContentId()).getContent(); List examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent, ExamPaperTitleItemObject.class); List questionIds = examPaperTitleItemObjects.stream().flatMap(t -> t.getQuestionItems().stream().map(q -> q.getId())).collect(Collectors.toList()); List questions = questionMapper.selectByIds(questionIds); setWhichPart(examPaperSubmitVM, examPaperAnswerInfo, examPaperTitleItemObjects, questions); examPaperAnswerInfo.setExamPaper(examPaper); return examPaperAnswerInfo; } private void setWhichPart(ExamPaperSubmitVM examPaperSubmitVM, ExamPaperAnswerInfo examPaperAnswerInfo, List examPaperTitleItemObjects, List questions) { if (Objects.isNull(examPaperSubmitVM.getPartOrder())) { return; } int answerPartOrder = examPaperSubmitVM.getPartOrder(); SubmitReport submitReport = new SubmitReport(); if (Objects.isNull(answerPartOrder)) { submitReport.setJump("this"); examPaperAnswerInfo.setSubmitReport(submitReport); return; } List answerItems = examPaperSubmitVM.getAnswerItems(); Map answerContentMap = getAnswerContentMap(answerItems); Map partMap = getParts(examPaperTitleItemObjects); Map questionPartOrdeMap = getQuestionPartOrdeMap(examPaperTitleItemObjects); Map partAnswerScoreMap = new HashMap<>(); Map partQuestionScoreMap = new HashMap<>(); Map partAnswerTimeMap = new HashMap<>(); setAnswerScoreMap(questions, answerContentMap, questionPartOrdeMap, partAnswerScoreMap, partQuestionScoreMap); examPaperAnswerInfo.setSubmitReport(submitReport); } private void setAnswerScoreMap(List questions, Map answerContentMap, Map questionPartOrdeMap, Map partAnswerScoreMap, Map partQuestionScoreMap) { for (TQuestion tQuestion : questions) { int partOrder = questionPartOrdeMap.get(tQuestion.getId().intValue()); if (Objects.isNull(partQuestionScoreMap.get(partOrder))){ partQuestionScoreMap.put(partOrder, 0); } if (Objects.isNull(partAnswerScoreMap.get(partOrder))) { partAnswerScoreMap.put(partOrder, 0); } if (Objects.nonNull(answerContentMap.get(tQuestion.getId().intValue()))) { setAnswerScoreMap(partQuestionScoreMap, tQuestion, partOrder); } if (Objects.equals(tQuestion.getCorrect(), answerContentMap.get(tQuestion.getId().intValue()))) { setAnswerScoreMap(partAnswerScoreMap, tQuestion, partOrder); } } } private Map getQuestionPartOrdeMap(List examPaperTitleItemObjects) { Map questionPartOrdeMap = new HashMap<>(); for (ExamPaperTitleItemObject examPaperTitleItemObject : examPaperTitleItemObjects) { List questionItems = examPaperTitleItemObject.getQuestionItems(); for (ExamPaperQuestionItemObject examPaperQuestionItemObject : questionItems) { questionPartOrdeMap.put(examPaperQuestionItemObject.getId(), examPaperQuestionItemObject.getPartOrder()); } } return questionPartOrdeMap; } private Map getParts(List examPaperTitleItemObjects) { List parts = examPaperTitleItemObjects.get(0).getParts(); Map partMap = new HashMap<>(); for (QuestionPart questionPart : parts) { partMap.put(questionPart.getPartOrder(), questionPart); } return partMap; } private Map getAnswerContentMap(List answerItems) { Map answerContentMap = new HashMap<>(); answerItems.stream().forEach(answerItem ->{ answerContentMap.put(answerItem.getQuestionId(), answerItem.getContent()); }); return answerContentMap; } private void setAnswerScoreMap(Map partQuestionScoreMap, TQuestion tQuestion, int partOrder) { int questonScore = Objects.nonNull(tQuestion.getScore()) ? tQuestion.getScore().intValue() : 0; if (Objects.nonNull(partQuestionScoreMap.get(partOrder))) { int score = partQuestionScoreMap.get(partOrder).intValue() + questonScore; partQuestionScoreMap.put(partOrder, score); } else { partQuestionScoreMap.put(partOrder, questonScore); } } private ExamPaperQuestionCustomerAnswer ExamPaperQuestionCustomerAnswerFromVM(TQuestion question, ExamPaperSubmitItemVM customerQuestionAnswer, TExamPaper examPaper, Integer itemOrder, ExamUser user, Date now) { ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = new ExamPaperQuestionCustomerAnswer(); examPaperQuestionCustomerAnswer.setQuestionId(question.getId().intValue()); examPaperQuestionCustomerAnswer.setExamPaperId(examPaper.getId().intValue()); examPaperQuestionCustomerAnswer.setItemOrder(itemOrder); examPaperQuestionCustomerAnswer.setCreateTime(now); examPaperQuestionCustomerAnswer.setCreateUser(user.getId()); examPaperQuestionCustomerAnswer.setQuestionType(question.getQuestionType().intValue()); examPaperQuestionCustomerAnswer.setQuestionTextContentId(question.getInfoTextContentId().intValue()); if (null == customerQuestionAnswer) { examPaperQuestionCustomerAnswer.setCustomerScore(0); } else { setSpecialFromVM(examPaperQuestionCustomerAnswer, question, customerQuestionAnswer); } return examPaperQuestionCustomerAnswer; } private void setSpecialFromVM(ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer, TQuestion question, ExamPaperSubmitItemVM customerQuestionAnswer) { QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromCode(examPaperQuestionCustomerAnswer.getQuestionType()); switch (questionTypeEnum) { case SingleChoice: case TrueFalse: examPaperQuestionCustomerAnswer.setAnswer(customerQuestionAnswer.getContent()); examPaperQuestionCustomerAnswer.setDoRight(question.getCorrect().equals(customerQuestionAnswer.getContent())); examPaperQuestionCustomerAnswer.setCustomerScore(0); break; case MultipleChoice: String customerAnswer = ExamUtil.contentToString(customerQuestionAnswer.getContentArray()); examPaperQuestionCustomerAnswer.setAnswer(customerAnswer); examPaperQuestionCustomerAnswer.setDoRight(customerAnswer.equals(question.getCorrect())); examPaperQuestionCustomerAnswer.setCustomerScore(0); break; case GapFilling: String correctAnswer = JsonUtil.toJsonStr(customerQuestionAnswer.getContentArray()); examPaperQuestionCustomerAnswer.setAnswer(correctAnswer); examPaperQuestionCustomerAnswer.setCustomerScore(0); break; default: examPaperQuestionCustomerAnswer.setAnswer(customerQuestionAnswer.getContent()); examPaperQuestionCustomerAnswer.setCustomerScore(0); break; } } @Override public PageInfo studentPage(ExamPaperAnswerPageVM requestVM) { return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperAnswerMapper.studentPage(requestVM)); } @Override public String judge(ExamPaperSubmitVM examPaperSubmitVM) { return null; } @Override public ExamPaperSubmitVM examPaperAnswerToVM(Integer id) { return null; } @Override public Integer selectAllCount() { return null; } @Override public List selectMothCount() { return null; } @Override public int deleteById(Integer id) { return 0; } @Override public int insert(ExamPaperAnswer record) { return examPaperAnswerMapper.insertSelective(record); } @Override public int insertByFilter(ExamPaperAnswer record) { return examPaperAnswerMapper.insertSelective(record); } @Override public ExamPaperAnswer selectById(Integer id) { return null; } @Override public int updateByIdFilter(ExamPaperAnswer record) { return 0; } @Override public int updateById(ExamPaperAnswer record) { return 0; } }