xiangpei
2024-05-31 16d10cef208de048f8b325facd143c54b7be9963
src/main/java/com/ycl/jxkg/service/impl/QuestionServiceImpl.java
@@ -7,20 +7,19 @@
import com.ycl.jxkg.domain.enums.QuestionTypeEnum;
import com.ycl.jxkg.domain.question.QuestionItemObject;
import com.ycl.jxkg.domain.question.QuestionObject;
import com.ycl.jxkg.repository.QuestionMapper;
import com.ycl.jxkg.mapper.QuestionMapper;
import com.ycl.jxkg.service.QuestionService;
import com.ycl.jxkg.service.SubjectService;
import com.ycl.jxkg.service.TextContentService;
import com.ycl.jxkg.utility.DateTimeUtil;
import com.ycl.jxkg.utility.JsonUtil;
import com.ycl.jxkg.utility.ModelMapperSingle;
import com.ycl.jxkg.utility.ExamUtil;
import com.ycl.jxkg.viewmodel.admin.question.QuestionEditItemVM;
import com.ycl.jxkg.viewmodel.admin.question.QuestionEditRequestVM;
import com.ycl.jxkg.viewmodel.admin.question.QuestionPageRequestVM;
import com.ycl.jxkg.utils.DateTimeUtil;
import com.ycl.jxkg.utils.JsonUtil;
import com.ycl.jxkg.utils.ExamUtil;
import com.ycl.jxkg.vo.admin.question.QuestionEditItemVO;
import com.ycl.jxkg.vo.admin.question.QuestionEditRequestVO;
import com.ycl.jxkg.vo.admin.question.QuestionPageRequestVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -32,7 +31,6 @@
@Service
public class QuestionServiceImpl extends BaseServiceImpl<Question> implements QuestionService {
    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
    private final QuestionMapper questionMapper;
    private final TextContentService textContentService;
    private final SubjectService subjectService;
@@ -46,7 +44,7 @@
    }
    @Override
    public PageInfo<Question> page(QuestionPageRequestVM requestVM) {
    public PageInfo<Question> page(QuestionPageRequestVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                questionMapper.page(requestVM)
        );
@@ -55,7 +53,7 @@
    @Override
    @Transactional
    public Question insertFullQuestion(QuestionEditRequestVM model, Integer userId) {
    public Question insertFullQuestion(QuestionEditRequestVO model, Integer userId) {
        Date now = new Date();
        Integer gradeLevel = subjectService.levelBySubjectId(model.getSubjectId());
@@ -83,7 +81,7 @@
    @Override
    @Transactional
    public Question updateFullQuestion(QuestionEditRequestVM model) {
    public Question updateFullQuestion(QuestionEditRequestVO model) {
        Integer gradeLevel = subjectService.levelBySubjectId(model.getSubjectId());
        Question question = questionMapper.selectByPrimaryKey(model.getId());
        question.setSubjectId(model.getSubjectId());
@@ -102,57 +100,59 @@
    }
    @Override
    public QuestionEditRequestVM getQuestionEditRequestVM(Integer questionId) {
    public QuestionEditRequestVO getQuestionEditRequestVM(Integer questionId) {
        //题目映射
        Question question = questionMapper.selectByPrimaryKey(questionId);
        return getQuestionEditRequestVM(question);
    }
    @Override
    public QuestionEditRequestVM getQuestionEditRequestVM(Question question) {
    public QuestionEditRequestVO getQuestionEditRequestVM(Question question) {
        //题目映射
        TextContent questionInfoTextContent = textContentService.selectById(question.getInfoTextContentId());
        QuestionObject questionObject = JsonUtil.toJsonObject(questionInfoTextContent.getContent(), QuestionObject.class);
        QuestionEditRequestVM questionEditRequestVM = modelMapper.map(question, QuestionEditRequestVM.class);
        questionEditRequestVM.setTitle(questionObject.getTitleContent());
        QuestionEditRequestVO questionEditRequestVO = new QuestionEditRequestVO();
        BeanUtils.copyProperties(question, questionEditRequestVO);
        questionEditRequestVO.setTitle(questionObject.getTitleContent());
        //答案
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromCode(question.getQuestionType());
        switch (questionTypeEnum) {
            case SingleChoice:
            case TrueFalse:
                questionEditRequestVM.setCorrect(question.getCorrect());
                questionEditRequestVO.setCorrect(question.getCorrect());
                break;
            case MultipleChoice:
                questionEditRequestVM.setCorrectArray(ExamUtil.contentToArray(question.getCorrect()));
                questionEditRequestVO.setCorrectArray(ExamUtil.contentToArray(question.getCorrect()));
                break;
            case GapFilling:
                List<String> correctContent = questionObject.getQuestionItemObjects().stream().map(d -> d.getContent()).collect(Collectors.toList());
                questionEditRequestVM.setCorrectArray(correctContent);
                questionEditRequestVO.setCorrectArray(correctContent);
                break;
            case ShortAnswer:
                questionEditRequestVM.setCorrect(questionObject.getCorrect());
                questionEditRequestVO.setCorrect(questionObject.getCorrect());
                break;
            default:
                break;
        }
        questionEditRequestVM.setScore(ExamUtil.scoreToVM(question.getScore()));
        questionEditRequestVM.setAnalyze(questionObject.getAnalyze());
        questionEditRequestVO.setScore(ExamUtil.scoreToVM(question.getScore()));
        questionEditRequestVO.setAnalyze(questionObject.getAnalyze());
        //题目项映射
        List<QuestionEditItemVM> editItems = questionObject.getQuestionItemObjects().stream().map(o -> {
            QuestionEditItemVM questionEditItemVM = modelMapper.map(o, QuestionEditItemVM.class);
        List<QuestionEditItemVO> editItems = questionObject.getQuestionItemObjects().stream().map(o -> {
            QuestionEditItemVO questionEditItemVO = new QuestionEditItemVO();
            BeanUtils.copyProperties(o, questionEditItemVO);
            if (o.getScore() != null) {
                questionEditItemVM.setScore(ExamUtil.scoreToVM(o.getScore()));
                questionEditItemVO.setScore(ExamUtil.scoreToVM(o.getScore()));
            }
            return questionEditItemVM;
            return questionEditItemVO;
        }).collect(Collectors.toList());
        questionEditRequestVM.setItems(editItems);
        return questionEditRequestVM;
        questionEditRequestVO.setItems(editItems);
        return questionEditRequestVO;
    }
    public void setQuestionInfoFromVM(TextContent infoTextContent, QuestionEditRequestVM model) {
    public void setQuestionInfoFromVM(TextContent infoTextContent, QuestionEditRequestVO model) {
        List<QuestionItemObject> itemObjects = model.getItems().stream().map(i ->
                {
                    QuestionItemObject item = new QuestionItemObject();