package com.ycl.jxkg.service.impl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ycl.jxkg.domain.*;
|
import com.ycl.jxkg.domain.enums.ExamPaperAnswerStatusEnum;
|
import com.ycl.jxkg.domain.enums.ExamPaperTypeEnum;
|
import com.ycl.jxkg.domain.enums.QuestionTypeEnum;
|
import com.ycl.jxkg.domain.exam.ExamPaperTitleItemObject;
|
import com.ycl.jxkg.domain.other.KeyValue;
|
import com.ycl.jxkg.domain.other.ExamPaperAnswerUpdate;
|
import com.ycl.jxkg.domain.task.TaskItemAnswerObject;
|
import com.ycl.jxkg.mapper.ExamPaperAnswerMapper;
|
import com.ycl.jxkg.mapper.ExamPaperMapper;
|
import com.ycl.jxkg.mapper.QuestionMapper;
|
import com.ycl.jxkg.mapper.TaskExamCustomerAnswerMapper;
|
import com.ycl.jxkg.service.ExamPaperAnswerService;
|
import com.ycl.jxkg.service.ExamPaperQuestionCustomerAnswerService;
|
import com.ycl.jxkg.service.TextContentService;
|
import com.ycl.jxkg.utils.DateTimeUtil;
|
import com.ycl.jxkg.utils.ExamUtil;
|
import com.ycl.jxkg.utils.JsonUtil;
|
import com.ycl.jxkg.vo.admin.paper.ExamPaperAnswerPageRequestVO;
|
import com.ycl.jxkg.vo.student.exam.ExamPaperSubmitItemVO;
|
import com.ycl.jxkg.vo.student.exam.ExamPaperSubmitVO;
|
import com.ycl.jxkg.vo.student.exampaper.ExamPaperAnswerPageVO;
|
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageInfo;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
@Service
|
@RequiredArgsConstructor
|
public class ExamPaperAnswerServiceImpl extends ServiceImpl<ExamPaperAnswerMapper ,ExamPaperAnswer> implements ExamPaperAnswerService {
|
|
private final ExamPaperAnswerMapper examPaperAnswerMapper;
|
private final ExamPaperMapper examPaperMapper;
|
private final TextContentService textContentService;
|
private final QuestionMapper questionMapper;
|
private final ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;
|
private final TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper;
|
|
|
@Override
|
public PageInfo<ExamPaperAnswer> studentPage(ExamPaperAnswerPageVO requestVM) {
|
return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
|
examPaperAnswerMapper.studentPage(requestVM));
|
}
|
|
|
@Override
|
public ExamPaperAnswerInfo calculateExamPaperAnswer(ExamPaperSubmitVO examPaperSubmitVO, User user) {
|
ExamPaperAnswerInfo examPaperAnswerInfo = new ExamPaperAnswerInfo();
|
Date now = new Date();
|
ExamPaper examPaper = examPaperMapper.selectById(examPaperSubmitVO.getId());
|
ExamPaperTypeEnum paperTypeEnum = ExamPaperTypeEnum.fromCode(examPaper.getPaperType());
|
//任务试卷只能做一次
|
if (paperTypeEnum == ExamPaperTypeEnum.Task) {
|
ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.getByPidUid(examPaperSubmitVO.getId(), user.getId());
|
if (null != examPaperAnswer)
|
return null;
|
}
|
String frameTextContent = textContentService.getById(examPaper.getFrameTextContentId()).getContent();
|
List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent, ExamPaperTitleItemObject.class);
|
List<Integer> questionIds = examPaperTitleItemObjects.stream().flatMap(t -> t.getQuestionItems().stream().map(q -> q.getId())).collect(Collectors.toList());
|
List<Question> questions = questionMapper.selectByIds(questionIds);
|
//将题目结构的转化为题目答案
|
List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers = examPaperTitleItemObjects.stream()
|
.flatMap(t -> t.getQuestionItems().stream()
|
.map(q -> {
|
Question question = questions.stream().filter(tq -> tq.getId().equals(q.getId())).findFirst().get();
|
ExamPaperSubmitItemVO customerQuestionAnswer = examPaperSubmitVO.getAnswerItems().stream()
|
.filter(tq -> tq.getQuestionId().equals(q.getId()))
|
.findFirst()
|
.orElse(null);
|
return ExamPaperQuestionCustomerAnswerFromVM(question, customerQuestionAnswer, examPaper, q.getItemOrder(), user, now);
|
})
|
).collect(Collectors.toList());
|
|
ExamPaperAnswer examPaperAnswer = ExamPaperAnswerFromVM(examPaperSubmitVO, examPaper, examPaperQuestionCustomerAnswers, user, now);
|
examPaperAnswerInfo.setExamPaper(examPaper);
|
examPaperAnswerInfo.setExamPaperAnswer(examPaperAnswer);
|
examPaperAnswerInfo.setExamPaperQuestionCustomerAnswers(examPaperQuestionCustomerAnswers);
|
return examPaperAnswerInfo;
|
}
|
|
@Override
|
@Transactional
|
public String judge(ExamPaperSubmitVO examPaperSubmitVO) {
|
ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.selectById(examPaperSubmitVO.getId());
|
List<ExamPaperSubmitItemVO> judgeItems = examPaperSubmitVO.getAnswerItems().stream().filter(d -> d.getDoRight() == null).collect(Collectors.toList());
|
List<ExamPaperAnswerUpdate> examPaperAnswerUpdates = new ArrayList<>(judgeItems.size());
|
Integer customerScore = examPaperAnswer.getUserScore();
|
Integer questionCorrect = examPaperAnswer.getQuestionCorrect();
|
for (ExamPaperSubmitItemVO d : judgeItems) {
|
ExamPaperAnswerUpdate examPaperAnswerUpdate = new ExamPaperAnswerUpdate();
|
examPaperAnswerUpdate.setId(d.getId());
|
examPaperAnswerUpdate.setCustomerScore(ExamUtil.scoreFromVM(d.getScore()));
|
boolean doRight = examPaperAnswerUpdate.getCustomerScore().equals(ExamUtil.scoreFromVM(d.getQuestionScore()));
|
examPaperAnswerUpdate.setDoRight(doRight);
|
examPaperAnswerUpdates.add(examPaperAnswerUpdate);
|
customerScore += examPaperAnswerUpdate.getCustomerScore();
|
if (examPaperAnswerUpdate.getDoRight()) {
|
++questionCorrect;
|
}
|
}
|
examPaperAnswer.setUserScore(customerScore);
|
examPaperAnswer.setQuestionCorrect(questionCorrect);
|
examPaperAnswer.setStatus(ExamPaperAnswerStatusEnum.Complete.getCode());
|
examPaperAnswerMapper.updateById(examPaperAnswer);
|
examPaperQuestionCustomerAnswerService.updateScore(examPaperAnswerUpdates);
|
|
ExamPaperTypeEnum examPaperTypeEnum = ExamPaperTypeEnum.fromCode(examPaperAnswer.getPaperType());
|
switch (examPaperTypeEnum) {
|
case Task:
|
//任务试卷批改完成后,需要更新任务的状态
|
ExamPaper examPaper = examPaperMapper.selectById(examPaperAnswer.getExamPaperId());
|
Integer taskId = examPaper.getTaskExamId();
|
Integer userId = examPaperAnswer.getCreateUser();
|
TaskExamCustomerAnswer taskExamCustomerAnswer = taskExamCustomerAnswerMapper.getByTUid(taskId, userId);
|
TextContent textContent = textContentService.getById(taskExamCustomerAnswer.getTextContentId());
|
List<TaskItemAnswerObject> taskItemAnswerObjects = JsonUtil.toJsonListObject(textContent.getContent(), TaskItemAnswerObject.class);
|
taskItemAnswerObjects.stream()
|
.filter(d -> d.getExamPaperAnswerId().equals(examPaperAnswer.getId()))
|
.findFirst().ifPresent(taskItemAnswerObject -> taskItemAnswerObject.setStatus(examPaperAnswer.getStatus()));
|
textContentService.jsonConvertUpdate(textContent, taskItemAnswerObjects, null);
|
textContentService.updateById(textContent);
|
break;
|
default:
|
break;
|
}
|
return ExamUtil.scoreToVM(customerScore);
|
}
|
|
@Override
|
public ExamPaperSubmitVO examPaperAnswerToVM(Integer id) {
|
ExamPaperSubmitVO examPaperSubmitVO = new ExamPaperSubmitVO();
|
ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.selectById(id);
|
examPaperSubmitVO.setId(examPaperAnswer.getId());
|
examPaperSubmitVO.setDoTime(examPaperAnswer.getDoTime());
|
examPaperSubmitVO.setScore(ExamUtil.scoreToVM(examPaperAnswer.getUserScore()));
|
List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers = examPaperQuestionCustomerAnswerService.selectListByPaperAnswerId(examPaperAnswer.getId());
|
List<ExamPaperSubmitItemVO> examPaperSubmitItemVOS = examPaperQuestionCustomerAnswers.stream()
|
.map(a -> examPaperQuestionCustomerAnswerService.examPaperQuestionCustomerAnswerToVM(a))
|
.collect(Collectors.toList());
|
examPaperSubmitVO.setAnswerItems(examPaperSubmitItemVOS);
|
return examPaperSubmitVO;
|
}
|
|
@Override
|
public Integer selectAllCount() {
|
return examPaperAnswerMapper.selectAllCount();
|
}
|
|
@Override
|
public List<Integer> selectMothCount() {
|
Date startTime = DateTimeUtil.getMonthStartDay();
|
Date endTime = DateTimeUtil.getMonthEndDay();
|
List<KeyValue> mouthCount = examPaperAnswerMapper.selectCountByDate(startTime, endTime);
|
List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
|
return mothStartToNowFormat.stream().map(md -> {
|
KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null);
|
return null == keyValue ? 0 : keyValue.getValue();
|
}).collect(Collectors.toList());
|
}
|
|
|
/**
|
* 用户提交答案的转化存储对象
|
*
|
* @param question question
|
* @param customerQuestionAnswer customerQuestionAnswer
|
* @param examPaper examPaper
|
* @param itemOrder itemOrder
|
* @param user user
|
* @param now now
|
* @return ExamPaperQuestionCustomerAnswer
|
*/
|
private ExamPaperQuestionCustomerAnswer ExamPaperQuestionCustomerAnswerFromVM(Question question, ExamPaperSubmitItemVO customerQuestionAnswer, ExamPaper examPaper, Integer itemOrder, User user, Date now) {
|
ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = new ExamPaperQuestionCustomerAnswer();
|
examPaperQuestionCustomerAnswer.setQuestionId(question.getId());
|
examPaperQuestionCustomerAnswer.setExamPaperId(examPaper.getId());
|
examPaperQuestionCustomerAnswer.setQuestionScore(question.getScore());
|
examPaperQuestionCustomerAnswer.setSubjectId(examPaper.getSubjectId());
|
examPaperQuestionCustomerAnswer.setItemOrder(itemOrder);
|
examPaperQuestionCustomerAnswer.setCreateTime(now);
|
examPaperQuestionCustomerAnswer.setCreateUser(user.getId());
|
examPaperQuestionCustomerAnswer.setQuestionType(question.getQuestionType());
|
examPaperQuestionCustomerAnswer.setQuestionTextContentId(question.getInfoTextContentId());
|
if (null == customerQuestionAnswer) {
|
examPaperQuestionCustomerAnswer.setCustomerScore(0);
|
} else {
|
setSpecialFromVM(examPaperQuestionCustomerAnswer, question, customerQuestionAnswer);
|
}
|
return examPaperQuestionCustomerAnswer;
|
}
|
|
/**
|
* 判断提交答案是否正确,保留用户提交的答案
|
*
|
* @param examPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer
|
* @param question question
|
* @param customerQuestionAnswer customerQuestionAnswer
|
*/
|
private void setSpecialFromVM(ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer, Question question, ExamPaperSubmitItemVO 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(examPaperQuestionCustomerAnswer.getDoRight() ? question.getScore() : 0);
|
break;
|
case MultipleChoice:
|
String customerAnswer = ExamUtil.contentToString(customerQuestionAnswer.getContentArray());
|
examPaperQuestionCustomerAnswer.setAnswer(customerAnswer);
|
examPaperQuestionCustomerAnswer.setDoRight(customerAnswer.equals(question.getCorrect()));
|
examPaperQuestionCustomerAnswer.setCustomerScore(examPaperQuestionCustomerAnswer.getDoRight() ? question.getScore() : 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;
|
}
|
}
|
|
private ExamPaperAnswer ExamPaperAnswerFromVM(ExamPaperSubmitVO examPaperSubmitVO, ExamPaper examPaper, List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers, User user, Date now) {
|
Integer systemScore = examPaperQuestionCustomerAnswers.stream().mapToInt(a -> a.getCustomerScore()).sum();
|
long questionCorrect = examPaperQuestionCustomerAnswers.stream().filter(a -> a.getCustomerScore().equals(a.getQuestionScore())).count();
|
ExamPaperAnswer examPaperAnswer = new ExamPaperAnswer();
|
examPaperAnswer.setPaperName(examPaper.getName());
|
examPaperAnswer.setDoTime(examPaperSubmitVO.getDoTime());
|
examPaperAnswer.setExamPaperId(examPaper.getId());
|
examPaperAnswer.setCreateUser(user.getId());
|
examPaperAnswer.setCreateTime(now);
|
examPaperAnswer.setSubjectId(examPaper.getSubjectId());
|
examPaperAnswer.setQuestionCount(examPaper.getQuestionCount());
|
examPaperAnswer.setPaperScore(examPaper.getScore());
|
examPaperAnswer.setPaperType(examPaper.getPaperType());
|
examPaperAnswer.setSystemScore(systemScore);
|
examPaperAnswer.setUserScore(systemScore);
|
examPaperAnswer.setTaskExamId(examPaper.getTaskExamId());
|
examPaperAnswer.setQuestionCorrect((int) questionCorrect);
|
boolean needJudge = examPaperQuestionCustomerAnswers.stream().anyMatch(d -> QuestionTypeEnum.needSaveTextContent(d.getQuestionType()));
|
if (needJudge) {
|
examPaperAnswer.setStatus(ExamPaperAnswerStatusEnum.WaitJudge.getCode());
|
} else {
|
examPaperAnswer.setStatus(ExamPaperAnswerStatusEnum.Complete.getCode());
|
}
|
return examPaperAnswer;
|
}
|
|
|
@Override
|
public PageInfo<ExamPaperAnswer> adminPage(ExamPaperAnswerPageRequestVO requestVM) {
|
return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
|
examPaperAnswerMapper.adminPage(requestVM));
|
}
|
}
|