| | |
| | | import cc.mrbird.febs.ai.mapper.AiProductQuestionJobMapper; |
| | | import cc.mrbird.febs.ai.mapper.AiProductQuestionMapper; |
| | | import cc.mrbird.febs.ai.req.AiProductQuestionAiDto; |
| | | import cc.mrbird.febs.ai.service.AiProductCategoryService; |
| | | import cc.mrbird.febs.ai.service.AiProductQuestionItemService; |
| | | import cc.mrbird.febs.ai.service.AiProductQuestionService; |
| | | import cc.mrbird.febs.ai.service.AiService; |
| | |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * AI产品题目 Service实现类 |
| | |
| | | private final AiProductQuestionMapper aiProductQuestionMapper; |
| | | private final AiProductQuestionJobMapper aiProductQuestionJobMapper; |
| | | private final AiProductQuestionItemService aiProductQuestionItemService; |
| | | private final AiProductCategoryService aiProductCategoryService; |
| | | private final AiService aiService; |
| | | private final AgentProducer agentProducer; |
| | | |
| | |
| | | query.ne(AiProductQuestion::getState, 2); |
| | | query.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | Page<AiProductQuestion> pages = aiProductQuestionMapper.selectPage(page, query); |
| | | List<AiProductQuestion> records = pages.getRecords(); |
| | | if (CollUtil.isNotEmpty( records)){ |
| | | //stream流操作records,获取全部的productCategoryId的set集合 |
| | | Set<String> productCategoryIds = records.stream().map(AiProductQuestion::getProductCategoryId).collect(Collectors.toSet()); |
| | | if(CollUtil.isNotEmpty( productCategoryIds)){ |
| | | Map<String,AiProductCategory> map = aiProductCategoryService.selectMapByIds(productCategoryIds); |
| | | for (AiProductQuestion record : records){ |
| | | AiProductCategory orDefault = map.getOrDefault(record.getProductCategoryId(), null); |
| | | if(ObjectUtil.isNotNull(orDefault)){ |
| | | record.setProductCategoryName(orDefault.getName()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return pages; |
| | | } |
| | | |
| | |
| | | .eq(AiProductQuestionJob::getId, id)); |
| | | return; |
| | | } |
| | | // int i = questionCnt - questionDoneCnt; |
| | | // if (i >= 5){ |
| | | // i = 5; |
| | | // } |
| | | int i = questionCnt - questionDoneCnt; |
| | | if (i >= 5){ |
| | | i = 5; |
| | | } |
| | | AiProductQuestionAiDto dto = new AiProductQuestionAiDto(); |
| | | String jsonFormat = "{\"question_list\":[{\"title\": \"消费者对透明质酸的主要担忧是什么?\",\"answer_list\":[{\"answer\": \"消费者担心透明质酸维持时间短,效果不明显。\",\"type\": 1,\"analysis\": \"\"},{\"answer\": \"消费者担心透明质酸注射后可能出现移位、凹陷、馒化、僵硬以及炎症反应。\",\"type\": 1,\"analysis\": \"\"},{\"answer\": \"消费者的主要担忧包括效果不明显、维持时间短、注射后出现移位、凹陷、馒化、僵硬以及炎症反应。他们希望既达到理想效果,又避免这些副作用。\",\"type\": 2,\"analysis\": \"标准答案涵盖了消费者对透明质酸的所有主要担忧,既包括效果问题,也涵盖副作用风险,全面反映消费者心理需求。\"}]}]} "; |
| | | dto.setJsonFormat(jsonFormat); |
| | |
| | | aiProductQuestion.setProductCategoryId(productCategoryId); |
| | | aiProductQuestion.setTitle(title); |
| | | aiProductQuestion.setDifficulty(difficulty); |
| | | aiProductQuestion.setJobId(id); |
| | | aiProductQuestion.setCreatedTime(createdTime); |
| | | this.save(aiProductQuestion); |
| | | JSONArray answerList = questionObj.getJSONArray("answer_list"); |
| | |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse updateLabel(String ids, String label) { |
| | | if (StrUtil.isEmpty(ids)) { |
| | | throw new FebsException("参数错误"); |
| | | } |
| | | List<String> idList = StrUtil.split(ids, ','); |
| | | this.update(null, |
| | | Wrappers.lambdaUpdate(AiProductQuestion.class) |
| | | .set(AiProductQuestion::getLabel, label) |
| | | .set(AiProductQuestion::getUpdatedTime, new Date()) |
| | | .in(AiProductQuestion::getId, idList)); |
| | | 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); // 明确设置为null |
| | | |
| | | String[] header = {"题目", "答案", "正确答案", "解析"}; |
| | | orderSheet.setHeaders(header); |
| | | |
| | | // 获取示例数据 |
| | | AiProductQuestion aiProductQuestion = aiProductQuestionMapper.selectOne( |
| | | Wrappers.lambdaQuery(AiProductQuestion.class) |
| | | .orderByAsc(AiProductQuestion::getCreatedTime) |
| | | .last("limit 1") |
| | | ); |
| | | |
| | | 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 = "(示例, 新增请删除整行内容)"; |
| | | 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); |
| | | } |
| | | } 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); |
| | | |
| | | // 设置响应头 |
| | | 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, Integer difficulty) { |
| | | try { |
| | | if (file.isEmpty()) { |
| | | return new FebsResponse().fail().message("文件不能为空"); |
| | | } |
| | | |
| | | 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().message("Excel文件内容为空"); |
| | | } |
| | | |
| | | List<List<Object>> dataList = data.get(0).getDataList(); |
| | | 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; |
| | | |
| | | // 解析标题栏,确定各字段的列索引 |
| | | 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> rowData = dataList.get(i); |
| | | |
| | | // 跳过空行 |
| | | if (CollUtil.isEmpty(rowData)) { |
| | | continue; |
| | | } |
| | | |
| | | String title = ""; |
| | | String answer = ""; |
| | | Integer correctAnswer = 0; |
| | | String answerAnalysis = ""; |
| | | |
| | | // 提取各字段的值 |
| | | if (titleIndex < rowData.size() && rowData.get(titleIndex) != null) { |
| | | title = rowData.get(titleIndex).toString().trim(); |
| | | } |
| | | |
| | | if (answerIndex < rowData.size() && rowData.get(answerIndex) != null) { |
| | | answer = rowData.get(answerIndex).toString().trim(); |
| | | } |
| | | |
| | | 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(difficulty); // 默认中等难度 |
| | | 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(questionMap.get(questionKey).getId()); |
| | | item.setCompanyId(companyId); |
| | | item.setTitle(title); // 选项内容 |
| | | item.setAnswer(answer); // 选项内容 |
| | | item.setCorrectAnswer(correctAnswer); |
| | | item.setAnswerAnalysis(answerAnalysis); |
| | | item.setCreatedTime(new Date()); |
| | | |
| | | itemsMap.get(questionKey).add(item); |
| | | } |
| | | } |
| | | |
| | | // 保存所有题目和选项 |
| | | 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().message(String.format("成功导入%d个题目,共%d个选项", |
| | | successCount, itemsMap.values().stream().mapToInt(List::size).sum())); |
| | | } catch (IOException e) { |
| | | log.error("导入Excel文件失败", e); |
| | | throw new RuntimeException("导入失败,请稍后重试"); |
| | | } |
| | | } |
| | | |
| | | private void saveQuestionWithItems(AiProductQuestion question, List<AiProductQuestionItem> items) { |
| | | // 保存题目(新增一次) |
| | | this.save(question); |
| | | |
| | | // 保存选项(有几条数据就增加几条) |
| | | for (AiProductQuestionItem item : items) { |
| | | aiProductQuestionItemService.save(item); |
| | | } |
| | | } |
| | | |
| | | } |