xiangpei
2024-06-06 c2dc62c3d40be033cdb65fa54473f7f355e67e88
src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
@@ -1,79 +1,65 @@
package com.ycl.jxkg.service.impl;
import com.ycl.jxkg.domain.TextContent;
import com.ycl.jxkg.domain.enums.ExamPaperTypeEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.jxkg.domain.entity.TextContent;
import com.ycl.jxkg.enums.ExamPaperTypeEnum;
import com.ycl.jxkg.domain.exam.ExamPaperQuestionItemObject;
import com.ycl.jxkg.domain.exam.ExamPaperTitleItemObject;
import com.ycl.jxkg.domain.other.KeyValue;
import com.ycl.jxkg.repository.ExamPaperMapper;
import com.ycl.jxkg.repository.QuestionMapper;
import com.ycl.jxkg.mapper.ExamPaperMapper;
import com.ycl.jxkg.mapper.QuestionMapper;
import com.ycl.jxkg.service.ExamPaperService;
import com.ycl.jxkg.service.QuestionService;
import com.ycl.jxkg.service.SubjectService;
import com.ycl.jxkg.service.TextContentService;
import com.ycl.jxkg.service.enums.ActionEnum;
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.exam.ExamPaperEditRequestVM;
import com.ycl.jxkg.viewmodel.admin.exam.ExamPaperPageRequestVM;
import com.ycl.jxkg.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.ycl.jxkg.viewmodel.admin.question.QuestionEditRequestVM;
import com.ycl.jxkg.viewmodel.student.dashboard.PaperFilter;
import com.ycl.jxkg.viewmodel.student.dashboard.PaperInfo;
import com.ycl.jxkg.viewmodel.student.exam.ExamPaperPageVM;
import com.ycl.jxkg.utils.DateTimeUtil;
import com.ycl.jxkg.utils.JsonUtil;
import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperEditRequestVO;
import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperPageRequestVO;
import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperTitleItemVO;
import com.ycl.jxkg.domain.vo.student.dashboard.PaperFilter;
import com.ycl.jxkg.domain.vo.student.dashboard.PaperInfo;
import com.ycl.jxkg.domain.vo.student.exam.ExamPaperPageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycl.jxkg.domain.ExamPaper;
import com.ycl.jxkg.domain.Question;
import com.ycl.jxkg.domain.User;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.ycl.jxkg.domain.entity.ExamPaper;
import com.ycl.jxkg.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
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.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@Service
public class ExamPaperServiceImpl extends BaseServiceImpl<ExamPaper> implements ExamPaperService {
@RequiredArgsConstructor
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {
    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
    private final ExamPaperMapper examPaperMapper;
    private final QuestionMapper questionMapper;
    private final TextContentService textContentService;
    private final QuestionService questionService;
    private final SubjectService subjectService;
    @Autowired
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService) {
        super(examPaperMapper);
        this.examPaperMapper = examPaperMapper;
        this.questionMapper = questionMapper;
        this.textContentService = textContentService;
        this.questionService = questionService;
        this.subjectService = subjectService;
    }
    @Override
    public PageInfo<ExamPaper> page(ExamPaperPageRequestVM requestVM) {
    public PageInfo<ExamPaper> page(ExamPaperPageRequestVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                examPaperMapper.page(requestVM));
    }
    @Override
    public PageInfo<ExamPaper> taskExamPage(ExamPaperPageRequestVM requestVM) {
    public PageInfo<ExamPaper> taskExamPage(ExamPaperPageRequestVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                examPaperMapper.taskExamPage(requestVM));
    }
    @Override
    public PageInfo<ExamPaper> studentPage(ExamPaperPageVM requestVM) {
    public PageInfo<ExamPaper> studentPage(ExamPaperPageVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                examPaperMapper.studentPage(requestVM));
    }
@@ -81,66 +67,68 @@
    @Override
    @Transactional
    public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) {
        ActionEnum actionEnum = (examPaperEditRequestVM.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE;
    public ExamPaper savePaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, User user) {
        ActionEnum actionEnum = (examPaperEditRequestVO.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE;
        Date now = new Date();
        List<ExamPaperTitleItemVM> titleItemsVM = examPaperEditRequestVM.getTitleItems();
        List<ExamPaperTitleItemVO> titleItemsVM = examPaperEditRequestVO.getTitleItems();
        List<ExamPaperTitleItemObject> frameTextContentList = frameTextContentFromVM(titleItemsVM);
        String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList);
        ExamPaper examPaper;
        ExamPaper examPaper = new ExamPaper();
        BeanUtils.copyProperties(examPaperEditRequestVO, examPaper);
        if (actionEnum == ActionEnum.ADD) {
            examPaper = modelMapper.map(examPaperEditRequestVM, ExamPaper.class);
            TextContent frameTextContent = new TextContent(frameTextContentStr, now);
            textContentService.insertByFilter(frameTextContent);
            examPaper.setFrameTextContentId(frameTextContent.getId());
            TextContent frameTextContent = new TextContent();
            frameTextContent.setContent(frameTextContentStr);
            frameTextContent.setCreateTime(now);
            textContentService.save(frameTextContent);
            examPaper.setCreateTime(now);
            examPaper.setCreateUser(user.getId());
            examPaper.setDeleted(false);
            examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM);
            examPaperMapper.insertSelective(examPaper);
            examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM);
            examPaperMapper.insert(examPaper);
        } else {
            examPaper = examPaperMapper.selectByPrimaryKey(examPaperEditRequestVM.getId());
            TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
            frameTextContent.setContent(frameTextContentStr);
            textContentService.updateByIdFilter(frameTextContent);
            modelMapper.map(examPaperEditRequestVM, examPaper);
            examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM);
            examPaperMapper.updateByPrimaryKeySelective(examPaper);
            examPaper = examPaperMapper.selectById(examPaperEditRequestVO.getId());
            //TODO:待完成
