| | |
| | | package com.ycl.jxkg.service.impl; |
| | | |
| | | import com.ycl.jxkg.domain.TextContent; |
| | | import com.ycl.jxkg.domain.enums.ExamPaperTypeEnum; |
| | | import com.ycl.jxkg.domain.exam.ExamPaperQuestionItemObject; |
| | | import com.ycl.jxkg.domain.exam.ExamPaperTitleItemObject; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.ycl.jxkg.base.Result; |
| | | import com.ycl.jxkg.base.SystemCode; |
| | | import com.ycl.jxkg.context.WebContext; |
| | | import com.ycl.jxkg.domain.entity.ExamPaper; |
| | | import com.ycl.jxkg.domain.entity.Question; |
| | | import com.ycl.jxkg.domain.entity.Subject; |
| | | import com.ycl.jxkg.domain.exam.PaperFixQuestionDTO; |
| | | import com.ycl.jxkg.domain.exam.PaperQuestion; |
| | | import com.ycl.jxkg.domain.exam.PaperQuestionSettingDTO; |
| | | import com.ycl.jxkg.domain.exam.PaperSettingItem; |
| | | import com.ycl.jxkg.domain.form.ExamPaperForm; |
| | | import com.ycl.jxkg.domain.other.KeyValue; |
| | | import com.ycl.jxkg.repository.ExamPaperMapper; |
| | | import com.ycl.jxkg.repository.QuestionMapper; |
| | | import com.ycl.jxkg.domain.question.QuestionItemObject; |
| | | import com.ycl.jxkg.domain.question.QuestionObject; |
| | | import com.ycl.jxkg.domain.question.RandomQuestionDTO; |
| | | import com.ycl.jxkg.domain.vo.admin.exam.*; |
| | | 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.ycl.jxkg.enums.ExamPaperTypeEnum; |
| | | import com.ycl.jxkg.enums.QuestionTypeEnum; |
| | | import com.ycl.jxkg.enums.VisibilityEnum; |
| | | import com.ycl.jxkg.enums.general.StatusEnum; |
| | | import com.ycl.jxkg.excel.*; |
| | | 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.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.utils.DateTimeUtil; |
| | | import com.ycl.jxkg.utils.PageInfoHelper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | import java.util.function.Consumer; |
| | | 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; |
| | | private final WebContext webContext; |
| | | |
| | | @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 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result addPaper(ExamPaperForm form) { |
| | | ExamPaper examPaper = ExamPaperForm.getEntityByForm(form, null); |
| | | examPaper.setScore(new BigDecimal(form.getScore())); |
| | | //随机试卷 |
| | | if (ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())) { |
| | | //校验题目数量 |
| | | List<PaperQuestionSettingDTO> questionSetting = form.getQuestionSetting(); |
| | | for (PaperQuestionSettingDTO settingDTO : questionSetting) { |
| | | Integer questionType = settingDTO.getQuestionType(); |
| | | for (PaperSettingItem item : settingDTO.getSettingList()) { |
| | | Integer num = item.getNum(); |
| | | Integer difficult = item.getDifficult(); |
| | | //需要配置的题目数量为0则跳过 |
| | | if(num ==null || num ==0 )continue; |
| | | //前端默认数据为0,转换为null |
| | | if (0 == item.getDifficult()){ |
| | | difficult= null; |
| | | } |
| | | RandomQuestionDTO randomQuestionDTO = new RandomQuestionDTO(); |
| | | randomQuestionDTO.setQuestionType(questionType); |
| | | randomQuestionDTO.setSubjectId(item.getSubjectId()); |
| | | randomQuestionDTO.setDifficult(difficult); |
| | | Integer numInData = questionMapper.selectByDifAndSub(randomQuestionDTO); |
| | | if(num > numInData)return Result.fail(SystemCode.InnerError.getCode(), QuestionTypeEnum.fromCode(questionType).getName()+"难度为"+item.getDifficult()+"的题目数量不足"); |
| | | } |
| | | } |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionSetting())); |
| | | baseMapper.insert(examPaper); |
| | | return Result.ok(); |
| | | } else if (ExamPaperTypeEnum.Fixed.getCode().equals(form.getPaperType())) { |
| | | //固定试卷 |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionTitleList())); |
| | | baseMapper.insert(examPaper); |
| | | return Result.ok(); |
| | | } else { |
| | | //随序试卷 |
| | | List<PaperQuestionSettingDTO> questionSetting = form.getQuestionSetting(); |
| | | //题目配置里配的试卷类型 |
| | | List<Integer> types = questionSetting.stream().map(PaperQuestionSettingDTO::getQuestionType).collect(Collectors.toList()); |
| | | if (CollectionUtils.isEmpty(types)) { |
| | | return Result.fail(SystemCode.InnerError.getCode(), "试卷题目类型不能为空"); |
| | | } |
| | | List<PaperFixQuestionDTO> questionTitleList = new ArrayList<>(); |
| | | for (PaperQuestionSettingDTO settingDTO : questionSetting) { |
| | | List<PaperSettingItem> settingList = settingDTO.getSettingList(); |
| | | List<PaperQuestion> questionList = new ArrayList<>(); |
| | | for (PaperSettingItem item : settingList) { |
| | | Integer num = item.getNum(); |
| | | Integer difficult = item.getDifficult(); |
| | | //需要配置的题目数量为0则跳过 |
| | | if(num ==null || num ==0 )continue; |
| | | //前端默认数据为0,转换为null |
| | | if (0 == difficult){ |
| | | difficult = null; |
| | | } |
| | | List<Question> questions = questionMapper.getRandomQuestion(item.getSubjectId(), settingDTO.getQuestionType(), difficult, item.getNum()); |
| | | if (CollectionUtils.isEmpty(questions) || item.getNum() > questions.size()) { |
| | | return Result.fail(SystemCode.InnerError.getCode(), QuestionTypeEnum.fromCode(settingDTO.getQuestionType()).getName()+"难度为:"+item.getDifficult()+"的题目数量不足"); |
| | | } |
| | | //转换数据 |
| | | convert(questionList, item, questions); |
| | | } |
| | | PaperFixQuestionDTO dto = new PaperFixQuestionDTO(); |
| | | dto.setTitle(settingDTO.getTitle()); |
| | | dto.setQuestionType(settingDTO.getQuestionType()); |
| | | dto.setQuestionList(questionList); |
| | | questionTitleList.add(dto); |
| | | } |
| | | examPaper.setContent(JSON.toJSONString(questionTitleList)); |
| | | baseMapper.insert(examPaper); |
| | | return Result.ok(); |
| | | } |
| | | } |
| | | //转换数据 |
| | | private void convert(List<PaperQuestion> questionList, PaperSettingItem item, List<Question> questions) { |
| | | for (Question question : questions) { |
| | | PaperQuestion paperQuestion = new PaperQuestion(); |
| | | BeanUtils.copyProperties(question,paperQuestion); |
| | | paperQuestion.setScore(item.getScore()); |
| | | //转换 |
| | | QuestionObject questionObject = JSONObject.parseObject(question.getContent(), QuestionObject.class); |
| | | if(questionObject != null){ |
| | | paperQuestion.setItems(questionObject.getQuestionItemObjects()); |
| | | paperQuestion.setAnalyze(questionObject.getAnalyze()); |
| | | paperQuestion.setTitle(questionObject.getTitleContent()); |
| | | } |
| | | questionList.add(paperQuestion); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public PageInfo<ExamPaper> page(ExamPaperPageRequestVM requestVM) { |
| | | return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | | public Result updateExamPaper(ExamPaperForm form) { |
| | | //TODO:验证是否是试卷创建人 |
| | | ExamPaper examPaper = ExamPaperForm.getEntityByForm(form, null); |
| | | examPaper.setScore(new BigDecimal(form.getScore())); |
| | | //随机试卷 |
| | | if (ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())) { |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionSetting())); |
| | | baseMapper.updateById(examPaper); |
| | | return Result.ok(); |
| | | } else if (ExamPaperTypeEnum.Fixed.getCode().equals(form.getPaperType())) { |
| | | //固定试卷 |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionTitleList())); |
| | | baseMapper.updateById(examPaper); |
| | | return Result.ok(); |
| | | } else { |
| | | //随序试卷 |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionTitleList())); |
| | | baseMapper.updateById(examPaper); |
| | | return Result.ok(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public PageInfo<ExamResponseVO> page(ExamPaperPageRequestVO requestVM) { |
| | | requestVM.setCreateUser(webContext.getCurrentUser().getId()); |
| | | 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())); |
| | | return vo; |
| | | }); |
| | | return pageVO; |
| | | } |
| | | |
| | | @Override |
| | | public PageInfo<ExamPaper> taskExamPage(ExamPaperPageRequestVM 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(ExamPaperPageVM requestVM) { |
| | | public PageInfo<ExamPaper> studentPage(ExamPaperPageVO requestVM) { |
| | | return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | | examPaperMapper.studentPage(requestVM)); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) { |
| | | ActionEnum actionEnum = (examPaperEditRequestVM.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; |
| | | Date now = new Date(); |
| | | List<ExamPaperTitleItemVM> titleItemsVM = examPaperEditRequestVM.getTitleItems(); |
| | | List<ExamPaperTitleItemObject> frameTextContentList = frameTextContentFromVM(titleItemsVM); |
| | | String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList); |
| | | // @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; |
| | | // } |
| | | |
| | | ExamPaper examPaper; |
| | | if (actionEnum == ActionEnum.ADD) { |
| | | examPaper = modelMapper.map(examPaperEditRequestVM, ExamPaper.class); |
| | | TextContent frameTextContent = new TextContent(frameTextContentStr, now); |
| | | textContentService.insertByFilter(frameTextContent); |
| | | examPaper.setFrameTextContentId(frameTextContent.getId()); |
| | | examPaper.setCreateTime(now); |
| | | examPaper.setCreateUser(user.getId()); |
| | | examPaper.setDeleted(false); |
| | | examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM); |
| | | examPaperMapper.insertSelective(examPaper); |
| | | @Override |
| | | public ExamPaperEditRequestVO examPaperToVM(Integer id) { |
| | | ExamPaper examPaper = examPaperMapper.selectById(id); |
| | | ExamPaperEditRequestVO vo = new ExamPaperEditRequestVO(); |
| | | BeanUtils.copyProperties(examPaper, vo); |
| | | vo.setVisibility(VisibilityEnum.valueOf(vo.getVisibility()).getCode()); |
| | | //随机试卷 |
| | | if (ExamPaperTypeEnum.Random.getCode().equals(examPaper.getPaperType())) { |
| | | vo.setQuestionSetting(JSONArray.parseArray(examPaper.getContent(), PaperQuestionSettingDTO.class)); |
| | | } else if (ExamPaperTypeEnum.Fixed.getCode().equals(examPaper.getPaperType())) { |
| | | //固定试卷 |
| | | vo.setQuestionTitleList(JSONArray.parseArray(examPaper.getContent(), PaperFixQuestionDTO.class)); |
| | | } 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); |
| | | //随序试卷 |
| | | vo.setQuestionTitleList(JSONArray.parseArray(examPaper.getContent(), PaperFixQuestionDTO.class)); |
| | | } |
| | | 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; |
| | | return vo; |
| | | } |
| | | |
| | | @Override |
| | |
| | | }).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) { |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | return titleItems.stream().map(t -> { |
| | | ExamPaperTitleItemObject titleItem = modelMapper.map(t, ExamPaperTitleItemObject.class); |
| | | List<ExamPaperQuestionItemObject> questionItems = t.getQuestionItems().stream() |
| | | .map(q -> { |
| | | ExamPaperQuestionItemObject examPaperQuestionItemObject = modelMapper.map(q, ExamPaperQuestionItemObject.class); |
| | | 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) { |
| | | 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; |
| | | } |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | | public void importTemplate(HttpServletResponse response) { |
| | | String fileName = URLEncoder.encode("试卷导入模板", "UTF-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | |
| | | // 构建模板样例数据 |
| | | List<ExamPaperImportVO> data = new ArrayList<>(8); |
| | | ExamPaperImportVO questionImportVO = new ExamPaperImportVO(); |
| | | questionImportVO.setLabel("标题1"); |
| | | questionImportVO.setSubject("语文"); |
| | | questionImportVO.setQuestionType("单选题"); |
| | | questionImportVO.setDifficult(2); |
| | | questionImportVO.setCorrect("A"); |
| | | questionImportVO.setScore(2); |
| | | questionImportVO.setAnalyze("A是对的"); |
| | | questionImportVO.setTitle("这是一道测试题目,使用该模板请删除或替换这道题"); |
| | | questionImportVO.setOptionName("A"); |
| | | questionImportVO.setOptionValue("选我"); |
| | | data.add(questionImportVO); |
| | | |
| | | ExamPaperImportVO questionImport1 = new ExamPaperImportVO(); |
| | | questionImport1.setOptionName("B"); |
| | | questionImport1.setOptionValue("选B"); |
| | | data.add(questionImport1); |
| | | |
| | | ExamPaperImportVO questionImport2 = new ExamPaperImportVO(); |
| | | questionImport2.setOptionName("C"); |
| | | questionImport2.setOptionValue("选C"); |
| | | data.add(questionImport2); |
| | | |
| | | ExamPaperImportVO questionImport3 = new ExamPaperImportVO(); |
| | | questionImport3.setOptionName("D"); |
| | | questionImport3.setOptionValue("选D"); |
| | | data.add(questionImport3); |
| | | |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectService.list(); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), ExamPaperImportVO.class) |
| | | .sheet("模板") |
| | | .registerWriteHandler(new PaperSelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 4, Arrays.asList(2, 3, 6, 7, 8, 9))) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 20, Arrays.asList(0, 1))) |
| | | .doWrite(data); |
| | | } |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | | public void export(QuestionExportVO query, HttpServletResponse response) { |
| | | // 查询导出数据 |
| | | List<ExamPaperImportVO> exportData = questionService.export(query); |
| | | // 构建数据 |
| | | List<ExamPaperImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | // 行合并规则 |
| | | List<RowItem> mergeRowList = new ArrayList<>(exportData.size()); |
| | | int j = 2; |
| | | for (ExamPaperImportVO data : exportData) { |
| | | QuestionObject questionContent = JSON.parseObject(data.getQuestionContent(), QuestionObject.class); |
| | | |
| | | RowItem rowItem = new RowItem(); |
| | | rowItem.setStart(j); |
| | | int end = j + questionContent.getQuestionItemObjects().size() - 1; |
| | | rowItem.setEnd(end); |
| | | mergeRowList.add(rowItem); |
| | | j = end + 1; |
| | | int i = 0; |
| | | for (QuestionItemObject option : questionContent.getQuestionItemObjects()) { |
| | | if (i == 0) { |
| | | ExamPaperImportVO master = new ExamPaperImportVO(); |
| | | BeanUtils.copyProperties(data, master); |
| | | if (org.springframework.util.StringUtils.hasText(data.getQuestionType())) { |
| | | master.setQuestionType(QuestionTypeEnum.fromCode(Integer.valueOf(data.getQuestionType())).getName()); |
| | | } |
| | | master.setOptionName(option.getPrefix()); |
| | | master.setOptionValue(option.getContent()); |
| | | master.setTitle(questionContent.getTitleContent()); |
| | | master.setAnalyze(questionContent.getAnalyze()); |
| | | master.setCorrect(data.getCorrect().replaceAll(",", "、")); |
| | | BigDecimal score = BigDecimal.valueOf(master.getScore()); |
| | | master.setScore(score.divide(BigDecimal.TEN).intValue()); |
| | | exportList.add(master); |
| | | } else { |
| | | ExamPaperImportVO optionItem = new ExamPaperImportVO(); |
| | | optionItem.setOptionName(option.getPrefix()); |
| | | optionItem.setOptionValue(option.getContent()); |
| | | exportList.add(optionItem); |
| | | } |
| | | i++; |
| | | } |
| | | } |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("题目导出数据", "UTF-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectService.list(); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), ExamPaperImportVO.class) |
| | | .sheet("题目导出数据") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new DynamicMergeCellStrategy(mergeRowList, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | | .doWrite(exportList); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | @SneakyThrows |
| | | public Result importPaper(MultipartFile file, ExamPaperForm form) { |
| | | List<Subject> subjects = subjectService.list(); |
| | | List<Integer> questionNum = new ArrayList<>(); |
| | | List<Integer> totalScore = new ArrayList<>(); |
| | | // 拿到试卷 |
| | | ExamPaper examPaper = ExamPaperForm.getEntityByForm(form, null); |
| | | // 题目集合用于批量保存 |
| | | ArrayList<Question> questions = new ArrayList<>(); |
| | | // 一张试卷多个标题 |
| | | ArrayList<PaperFixQuestionDTO> list = new ArrayList<>(); |
| | | |
| | | Consumer<List<ExamPaperImportVO>> consumer = (data) -> { |
| | | // 循环每一行 |
| | | for (int i = 0; i < data.size(); i++) { |
| | | // 读取的题目 |
| | | ExamPaperImportVO excelQuestion = data.get(i); |
| | | String questionType = excelQuestion.getQuestionType(); |
| | | String label = excelQuestion.getLabel(); |
| | | // 判断是否标题 |
| | | if (excelQuestion.master()) { |
| | | // 一个标题多个题目 |
| | | ArrayList<PaperQuestion> paperQuestions = new ArrayList<>(); |
| | | // 循环题目 |
| | | while (Boolean.TRUE) { |
| | | // 更新读取的题目 |
| | | excelQuestion = data.get(i); |
| | | // 判断是否题目 |
| | | if (excelQuestion.intact()) { |
| | | Question question = new Question(); |
| | | totalScore.add(excelQuestion.getScore()); |
| | | // 该题的选项 |
| | | List<QuestionItemObject> options = new ArrayList<>(8); |
| | | // 选项内容 |
| | | QuestionItemObject option = new QuestionItemObject(); |
| | | option.setPrefix(excelQuestion.getOptionName()); |
| | | option.setContent(excelQuestion.getOptionValue()); |
| | | options.add(option); |
| | | // 循环选项 |
| | | while (Boolean.TRUE) { |
| | | // 判断是否是最后一条 |
| | | if (i + 1 == data.size()) { |
| | | break; |
| | | } |
| | | ExamPaperImportVO nextQuestion = data.get(1 + i); |
| | | if (nextQuestion.intact()) { |
| | | break; |
| | | } |
| | | QuestionItemObject nextOption = new QuestionItemObject(); |
| | | nextOption.setPrefix(nextQuestion.getOptionName()); |
| | | nextOption.setContent(nextQuestion.getOptionValue()); |
| | | options.add(nextOption); |
| | | i++; |
| | | } |
| | | // 保存题目内容 |
| | | QuestionObject questionObject = new QuestionObject(); |
| | | questionObject.setQuestionItemObjects(options); |
| | | questionObject.setAnalyze(excelQuestion.getAnalyze()); |
| | | questionObject.setTitleContent(excelQuestion.getTitle()); |
| | | questionObject.setCorrect(excelQuestion.getCorrect()); |
| | | question.setTitle(excelQuestion.getTitle()); |
| | | question.setContent(JSON.toJSONString(questionObject)); |
| | | question.setQuestionType(QuestionTypeEnum.get(excelQuestion.getQuestionType())); |
| | | // 答案(多选需要用、分割保存字符串到数据库) |
| | | String[] corrects = excelQuestion.getCorrect().split("、"); |
| | | if (corrects.length > 1) { |
| | | question.setCorrect(String.join(",", corrects)); |
| | | } else { |
| | | question.setCorrect(excelQuestion.getCorrect()); |
| | | } |
| | | // 难度 |
| | | question.setDifficult(excelQuestion.getDifficult()); |
| | | // 创建人 |
| | | question.setCreateUser(2); |
| | | question.setStatus(StatusEnum.ENABLE); |
| | | question.setCreateTime(new Date()); |
| | | question.setDeleted(0); |
| | | question.setQuestionType(QuestionTypeEnum.get(questionType)); |
| | | // 根据科目名称获取id |
| | | ExamPaperImportVO finalExcelQuestion = excelQuestion; |
| | | question.setSubjectId(subjects.stream().filter(subject -> subject.getName().equals(finalExcelQuestion.getSubject())).findFirst().get().getId()); |
| | | questions.add(question); |
| | | |
| | | PaperQuestion paperQuestion = new PaperQuestion(); |
| | | BeanUtils.copyProperties(question, paperQuestion); |
| | | paperQuestion.setItems(options); |
| | | paperQuestion.setAnalyze(excelQuestion.getAnalyze()); |
| | | paperQuestion.setScore(BigDecimal.valueOf(excelQuestion.getScore())); |
| | | paperQuestions.add(paperQuestion); |
| | | } |
| | | // 下一条数据是标题 |
| | | if (i + 1 == data.size() || data.get(i + 1).master()) { |
| | | break; |
| | | } |
| | | i++; |
| | | } |
| | | // 组装试卷内容 |
| | | PaperFixQuestionDTO paperFixQuestionDTO = new PaperFixQuestionDTO(); |
| | | paperFixQuestionDTO.setTitle(label); |
| | | paperFixQuestionDTO.setQuestionType(QuestionTypeEnum.get(questionType)); |
| | | paperFixQuestionDTO.setQuestionList(paperQuestions); |
| | | questionNum.add(paperQuestions.size()); |
| | | list.add(paperFixQuestionDTO); |
| | | } |
| | | } |
| | | }; |
| | | EasyExcel.read(file.getInputStream(), ExamPaperImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | // 保存题目 |
| | | questionService.saveBatch(questions); |
| | | // 保存试卷 |
| | | examPaper.setContent(JSON.toJSONString(list)); |
| | | examPaper.setVisibility(VisibilityEnum.fromCode(form.getVisibility()).getName()); |
| | | examPaper.setCreateUser(2); |
| | | examPaper.setCreateTime(new Date()); |
| | | Integer score = totalScore.stream().reduce(Integer::sum).orElse(0); |
| | | Integer num = questionNum.stream().reduce(Integer::sum).orElse(0); |
| | | examPaper.setScore(new BigDecimal(score)); |
| | | examPaper.setNum(num); |
| | | examPaperMapper.insert(examPaper); |
| | | return Result.ok(); |
| | | } |
| | | } |