diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseAiChatServiceImpl.java b/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseAiChatServiceImpl.java index ef24d226..a8196670 100644 --- a/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseAiChatServiceImpl.java +++ b/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseAiChatServiceImpl.java @@ -108,6 +108,9 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService { public SseEmitter chat(CaseAiChatDto caseAiChatDto, CurrentUser currentUser) { // 1. 获取conversationId String conversationId = getOrCreateConversationId(caseAiChatDto, currentUser); + + // 2. 查询历史 + List historyMessages = elasticSearchIndexService.queryData(conversationId); // 3. 构建请求参数 String userId = currentUser.getCode(); @@ -120,6 +123,20 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService { chatParam.put("query", caseAiChatDto.getQuery()); chatParam.put("conversationId", conversationId); chatParam.put("enableThinking", Objects.equals(caseAiChatDto.getEnableThinking(), 1)); + if (historyMessages != null && !historyMessages.isEmpty()) { + // 最多10条历史,从后往前 + JSONArray historyList = new JSONArray(); + int size = historyMessages.size(); + int startIndex = Math.max(0, size - 10); + for (int i = startIndex; i < size; i++) { + JSONObject conversationDetail = new JSONObject(); + CaseAiMessageVo message = historyMessages.get(i); + conversationDetail.put("query", message.getQuery()); + conversationDetail.put("content", message.getAnswer()); + historyList.add(conversationDetail); + } + chatParam.put("historyList", historyList); + } String chatParamStr = chatParam.toJSONString(); log.info("案例问答接口请求参数: [{}]", chatParamStr); diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/boecase/task/CaseAiChatDataTask.java b/servers/boe-server-all/src/main/java/com/xboe/module/boecase/task/CaseAiChatDataTask.java index 3ffda0e8..99db06bd 100644 --- a/servers/boe-server-all/src/main/java/com/xboe/module/boecase/task/CaseAiChatDataTask.java +++ b/servers/boe-server-all/src/main/java/com/xboe/module/boecase/task/CaseAiChatDataTask.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.time.YearMonth; +import java.time.format.DateTimeFormatter; @Component @Slf4j @@ -22,17 +23,55 @@ public class CaseAiChatDataTask { * cron: 0/10 * * * * ? */ @XxlJob("chatDataExcelDownloadJob") - public void chatDataExcelDownload() { - LocalDateTime now = LocalDateTime.now(); - // 取上个月的1号00:00:00到上个月最后一天的23:59:59 - YearMonth lastMonth = YearMonth.from(now).minusMonths(1); - LocalDateTime startTime = now.minusMonths(1) - .withDayOfMonth(1) - .withHour(0) - .withMinute(0) - .withSecond(0); - LocalDateTime endTime = lastMonth.atEndOfMonth().atTime(23, 59, 59); + public void chatDataExcelDownload(String param) { + LocalDateTime startTime; + LocalDateTime endTime; + + if (param != null && !param.isEmpty()) { + // 解析参数,格式应为 "startTime,endTime",例如 "2023-01-01T00:00:00,2023-01-31T23:59:59" + String[] times = param.split(","); + if (times.length == 2) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); + try { + startTime = LocalDateTime.parse(times[0], formatter); + endTime = LocalDateTime.parse(times[1], formatter); + log.info("使用参数指定的时间范围: {} 到 {}", startTime, endTime); + } catch (Exception e) { + log.error("解析时间参数失败,使用默认时间范围", e); + // 使用默认逻辑 + LocalDateTime now = LocalDateTime.now(); + YearMonth lastMonth = YearMonth.from(now).minusMonths(1); + startTime = now.minusMonths(1) + .withDayOfMonth(1) + .withHour(0) + .withMinute(0) + .withSecond(0); + endTime = lastMonth.atEndOfMonth().atTime(23, 59, 59); + } + } else { + // 使用默认逻辑 + LocalDateTime now = LocalDateTime.now(); + YearMonth lastMonth = YearMonth.from(now).minusMonths(1); + startTime = now.minusMonths(1) + .withDayOfMonth(1) + .withHour(0) + .withMinute(0) + .withSecond(0); + endTime = lastMonth.atEndOfMonth().atTime(23, 59, 59); + } + } else { + // 使用默认逻辑 + LocalDateTime now = LocalDateTime.now(); + YearMonth lastMonth = YearMonth.from(now).minusMonths(1); + startTime = now.minusMonths(1) + .withDayOfMonth(1) + .withHour(0) + .withMinute(0) + .withSecond(0); + endTime = lastMonth.atEndOfMonth().atTime(23, 59, 59); + } + // 执行 caseAiChatService.downloadConversationExcel(startTime, endTime); } -} +} \ No newline at end of file