//            TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
//            frameTextContent.setContent(frameTextContentStr);
//            textContentService.updateById(frameTextContent);
            examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM);
            examPaperMapper.updateById(examPaper);
        }
        return examPaper;
    }
    @Override
    public ExamPaperEditRequestVM examPaperToVM(Integer id) {
        ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(id);
        ExamPaperEditRequestVM vm = modelMapper.map(examPaper, ExamPaperEditRequestVM.class);
        vm.setLevel(examPaper.getGradeLevel());
        TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), 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<ExamPaperTitleItemVM> examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> {
            ExamPaperTitleItemVM tTitleVM = modelMapper.map(t, ExamPaperTitleItemVM.class);
            List<QuestionEditRequestVM> questionItemsVM = t.getQuestionItems().stream().map(i -> {
                Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
                QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
                questionEditRequestVM.setItemOrder(i.getItemOrder());
                return questionEditRequestVM;
            }).collect(Collectors.toList());
            tTitleVM.setQuestionItems(questionItemsVM);
            return tTitleVM;
        }).collect(Collectors.toList());
        vm.setTitleItems(examPaperTitleItemVMS);
        vm.setScore(ExamUtil.scoreToVM(examPaper.getScore()));
        if (ExamPaperTypeEnum.TimeLimit == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
            List<String> limitDateTime = Arrays.asList(DateTimeUtil.dateFormat(examPaper.getLimitStartTime()), DateTimeUtil.dateFormat(examPaper.getLimitEndTime()));
            vm.setLimitDateTime(limitDateTime);
        }
        return vm;
    public ExamPaperEditRequestVO examPaperToVM(Integer id) {
        ExamPaper examPaper = examPaperMapper.selectById(id);
        ExamPaperEditRequestVO vo = new ExamPaperEditRequestVO();
        BeanUtils.copyProperties(examPaper, vo);
        //TODO:待完成
//        TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
//        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), 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<ExamPaperTitleItemVO> examPaperTitleItemVOS = examPaperTitleItemObjects.stream().map(t -> {
//            ExamPaperTitleItemVO tTitleVM = new ExamPaperTitleItemVO();
//            BeanUtils.copyProperties(t, tTitleVM);
//            List<QuestionEditRequestVO> questionItemsVM = t.getQuestionItems().stream().map(i -> {
//                Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
//                QuestionEditRequestVO questionEditRequestVO = questionService.getQuestionEditRequestVM(question);
//                questionEditRequestVO.setItemOrder(i.getItemOrder());
//                return questionEditRequestVO;
//            }).collect(Collectors.toList());
//            tTitleVM.setQuestionItems(questionItemsVM);
//            return tTitleVM;
//        }).collect(Collectors.toList());
//        vo.setTitleItems(examPaperTitleItemVOS);
//        vo.setScore(ExamUtil.scoreToVM(examPaper.getScore()));
//        if (ExamPaperTypeEnum.Random == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
//            List<String> limitDateTime = Arrays.asList(DateTimeUtil.dateFormat(examPaper.getLimitStartTime()), DateTimeUtil.dateFormat(examPaper.getLimitEndTime()));
//            vo.setLimitDateTime(limitDateTime);
//        }
        return vo;
    }
    @Override
