refactor(oa): 重构 AI 聊天接口

- 新增 OaAiChatBo 类封装请求参数
- 修改 chat 方法签名,使用 OaAiChatBo作为参数
- 更新方法内部逻辑,使用 OaAiChatBo 中的属性
This commit is contained in:
2025-08-04 15:02:39 +08:00
parent e0e5da4e1a
commit 695659ba6f
2 changed files with 20 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ package com.ruoyi.oa.controller;
import java.util.*;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.oa.domain.bo.OaAiChatBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
@@ -227,32 +228,32 @@ public class SysOaAiController extends BaseController {
* 发送消息给AI
*/
@PostMapping("/chat")
public R<Map<String, Object>> chat(@RequestParam Long conversationId, @RequestParam String message) {
public R<Map<String, Object>> chat(@RequestBody OaAiChatBo bo) {
try {
// 1. 保存用户消息
messageService.addUserMessage(conversationId, message);
messageService.addUserMessage(bo.getConversationId(), bo.getMessage());
// 2. 获取对话历史优先从Redis缓存获取
List<SysOaAiMessageVo> conversationHistory = aiServiceUtil.getCachedConversationHistory(conversationId);
List<SysOaAiMessageVo> conversationHistory = aiServiceUtil.getCachedConversationHistory(bo.getConversationId());
if (conversationHistory == null) {
// 缓存中没有,从数据库获取
conversationHistory = messageService.queryByConversationId(conversationId);
conversationHistory = messageService.queryByConversationId(bo.getConversationId());
// 缓存对话历史
aiServiceUtil.cacheConversationHistory(conversationId, conversationHistory);
aiServiceUtil.cacheConversationHistory(bo.getConversationId(), conversationHistory);
}
// 3. 调用AI服务获取回复传递对话历史
String aiResponse = callAiServiceWithHistory(message, conversationHistory);
String aiResponse = callAiServiceWithHistory(bo.getMessage(), conversationHistory);
// 4. 保存AI回复
messageService.addAiMessage(conversationId, aiResponse, 0, java.math.BigDecimal.ZERO);
messageService.addAiMessage(bo. getConversationId(), aiResponse, 0, java.math.BigDecimal.ZERO);
// 5. 更新对话历史缓存
List<SysOaAiMessageVo> updatedHistory = messageService.queryByConversationId(conversationId);
aiServiceUtil.cacheConversationHistory(conversationId, updatedHistory);
List<SysOaAiMessageVo> updatedHistory = messageService.queryByConversationId(bo.getConversationId());
aiServiceUtil.cacheConversationHistory(bo.getConversationId(), updatedHistory);
Map<String, Object> result = new HashMap<>();
result.put("conversationId", conversationId);
result.put("conversationId", bo.getConversationId());
result.put("response", aiResponse);
return R.ok(result);

View File

@@ -0,0 +1,9 @@
package com.ruoyi.oa.domain.bo;
import lombok.Data;
@Data
public class OaAiChatBo {
Long conversationId;
String message;
}