| | |
| | | private final QuestionSubjectService questionSubjectService; |
| | | private final DeptQuestionMapper deptQuestionMapper; |
| | | |
| | | private static final String SPLIT = "、"; |
| | | private static final String SPLIT = ","; |
| | | |
| | | public QuestionController(QuestionService questionService, TextContentService textContentService, SubjectMapper subjectMapper, DepartmentMapper departmentMapper, QuestionSubjectService questionSubjectService, DeptQuestionMapper deptQuestionMapper) { |
| | | this.questionService = questionService; |
| | |
| | | * @param file |
| | | * @throws IOException |
| | | */ |
| | | @PostMapping("/question/import") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public RestResponse importQuestion(@RequestPart("file") MultipartFile file) throws IOException { |
| | | Consumer<List<QuestionImportVO>> consumer = (data) -> { |
| | | // 题目的课目信息 |
| | | List<QuestionSubject> questionSubjectsList = new ArrayList<>(48); |
| | | for (int i = 0; i < data.size(); i++) { |
| | | // 题目实体 |
| | | Question question = new Question(); |
| | | // 读取的题目 |
| | | QuestionImportVO excelQuestion = data.get(i); |
| | | |
| | | // 如果是第一条完整数据,那么继续往后读取选项内容 |
| | | if (excelQuestion.master()) { |
| | | // 该题的选项 |
| | | List<QuestionItemObject> options = new ArrayList<>(4); |
| | | // 选项内容 |
| | | QuestionItemObject option = new QuestionItemObject(); |
| | | option.setPrefix(excelQuestion.getOptionName()); |
| | | option.setContent(excelQuestion.getOptionValue()); |
| | | options.add(option); |
| | | int next = 1; |
| | | // 继续往后读选项 |
| | | while (Boolean.TRUE) { |
| | | // 判断是否是最后一条 |
| | | if (next + i == data.size()) { |
| | | break; |
| | | } |
| | | QuestionImportVO nextQuestion = data.get(next + i); |
| | | if (nextQuestion.master()) { |
| | | break; |
| | | } |
| | | QuestionItemObject nextOption = new QuestionItemObject(); |
| | | nextOption.setPrefix(nextQuestion.getOptionName()); |
| | | nextOption.setContent(nextQuestion.getOptionValue()); |
| | | options.add(nextOption); |
| | | next++; |
| | | } |
| | | i += next; |
| | | |
| | | // 保存题目内容 |
| | | QuestionObject questionObject = new QuestionObject(); |
| | | questionObject.setQuestionItemObjects(options); |
| | | questionObject.setAnalyze(excelQuestion.getAnalyze()); |
| | | questionObject.setTitleContent(excelQuestion.getTitle()); |
| | | questionObject.setCorrect(excelQuestion.getCorrect()); |
| | | |
| | | TextContent textContent = new TextContent(); |
| | | textContent.setContent(JSON.toJSONString(questionObject)); |
| | | textContent.setCreateTime(new Date()); |
| | | textContentService.insert(textContent); |
| | | |
| | | // 保存题目信息 |
| | | // 设置题型 |
| | | question.setQuestionType(QuestionTypeEnum.get(excelQuestion.getQuestionType())); |
| | | // 答案(多选需要用,分割保存字符串到数据库) |
| | | String[] corrects = excelQuestion.getCorrect().split(SPLIT); |
| | | if (corrects.length > 1) { |
| | | question.setCorrect(Arrays.asList(corrects).stream().collect(Collectors.joining(","))); |
| | | } else { |
| | | question.setCorrect(excelQuestion.getCorrect()); |
| | | } |
| | | |
| | | // 难度 |
| | | question.setDifficult(excelQuestion.getDifficult()); |
| | | // 分数 |
| | | question.setScore(ExamUtil.scoreFromVM(String.valueOf(excelQuestion.getScore()))); |
| | | // 创建人 |
| | | question.setCreateUser(1); |
| | | question.setStatus(QuestionStatusEnum.OK.getCode()); |
| | | question.setCreateTime(new Date()); |
| | | question.setDeleted(Boolean.FALSE); |
| | | question.setInfoTextContentId(textContent.getId()); |
| | | questionService.insert(question); |
| | | |
| | | |
| | | // 查出所有的课目 |
| | | 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())) |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isEmpty(targetSubject)) { |
| | | // todo 记录这个错误 |
| | | continue; |
| | | } |
| | | // 构建课目-题目信息 |
| | | questionSubjectsList = targetSubject.stream().map(subject -> { |
| | | QuestionSubject questionSubject = new QuestionSubject(); |
| | | questionSubject.setQuestionId(question.getId()); |
| | | questionSubject.setSubjectId(subject.getId()); |
| | | questionSubject.setDeleted(0); |
| | | return questionSubject; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | System.out.println(question); |
| | | } |
| | | // 批量保存题目-课目信息 |
| | | if (! CollectionUtils.isEmpty(questionSubjectsList)) { |
| | | questionSubjectService.saves(questionSubjectsList); |
| | | } |
| | | }; |
| | | EasyExcel.read(file.getInputStream(), QuestionImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | return RestResponse.ok(); |
| | | } |
| | | // @PostMapping("/question/import") |
| | | // @Transactional(rollbackFor = Exception.class) |
| | | // public RestResponse importQuestion(@RequestPart("file") MultipartFile file) throws IOException { |
| | | // Consumer<List<QuestionImportVO>> consumer = (data) -> { |
| | | // // 题目的课目信息 |
| | | // List<QuestionSubject> questionSubjectsList = new ArrayList<>(48); |
| | | // for (int i = 0; i < data.size(); i++) { |
| | | // // 题目实体 |
| | | // Question question = new Question(); |
| | | // // 读取的题目 |
| | | // QuestionImportVO excelQuestion = data.get(i); |
| | | // |
| | | // // 如果是第一条完整数据,那么继续往后读取选项内容 |
| | | // if (excelQuestion.master()) { |
| | | // // 该题的选项 |
| | | // List<QuestionItemObject> options = new ArrayList<>(4); |
| | | // // 选项内容 |
| | | // QuestionItemObject option = new QuestionItemObject(); |
| | | // option.setPrefix(excelQuestion.getOptionName()); |
| | | // option.setContent(excelQuestion.getOptionValue()); |
| | | // options.add(option); |
| | | // int next = 1; |
| | | // // 继续往后读选项 |
| | | // while (Boolean.TRUE) { |
| | | // // 判断是否是最后一条 |
| | | // if (next + i == data.size()) { |
| | | // break; |
| | | // } |
| | | // QuestionImportVO nextQuestion = data.get(next + i); |
| | | // if (nextQuestion.master()) { |
| | | // break; |
| | | // } |
| | | // QuestionItemObject nextOption = new QuestionItemObject(); |
| | | // nextOption.setPrefix(nextQuestion.getOptionName()); |
| | | // nextOption.setContent(nextQuestion.getOptionValue()); |
| | | // options.add(nextOption); |
| | | // next++; |
| | | // } |
| | | // i += next; |
| | | // |
| | | // // 保存题目内容 |
| | | // QuestionObject questionObject = new QuestionObject(); |
| | | // questionObject.setQuestionItemObjects(options); |
| | | // questionObject.setAnalyze(excelQuestion.getAnalyze()); |
| | | // questionObject.setTitleContent(excelQuestion.getTitle()); |
| | | // questionObject.setCorrect(excelQuestion.getCorrect()); |
| | | // |
| | | // TextContent textContent = new TextContent(); |
| | | // textContent.setContent(JSON.toJSONString(questionObject)); |
| | | // textContent.setCreateTime(new Date()); |
| | | // textContentService.insert(textContent); |
| | | // |
| | | // // 保存题目信息 |
| | | // // 设置题型 |
| | | // question.setQuestionType(QuestionTypeEnum.get(excelQuestion.getQuestionType())); |
| | | // // 答案(多选需要用,分割保存字符串到数据库) |
| | | // String[] corrects = excelQuestion.getCorrect().split(SPLIT); |
| | | // if (corrects.length > 1) { |
| | | // question.setCorrect(Arrays.asList(corrects).stream().collect(Collectors.joining(","))); |
| | | // } else { |
| | | // question.setCorrect(excelQuestion.getCorrect()); |
| | | // } |
| | | // |
| | | // // 难度 |
| | | // question.setDifficult(excelQuestion.getDifficult()); |
| | | // // 分数 |
| | | // question.setScore(ExamUtil.scoreFromVM(String.valueOf(excelQuestion.getScore()))); |
| | | // // 创建人 |
| | | // question.setCreateUser(1); |
| | | // question.setStatus(QuestionStatusEnum.OK.getCode()); |
| | | // question.setCreateTime(new Date()); |
| | | // question.setDeleted(Boolean.FALSE); |
| | | // question.setInfoTextContentId(textContent.getId()); |
| | | // questionService.insert(question); |
| | | // |
| | | // |
| | | // // 查出所有的课目 |
| | | // 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())) |
| | | // .collect(Collectors.toList()); |
| | | // if (CollectionUtils.isEmpty(targetSubject)) { |
| | | // // todo 记录这个错误 |
| | | // continue; |
| | | // } |
| | | // // 构建课目-题目信息 |
| | | // questionSubjectsList = targetSubject.stream().map(subject -> { |
| | | // QuestionSubject questionSubject = new QuestionSubject(); |
| | | // questionSubject.setQuestionId(question.getId()); |
| | | // questionSubject.setSubjectId(subject.getId()); |
| | | // questionSubject.setDeleted(0); |
| | | // return questionSubject; |
| | | // }).collect(Collectors.toList()); |
| | | // } |
| | | // System.out.println(question); |
| | | // } |
| | | // // 批量保存题目-课目信息 |
| | | // if (! CollectionUtils.isEmpty(questionSubjectsList)) { |
| | | // questionSubjectService.saves(questionSubjectsList); |
| | | // } |
| | | // }; |
| | | // EasyExcel.read(file.getInputStream(), QuestionImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | // return RestResponse.ok(); |
| | | // } |
| | | |
| | | @PostMapping("/import") |
| | | public RestResponse importUser(@RequestPart("file") MultipartFile file) throws Exception { |
| | | public RestResponse importQuestion(@RequestPart("file") MultipartFile file) throws Exception { |
| | | List<QuestionEditVO> questionEditVOS = ExcelUtils.readMultipartFile(file, QuestionEditVO.class) |
| | | .stream().map(e -> { |
| | | e.setQuestionType(QuestionTypeEnum.get(e.getType())); |
| | |
| | | |
| | | //组装题目 |
| | | for (QuestionEditRequestVM vm : list) { |
| | | // 判断题干是否重复,重复不添加 |
| | | Integer num = questionService.countQuestionByTitle(vm.getTitle()); |
| | | if (num > 0) { |
| | | continue; |
| | | } |
| | | |
| | | questionEditItemVM.setPrefix("A"); |
| | | questionEditItemVM.setContent(vm.getA()); |
| | | |
| | |
| | | questionEditItemVMS.add(questionEditItemVM); |
| | | } |
| | | vm.setItems(questionEditItemVMS); |
| | | List<String> str = Arrays.asList(vm.getCorrect().split(",")); |
| | | // 清空前后的逗号,以免后续作答判断为错误 |
| | | List<String> str = Arrays.asList(vm.getCorrect().replaceAll("^,+|,+$", "").split(SPLIT)); |
| | | |
| | | List<Subject> subjectList = subjectMapper.getNames(vm.getSbNames().split(",")); |
| | | List<Subject> subjectList = subjectMapper.getNames(vm.getSbNames().split(SPLIT)); |
| | | |
| | | Integer[] arr =subjectList.stream() |
| | | .map(Subject::getId).toArray(Integer[]::new); |