From 9797dca8f3a12c0e72942b8043c1b225bde2bbaf Mon Sep 17 00:00:00 2001 From: 龚焕茏 <2842157468@qq.com> Date: 星期二, 09 七月 2024 16:26:02 +0800 Subject: [PATCH] feat:题目导入 --- src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java | 40 +++++++++++++++++++++++----------------- 1 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java index 4e85c14..612d808 100644 --- a/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java +++ b/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java @@ -350,9 +350,10 @@ 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"); @@ -363,23 +364,27 @@ questionImportVO.setOptionValue("閫夋垜"); data.add(questionImportVO); - QuestionImportVO questionImport1 = new QuestionImportVO(); + ExamPaperImportVO questionImport1 = new ExamPaperImportVO(); questionImport1.setOptionName("B"); questionImport1.setOptionValue("閫塀"); data.add(questionImport1); - QuestionImportVO questionImport2 = new QuestionImportVO(); + ExamPaperImportVO questionImport2 = new ExamPaperImportVO(); questionImport2.setOptionName("C"); questionImport2.setOptionValue("閫塁"); data.add(questionImport2); - QuestionImportVO questionImport3 = new QuestionImportVO(); + ExamPaperImportVO questionImport3 = new ExamPaperImportVO(); questionImport3.setOptionName("D"); questionImport3.setOptionValue("閫塂"); data.add(questionImport3); - EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) + // 鏌ュ嚭鎵�鏈夌殑璇剧洰锛坋xcel涓嬫媺鏁版嵁锛� + 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); @@ -389,13 +394,13 @@ @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(); @@ -407,7 +412,7 @@ 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()); @@ -421,7 +426,7 @@ 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); @@ -438,7 +443,7 @@ // 鏌ュ嚭鎵�鏈夌殑璇剧洰锛坋xcel涓嬫媺鏁版嵁锛� 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))) @@ -459,11 +464,11 @@ // 涓�寮犺瘯鍗峰涓爣棰� 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(); // 鍒ゆ柇鏄惁鏍囬 @@ -491,7 +496,7 @@ if (i + 1 == data.size()) { break; } - QuestionImportVO nextQuestion = data.get(1 + i); + ExamPaperImportVO nextQuestion = data.get(1 + i); if (nextQuestion.intact()) { break; } @@ -526,7 +531,7 @@ 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); @@ -537,6 +542,7 @@ paperQuestion.setScore(BigDecimal.valueOf(excelQuestion.getScore())); paperQuestions.add(paperQuestion); } + // 涓嬩竴鏉℃暟鎹槸鏍囬 if (i + 1 == data.size() || data.get(i + 1).master()) { break; } @@ -552,7 +558,7 @@ } } }; - 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); // 淇濆瓨璇曞嵎 -- Gitblit v1.8.0