package com.mindskip.xzs.service.impl;
import com.mindskip.xzs.domain.*;
import com.mindskip.xzs.domain.enums.ExamPaperAnswerStatusEnum;
import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum;
import com.mindskip.xzs.domain.enums.QuestionTypeEnum;
import com.mindskip.xzs.domain.exam.ExamPaperTitleItemObject;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.domain.other.ExamPaperAnswerUpdate;
import com.mindskip.xzs.domain.task.TaskItemAnswerObject;
import com.mindskip.xzs.repository.*;
import com.mindskip.xzs.repository.ExamPaperAnswerMapper;
import com.mindskip.xzs.repository.ExamPaperMapper;
import com.mindskip.xzs.repository.QuestionMapper;
import com.mindskip.xzs.repository.TaskExamCustomerAnswerMapper;
import com.mindskip.xzs.service.ExamPaperAnswerService;
import com.mindskip.xzs.service.ExamPaperQuestionCustomerAnswerService;
import com.mindskip.xzs.service.TextContentService;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.utility.JsonUtil;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperSubmitItemVM;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperSubmitVM;
import com.mindskip.xzs.viewmodel.student.exampaper.ExamPaperAnswerPageVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.*;
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
public class ExamPaperAnswerServiceImpl extends BaseServiceImpl<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;
@Autowired
public ExamPaperAnswerServiceImpl(ExamPaperAnswerMapper examPaperAnswerMapper, ExamPaperMapper examPaperMapper, TextContentService textContentService, QuestionMapper questionMapper, ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService, TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper) {
super(examPaperAnswerMapper);
this.examPaperAnswerMapper = examPaperAnswerMapper;
this.examPaperMapper = examPaperMapper;
this.textContentService = textContentService;
this.questionMapper = questionMapper;
this.examPaperQuestionCustomerAnswerService = examPaperQuestionCustomerAnswerService;
this.taskExamCustomerAnswerMapper = taskExamCustomerAnswerMapper;
}
@Override
public PageInfo<ExamPaperAnswer> studentPage(ExamPaperAnswerPageVM requestVM) {
return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
examPaperAnswerMapper.studentPage(requestVM));
}
@Override
public ExamPaperAnswerInfo calculateExamPaperAnswer(ExamPaperSubmitVM examPaperSubmitVM, User user) {
ExamPaperAnswerInfo examPaperAnswerInfo = new ExamPaperAnswerInfo();
Date now = new Date();
ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(examPaperSubmitVM.getId());
ExamPaperTypeEnum paperTypeEnum = ExamPaperTypeEnum.fromCode(examPaper.getPaperType());
//任务试卷只能做一次
if (paperTypeEnum == ExamPaperTypeEnum.Task) {
ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.getByPidUid(examPaperSubmitVM.getId(), user.getId());
if (null != examPaperAnswer)
return null;
}
String frameTextContent = textContentService.selectById(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();
ExamPaperSubmitItemVM customerQuestionAnswer = examPaperSubmitVM.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(examPaperSubmitVM, examPaper, examPaperQuestionCustomerAnswers, user, now);
examPaperAnswerInfo.setExamPaper(examPaper);
examPaperAnswerInfo.setExamPaperAnswer(examPaperAnswer);
examPaperAnswerInfo.setExamPaperQuestionCustomerAnswers(examPaperQuestionCustomerAnswers);
return examPaperAnswerInfo;
}
@Override
@Transactional
public String judge(ExamPaperSubmitVM examPaperSubmitVM) {
ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.selectByPrimaryKey(examPaperSubmitVM.getId());
List<ExamPaperSubmitItemVM> judgeItems = examPaperSubmitVM.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 (ExamPaperSubmitItemVM 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.updateByPrimaryKeySelective(examPaperAnswer);
examPaperQuestionCustomerAnswerService.updateScore(examPaperAnswerUpdates);
ExamPaperTypeEnum examPaperTypeEnum = ExamPaperTypeEnum.fromCode(examPaperAnswer.getPaperType());
switch (examPaperTypeEnum) {
case Task:
//任务试卷批改完成后,需要更新任务的状态
ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(examPaperAnswer.getExamPaperId());
Integer taskId = examPaper.getTaskExamId();
Integer userId = examPaperAnswer.getCreateUser();
TaskExamCustomerAnswer taskExamCustomerAnswer = taskExamCustomerAnswerMapper.getB