| | |
| | | import com.mindskip.xzs.viewmodel.admin.education.SubjectPageRequestVM; |
| | | import com.mindskip.xzs.viewmodel.admin.question.*; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.mindskip.xzs.vo.QuestionExportData; |
| | | import com.mindskip.xzs.vo.QuestionExportVO; |
| | | import com.mindskip.xzs.vo.QuestionImportVO; |
| | | import org.apache.commons.lang3.StringUtils; |
| | |
| | | |
| | | // 构建模板样例数据 |
| | | List<QuestionImportVO> data = new ArrayList<>(4); |
| | | QuestionImportVO questionImportVO = new QuestionImportVO(); |
| | | questionImportVO.setQuestionType("单选题"); |
| | | questionImportVO.setDifficult(2); |
| | | questionImportVO.setCorrect("B"); |
| | | questionImportVO.setScore(2); |
| | | questionImportVO.setSubjectName("测试课目"); |
| | | questionImportVO.setAnalyze("B是对的"); |
| | | questionImportVO.setTitle("这是一道测试题目,使用该模板请删除或替换这道题"); |
| | | questionImportVO.setOptionName("A"); |
| | | questionImportVO.setOptionValue("选我"); |
| | | data.add(questionImportVO); |
| | | |
| | | QuestionImportVO questionImportVO1 = new QuestionImportVO(); |
| | | questionImportVO1.setOptionName("B"); |
| | | questionImportVO1.setOptionValue("选B"); |
| | | data.add(questionImportVO1); |
| | | |
| | | QuestionImportVO questionImportVO2 = new QuestionImportVO(); |
| | | questionImportVO2.setOptionName("C"); |
| | | questionImportVO2.setOptionValue("选C"); |
| | | data.add(questionImportVO2); |
| | | |
| | | QuestionImportVO questionImportVO3 = new QuestionImportVO(); |
| | | questionImportVO3.setOptionName("D"); |
| | | questionImportVO3.setOptionValue("选D"); |
| | | data.add(questionImportVO3); |
| | | |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectMapper.allSubject(new ArrayList<>(), Boolean.TRUE); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | EasyExcel.write(response.getOutputStream(), QuestionExportData.class) |
| | | .sheet("模板") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 4, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | | .doWrite(data); |
| | | } |
| | | |
| | | @PostMapping("/question/export") |
| | | public void exportQuestion(@RequestBody QuestionExportVO query, HttpServletResponse response) throws IOException { |
| | | // 查询导出数据 |
| | | List<QuestionImportVO> exportData = questionService.export(query); |
| | | // 构建数据 |
| | | List<QuestionImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | // 行合并规则 |
| | | List<RowItem> mergeRowList = new ArrayList<>(exportData.size()); |
| | | int j = 2; |
| | | for (QuestionImportVO 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) { |
| | | QuestionImportVO master = new QuestionImportVO(); |
| | | 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.setSubjectName(data.getSubjectList().stream().collect(Collectors.joining("、"))); |
| | | master.setCorrect(data.getCorrect().replaceAll(",", "、")); |
| | | BigDecimal score = BigDecimal.valueOf(master.getScore()); |
| | | master.setScore(score.divide(BigDecimal.TEN).intValue()); |
| | | exportList.add(master); |
| | | } else { |
| | | QuestionImportVO optionItem = new QuestionImportVO(); |
| | | 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 = subjectMapper.allSubject(new ArrayList<>(), Boolean.TRUE); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | .sheet("题目导出数据") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new DynamicMergeCellStrategy(mergeRowList, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | | .doWrite(exportList); |
| | | } |
| | | // @PostMapping("/question/export") |
| | | // public void exportQuestion(@RequestBody QuestionExportVO query, HttpServletResponse response) throws IOException { |
| | | // // 查询导出数据 |
| | | // List<QuestionImportVO> exportData = questionService.export(query); |
| | | // // 构建数据 |
| | | // List<QuestionImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | // // 行合并规则 |
| | | // List<RowItem> mergeRowList = new ArrayList<>(exportData.size()); |
| | | // int j = 2; |
| | | // for (QuestionImportVO 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) { |
| | | // QuestionImportVO master = new QuestionImportVO(); |
| | | // 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.setSubjectName(data.getSubjectList().stream().collect(Collectors.joining("、"))); |
| | | // master.setCorrect(data.getCorrect().replaceAll(",", "、")); |
| | | // BigDecimal score = BigDecimal.valueOf(master.getScore()); |
| | | // master.setScore(score.divide(BigDecimal.TEN).intValue()); |
| | | // exportList.add(master); |
| | | // } else { |
| | | // QuestionImportVO optionItem = new QuestionImportVO(); |
| | | // 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 = subjectMapper.allSubject(new ArrayList<>(), Boolean.TRUE); |
| | | // List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | // EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | // .sheet("题目导出数据") |
| | | // .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | // .registerWriteHandler(new DynamicMergeCellStrategy(mergeRowList, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | | // .doWrite(exportList); |
| | | // } |
| | | |
| | | /** |
| | | * 题目导入 |
| | |
| | | } |
| | | return RestResponse.ok(); |
| | | } |
| | | |
| | | @PostMapping("/question/export") |
| | | public void exportQuestion(@RequestBody QuestionExportVO query, HttpServletResponse response) throws IOException { |
| | | // 查询导出数据 |
| | | List<QuestionExportData> exportData = questionService.exportData(query); |
| | | // 处理完善数据 |
| | | exportData.stream().forEach(question -> { |
| | | if (StringUtils.isNotBlank(question.getContent())) { |
| | | QuestionObject questionContent = JSON.parseObject(question.getContent(), QuestionObject.class); |
| | | // 设置选项 |
| | | for (QuestionItemObject option : questionContent.getQuestionItemObjects()) { |
| | | String optionContent = option.getContent(); |
| | | if ("A".equals(option.getPrefix())) { |
| | | question.setOptionA(optionContent); |
| | | } else if ("B".equals(option.getPrefix())) { |
| | | question.setOptionB(optionContent); |
| | | } else if ("C".equals(option.getPrefix())) { |
| | | question.setOptionC(optionContent); |
| | | } else if ("D".equals(option.getPrefix())) { |
| | | question.setOptionD(optionContent); |
| | | } |
| | | } |
| | | // 设置题干、解析 |
| | | question.setAnalyze(questionContent.getAnalyze()); |
| | | question.setTitle(questionContent.getTitleContent()); |
| | | // 设置课目 |
| | | question.setSubject(question.getSubjectList().stream().collect(Collectors.joining(","))); |
| | | // 设置题型 |
| | | question.setQuestionType(QuestionTypeEnum.fromCode(Integer.valueOf(question.getQuestionType())).getName()); |
| | | } |
| | | }); |
| | | 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下拉数据) |
| | | EasyExcel.write(response.getOutputStream(), QuestionExportData.class) |
| | | .sheet("题目导出数据") |
| | | .doWrite(exportData); |
| | | } |
| | | } |