| | |
| | | import org.springframework.core.ParameterizedTypeReference; |
| | | import org.springframework.dao.DuplicateKeyException; |
| | | import org.springframework.http.*; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.reactive.function.client.ExchangeStrategies; |
| | | import org.springframework.web.reactive.function.client.WebClient; |
| | | |
| | |
| | | } |
| | | } |
| | | //更新提问词和引用数据 |
| | | // private Mono<Void> updateQuestionAndReference(TaskResultResponse result) { |
| | | // return Mono.fromRunnable(() -> { |
| | | // try { |
| | | // //查看每个账号信息的status是否正常 |
| | | // |
| | | // // 1. 根据KeywordTask更新关键词状态 |
| | | // // 查询关键词ID |
| | | // LambdaQueryWrapper<KeywordTask> keywordTaskWrapper = new LambdaQueryWrapper<>(); |
| | | // keywordTaskWrapper.eq(KeywordTask::getTask_id, result.getTask_id()); |
| | | // KeywordTask keywordTask = keywordTaskService.getOne(keywordTaskWrapper); |
| | | // keywordTask.setStatus("completed"); |
| | | // keywordTaskService.updateById(keywordTask); |
| | | // Keyword keyword = keywordService.getById(keywordTask.getKeyword_id()); |
| | | // |
| | | // if (keyword == null) { |
| | | // System.out.println("未找到关联的关键词,task_id: " + result.getTask_id()); |
| | | // //报错 |
| | | // throw new Exception("未找到关联的关键词,task_id: " + result.getTask_id()); |
| | | // |
| | | // } |
| | | // LambdaQueryWrapper<KeywordTask> keywordTaskWrapper2 = new LambdaQueryWrapper<>(); |
| | | // keywordTaskWrapper2.eq(KeywordTask::getKeyword_id, keyword.getKeyword_id()); |
| | | // List<KeywordTask> keywordTasks = keywordTaskService.list(keywordTaskWrapper2); |
| | | // |
| | | // //如果全部为completed 或者错误、取消、任务不存在 关键词也为completed ,如果关联关系没有任务id,或者状态为running ,关键词为submitted, |
| | | // if (keywordTasks.stream().allMatch(task -> "completed".equals(task.getStatus()) || "false".equals(task.getStatus()) || "cancelled".equals(task.getStatus()) ||"canceled".equals(task.getStatus()) || "nonentity".equals(task.getStatus())) ) { |
| | | // keyword.setStatus("completed"); |
| | | // keywordService.updateById(keyword); |
| | | // |
| | | // } |
| | | // |
| | | // String orderId = keyword.getOrder_id(); |
| | | // if (orderId == null || orderId.isEmpty()) { |
| | | // System.out.println("关键词[" + keyword.getKeyword_id() + "]未关联订单,跳过订单状态更新"); |
| | | // return; |
| | | // } |
| | | // |
| | | // // 2.更新订单状态为待处理 查询该订单下的所有关键词,更新订单状态(有取消) |
| | | // LambdaQueryWrapper<Keyword> orderKeywordsWrapper = new LambdaQueryWrapper<>(); |
| | | // orderKeywordsWrapper.eq(Keyword::getOrder_id, orderId); |
| | | // List<Keyword> orderKeywords = keywordService.list(orderKeywordsWrapper); |
| | | // |
| | | // if (orderKeywords.isEmpty()) { |
| | | // System.out.println("订单[" + orderId + "]下无关键词,跳过状态更新"); |
| | | // return; |
| | | // } |
| | | // boolean allValid2 = orderKeywords.stream() |
| | | // .allMatch(k -> "completed".equals(k.getStatus()) || "false".equals(k.getStatus()) || "cancelled".equals(k.getStatus())); |
| | | // if (allValid2) { |
| | | // Orders orders = orderService.getById(orderId); |
| | | // if (orders != null) { |
| | | // orders.setStatus(1); // 假设Orders有Integer类型的status字段 |
| | | // orderService.updateById(orders); |
| | | // System.out.println("订单[" + orderId + "]所有关键词采集完成或者取消,已更新状态为1"); |
| | | // } else { |
| | | // System.out.println("未找到订单[" + orderId + "],无法更新状态"); |
| | | // } |
| | | // } |
| | | // // 3.更新订单状态为完成 检查所有关键词的状态是否均为 completed 或 false |
| | | // boolean allValid = orderKeywords.stream() |
| | | // .allMatch(k -> "completed".equals(k.getStatus()) || "false".equals(k.getStatus())); |
| | | // |
| | | // // 4. 若所有关键词状态均有效,更新订单状态为3 |
| | | // if (allValid) { |
| | | // Orders orders = orderService.getById(orderId); |
| | | // if (orders != null) { |
| | | // orders.setStatus(3); // 假设Orders有Integer类型的status字段 |
| | | // orderService.updateById(orders); |
| | | // System.out.println("订单[" + orderId + "]所有关键词状态符合条件,已更新状态为3"); |
| | | // } else { |
| | | // System.out.println("未找到订单[" + orderId + "],无法更新状态"); |
| | | // } |
| | | // } |
| | | // |
| | | // |
| | | // Orders orders = orderService.getById(keyword.getOrder_id()); |
| | | // |
| | | // // 2. 批量查询所有问题 |
| | | // LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>(); |
| | | // queryWrapper.eq(Question::getKeyword_id, keyword.getKeyword_id()); |
| | | // List<Question> questions = questionService.list(queryWrapper); |
| | | // |
| | | // // 构建问题映射表,用于快速查找 |
| | | // Map<String, Question> questionMap = questions.stream() |
| | | // .collect(Collectors.toMap(Question::getQuestion, q -> q)); |
| | | // |
| | | // // 3. 收集所有需要更新的问题和引用 |
| | | // List<Question> questionsToUpdate = new ArrayList<>(); |
| | | // List<Reference> allReferences = new ArrayList<>(); |
| | | // List<Reference> resultList = new ArrayList<>(); |
| | | // |
| | | // // 遍历账号 |
| | | // for (UserResult userResult : result.getResults()) { |
| | | // //更新账号状态 |
| | | // if ( "failed".equals(userResult.getStatus())){ |
| | | // if (userResult.getError().contains("登录失败")){ |
| | | // LambdaUpdateWrapper<User> userWrapper = new LambdaUpdateWrapper<>(); |
| | | // userWrapper.eq(User::getUser_email, userResult.getUser_email()); |
| | | // userWrapper.set(User::getStatus, "无法登录"); |
| | | // userService.update(userWrapper); |
| | | // //更新所有提问词的状态 |
| | | // questionService.update(new LambdaUpdateWrapper<Question>().eq(Question::getKeyword_id, keyword.getKeyword_id()) |
| | | // .set(Question::getStatus, "failed") |
| | | // .set(Question::getError, "账户登录失败")); |
| | | // |
| | | // }else if (userResult.getError().contains("信息错误")){ |
| | | // LambdaUpdateWrapper<User> userWrapper = new LambdaUpdateWrapper<>(); |
| | | // userWrapper.eq(User::getUser_email, userResult.getUser_email()); |
| | | // userWrapper.set(User::getStatus, "信息错误"); |
| | | // userService.update(userWrapper); |
| | | // } |
| | | // } |
| | | // for (QuestionResult questionResult : userResult.getQuestions_results()) { |
| | | // try { |
| | | // Question question = questionMap.get(questionResult.getQuestion()); |
| | | // if (question != null) { |
| | | // |
| | | // |
| | | // //保存问题结果 |
| | | // QuestionResultList questionResultList = new QuestionResultList(); |
| | | // questionResultList.setKeyword_id(keyword.getKeyword_id()); |
| | | // questionResultList.setQuestion(questionResult.getQuestion()); |
| | | // questionResultList.setResponse(questionResult.getResponse()); |
| | | // questionResultList.setStatus(questionResult.getStatus()); |
| | | // questionResultList.setExtracted_count(questionResult.getExtracted_count()); |
| | | // questionResultList.setKeyword_task_id(result.getTask_id()); |
| | | // questionResultList.setError(questionResult.getError()); |
| | | // questionResultList.setNum(keyword.getNum()); |
| | | // if (questionResult.getTimestamp() != null) { |
| | | // DateTimeFormatter formatter = DateTimeFormatter |
| | | // .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS"); |
| | | // questionResultList.setTimestamp( |
| | | // LocalDateTime.parse(questionResult.getTimestamp(), formatter)); |
| | | // } |
| | | // // 保存问题结果列表(新增保存逻辑) |
| | | // questionResultService.save(questionResultList); |
| | | // // 查询当前轮次下该提问词的所有结果 |
| | | // List<QuestionResultList> allResults = questionResultService.list( |
| | | // new LambdaQueryWrapper<QuestionResultList>() |
| | | // .eq(QuestionResultList::getKeyword_id, keyword.getKeyword_id()) |
| | | // .eq(QuestionResultList::getQuestion, question.getQuestion()) |
| | | // .eq(QuestionResultList::getNum, keyword.getNum()) |
| | | // ); |
| | | // |
| | | // // 判断最终状态 |
| | | // String finalStatus = determineFinalStatus(allResults); |
| | | // if ("success".equals(finalStatus)){ |
| | | // question.setStatus("success"); |
| | | // question.setError(""); |
| | | // }else if ("no_results".equals(finalStatus)){ |
| | | // question.setStatus("success"); |
| | | // question.setError("采集结果无引用数据"); |
| | | // }else if ("busyness".equals(finalStatus)){ |
| | | // question.setStatus("failed"); |
| | | // question.setError("DeepSeek繁忙,请稍后尝试"); |
| | | // } |
| | | // |
| | | // // 更新问题对象 |
| | | // question.setResponse(questionResult.getResponse()); |
| | | // question.setExtracted_count(questionResult.getExtracted_count()); |
| | | //// question.setError(questionResult.getError()); |
| | | // question.setKeyword_id(keyword.getKeyword_id()); |
| | | // |
| | | // if (questionResult.getTimestamp() != null) { |
| | | // DateTimeFormatter formatter = DateTimeFormatter |
| | | // .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS"); |
| | | // question.setTimestamp( |
| | | // LocalDateTime.parse(questionResult.getTimestamp(), formatter)); |
| | | // } |
| | | // |
| | | // questionsToUpdate.add(question); |
| | | // // 初始化引用列表(避免null) |
| | | // List<Reference> references = new ArrayList<>(); |
| | | // List<TaskResultResponse.Reference> originalReferences = questionResult.getReferences(); |
| | | // if (originalReferences == null) { |
| | | // originalReferences = Collections.emptyList(); |
| | | // } |
| | | // |
| | | // // 遍历原始引用列表,转换为Reference对象 |
| | | // for (TaskResultResponse.Reference ref : originalReferences) { |
| | | // Reference reference = new Reference(); |
| | | // // 设置基本字段 |
| | | // reference.setQuestion_id(question.getQuestion_id()); |
| | | // reference.setTitle(ref.getTitle()); |
| | | // reference.setUrl(ref.getUrl()); |
| | | // reference.setDomain(ref.getDomain()); |
| | | // reference.setNum(keyword.getNum()); |
| | | // reference.setTask_id(result.getTask_id()); |
| | | // reference.setKeyword_id(keyword.getKeyword_id()); |
| | | // if (null!=ref.getPublish_time()) { |
| | | // reference.setCreate_time(ref.getPublish_time().atStartOfDay()); |
| | | // } |
| | | // |
| | | // // 关键:使用优化后的方法获取平台,避免重复创建 |
| | | // Platform platform = getOrCreatePlatform(ref.getDomain(),ref.getPlatform_name()); |
| | | // reference.setPlatform_id(platform.getPlatform_id()); |
| | | // reference.setType_id(platform.getType_id()); // 直接从平台获取类型ID,更可靠 |
| | | // // 添加到结果列表 |
| | | // references.add(reference); |
| | | // } |
| | | // // 添加到总引用列表 |
| | | // if (!references.isEmpty()) { |
| | | // allReferences.addAll(references); |
| | | // } |
| | | // |
| | | // //取数据库中当前关键词的当前轮次的当前问题id结果拿出来 |
| | | // List<Reference> dbList = referenceService.list(new LambdaQueryWrapper<Reference>().eq(Reference::getKeyword_id, keyword.getKeyword_id()) |
| | | // .eq(Reference::getNum, keyword.getNum()) |
| | | // .eq(Reference::getQuestion_id, question.getQuestion_id()) |
| | | // ); |
| | | // |
| | | // // 1. 合并两个列表 |
| | | // List<Reference> combinedList = new ArrayList<>(); |
| | | // combinedList.addAll(allReferences); |
| | | // combinedList.addAll(dbList); |
| | | // |
| | | // // 2. 创建复合键的Map,用于统计完全匹配的记录 |
| | | // Map<String, List<Reference>> compositeKeyMap = combinedList.stream() |
| | | // .collect(Collectors.groupingBy( |
| | | // ref -> ref.getTitle() + "|" + ref.getUrl() + "|" + ref.getDomain() |
| | | // )); |
| | | // |
| | | // // 3. 处理每组重复记录 |
| | | // compositeKeyMap.forEach((key, refGroup) -> { |
| | | // // 3.1 找出组内有ID的记录(优先从dbList中获取) |
| | | // Optional<Reference> existingRecord = refGroup.stream() |
| | | // .filter(ref -> ref.getReference_id() != null) |
| | | // .findFirst(); |
| | | // |
| | | // // 3.2 统计该组的重复次数(总数-1) |
| | | // int repetitionCount = refGroup.size() - 1; |
| | | // |
| | | // // 3.3 决定最终保留的记录 |
| | | // Reference recordToSave = new Reference(); |
| | | // if (existingRecord.isPresent()) { |
| | | // // 使用已有ID的记录并更新重复次数 |
| | | // recordToSave = existingRecord.get(); |
| | | // recordToSave.setRepetition_num( |
| | | // (recordToSave.getRepetition_num() == null ? 1 : recordToSave.getRepetition_num()) |
| | | // + repetitionCount |
| | | // ); |
| | | // } else { |
| | | // // 没有ID记录则取第一条并设置重复次数 |
| | | // recordToSave = refGroup.get(0); |
| | | // recordToSave.setRepetition_num(1+repetitionCount); |
| | | // } |
| | | // |
| | | // resultList.add(recordToSave); |
| | | // }); |
| | | // referenceService.saveOrUpdateBatch(resultList); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // log.error(e.getMessage(), e); |
| | | // System.out.println("处理问题结果失败: " + e.getMessage()); |
| | | // } |
| | | // } |
| | | // |
| | | // |
| | | // } |
| | | // |
| | | // // 4. 批量更新问题 |
| | | // System.out.println(questionsToUpdate); |
| | | // if (!questionsToUpdate.isEmpty()) { |
| | | // questionService.updateBatchById(questionsToUpdate); |
| | | // System.out.println("成功批量更新 " + questionsToUpdate.size() + " 个问题"); |
| | | // } |
| | | // |
| | | // } catch (Exception e) { |
| | | // log.error("更新问题和引用数据失败: " ,e.getMessage(), e); |
| | | // throw new RuntimeException("更新问题和引用数据失败", e); |
| | | // } |
| | | // }); |
| | | // } |
| | | // // 根据所有批次的结果判断最终状态 |
| | | // private String determineFinalStatus(List<QuestionResultList> results) { |
| | | // if (results.isEmpty()) { |
| | | // return "no_results"; // 无结果 |
| | | // } |
| | | // |
| | | // // 统计关键指标 |
| | | // int totalCount = results.size(); |
| | | // int emptyResponseCount = 0; |
| | | // int systemBusyCount = 0; |
| | | // |
| | | // for (QuestionResultList result : results) { |
| | | // // 判断回答是否为空 |
| | | // if (result.getExtracted_count() == 0 ) { |
| | | // emptyResponseCount++; |
| | | // } |
| | | // |
| | | // // 判断是否为系统繁忙 |
| | | // if ("success".equals(result.getStatus()) && (result.getResponse().isEmpty()|| result.getResponse().contains("WebDriver连接中断") || result.getResponse().contains("响应超时"))) { |
| | | // systemBusyCount++; |
| | | // } |
| | | // |
| | | // } |
| | | // |
| | | // // 全返回系统繁忙 |
| | | // if (systemBusyCount == totalCount) { |
| | | // return "busyness"; |
| | | // } |
| | | // // 全返回信息为空 |
| | | // if (emptyResponseCount == totalCount) { |
| | | // return "no_results"; |
| | | // } |
| | | // |
| | | // |
| | | // // 系统繁忙比例超过阈值(可配置,这里设为70%) |
| | | //// double busyRate = (double) systemBusyCount / totalCount; |
| | | //// if (busyRate >= 0.7) { |
| | | //// return "系统繁忙,请稍后尝试"; |
| | | //// } |
| | | // |
| | | // // 其他情况返回成功 |
| | | // return "success"; |
| | | // } |
| | | |
| | | private Mono<Void> updateQuestionAndReference(TaskResultResponse result) { |
| | | return Mono.fromRunnable(() -> { |
| | | try { |
| | | //查看每个账号信息的status是否正常 |
| | | |
| | | // 1. 根据KeywordTask更新关键词状态 |
| | | // 查询关键词ID |
| | | LambdaQueryWrapper<KeywordTask> keywordTaskWrapper = new LambdaQueryWrapper<>(); |
| | | keywordTaskWrapper.eq(KeywordTask::getTask_id, result.getTask_id()); |
| | | KeywordTask keywordTask = keywordTaskService.getOne(keywordTaskWrapper); |
| | | keywordTask.setStatus("completed"); |
| | | keywordTaskService.updateById(keywordTask); |
| | | Keyword keyword = keywordService.getById(keywordTask.getKeyword_id()); |
| | | |
| | | if (keyword == null) { |
| | | System.out.println("未找到关联的关键词,task_id: " + result.getTask_id()); |
| | | //报错 |
| | | throw new Exception("未找到关联的关键词,task_id: " + result.getTask_id()); |
| | | |
| | | } |
| | | LambdaQueryWrapper<KeywordTask> keywordTaskWrapper2 = new LambdaQueryWrapper<>(); |
| | | keywordTaskWrapper2.eq(KeywordTask::getKeyword_id, keyword.getKeyword_id()); |
| | | List<KeywordTask> keywordTasks = keywordTaskService.list(keywordTaskWrapper2); |
| | | |
| | | //如果全部为completed 或者错误、取消、任务不存在 关键词也为completed ,如果关联关系没有任务id,或者状态为running ,关键词为submitted, |
| | | if (keywordTasks.stream().allMatch(task -> "completed".equals(task.getStatus()) || "false".equals(task.getStatus()) || "cancelled".equals(task.getStatus()) ||"canceled".equals(task.getStatus()) || "nonentity".equals(task.getStatus())) ) { |
| | | keyword.setStatus("completed"); |
| | | keywordService.updateById(keyword); |
| | | |
| | | } |
| | | |
| | | // 更新关键词状态 |
| | | String orderId = keyword.getOrder_id(); |
| | | if (orderId == null || orderId.isEmpty()) { |
| | | System.out.println("关键词[" + keyword.getKeyword_id() + "]未关联订单,跳过订单状态更新"); |
| | | return; |
| | | } |
| | | |
| | | // 2. 查询该订单下的所有关键词 |
| | | LambdaQueryWrapper<Keyword> orderKeywordsWrapper = new LambdaQueryWrapper<>(); |
| | | orderKeywordsWrapper.eq(Keyword::getOrder_id, orderId); |
| | | List<Keyword> orderKeywords = keywordService.list(orderKeywordsWrapper); |
| | | |
| | | if (orderKeywords.isEmpty()) { |
| | | System.out.println("订单[" + orderId + "]下无关键词,跳过状态更新"); |
| | | return; |
| | | } |
| | | boolean allValid2 = orderKeywords.stream() |
| | | .allMatch(k -> "completed".equals(k.getStatus()) || "false".equals(k.getStatus()) || "cancelled".equals(k.getStatus())); |
| | | if (allValid2) { |
| | | Orders orders = orderService.getById(orderId); |
| | | if (orders != null) { |
| | | orders.setStatus(1); // 假设Orders有Integer类型的status字段 |
| | | orderService.updateById(orders); |
| | | System.out.println("订单[" + orderId + "]所有关键词采集完成或者取消,已更新状态为1"); |
| | | } else { |
| | | System.out.println("未找到订单[" + orderId + "],无法更新状态"); |
| | | } |
| | | } |
| | | // 3. 检查所有关键词的状态是否均为 completed 或 false |
| | | boolean allValid = orderKeywords.stream() |
| | | .allMatch(k -> "completed".equals(k.getStatus()) || "false".equals(k.getStatus())); |
| | | |
| | | // 4. 若所有关键词状态均有效,更新订单状态为3 |
| | | if (allValid) { |
| | | Orders orders = orderService.getById(orderId); |
| | | if (orders != null) { |
| | | orders.setStatus(3); // 假设Orders有Integer类型的status字段 |
| | | orderService.updateById(orders); |
| | | System.out.println("订单[" + orderId + "]所有关键词状态符合条件,已更新状态为3"); |
| | | } else { |
| | | System.out.println("未找到订单[" + orderId + "],无法更新状态"); |
| | | } |
| | | } |
| | | |
| | | |
| | | Orders orders = orderService.getById(keyword.getOrder_id()); |
| | | |
| | | // 2. 批量查询所有问题 |
| | | LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(Question::getKeyword_id, keyword.getKeyword_id()); |
| | | List<Question> questions = questionService.list(queryWrapper); |
| | | |
| | | // 构建问题映射表,用于快速查找 |
| | | Map<String, Question> questionMap = questions.stream() |
| | | .collect(Collectors.toMap(Question::getQuestion, q -> q)); |
| | | |
| | | // 3. 收集所有需要更新的问题和引用 |
| | | List<Question> questionsToUpdate = new ArrayList<>(); |
| | | List<Reference> allReferences = new ArrayList<>(); |
| | | List<Reference> resultList = new ArrayList<>(); |
| | | |
| | | // 遍历结果 |
| | | for (UserResult userResult : result.getResults()) { |
| | | //更新账号状态 |
| | | if ( "failed".equals(userResult.getStatus())){ |
| | | if (userResult.getError().contains("登录失败")){ |
| | | LambdaUpdateWrapper<User> userWrapper = new LambdaUpdateWrapper<>(); |
| | | userWrapper.eq(User::getUser_email, userResult.getUser_email()); |
| | | userWrapper.set(User::getStatus, "无法登录"); |
| | | userService.update(userWrapper); |
| | | //更新所有提问词的状态 |
| | | questionService.update(new LambdaUpdateWrapper<Question>().eq(Question::getKeyword_id, keyword.getKeyword_id()) |
| | | .set(Question::getStatus, "failed") |
| | | .set(Question::getError, "账户登录失败")); |
| | | |
| | | }else if (userResult.getError().contains("信息错误")){ |
| | | LambdaUpdateWrapper<User> userWrapper = new LambdaUpdateWrapper<>(); |
| | | userWrapper.eq(User::getUser_email, userResult.getUser_email()); |
| | | userWrapper.set(User::getStatus, "信息错误"); |
| | | userService.update(userWrapper); |
| | | } |
| | | } |
| | | for (QuestionResult questionResult : userResult.getQuestions_results()) { |
| | | try { |
| | | Question question = questionMap.get(questionResult.getQuestion()); |
| | | if (question != null) { |
| | | |
| | | |
| | | //保存问题结果 |
| | | QuestionResultList questionResultList = new QuestionResultList(); |
| | | questionResultList.setKeyword_id(keyword.getKeyword_id()); |
| | | questionResultList.setQuestion(questionResult.getQuestion()); |
| | | questionResultList.setResponse(questionResult.getResponse()); |
| | | questionResultList.setStatus(questionResult.getStatus()); |
| | | questionResultList.setExtracted_count(questionResult.getExtracted_count()); |
| | | questionResultList.setKeyword_task_id(result.getTask_id()); |
| | | questionResultList.setError(questionResult.getError()); |
| | | questionResultList.setNum(keyword.getNum()); |
| | | if (questionResult.getTimestamp() != null) { |
| | | DateTimeFormatter formatter = DateTimeFormatter |
| | | .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS"); |
| | | questionResultList.setTimestamp( |
| | | LocalDateTime.parse(questionResult.getTimestamp(), formatter)); |
| | | } |
| | | // 保存问题结果列表(新增保存逻辑) |
| | | questionResultService.save(questionResultList); |
| | | // 查询当前轮次下该提问词的所有结果 |
| | | List<QuestionResultList> allResults = questionResultService.list( |
| | | new LambdaQueryWrapper<QuestionResultList>() |
| | | .eq(QuestionResultList::getKeyword_id, keyword.getKeyword_id()) |
| | | .eq(QuestionResultList::getQuestion, question.getQuestion()) |
| | | .eq(QuestionResultList::getNum, keyword.getNum()) |
| | | ); |
| | | |
| | | // 判断最终状态 |
| | | String finalStatus = determineFinalStatus(allResults); |
| | | if ("success".equals(finalStatus)){ |
| | | question.setStatus("success"); |
| | | question.setError(""); |
| | | }else if ("no_results".equals(finalStatus)){ |
| | | question.setStatus("success"); |
| | | question.setError("采集结果无引用数据"); |
| | | }else if ("busyness".equals(finalStatus)){ |
| | | question.setStatus("failed"); |
| | | question.setError("DeepSeek繁忙,请稍后尝试"); |
| | | } |
| | | |
| | | // 更新问题对象 |
| | | question.setResponse(questionResult.getResponse()); |
| | | question.setExtracted_count(questionResult.getExtracted_count()); |
| | | // question.setError(questionResult.getError()); |
| | | question.setKeyword_id(keyword.getKeyword_id()); |
| | | |
| | | if (questionResult.getTimestamp() != null) { |
| | | DateTimeFormatter formatter = DateTimeFormatter |
| | | .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS"); |
| | | question.setTimestamp( |
| | | LocalDateTime.parse(questionResult.getTimestamp(), formatter)); |
| | | } |
| | | |
| | | questionsToUpdate.add(question); |
| | | // 初始化引用列表(避免null) |
| | | List<Reference> references = new ArrayList<>(); |
| | | List<TaskResultResponse.Reference> originalReferences = questionResult.getReferences(); |
| | | if (originalReferences == null) { |
| | | originalReferences = Collections.emptyList(); |
| | | } |
| | | |
| | | // 遍历原始引用列表,转换为Reference对象 |
| | | for (TaskResultResponse.Reference ref : originalReferences) { |
| | | Reference reference = new Reference(); |
| | | // 设置基本字段 |
| | | reference.setQuestion_id(question.getQuestion_id()); |
| | | reference.setTitle(ref.getTitle()); |
| | | reference.setUrl(ref.getUrl()); |
| | | reference.setDomain(ref.getDomain()); |
| | | reference.setNum(keyword.getNum()); |
| | | reference.setTask_id(result.getTask_id()); |
| | | reference.setKeyword_id(keyword.getKeyword_id()); |
| | | if (null!=ref.getPublish_time()) { |
| | | reference.setCreate_time(ref.getPublish_time().atStartOfDay()); |
| | | } |
| | | |
| | | // 关键:使用优化后的方法获取平台,避免重复创建 |
| | | Platform platform = getOrCreatePlatform(ref.getDomain(),ref.getPlatform_name()); |
| | | reference.setPlatform_id(platform.getPlatform_id()); |
| | | reference.setType_id(platform.getType_id()); // 直接从平台获取类型ID,更可靠 |
| | | // 添加到结果列表 |
| | | references.add(reference); |
| | | } |
| | | // 添加到总引用列表 |
| | | if (!references.isEmpty()) { |
| | | allReferences.addAll(references); |
| | | } |
| | | |
| | | //取数据库中当前关键词的当前轮次的当前问题id结果拿出来 |
| | | List<Reference> dbList = referenceService.list(new LambdaQueryWrapper<Reference>().eq(Reference::getKeyword_id, keyword.getKeyword_id()) |
| | | .eq(Reference::getNum, keyword.getNum()) |
| | | .eq(Reference::getQuestion_id, question.getQuestion_id()) |
| | | ); |
| | | |
| | | // 1. 合并两个列表 |
| | | List<Reference> combinedList = new ArrayList<>(); |
| | | combinedList.addAll(allReferences); |
| | | combinedList.addAll(dbList); |
| | | |
| | | // 2. 创建复合键的Map,用于统计完全匹配的记录 |
| | | Map<String, List<Reference>> compositeKeyMap = combinedList.stream() |
| | | .collect(Collectors.groupingBy( |
| | | ref -> ref.getTitle() + "|" + ref.getUrl() + "|" + ref.getDomain() |
| | | )); |
| | | |
| | | // 3. 处理每组重复记录 |
| | | |
| | | compositeKeyMap.forEach((key, refGroup) -> { |
| | | // 3.1 找出组内有ID的记录(优先从dbList中获取) |
| | | Optional<Reference> existingRecord = refGroup.stream() |
| | | .filter(ref -> ref.getReference_id() != null) |
| | | .findFirst(); |
| | | |
| | | // 3.2 统计该组的重复次数(总数-1) |
| | | int repetitionCount = refGroup.size() - 1; |
| | | |
| | | // 3.3 决定最终保留的记录 |
| | | Reference recordToSave = new Reference(); |
| | | if (existingRecord.isPresent()) { |
| | | // 使用已有ID的记录并更新重复次数 |
| | | recordToSave = existingRecord.get(); |
| | | recordToSave.setRepetition_num( |
| | | (recordToSave.getRepetition_num() == null ? 1 : recordToSave.getRepetition_num()) |
| | | + repetitionCount |
| | | ); |
| | | } else { |
| | | // 没有ID记录则取第一条并设置重复次数 |
| | | recordToSave = refGroup.get(0); |
| | | recordToSave.setRepetition_num(1+repetitionCount); |
| | | } |
| | | |
| | | resultList.add(recordToSave); |
| | | }); |
| | | referenceService.saveOrUpdateBatch(resultList); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | System.out.println("处理问题结果失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | // 4. 批量更新问题 |
| | | System.out.println(questionsToUpdate); |
| | | if (!questionsToUpdate.isEmpty()) { |
| | | questionService.updateBatchById(questionsToUpdate); |
| | | System.out.println("成功批量更新 " + questionsToUpdate.size() + " 个问题"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("更新问题和引用数据失败: " ,e.getMessage(), e); |
| | | throw new RuntimeException("更新问题和引用数据失败", e); |
| | | } |
| | | }); |
| | | return Mono.fromRunnable(() -> doUpdateQuestionAndReference(result)) |
| | | .onErrorResume(e -> { |
| | | log.error("处理任务结果失败", e); |
| | | return Mono.error(e); // 传播异常,触发事务回滚 |
| | | }).then(); |
| | | } |
| | | // 根据所有批次的结果判断最终状态 |
| | | private String determineFinalStatus(List<QuestionResultList> results) { |
| | | if (results.isEmpty()) { |
| | | return "no_results"; // 无结果 |
| | | |
| | | // 核心业务逻辑,添加事务注解保证原子性 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void doUpdateQuestionAndReference(TaskResultResponse result) { |
| | | try { |
| | | // 1. 查询关键词任务并更新状态 |
| | | LambdaQueryWrapper<KeywordTask> keywordTaskWrapper = new LambdaQueryWrapper<>(); |
| | | keywordTaskWrapper.eq(KeywordTask::getTask_id, result.getTask_id()); |
| | | KeywordTask keywordTask = keywordTaskService.getOne(keywordTaskWrapper); |
| | | if (keywordTask == null) { |
| | | throw new Exception("未找到关键词任务,task_id: " + result.getTask_id()); |
| | | } |
| | | keywordTask.setStatus("completed"); |
| | | keywordTaskService.updateById(keywordTask); |
| | | |
| | | // 2. 查询关键词信息 |
| | | Keyword keyword = keywordService.getById(keywordTask.getKeyword_id()); |
| | | if (keyword == null) { |
| | | throw new Exception("未找到关联的关键词,task_id: " + result.getTask_id()); |
| | | } |
| | | |
| | | // 3. 更新关键词状态(基于关联任务状态) |
| | | updateKeywordStatus(keyword); |
| | | |
| | | // 4. 更新订单状态(基于关键词状态) |
| | | updateOrderStatus(keyword); |
| | | |
| | | // 5. 预查询问题列表(一次查询,内存映射) |
| | | LambdaQueryWrapper<Question> questionWrapper = new LambdaQueryWrapper<>(); |
| | | questionWrapper.eq(Question::getKeyword_id, keyword.getKeyword_id()); |
| | | List<Question> questions = questionService.list(questionWrapper); |
| | | Map<String, Question> questionMap = questions.stream() |
| | | .collect(Collectors.toMap(Question::getQuestion, q -> q)); |
| | | |
| | | // 6. 收集批量保存的数据(避免循环内保存) |
| | | List<QuestionResultList> questionResultsToSave = new ArrayList<>(); // 批量保存问题结果 |
| | | List<Reference> allReferences = new ArrayList<>(); // 收集所有引用,后续统一处理 |
| | | |
| | | // 7. 遍历结果处理问题和引用 |
| | | for (UserResult userResult : result.getResults()) { |
| | | // 7.1 更新用户状态(失败处理) |
| | | handleUserStatus(userResult, keyword); |
| | | |
| | | // 7.2 处理问题结果 |
| | | for (QuestionResult questionResult : userResult.getQuestions_results()) { |
| | | Question question = questionMap.get(questionResult.getQuestion()); |
| | | if (question == null) { |
| | | log.warn("未找到问题记录: {}", questionResult.getQuestion()); |
| | | continue; |
| | | } |
| | | |
| | | // 7.2.1 构建问题结果并加入批量列表 |
| | | QuestionResultList questionResultList = buildQuestionResultList(questionResult, keyword, result); |
| | | questionResultsToSave.add(questionResultList); |
| | | |
| | | // 7.2.2 处理引用数据(仅收集,不立即保存) |
| | | List<Reference> references = buildReferences(questionResult, question, keyword, result); |
| | | allReferences.addAll(references); |
| | | } |
| | | } |
| | | |
| | | // 8. 批量保存问题结果(一次数据库交互) |
| | | if (!questionResultsToSave.isEmpty()) { |
| | | questionResultService.saveBatch(questionResultsToSave); |
| | | log.info("批量保存问题结果 {} 条", questionResultsToSave.size()); |
| | | } |
| | | |
| | | // 9. 批量处理引用数据(去重+更新重复次数+批量保存) |
| | | if (!allReferences.isEmpty()) { |
| | | handleReferencesInBatch(allReferences, keyword); |
| | | } |
| | | |
| | | // 10. 批量更新问题状态(基于最终结果) |
| | | updateQuestionsStatusInBatch(questions, keyword); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("更新数据失败", e); |
| | | throw new RuntimeException("更新数据失败", e); // 触发事务回滚 |
| | | } |
| | | } |
| | | |
| | | // 更新关键词状态 |
| | | private void updateKeywordStatus(Keyword keyword) { |
| | | LambdaQueryWrapper<KeywordTask> taskWrapper = new LambdaQueryWrapper<>(); |
| | | taskWrapper.eq(KeywordTask::getKeyword_id, keyword.getKeyword_id()); |
| | | List<KeywordTask> keywordTasks = keywordTaskService.list(taskWrapper); |
| | | |
| | | boolean allCompletedOrFailed = keywordTasks.stream() |
| | | .allMatch(task -> "completed".equals(task.getStatus()) |
| | | || "false".equals(task.getStatus()) |
| | | || "cancelled".equals(task.getStatus()) |
| | | || "canceled".equals(task.getStatus()) |
| | | || "nonentity".equals(task.getStatus())); |
| | | |
| | | if (allCompletedOrFailed) { |
| | | keyword.setStatus("completed"); |
| | | keywordService.updateById(keyword); |
| | | } |
| | | } |
| | | |
| | | // 更新订单状态 |
| | | private void updateOrderStatus(Keyword keyword) { |
| | | String orderId = keyword.getOrder_id(); |
| | | if (orderId == null || orderId.isEmpty()) { |
| | | log.info("关键词[{}]未关联订单,跳过订单更新", keyword.getKeyword_id()); |
| | | return; |
| | | } |
| | | |
| | | // 统计关键指标 |
| | | LambdaQueryWrapper<Keyword> orderKeywordsWrapper = new LambdaQueryWrapper<>(); |
| | | orderKeywordsWrapper.eq(Keyword::getOrder_id, orderId); |
| | | List<Keyword> orderKeywords = keywordService.list(orderKeywordsWrapper); |
| | | if (orderKeywords.isEmpty()) { |
| | | log.info("订单[{}]无关键词,跳过状态更新", orderId); |
| | | return; |
| | | } |
| | | |
| | | boolean allValid2 = orderKeywords.stream() |
| | | .allMatch(k -> "completed".equals(k.getStatus()) |
| | | || "false".equals(k.getStatus()) |
| | | || "cancelled".equals(k.getStatus())); |
| | | if (allValid2) { |
| | | updateOrderStatus(orderId, 1, "所有关键词采集完成或取消"); |
| | | } |
| | | |
| | | boolean allValid = orderKeywords.stream() |
| | | .allMatch(k -> "completed".equals(k.getStatus()) || "false".equals(k.getStatus())); |
| | | if (allValid) { |
| | | updateOrderStatus(orderId, 3, "所有关键词状态符合条件"); |
| | | } |
| | | } |
| | | |
| | | // 封装订单状态更新 |
| | | private void updateOrderStatus(String orderId, Integer status, String logMsg) { |
| | | Orders orders = orderService.getById(orderId); |
| | | if (orders != null) { |
| | | orders.setStatus(status); |
| | | orderService.updateById(orders); |
| | | log.info("订单[{}]{},已更新状态为{}", orderId, logMsg, status); |
| | | } else { |
| | | log.warn("未找到订单[{}],无法更新状态", orderId); |
| | | } |
| | | } |
| | | |
| | | // 构建问题结果对象 |
| | | private QuestionResultList buildQuestionResultList(QuestionResult questionResult, Keyword keyword, TaskResultResponse result) { |
| | | QuestionResultList questionResultList = new QuestionResultList(); |
| | | questionResultList.setKeyword_id(keyword.getKeyword_id()); |
| | | questionResultList.setQuestion(questionResult.getQuestion()); |
| | | questionResultList.setResponse(questionResult.getResponse()); |
| | | questionResultList.setStatus(questionResult.getStatus()); |
| | | questionResultList.setExtracted_count(questionResult.getExtracted_count()); |
| | | questionResultList.setKeyword_task_id(result.getTask_id()); |
| | | questionResultList.setError(questionResult.getError()); |
| | | questionResultList.setNum(keyword.getNum()); |
| | | |
| | | if (questionResult.getTimestamp() != null) { |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS"); |
| | | questionResultList.setTimestamp(LocalDateTime.parse(questionResult.getTimestamp(), formatter)); |
| | | } |
| | | return questionResultList; |
| | | } |
| | | |
| | | // 构建引用数据列表 |
| | | private List<Reference> buildReferences(QuestionResult questionResult, Question question, Keyword keyword, TaskResultResponse result) { |
| | | List<Reference> references = new ArrayList<>(); |
| | | List<TaskResultResponse.Reference> originalReferences = questionResult.getReferences(); |
| | | if (originalReferences == null) { |
| | | return references; |
| | | } |
| | | |
| | | for (TaskResultResponse.Reference ref : originalReferences) { |
| | | // 过滤无效引用(标题、URL、域名不能为空) |
| | | if (ref.getTitle() == null || ref.getUrl() == null || ref.getDomain() == null) { |
| | | log.warn("引用数据字段缺失,跳过:title={}, url={}, domain={}", |
| | | ref.getTitle(), ref.getUrl(), ref.getDomain()); |
| | | continue; |
| | | } |
| | | |
| | | Reference reference = new Reference(); |
| | | reference.setQuestion_id(question.getQuestion_id()); |
| | | reference.setTitle(ref.getTitle()); |
| | | reference.setUrl(ref.getUrl()); |
| | | reference.setDomain(ref.getDomain()); |
| | | reference.setNum(keyword.getNum()); |
| | | reference.setTask_id(result.getTask_id()); |
| | | reference.setKeyword_id(keyword.getKeyword_id()); |
| | | |
| | | if (ref.getPublish_time() != null) { |
| | | reference.setCreate_time(ref.getPublish_time().atStartOfDay()); |
| | | } |
| | | |
| | | Platform platform = getOrCreatePlatform(ref.getDomain(), ref.getPlatform_name()); |
| | | // 校验平台信息非空 |
| | | if (platform == null || platform.getPlatform_id() == null) { |
| | | log.warn("平台信息无效,跳过引用:domain={}", ref.getDomain()); |
| | | continue; |
| | | } |
| | | reference.setPlatform_id(platform.getPlatform_id()); |
| | | reference.setType_id(platform.getType_id()); |
| | | references.add(reference); |
| | | } |
| | | return references; |
| | | } |
| | | |
| | | // 批量处理引用数据(去重+更新重复次数) |
| | | private void handleReferencesInBatch(List<Reference> allReferences, Keyword keyword) { |
| | | // 1. 过滤原始列表中的 null 元素 |
| | | List<Reference> validReferences = allReferences.stream() |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (validReferences.isEmpty()) { |
| | | log.info("无有效引用数据,跳过批量保存"); |
| | | return; |
| | | } |
| | | |
| | | // 2. 查询数据库中已存在的引用并过滤 null |
| | | LambdaQueryWrapper<Reference> dbRefWrapper = new LambdaQueryWrapper<>(); |
| | | dbRefWrapper.eq(Reference::getKeyword_id, keyword.getKeyword_id()) |
| | | .eq(Reference::getNum, keyword.getNum()); |
| | | List<Reference> dbReferences = referenceService.list(dbRefWrapper); |
| | | List<Reference> validDbReferences = dbReferences.stream() |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 3. 合并并去重 |
| | | Map<String, List<Reference>> compositeKeyMap = new HashMap<>(); |
| | | validReferences.forEach(ref -> addToCompositeMap(compositeKeyMap, ref)); |
| | | validDbReferences.forEach(ref -> addToCompositeMap(compositeKeyMap, ref)); |
| | | |
| | | // 4. 处理重复次数 |
| | | List<Reference> referencesToSave = new ArrayList<>(); |
| | | compositeKeyMap.forEach((key, refGroup) -> { |
| | | List<Reference> validRefGroup = refGroup.stream() |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | if (validRefGroup.isEmpty()) return; |
| | | |
| | | Optional<Reference> existingRef = validRefGroup.stream() |
| | | .filter(ref -> ref.getReference_id() != null) |
| | | .findFirst(); |
| | | |
| | | Reference finalRef = existingRef.orElse(validRefGroup.get(0)); |
| | | int repetitionNum = (finalRef.getRepetition_num() == null ? 1 : finalRef.getRepetition_num()) |
| | | + (validRefGroup.size() - 1); |
| | | finalRef.setRepetition_num(repetitionNum); |
| | | referencesToSave.add(finalRef); |
| | | }); |
| | | |
| | | // 5. 最终校验并保存 |
| | | List<Reference> finalSaveList = referencesToSave.stream() |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 关键校验:列表非空且元素有效 |
| | | if (finalSaveList.isEmpty()) { |
| | | log.info("处理后无有效引用数据可保存"); |
| | | return; |
| | | } |
| | | if (finalSaveList.stream().anyMatch(ref -> !(ref instanceof Reference))) { |
| | | log.error("引用数据类型异常,无法保存"); |
| | | return; |
| | | } |
| | | |
| | | // 执行保存 |
| | | try { |
| | | referenceService.saveOrUpdateBatch(finalSaveList); |
| | | log.info("批量保存引用数据成功,数量:{}", finalSaveList.size()); |
| | | } catch (Exception e) { |
| | | log.error("批量保存引用数据失败", e); |
| | | throw new RuntimeException("保存引用数据失败", e); |
| | | } |
| | | } |
| | | |
| | | // 辅助方法:将引用添加到复合键Map |
| | | private void addToCompositeMap(Map<String, List<Reference>> map, Reference ref) { |
| | | // 再次校验引用的核心字段非空 |
| | | if (ref.getTitle() == null || ref.getUrl() == null || ref.getDomain() == null) { |
| | | log.warn("引用核心字段为空,跳过映射:{}", ref); |
| | | return; |
| | | } |
| | | String key = ref.getQuestion_id() + "|" + ref.getTitle() + "|" + ref.getUrl() + "|" + ref.getDomain(); |
| | | map.computeIfAbsent(key, k -> new ArrayList<>()).add(ref); |
| | | } |
| | | |
| | | // 批量更新问题状态 |
| | | private void updateQuestionsStatusInBatch(List<Question> questions, Keyword keyword) { |
| | | if (questions.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 一次性查询所有问题结果(基于关键词+轮次) |
| | | LambdaQueryWrapper<QuestionResultList> resultWrapper = new LambdaQueryWrapper<>(); |
| | | resultWrapper.eq(QuestionResultList::getKeyword_id, keyword.getKeyword_id()) |
| | | .eq(QuestionResultList::getNum, keyword.getNum()); |
| | | List<QuestionResultList> allQuestionResults = questionResultService.list(resultWrapper); |
| | | |
| | | // 按问题分组,便于查询 |
| | | Map<String, List<QuestionResultList>> questionResultsMap = allQuestionResults.stream() |
| | | .collect(Collectors.groupingBy(QuestionResultList::getQuestion)); |
| | | |
| | | // 批量更新问题状态 |
| | | List<Question> questionsToUpdate = new ArrayList<>(); |
| | | questions.forEach(question -> { |
| | | List<QuestionResultList> results = questionResultsMap.getOrDefault(question.getQuestion(), Collections.emptyList()); |
| | | String finalStatus = determineFinalStatus(results); |
| | | |
| | | if ("success".equals(finalStatus)) { |
| | | question.setStatus("success"); |
| | | question.setError(""); |
| | | } else if ("no_results".equals(finalStatus)) { |
| | | question.setStatus("success"); |
| | | question.setError("采集结果无引用数据"); |
| | | } else if ("busyness".equals(finalStatus)) { |
| | | question.setStatus("failed"); |
| | | question.setError("DeepSeek繁忙,请稍后尝试"); |
| | | } |
| | | // 更新其他字段(响应、时间戳等) |
| | | results.stream().findFirst().ifPresent(result -> { |
| | | question.setResponse(result.getResponse()); |
| | | question.setExtracted_count(result.getExtracted_count()); |
| | | question.setTimestamp(result.getTimestamp()); |
| | | }); |
| | | questionsToUpdate.add(question); |
| | | }); |
| | | |
| | | if (!questionsToUpdate.isEmpty()) { |
| | | questionService.updateBatchById(questionsToUpdate); |
| | | log.info("批量更新问题状态 {} 条", questionsToUpdate.size()); |
| | | } |
| | | } |
| | | |
| | | // 处理用户状态异常 |
| | | private void handleUserStatus(UserResult userResult, Keyword keyword) { |
| | | if ("failed".equals(userResult.getStatus())) { |
| | | LambdaUpdateWrapper<User> userWrapper = new LambdaUpdateWrapper<>(); |
| | | userWrapper.eq(User::getUser_email, userResult.getUser_email()); |
| | | |
| | | if (userResult.getError().contains("登录失败")) { |
| | | userWrapper.set(User::getStatus, "无法登录"); |
| | | userService.update(userWrapper); |
| | | // 批量更新问题状态为失败 |
| | | questionService.update(new LambdaUpdateWrapper<Question>() |
| | | .eq(Question::getKeyword_id, keyword.getKeyword_id()) |
| | | .set(Question::getStatus, "failed") |
| | | .set(Question::getError, "账户登录失败")); |
| | | } else if (userResult.getError().contains("信息错误")) { |
| | | userWrapper.set(User::getStatus, "信息错误"); |
| | | userService.update(userWrapper); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 原方法:判断最终状态(复用) |
| | | private String determineFinalStatus(List<QuestionResultList> results) { |
| | | if (results.isEmpty()) { |
| | | return "no_results"; |
| | | } |
| | | |
| | | int totalCount = results.size(); |
| | | int emptyResponseCount = 0; |
| | | int systemBusyCount = 0; |
| | | |
| | | for (QuestionResultList result : results) { |
| | | // 判断回答是否为空 |
| | | if (result.getExtracted_count() == 0 ) { |
| | | if (result.getExtracted_count() == 0) { |
| | | emptyResponseCount++; |
| | | } |
| | | |
| | | // 判断是否为系统繁忙 |
| | | if ("success".equals(result.getStatus()) && (result.getResponse().isEmpty()|| result.getResponse().contains("WebDriver连接中断") || result.getResponse().contains("响应超时"))) { |
| | | if ("success".equals(result.getStatus()) && |
| | | (result.getResponse() == null || result.getResponse().isEmpty() |
| | | || result.getResponse().contains("WebDriver连接中断") |
| | | || result.getResponse().contains("响应超时"))) { |
| | | systemBusyCount++; |
| | | } |
| | | |
| | | } |
| | | |
| | | // 全返回系统繁忙 |
| | | if (systemBusyCount == totalCount) { |
| | | return "busyness"; |
| | | } |
| | | // 全返回信息为空 |
| | | if (emptyResponseCount == totalCount) { |
| | | return "no_results"; |
| | | } |
| | | |
| | | |
| | | // 系统繁忙比例超过阈值(可配置,这里设为70%) |
| | | // double busyRate = (double) systemBusyCount / totalCount; |
| | | // if (busyRate >= 0.7) { |
| | | // return "系统繁忙,请稍后尝试"; |
| | | // } |
| | | |
| | | // 其他情况返回成功 |
| | | return "success"; |
| | | } |
| | | |
| | | @GetMapping("/tasks/all") |
| | | @ApiOperation(value = "获取所有任务列表") |
| | | public Mono<TaskListResponse> getAllTasks() { |