| | |
| | | import com.mindskip.xzs.domain.enums.QuestionTypeEnum; |
| | | import com.mindskip.xzs.domain.question.QuestionItemObject; |
| | | import com.mindskip.xzs.domain.question.QuestionObject; |
| | | import com.mindskip.xzs.excel.CurrencyDataListener; |
| | | import com.mindskip.xzs.excel.FixedMergeCellStrategy; |
| | | import com.mindskip.xzs.excel.SelectExcel; |
| | | import com.mindskip.xzs.domain.vo.DeptQuestionVO; |
| | | import com.mindskip.xzs.excel.*; |
| | | import com.mindskip.xzs.repository.DepartmentMapper; |
| | | import com.mindskip.xzs.repository.DeptQuestionMapper; |
| | | import com.mindskip.xzs.repository.SubjectMapper; |
| | | import com.mindskip.xzs.service.*; |
| | | import com.mindskip.xzs.utility.*; |
| | |
| | | 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; |
| | | import org.apache.poi.ss.usermodel.DataValidationHelper; |
| | |
| | | import javax.validation.Valid; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | import java.util.function.Consumer; |
| | |
| | | |
| | | private final QuestionService questionService; |
| | | private final TextContentService textContentService; |
| | | |
| | | private final SubjectMapper subjectMapper; |
| | | |
| | | private final DepartmentMapper departmentMapper; |
| | | |
| | | private final QuestionSubjectService questionSubjectService; |
| | | private final DeptQuestionMapper deptQuestionMapper; |
| | | |
| | | private static final String SPLIT = "、"; |
| | | |
| | | @Autowired |
| | | public QuestionController(QuestionService questionService, TextContentService textContentService, SubjectMapper subjectMapper, DepartmentMapper departmentMapper, QuestionSubjectService questionSubjectService) { |
| | | public QuestionController(QuestionService questionService, TextContentService textContentService, SubjectMapper subjectMapper, DepartmentMapper departmentMapper, QuestionSubjectService questionSubjectService, DeptQuestionMapper deptQuestionMapper) { |
| | | this.questionService = questionService; |
| | | this.textContentService = textContentService; |
| | | this.subjectMapper = subjectMapper; |
| | | this.departmentMapper = departmentMapper; |
| | | this.questionSubjectService = questionSubjectService; |
| | | this.deptQuestionMapper = deptQuestionMapper; |
| | | } |
| | | |
| | | @RequestMapping(value = "/page", method = RequestMethod.POST) |
| | |
| | | e.setSubName(subjectMapper.page(subjectPageRequestVM).get(0).getName()); |
| | | return e; |
| | | }).collect(Collectors.toList())); |
| | | // 查询题目所属部门 |
| | | String deptNames = deptQuestionMapper.deptByQuestionId(q.getId()).stream().map(DeptQuestionVO::getDeptName).collect(Collectors.joining("、")); |
| | | vm.setDeptNames(deptNames); |
| | | return vm; |
| | | }); |
| | | return RestResponse.ok(page); |
| | |
| | | |
| | | // 构建模板样例数据 |
| | | 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(); |
| | | 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(1, 2, 3, 6, 7, 8, 9))) |
| | | .doWrite(data); |
| | | } |
| | | |
| | | @PostMapping("/question/export") |
| | | public void importQuestion(QuestionPageRequestVM query, HttpServletResponse response) throws IOException { |
| | | // 查询导出数据 |
| | | List<QuestionImportVO> exportData = questionService.export(query); |
| | | 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"); |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class).sheet("题目导出数据").doWrite(exportData); |
| | | } |
| | | // @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); |
| | | // } |
| | | |
| | | /** |
| | | * 题目导入 |
| | |
| | | |
| | | |
| | | // 查出所有的课目 |
| | | List<Subject> subjects = subjectMapper.allSubject(); |
| | | List<Subject> subjects = subjectMapper.allSubject(new ArrayList<>(), Boolean.TRUE); |
| | | List<String> subjectNames = Arrays.asList(excelQuestion.getSubjectName().split(SPLIT)); |
| | | List<Subject> targetSubject = subjects.stream() |
| | | .filter(subject -> subjectNames.contains(subject.getName())) |
| | |
| | | } |
| | | 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); |
| | | } |
| | | } |