| | |
| | | return new FebsResponse().success().message("操作成功"); |
| | | } |
| | | |
| | | @Override |
| | | public void exportNewProductQuestion(AiProductQuestion dto, HttpServletResponse response) { |
| | | try { |
| | | List<ExcelSheetPO> res = new ArrayList<>(); |
| | | ExcelSheetPO orderSheet = new ExcelSheetPO(); |
| | | String title = "单选题目列表"; |
| | | orderSheet.setSheetName(title); |
| | | orderSheet.setTitle(title); |
| | | String[] header = {"题目", "答案", "正确答案", "解析"}; |
| | | orderSheet.setHeaders(header); |
| | | |
| | | QueryRequest request = new QueryRequest(); |
| | | request.setPageNum(1); |
| | | request.setPageSize(9999); |
| | | |
| | | AiProductQuestion aiProductQuestion = aiProductQuestionMapper.selectOne( |
| | | Wrappers.lambdaQuery(AiProductQuestion.class) |
| | | .orderByAsc(AiProductQuestion::getCreatedTime) |
| | | .last("limit 1") |
| | | ); |
| | | List<AiProductQuestionItem> aiProductQuestionItems = aiProductQuestionItemService.getListByQuery( |
| | | Wrappers.lambdaQuery(AiProductQuestionItem.class) |
| | | .eq(AiProductQuestionItem::getProductQuestionId, aiProductQuestion.getId()) |
| | | .orderByAsc(AiProductQuestionItem::getProductQuestionId) |
| | | ); |
| | | |
| | | List<List<Object>> list = new ArrayList<>(); |
| | | if (CollUtil.isNotEmpty(aiProductQuestionItems)) { |
| | | String label = "(示例,新增请删除整行内容)"; |
| | | for (AiProductQuestionItem item : aiProductQuestionItems) { |
| | | List<Object> temp = new ArrayList<>(); |
| | | temp.add(label + item.getTitle()); |
| | | temp.add(item.getAnswer()); |
| | | temp.add(item.getCorrectAnswer() == 1 ? "是" : "否"); |
| | | temp.add(item.getAnswerAnalysis()); |
| | | list.add(temp); |
| | | } |
| | | } |
| | | orderSheet.setDataList(list); |
| | | res.add(orderSheet); |
| | | |
| | | // 设置响应头 |
| | | response = ResponseHeadUtil.setExcelHead(response); |
| | | String fileName = title + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx"; |
| | | response.setHeader("Content-Disposition", |
| | | "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); |
| | | |
| | | // 写入 Excel 文件 |
| | | try (OutputStream os = response.getOutputStream()) { |
| | | ExcelUtil.createWorkbookAtOutStream(ExcelVersion.V2007, res, os, true); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("导出模板产品失败", e); |
| | | throw new RuntimeException("导出失败,请稍后重试"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse importNewProductQuestion(MultipartFile file, String categoryId, String companyId) { |
| | | try { |
| | | if (file.isEmpty()) { |
| | | return new FebsResponse().fail(); |
| | | } |
| | | |
| | | String fileName = file.getOriginalFilename(); |
| | | String dirPath = "/home/javaweb/webresource/ai/import/"; |
| | | |
| | | File saveFile = new File(new File(dirPath).getAbsolutePath() + File.separator + fileName); |
| | | if (!saveFile.exists()) { |
| | | if (!saveFile.getParentFile().exists()) { |
| | | saveFile.getParentFile().mkdirs(); |
| | | } |
| | | } |
| | | file.transferTo(saveFile); |
| | | |
| | | List<ExcelSheetPO> data = ExcelUtil.readExcel(saveFile, null, null); |
| | | if (CollUtil.isEmpty(data)) { |
| | | return new FebsResponse().fail(); |
| | | } |
| | | |
| | | List<List<Object>> dataList = data.get(0).getDataList(); |
| | | // String[] header = {"题目", "答案", "正确答案", "解析"}; |
| | | int titleIndex = -1; |
| | | int answerIndex = -1; |
| | | int correctAnswerIndex = -1; |
| | | int answerAnalysisIndex = -1; |
| | | |
| | | // 用于存储当前处理的题目信息 |
| | | AiProductQuestion currentQuestion = null; |
| | | List<AiProductQuestionItem> currentItems = new ArrayList<>(); |
| | | |
| | | for (int i = 2; i < dataList.size(); i++) { |
| | | List<Object> objects = dataList.get(i); |
| | | |
| | | String title = ""; |
| | | String answer = ""; |
| | | Integer correctAnswer = 0; |
| | | String answerAnalysis = ""; |
| | | |
| | | for (int j = 0; j < objects.size(); j++) { |
| | | Object obj = objects.get(j); |
| | | if ("题目".equals(obj)) { |
| | | titleIndex = j; |
| | | } |
| | | if ("答案".equals(obj)) { |
| | | answerIndex = j; |
| | | } |
| | | if ("正确答案".equals(obj)) { |
| | | correctAnswerIndex = j; |
| | | } |
| | | if ("解析".equals(obj)) { |
| | | answerAnalysisIndex = j; |
| | | } |
| | | if (j == titleIndex && obj != null) { |
| | | title = obj.toString(); |
| | | } |
| | | if (j == answerIndex && obj != null) { |
| | | answer = obj.toString(); |
| | | } |
| | | if (j == correctAnswerIndex && obj != null) { |
| | | correctAnswer = "是".equals(obj.toString()) ? 1 : 0; |
| | | } |
| | | if (j == answerAnalysisIndex && obj != null) { |
| | | answerAnalysis = obj.toString(); |
| | | } |
| | | } |
| | | |
| | | if (StrUtil.isNotBlank(title)) { |
| | | // 如果有新的题目,先保存之前的题目 |
| | | if (currentQuestion != null && CollUtil.isNotEmpty(currentItems)) { |
| | | saveQuestionWithItems(currentQuestion, currentItems); |
| | | } |
| | | |
| | | // 创建新的题目 |
| | | currentQuestion = new AiProductQuestion(); |
| | | currentQuestion.setId(UUID.getSimpleUUIDString()); |
| | | currentQuestion.setCompanyId("1"); // 假设默认公司ID |
| | | currentQuestion.setProductCategoryId(categoryId); |
| | | currentQuestion.setCompanyId(companyId); |
| | | currentQuestion.setTitle(title); |
| | | currentQuestion.setDifficulty(2); // 默认中等难度 |
| | | currentQuestion.setState(1); // 默认启用 |
| | | currentQuestion.setCreatedTime(new Date()); |
| | | currentItems = new ArrayList<>(); |
| | | } |
| | | |
| | | if (currentQuestion != null && StrUtil.isNotBlank(answer)) { |
| | | // 添加答案选项 |
| | | AiProductQuestionItem item = new AiProductQuestionItem(); |
| | | item.setId(UUID.getSimpleUUIDString()); |
| | | item.setProductQuestionId(currentQuestion.getId()); |
| | | item.setCompanyId(companyId); // 假设默认公司ID |
| | | item.setTitle(currentQuestion.getTitle()); |
| | | item.setAnswer(answer); |
| | | item.setCorrectAnswer(correctAnswer); |
| | | item.setAnswerAnalysis(answerAnalysis); |
| | | item.setCreatedTime(new Date()); |
| | | currentItems.add(item); |
| | | } |
| | | } |
| | | |
| | | // 保存最后一个题目 |
| | | if (currentQuestion != null && CollUtil.isNotEmpty(currentItems)) { |
| | | saveQuestionWithItems(currentQuestion, currentItems); |
| | | } |
| | | |
| | | return new FebsResponse().success(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("导入失败,请稍后重试"); |
| | | } |
| | | } |
| | | |
| | | private void saveQuestionWithItems(AiProductQuestion question, List<AiProductQuestionItem> items) { |
| | | this.save(question); |
| | | for (AiProductQuestionItem item : items) { |
| | | aiProductQuestionItemService.getBaseMapper().insert(item); |
| | | } |
| | | } |
| | | |
| | | } |