| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | |
| | | examPaper.setScore(new BigDecimal(form.getScore())); |
| | | //随机试卷 |
| | | if (ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())) { |
| | | //校验题目数量 |
| | | //校验标题是否填写、校验题目数量 |
| | | List<PaperQuestionSettingDTO> questionSetting = form.getQuestionSetting(); |
| | | for (PaperQuestionSettingDTO settingDTO : questionSetting) { |
| | | if(StringUtils.isEmpty(settingDTO.getTitle())){ |
| | | return Result.fail(SystemCode.InnerError.getCode(),"标题不能为空"); |
| | | } |
| | | Integer questionType = settingDTO.getQuestionType(); |
| | | for (PaperSettingItem item : settingDTO.getSettingList()) { |
| | | Integer num = item.getNum(); |
| | |
| | | return Result.ok(); |
| | | } else if (ExamPaperTypeEnum.Fixed.getCode().equals(form.getPaperType())) { |
| | | //固定试卷 |
| | | List<PaperFixQuestionDTO> questionTitleList = form.getQuestionTitleList(); |
| | | for (PaperFixQuestionDTO dto : questionTitleList) { |
| | | if(StringUtils.isEmpty(dto.getTitle())){ |
| | | return Result.fail(SystemCode.InnerError.getCode(),"标题不能为空"); |
| | | } |
| | | } |
| | | examPaper.setContent(JSON.toJSONString(form.getQuestionTitleList())); |
| | | baseMapper.insert(examPaper); |
| | | return Result.ok(); |
| | |
| | | List<PaperSettingItem> settingList = settingDTO.getSettingList(); |
| | | List<PaperQuestion> questionList = new ArrayList<>(); |
| | | for (PaperSettingItem item : settingList) { |
| | | if(StringUtils.isEmpty(settingDTO.getTitle())){ |
| | | return Result.fail(SystemCode.InnerError.getCode(),"标题不能为空"); |
| | | } |
| | | Integer num = item.getNum(); |
| | | Integer difficult = item.getDifficult(); |
| | | //需要配置的题目数量为0则跳过 |
| | |
| | | |
| | | @Override |
| | | public PageInfo<ExamResponseVO> page(ExamPaperPageRequestVO requestVM) { |
| | | //TODO: 数据权限 |
| | | 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 -> { |
| | |
| | | public List<ExamPaper> myExamPaper(Integer paperType) { |
| | | Integer userId = webContext.getCurrentUser().getId(); |
| | | List<ExamPaper> list = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .select(ExamPaper::getId, ExamPaper::getName, ExamPaper::getVisibility) |
| | | .select(ExamPaper::getId, ExamPaper::getName, ExamPaper::getVisibility,ExamPaper::getSuggestTime) |
| | | .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) |
| | | .orderByDesc(ExamPaper::getId) |
| | | .list(); |
| | | list.stream().forEach(item -> { |
| | | if (VisibilityEnum.Public.getName().equals(item.getVisibility())) { |
| | |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | |
| | | // 构建模板样例数据 |
| | | List<QuestionImportVO> data = new ArrayList<>(8); |
| | | QuestionImportVO questionImportVO = new QuestionImportVO(); |
| | | List<ExamPaperImportVO> data = new ArrayList<>(8); |
| | | ExamPaperImportVO questionImportVO = new ExamPaperImportVO(); |
| | | questionImportVO.setLabel("标题1"); |
| | | questionImportVO.setSubject("语文"); |
| | | questionImportVO.setQuestionType("单选题"); |
| | | questionImportVO.setDifficult(2); |
| | | questionImportVO.setCorrect("A"); |
| | |
| | | questionImportVO.setOptionValue("选我"); |
| | | data.add(questionImportVO); |
| | | |
| | | QuestionImportVO questionImport1 = new QuestionImportVO(); |
| | | ExamPaperImportVO questionImport1 = new ExamPaperImportVO(); |
| | | questionImport1.setOptionName("B"); |
| | | questionImport1.setOptionValue("选B"); |
| | | data.add(questionImport1); |
| | | |
| | | QuestionImportVO questionImport2 = new QuestionImportVO(); |
| | | ExamPaperImportVO questionImport2 = new ExamPaperImportVO(); |
| | | questionImport2.setOptionName("C"); |
| | | questionImport2.setOptionValue("选C"); |
| | | data.add(questionImport2); |
| | | |
| | | QuestionImportVO questionImport3 = new QuestionImportVO(); |
| | | ExamPaperImportVO questionImport3 = new ExamPaperImportVO(); |
| | | questionImport3.setOptionName("D"); |
| | | questionImport3.setOptionValue("选D"); |
| | | data.add(questionImport3); |
| | | |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | // 查出所有的课目(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); |
| | |
| | | @SneakyThrows |
| | | public void export(QuestionExportVO query, HttpServletResponse response) { |
| | | // 查询导出数据 |
| | | List<QuestionImportVO> exportData = questionService.export(query); |
| | | List<ExamPaperImportVO> exportData = questionService.export(query); |
| | | // 构建数据 |
| | | List<QuestionImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | List<ExamPaperImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | // 行合并规则 |
| | | List<RowItem> mergeRowList = new ArrayList<>(exportData.size()); |
| | | int j = 2; |
| | | for (QuestionImportVO data : exportData) { |
| | | for (ExamPaperImportVO data : exportData) { |
| | | QuestionObject questionContent = JSON.parseObject(data.getQuestionContent(), QuestionObject.class); |
| | | |
| | | RowItem rowItem = new RowItem(); |
| | |
| | | int i = 0; |
| | | for (QuestionItemObject option : questionContent.getQuestionItemObjects()) { |
| | | if (i == 0) { |
| | | QuestionImportVO master = new QuestionImportVO(); |
| | | 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.setScore(score.divide(BigDecimal.TEN).intValue()); |
| | | exportList.add(master); |
| | | } else { |
| | | QuestionImportVO optionItem = new QuestionImportVO(); |
| | | ExamPaperImportVO optionItem = new ExamPaperImportVO(); |
| | | optionItem.setOptionName(option.getPrefix()); |
| | | optionItem.setOptionValue(option.getContent()); |
| | | exportList.add(optionItem); |
| | |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectService.list(); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | EasyExcel.write(response.getOutputStream(), ExamPaperImportVO.class) |
| | | .sheet("题目导出数据") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new DynamicMergeCellStrategy(mergeRowList, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | |
| | | // 一张试卷多个标题 |
| | | ArrayList<PaperFixQuestionDTO> list = new ArrayList<>(); |
| | | |
| | | Consumer<List<QuestionImportVO>> consumer = (data) -> { |
| | | Consumer<List<ExamPaperImportVO>> consumer = (data) -> { |
| | | // 循环每一行 |
| | | for (int i = 0; i < data.size(); i++) { |
| | | // 读取的题目 |
| | | QuestionImportVO excelQuestion = data.get(i); |
| | | ExamPaperImportVO excelQuestion = data.get(i); |
| | | String questionType = excelQuestion.getQuestionType(); |
| | | String label = excelQuestion.getLabel(); |
| | | // 判断是否标题 |
| | |
| | | if (i + 1 == data.size()) { |
| | | break; |
| | | } |
| | | QuestionImportVO nextQuestion = data.get(1 + i); |
| | | ExamPaperImportVO nextQuestion = data.get(1 + i); |
| | | if (nextQuestion.intact()) { |
| | | break; |
| | | } |
| | |
| | | question.setDeleted(0); |
| | | question.setQuestionType(QuestionTypeEnum.get(questionType)); |
| | | // 根据科目名称获取id |
| | | QuestionImportVO finalExcelQuestion = excelQuestion; |
| | | ExamPaperImportVO finalExcelQuestion = excelQuestion; |
| | | question.setSubjectId(subjects.stream().filter(subject -> subject.getName().equals(finalExcelQuestion.getSubject())).findFirst().get().getId()); |
| | | questions.add(question); |
| | | |
| | |
| | | paperQuestion.setScore(BigDecimal.valueOf(excelQuestion.getScore())); |
| | | paperQuestions.add(paperQuestion); |
| | | } |
| | | // 下一条数据是标题 |
| | | if (i + 1 == data.size() || data.get(i + 1).master()) { |
| | | break; |
| | | } |
| | |
| | | } |
| | | } |
| | | }; |
| | | EasyExcel.read(file.getInputStream(), QuestionImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | EasyExcel.read(file.getInputStream(), ExamPaperImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | // 保存题目 |
| | | questionService.saveBatch(questions); |
| | | // 保存试卷 |