| | |
| | | ExcelSheetPO orderSheet = new ExcelSheetPO(); |
| | | String title = "单选题目列表"; |
| | | orderSheet.setSheetName(title); |
| | | orderSheet.setTitle(title); |
| | | |
| | | // 确保不设置标题行 |
| | | orderSheet.setTitle(title); // 明确设置为null |
| | | |
| | | 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<AiProductQuestionItem> aiProductQuestionItems = new ArrayList<>(); |
| | | if (aiProductQuestion != null) { |
| | | 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 = "(示例,新增请删除整行内容)"; |
| | | String label = "(示例, 新增请删除整行内容)"; |
| | | for (AiProductQuestionItem item : aiProductQuestionItems) { |
| | | List<Object> temp = new ArrayList<>(); |
| | | temp.add(label + item.getTitle()); |
| | |
| | | temp.add(item.getAnswerAnalysis()); |
| | | list.add(temp); |
| | | } |
| | | } else { |
| | | // 如果没有数据,添加一个示例行 |
| | | List<Object> exampleRow = new ArrayList<>(); |
| | | exampleRow.add("(示例, 新增请删除整行内容)我今年32岁, 最近感觉脸部开始有松弛现象, 尤其是下颌线不清晰了。朋友推荐我做MFU™pro美拉, 说是'无创提拉', 这个项目真的有效吗? 适合我这个年龄段吗?"); |
| | | exampleRow.add("这个项目否"); |
| | | exampleRow.add("是"); |
| | | exampleRow.add("根据文档内容, MFU™pro美拉推荐年龄为25+, 适用于轻中度松弛人群, 具有即刻紧致与长期提拉效果, 适合早期抗衰需求"); |
| | | list.add(exampleRow); |
| | | } |
| | | |
| | | orderSheet.setDataList(list); |
| | | res.add(orderSheet); |
| | | |
| | |
| | | public FebsResponse importNewProductQuestion(MultipartFile file, String categoryId, String companyId) { |
| | | try { |
| | | if (file.isEmpty()) { |
| | | return new FebsResponse().fail(); |
| | | return new FebsResponse().fail().message("文件不能为空"); |
| | | } |
| | | |
| | | String fileName = file.getOriginalFilename(); |
| | |
| | | |
| | | List<ExcelSheetPO> data = ExcelUtil.readExcel(saveFile, null, null); |
| | | if (CollUtil.isEmpty(data)) { |
| | | return new FebsResponse().fail(); |
| | | return new FebsResponse().fail().message("Excel文件内容为空"); |
| | | } |
| | | |
| | | List<List<Object>> dataList = data.get(0).getDataList(); |
| | | // String[] header = {"题目", "答案", "正确答案", "解析"}; |
| | | if (CollUtil.isEmpty(dataList) || dataList.size() < 3) { |
| | | return new FebsResponse().fail().message("Excel数据行数不足"); |
| | | } |
| | | |
| | | // 第一行是文件名称,第二行是标题栏,从第三行开始才是数据 |
| | | // 从第二行获取标题栏,确定列索引 |
| | | List<Object> headerRow = dataList.get(1); // 第二行是标题栏 |
| | | int titleIndex = -1; |
| | | int answerIndex = -1; |
| | | int correctAnswerIndex = -1; |
| | | int answerAnalysisIndex = -1; |
| | | |
| | | // 用于存储当前处理的题目信息 |
| | | AiProductQuestion currentQuestion = null; |
| | | List<AiProductQuestionItem> currentItems = new ArrayList<>(); |
| | | // 解析标题栏,确定各字段的列索引 |
| | | for (int j = 0; j < headerRow.size(); j++) { |
| | | Object headerCell = headerRow.get(j); |
| | | if (headerCell != null) { |
| | | String headerText = headerCell.toString().trim(); |
| | | switch (headerText) { |
| | | case "题目": |
| | | titleIndex = j; |
| | | break; |
| | | case "答案": |
| | | answerIndex = j; |
| | | break; |
| | | case "正确答案": |
| | | correctAnswerIndex = j; |
| | | break; |
| | | case "解析": |
| | | answerAnalysisIndex = j; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 验证必要的列是否存在 |
| | | if (titleIndex == -1 || answerIndex == -1) { |
| | | return new FebsResponse().fail().message("Excel缺少必要的列(题目、答案)"); |
| | | } |
| | | |
| | | // 用于存储题目和选项 |
| | | Map<String, AiProductQuestion> questionMap = new HashMap<>(); |
| | | Map<String, List<AiProductQuestionItem>> itemsMap = new HashMap<>(); |
| | | |
| | | // 从第三行开始处理数据(索引2) |
| | | for (int i = 2; i < dataList.size(); i++) { |
| | | List<Object> objects = dataList.get(i); |
| | | List<Object> rowData = dataList.get(i); |
| | | |
| | | // 跳过空行 |
| | | if (CollUtil.isEmpty(rowData)) { |
| | | continue; |
| | | } |
| | | |
| | | 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 (titleIndex < rowData.size() && rowData.get(titleIndex) != null) { |
| | | title = rowData.get(titleIndex).toString().trim(); |
| | | } |
| | | |
| | | 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 (answerIndex < rowData.size() && rowData.get(answerIndex) != null) { |
| | | answer = rowData.get(answerIndex).toString().trim(); |
| | | } |
| | | |
| | | if (currentQuestion != null && StrUtil.isNotBlank(answer)) { |
| | | // 添加答案选项 |
| | | if (correctAnswerIndex != -1 && correctAnswerIndex < rowData.size() && rowData.get(correctAnswerIndex) != null) { |
| | | String correctAnswerStr = rowData.get(correctAnswerIndex).toString().trim(); |
| | | // 支持多种表示正确的方式 |
| | | correctAnswer = ("是".equals(correctAnswerStr) || "1".equals(correctAnswerStr) |
| | | || "正确".equals(correctAnswerStr) || "对".equals(correctAnswerStr)) ? 1 : 0; |
| | | } |
| | | |
| | | if (answerAnalysisIndex != -1 && answerAnalysisIndex < rowData.size() && rowData.get(answerAnalysisIndex) != null) { |
| | | answerAnalysis = rowData.get(answerAnalysisIndex).toString().trim(); |
| | | } |
| | | |
| | | // 如果题目为空,跳过该行 |
| | | if (StrUtil.isBlank(title)) { |
| | | continue; |
| | | } |
| | | |
| | | // 使用题目内容作为key,确保同一个题目只创建一次 |
| | | String questionKey = title; |
| | | |
| | | // 如果这个题目还没有创建过,创建题目 |
| | | if (!questionMap.containsKey(questionKey)) { |
| | | AiProductQuestion question = new AiProductQuestion(); |
| | | question.setId(UUID.getSimpleUUIDString()); |
| | | question.setCompanyId(companyId); |
| | | question.setProductCategoryId(categoryId); |
| | | question.setTitle(title); |
| | | question.setDifficulty(2); // 默认中等难度 |
| | | question.setState(0); // 默认启用 |
| | | question.setCreatedTime(new Date()); |
| | | |
| | | questionMap.put(questionKey, question); |
| | | itemsMap.put(questionKey, new ArrayList<>()); |
| | | } |
| | | |
| | | // 添加选项(只要答案不为空) |
| | | if (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.setProductQuestionId(questionMap.get(questionKey).getId()); |
| | | item.setCompanyId(companyId); |
| | | item.setTitle(title); // 选项内容 |
| | | item.setAnswer(answer); // 选项内容 |
| | | item.setCorrectAnswer(correctAnswer); |
| | | item.setAnswerAnalysis(answerAnalysis); |
| | | item.setCreatedTime(new Date()); |
| | | currentItems.add(item); |
| | | |
| | | itemsMap.get(questionKey).add(item); |
| | | } |
| | | } |
| | | |
| | | // 保存最后一个题目 |
| | | if (currentQuestion != null && CollUtil.isNotEmpty(currentItems)) { |
| | | saveQuestionWithItems(currentQuestion, currentItems); |
| | | // 保存所有题目和选项 |
| | | int successCount = 0; |
| | | for (Map.Entry<String, AiProductQuestion> entry : questionMap.entrySet()) { |
| | | String questionKey = entry.getKey(); |
| | | AiProductQuestion question = entry.getValue(); |
| | | List<AiProductQuestionItem> items = itemsMap.get(questionKey); |
| | | |
| | | if (CollUtil.isNotEmpty(items)) { |
| | | saveQuestionWithItems(question, items); |
| | | successCount++; |
| | | } |
| | | } |
| | | |
| | | return new FebsResponse().success(); |
| | | return new FebsResponse().success().message(String.format("成功导入%d个题目,共%d个选项", |
| | | successCount, itemsMap.values().stream().mapToInt(List::size).sum())); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.error("导入Excel文件失败", e); |
| | | throw new RuntimeException("导入失败,请稍后重试"); |
| | | } |
| | | } |
| | | |
| | | private void saveQuestionWithItems(AiProductQuestion question, List<AiProductQuestionItem> items) { |
| | | // 保存题目(新增一次) |
| | | this.save(question); |
| | | |
| | | // 保存选项(有几条数据就增加几条) |
| | | for (AiProductQuestionItem item : items) { |
| | | aiProductQuestionItemService.getBaseMapper().insert(item); |
| | | aiProductQuestionItemService.save(item); |
| | | } |
| | | } |
| | | |