@@ -166,31 +154,34 @@
        }).collect(Collectors.toList());
    }
    private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List<ExamPaperTitleItemVM> titleItemsVM) {
        Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVM.getSubjectId());
        Integer questionCount = titleItemsVM.stream()
                .mapToInt(t -> t.getQuestionItems().size()).sum();
        Integer score = titleItemsVM.stream().
                flatMapToInt(t -> t.getQuestionItems().stream()
                        .mapToInt(q -> ExamUtil.scoreFromVM(q.getScore()))
                ).sum();
        examPaper.setQuestionCount(questionCount);
        examPaper.setScore(score);
        examPaper.setGradeLevel(gradeLevel);
        List<String> dateTimes = examPaperEditRequestVM.getLimitDateTime();
        if (ExamPaperTypeEnum.TimeLimit == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
            examPaper.setLimitStartTime(DateTimeUtil.parse(dateTimes.get(0), DateTimeUtil.STANDER_FORMAT));
            examPaper.setLimitEndTime(DateTimeUtil.parse(dateTimes.get(1), DateTimeUtil.STANDER_FORMAT));
        }
    private void examPaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, ExamPaper examPaper, List<ExamPaperTitleItemVO> titleItemsVM) {
        //TODO:待完成
//        Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVO.getSubjectId());
//        Integer questionCount = titleItemsVM.stream()
//                .mapToInt(t -> t.getQuestionItems().size()).sum();
//        Integer score = titleItemsVM.stream().
//                flatMapToInt(t -> t.getQuestionItems().stream()
//                        .mapToInt(q -> ExamUtil.scoreFromVM(q.getScore()))
//                ).sum();
//        examPaper.setQuestionCount(questionCount);
//        examPaper.setScore(score);
//        examPaper.setGradeLevel(gradeLevel);
//        List<String> dateTimes = examPaperEditRequestVO.getLimitDateTime();
//        if (ExamPaperTypeEnum.Random == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
//            examPaper.setLimitStartTime(DateTimeUtil.parse(dateTimes.get(0), DateTimeUtil.STANDER_FORMAT));
//            examPaper.setLimitEndTime(DateTimeUtil.parse(dateTimes.get(1), DateTimeUtil.STANDER_FORMAT));
//        }
    }
    private List<ExamPaperTitleItemObject> frameTextContentFromVM(List<ExamPaperTitleItemVM> titleItems) {
    private List<ExamPaperTitleItemObject> frameTextContentFromVM(List<ExamPaperTitleItemVO> titleItems) {
        AtomicInteger index = new AtomicInteger(1);
        return titleItems.stream().map(t -> {
            ExamPaperTitleItemObject titleItem = modelMapper.map(t, ExamPaperTitleItemObject.class);
            ExamPaperTitleItemObject titleItem = new ExamPaperTitleItemObject();
            BeanUtils.copyProperties(t, titleItem);
            List<ExamPaperQuestionItemObject> questionItems = t.getQuestionItems().stream()
                    .map(q -> {
                        ExamPaperQuestionItemObject examPaperQuestionItemObject = modelMapper.map(q, ExamPaperQuestionItemObject.class);
                        ExamPaperQuestionItemObject examPaperQuestionItemObject = new ExamPaperQuestionItemObject();
                        BeanUtils.copyProperties(q, examPaperQuestionItemObject);
                        examPaperQuestionItemObject.setItemOrder(index.getAndIncrement());
                        return examPaperQuestionItemObject;
                    })
@@ -199,4 +190,6 @@
            return titleItem;
        }).collect(Collectors.toList());
    }
}