| | |
| | | package com.ycl.jxkg.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ycl.jxkg.domain.entity.ExamTemplate; |
| | | import com.ycl.jxkg.context.WebContext; |
| | | import com.ycl.jxkg.domain.entity.TextContent; |
| | | import com.ycl.jxkg.domain.enums.ExamPaperTypeEnum; |
| | | import com.ycl.jxkg.domain.exam.ExamPaperTempDTO; |
| | | import com.ycl.jxkg.domain.form.ExamPaperForm; |
| | | import com.ycl.jxkg.domain.question.TemplateQuestionDTO; |
| | | import com.ycl.jxkg.domain.vo.admin.exam.ExamResponseVO; |
| | | 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.enums.QuestionTypeEnum; |
| | | import com.ycl.jxkg.enums.VisibilityEnum; |
| | | import com.ycl.jxkg.enums.VisibilityEnum; |
| | | import com.ycl.jxkg.mapper.ExamPaperMapper; |
| | | import com.ycl.jxkg.mapper.ExamTemplateMapper; |
| | | import com.ycl.jxkg.mapper.QuestionMapper; |
| | | import com.ycl.jxkg.service.ExamPaperService; |
| | | import com.ycl.jxkg.service.QuestionService; |
| | |
| | | import com.ycl.jxkg.service.enums.ActionEnum; |
| | | import com.ycl.jxkg.utils.DateTimeUtil; |
| | | import com.ycl.jxkg.utils.JsonUtil; |
| | | import com.ycl.jxkg.utils.ExamUtil; |
| | | 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.admin.question.QuestionEditRequestVO; |
| | | 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.entity.ExamPaper; |
| | | import com.ycl.jxkg.domain.entity.Question; |
| | | import com.ycl.jxkg.domain.entity.User; |
| | | import com.ycl.jxkg.utils.PageInfoHelper; |
| | | 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.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private final TextContentService textContentService; |
| | | private final QuestionService questionService; |
| | | private final SubjectService subjectService; |
| | | private final ExamTemplateMapper examTemplateMapper; |
| | | private final WebContext webContext; |
| | | |
| | | |
| | | @Override |
| | | public PageInfo<ExamPaper> page(ExamPaperPageRequestVO requestVM) { |
| | | return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | | examPaperMapper.page(requestVM)); |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addPaper(ExamPaperForm form) { |
| | | ExamPaper examPaper = ExamPaperForm.getEntityByForm(form,null); |
| | | baseMapper.insert(examPaper); |
| | | if(ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())){ |
| | | ExamTemplate examTemplate = getExamTemplate(form, examPaper); |
| | | examTemplateMapper.insert(examTemplate); |
| | | baseMapper.updateById(examPaper); |
| | | } |
| | | //TODO:随序试卷生成题目 |
| | | |
| | | } |
| | | |
| | | private ExamTemplate getExamTemplate(ExamPaperForm form, ExamPaper examPaper) { |
| | | //随机试卷存模板 |
| | | List<TemplateQuestionDTO> questionList = form.getQuestionList(); |
| | | BigDecimal score = BigDecimal.ZERO; |
| | | Integer num=0; |
| | | ExamTemplate examTemplate = new ExamTemplate(); |
| | | //设置题目信息 |
| | | for (TemplateQuestionDTO dto : questionList) { |
| | | num += dto.getNum(); |
| | | score = score.add(dto.getScore().multiply(BigDecimal.valueOf(dto.getNum()))); |
| | | Integer questionType = dto.getQuestionType(); |
| | | //整理List为对象 |
| | | switch (QuestionTypeEnum.fromCode(questionType)){ |
| | | //单选 |
| | | case SingleChoice: |
| | | examTemplate.setSingleChoice(dto.getNum()); |
| | | examTemplate.setSingleScore(dto.getScore()); |
| | | break; |
| | | //多选 |
| | | case MultipleChoice: |
| | | examTemplate.setMultipleChoice(dto.getNum()); |
| | | examTemplate.setMultipleScore(dto.getScore()); |
| | | break; |
| | | //判断 |
| | | case TrueFalse: |
| | | examTemplate.setTrueFalse(dto.getNum()); |
| | | examTemplate.setTrueFalseScore(dto.getScore()); |
| | | break; |
| | | //填空 |
| | | case GapFilling: |
| | | examTemplate.setGapFilling(dto.getNum()); |
| | | examTemplate.setGapScore(dto.getScore()); |
| | | break; |
| | | //简答 |
| | | case ShortAnswer: |
| | | examTemplate.setShortAnswer(dto.getNum()); |
| | | examTemplate.setShortAnswerScore(dto.getScore()); |
| | | break; |
| | | //计算 |
| | | case Calculation: |
| | | examTemplate.setCalculation(dto.getNum()); |
| | | examTemplate.setCalculationScore(dto.getScore()); |
| | | break; |
| | | } |
| | | } |
| | | examTemplate.setExamPaperId(examPaper.getId()); |
| | | //回填 |
| | | examPaper.setQuestionCount(num); |
| | | examPaper.setScore(score); |
| | | return examTemplate; |
| | | } |
| | | |
| | | @Override |
| | | public void updateExamPaper(ExamPaperForm form) { |
| | | ExamPaper entity = ExamPaperForm.getEntityByForm(form,null); |
| | | //如果是随机试卷修改template表 |
| | | if(ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())){ |
| | | ExamTemplate examTemplate = getExamTemplate(form, entity); |
| | | examTemplateMapper.removeById(form.getId()); |
| | | examTemplateMapper.insert(examTemplate); |
| | | } |
| | | baseMapper.updateById(entity); |
| | | } |
| | | |
| | | @Override |
| | | public PageInfo<ExamResponseVO> page(ExamPaperPageRequestVO requestVM) { |
| | | PageInfo<ExamPaperTempDTO> page = PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | | examPaperMapper.page(requestVM)); |
| | | PageInfo<ExamResponseVO> pageVO = PageInfoHelper.copyMap(page, e -> { |
| | | ExamResponseVO vo = new ExamResponseVO(); |
| | | BeanUtils.copyProperties(e, vo); |
| | | vo.setVisibility(VisibilityEnum.valueOf(vo.getVisibility()).getCode() + ""); |
| | | vo.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); |
| | | if(ExamPaperTypeEnum.Random.getCode().equals(e.getPaperType())) { |
| | | //整理题目信息为集合 |
| | | ExamTemplate examTemplate = e.getExamTemplate(); |
| | | List<TemplateQuestionDTO> questionList = new ArrayList<>(); |
| | | addQuestionList(questionList, QuestionTypeEnum.SingleChoice.getCode(), examTemplate.getSingleScore(), examTemplate.getSingleChoice()); |
| | | addQuestionList(questionList, QuestionTypeEnum.MultipleChoice.getCode(), examTemplate.getMultipleScore(), examTemplate.getMultipleChoice()); |
| | | addQuestionList(questionList, QuestionTypeEnum.TrueFalse.getCode(), examTemplate.getTrueFalseScore(), examTemplate.getTrueFalse()); |
| | | addQuestionList(questionList, QuestionTypeEnum.GapFilling.getCode(), examTemplate.getGapScore(), examTemplate.getGapFilling()); |
| | | addQuestionList(questionList, QuestionTypeEnum.ShortAnswer.getCode(), examTemplate.getShortAnswerScore(), examTemplate.getShortAnswer()); |
| | | addQuestionList(questionList, QuestionTypeEnum.Calculation.getCode(), examTemplate.getCalculationScore(), examTemplate.getCalculation()); |
| | | vo.setQuestionList(questionList); |
| | | } |
| | | return vo; |
| | | }); |
| | | return pageVO; |
| | | } |
| | | |
| | | private void addQuestionList(List<TemplateQuestionDTO> questionList, Integer code,BigDecimal score,Integer num) { |
| | | TemplateQuestionDTO questionDTO = new TemplateQuestionDTO(); |
| | | questionDTO.setQuestionType(code); |
| | | questionDTO.setScore(score); |
| | | questionDTO.setNum(num); |
| | | questionList.add(questionDTO); |
| | | } |
| | | @Override |
| | | public PageInfo<ExamPaper> taskExamPage(ExamPaperPageRequestVO requestVM) { |
| | | return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | |
| | | frameTextContent.setContent(frameTextContentStr); |
| | | frameTextContent.setCreateTime(now); |
| | | textContentService.save(frameTextContent); |
| | | examPaper.setFrameTextContentId(frameTextContent.getId()); |
| | | examPaper.setCreateTime(now); |
| | | examPaper.setCreateUser(user.getId()); |
| | | examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM); |
| | | examPaperMapper.insert(examPaper); |
| | | } else { |
| | | examPaper = examPaperMapper.selectById(examPaperEditRequestVO.getId()); |
| | | TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId()); |
| | | frameTextContent.setContent(frameTextContentStr); |
| | | textContentService.updateById(frameTextContent); |
| | | //TODO: |
| | | // TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId()); |
| | | // frameTextContent.setContent(frameTextContentStr); |
| | | // textContentService.updateById(frameTextContent); |
| | | examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM); |
| | | examPaperMapper.updateById(examPaper); |
| | | } |
| | |
| | | ExamPaper examPaper = examPaperMapper.selectById(id); |
| | | ExamPaperEditRequestVO vo = new ExamPaperEditRequestVO(); |
| | | BeanUtils.copyProperties(examPaper, vo); |
| | | vo.setLevel(examPaper.getGradeLevel()); |
| | | 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.TimeLimit == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) { |
| | | List<String> limitDateTime = Arrays.asList(DateTimeUtil.dateFormat(examPaper.getLimitStartTime()), DateTimeUtil.dateFormat(examPaper.getLimitEndTime())); |
| | | vo.setLimitDateTime(limitDateTime); |
| | | } |
| | | //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; |
| | | } |
| | | |
| | |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | |
| | | |
| | | |
| | | private void examPaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, ExamPaper examPaper, List<ExamPaperTitleItemVO> titleItemsVM) { |
| | | 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.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)); |
| | | } |
| | | //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<ExamPaperTitleItemVO> titleItems) { |
| | |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<ExamPaper> myExamPaper(Integer paperType) { |
| | | Integer userId = webContext.getCurrentUser().getId(); |
| | | List<ExamPaper> list = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .select(ExamPaper::getId, ExamPaper::getName, ExamPaper::getVisibility) |
| | | .eq(ExamPaper::getCreateUser, userId) |
| | | .eq(Objects.nonNull(paperType), ExamPaper::getPaperType, paperType) |
| | | .or() |
| | | .eq(ExamPaper::getVisibility, VisibilityEnum.Public.getName()) |
| | | .eq(Objects.nonNull(paperType), ExamPaper::getPaperType, paperType) |
| | | .list(); |
| | | list.stream().forEach(item -> { |
| | | if (VisibilityEnum.Public.getName().equals(item.getVisibility())) { |
| | | item.setName(item.getName() + " (公开)"); |
| | | } |
| | | }); |
| | | return list; |
| | | } |
| | | } |