| | |
| | | package com.ycl.jxkg.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | 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.exam.ExamPaperTempDTO; |
| | | import com.ycl.jxkg.domain.exam.PaperQuestion; |
| | | import com.ycl.jxkg.domain.exam.PaperQuestionDTO; |
| | | 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.SubjectService; |
| | | import com.ycl.jxkg.service.TextContentService; |
| | | import com.ycl.jxkg.service.enums.ActionEnum; |
| | | 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.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.ycl.jxkg.domain.entity.ExamPaper; |
| | | 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.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; |
| | | |
| | | @Service |
| | |
| | | private final TextContentService textContentService; |
| | | private final QuestionService questionService; |
| | | private final SubjectService subjectService; |
| | | private final ExamTemplateMapper examTemplateMapper; |
| | | private final WebContext webContext; |
| | | |
| | | |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addPaper(ExamPaperForm form) { |
| | | ExamPaper examPaper = ExamPaperForm.getEntityByForm(form,null); |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionList())); |
| | | 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; |
| | | // if(ExamPaperTypeEnum.RandomOrder.getCode().equals(form.getPaperType())){ |
| | | // List<PaperQuestionDTO> questionList = form.getQuestionList(); |
| | | // List<PaperQuestion> list = questionMapper.selectQuestion(); |
| | | // } |
| | | } |
| | | |
| | | @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(() -> |
| | | PageInfo<ExamPaper> 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); |
| | | } |
| | | vo.setQuestionList(JSONArray.parseArray(e.getContent(),PaperQuestionDTO.class)); |
| | | 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(() -> |
| | | examPaperMapper.taskExamPage(requestVM)); |
| | | } |
| | | |
| | | // @Override |
| | | // public PageInfo<ExamPaper> taskExamPage(ExamPaperPageRequestVO requestVM) { |
| | | // return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | | // examPaperMapper.taskExamPage(requestVM)); |
| | | // } |
| | | |
| | | @Override |
| | | public PageInfo<ExamPaper> studentPage(ExamPaperPageVO requestVM) { |
| | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public ExamPaper savePaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, User user) { |
| | | ActionEnum actionEnum = (examPaperEditRequestVO.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; |
| | | Date now = new Date(); |
| | | List<ExamPaperTitleItemVO> titleItemsVM = examPaperEditRequestVO.getTitleItems(); |
| | | List<ExamPaperTitleItemObject> frameTextContentList = frameTextContentFromVM(titleItemsVM); |
| | | String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList); |
| | | |
| | | ExamPaper examPaper = new ExamPaper(); |
| | | BeanUtils.copyProperties(examPaperEditRequestVO, examPaper); |
| | | if (actionEnum == ActionEnum.ADD) { |
| | | TextContent frameTextContent = new TextContent(); |
| | | frameTextContent.setContent(frameTextContentStr); |
| | | frameTextContent.setCreateTime(now); |
| | | textContentService.save(frameTextContent); |
| | | examPaper.setCreateTime(now); |
| | | examPaper.setCreateUser(user.getId()); |
| | | examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM); |
| | | examPaperMapper.insert(examPaper); |
| | | } else { |
| | | examPaper = examPaperMapper.selectById(examPaperEditRequestVO.getId()); |
| | | //TODO: |
| | | // @Override |
| | | // @Transactional |
| | | // public ExamPaper savePaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, User user) { |
| | | // ActionEnum actionEnum = (examPaperEditRequestVO.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; |
| | | // Date now = new Date(); |
| | | // List<ExamPaperTitleItemVO> titleItemsVM = examPaperEditRequestVO.getTitleItems(); |
| | | // List<ExamPaperTitleItemObject> frameTextContentList = frameTextContentFromVM(titleItemsVM); |
| | | // String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList); |
| | | // |
| | | // ExamPaper examPaper = new ExamPaper(); |
| | | // BeanUtils.copyProperties(examPaperEditRequestVO, examPaper); |
| | | // if (actionEnum == ActionEnum.ADD) { |
| | | // TextContent frameTextContent = new TextContent(); |
| | | // frameTextContent.setContent(frameTextContentStr); |
| | | // frameTextContent.setCreateTime(now); |
| | | // textContentService.save(frameTextContent); |
| | | // examPaper.setCreateTime(now); |
| | | // examPaper.setCreateUser(user.getId()); |
| | | // examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM); |
| | | // examPaperMapper.insert(examPaper); |
| | | // } else { |
| | | // 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; |
| | | } |
| | | // examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM); |
| | | // examPaperMapper.updateById(examPaper); |
| | | // } |
| | | // return examPaper; |
| | | // } |
| | | |
| | | @Override |
| | | public ExamPaperEditRequestVO examPaperToVM(Integer id) { |
| | |
| | | // } |
| | | } |
| | | |
| | | private List<ExamPaperTitleItemObject> frameTextContentFromVM(List<ExamPaperTitleItemVO> titleItems) { |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | return titleItems.stream().map(t -> { |
| | | ExamPaperTitleItemObject titleItem = new ExamPaperTitleItemObject(); |
| | | BeanUtils.copyProperties(t, titleItem); |
| | | List<ExamPaperQuestionItemObject> questionItems = t.getQuestionItems().stream() |
| | | .map(q -> { |
| | | ExamPaperQuestionItemObject examPaperQuestionItemObject = new ExamPaperQuestionItemObject(); |
| | | BeanUtils.copyProperties(q, examPaperQuestionItemObject); |
| | | examPaperQuestionItemObject.setItemOrder(index.getAndIncrement()); |
| | | return examPaperQuestionItemObject; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | titleItem.setQuestionItems(questionItems); |
| | | return titleItem; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | // private List<ExamPaperTitleItemObject> frameTextContentFromVM(List<ExamPaperTitleItemVO> titleItems) { |
| | | // AtomicInteger index = new AtomicInteger(1); |
| | | // return titleItems.stream().map(t -> { |
| | | // ExamPaperTitleItemObject titleItem = new ExamPaperTitleItemObject(); |
| | | // BeanUtils.copyProperties(t, titleItem); |
| | | // List<ExamPaperQuestionItemObject> questionItems = t.getQuestionItems().stream() |
| | | // .map(q -> { |
| | | // ExamPaperQuestionItemObject examPaperQuestionItemObject = new ExamPaperQuestionItemObject(); |
| | | // BeanUtils.copyProperties(q, examPaperQuestionItemObject); |
| | | // examPaperQuestionItemObject.setItemOrder(index.getAndIncrement()); |
| | | // return examPaperQuestionItemObject; |
| | | // }) |
| | | // .collect(Collectors.toList()); |
| | | // titleItem.setQuestionItems(questionItems); |
| | | // return titleItem; |
| | | // }).collect(Collectors.toList()); |
| | | // } |
| | | |
| | | @Override |
| | | public List<ExamPaper> myExamPaper(Integer paperType) { |