|
|
@@ -1,29 +1,20 @@
|
|
|
package org.dromara.talk.controller.api;
|
|
|
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dromara.common.core.domain.dto.DictDataDTO;
|
|
|
import org.dromara.common.core.service.DictService;
|
|
|
import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
import org.dromara.talk.domain.bo.TalkAgentBo;
|
|
|
-import org.dromara.talk.domain.bo.TalkSessionBo;
|
|
|
import org.dromara.talk.domain.dto.MessageStreamRequest;
|
|
|
import org.dromara.talk.domain.vo.TalkAgentVo;
|
|
|
import org.dromara.talk.service.IChatService;
|
|
|
import org.dromara.talk.service.ITalkAgentService;
|
|
|
-import org.dromara.talk.service.ITalkSessionService;
|
|
|
-import org.dromara.talk.service.IPhoneUserService;
|
|
|
-import org.dromara.talk.domain.bo.PhoneUserBo;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 对话接口(对话前端)
|
|
|
@@ -41,8 +32,6 @@ public class ChatController {
|
|
|
private final IChatService chatService;
|
|
|
private final DictService dictService;
|
|
|
private final ITalkAgentService talkAgentService;
|
|
|
- private final ITalkSessionService talkSessionService;
|
|
|
- private final IPhoneUserService phoneUserService;
|
|
|
|
|
|
/**
|
|
|
* 获取TTS发言人字典
|
|
|
@@ -107,47 +96,15 @@ public class ChatController {
|
|
|
*/
|
|
|
@PostMapping("/agent/{id}/start")
|
|
|
public Map<String, Object> startChat(@PathVariable Long id, @RequestBody(required = false) Map<String, String> request) {
|
|
|
- boolean success = talkAgentService.changeAgentStatus(id, "2");
|
|
|
- if (success) {
|
|
|
- Long userId = 0L;
|
|
|
- try {
|
|
|
- userId = LoginHelper.getUserId();
|
|
|
- } catch (Exception e) {
|
|
|
- log.warn("获取登录用户ID失败,使用默认值", e);
|
|
|
- }
|
|
|
-
|
|
|
- String tempSessionId = UUID.randomUUID().toString();
|
|
|
- String customerPhone = request != null ? request.get("customerPhone") : null;
|
|
|
-
|
|
|
- // 如果有客户电话,检查并保存到 phone_user 表
|
|
|
- if (customerPhone != null && !customerPhone.isEmpty()) {
|
|
|
- try {
|
|
|
- PhoneUserBo phoneUserBo = new PhoneUserBo();
|
|
|
- phoneUserBo.setCustomerPhone(customerPhone);
|
|
|
- phoneUserService.insertByBo(phoneUserBo);
|
|
|
- log.info("新客户电话已保存: {}", customerPhone);
|
|
|
- } catch (Exception e) {
|
|
|
- // 如果插入失败(可能是重复),忽略错误继续执行
|
|
|
- log.debug("客户电话可能已存在: {}", customerPhone);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- TalkSessionBo sessionBo = new TalkSessionBo();
|
|
|
- sessionBo.setSessionId(tempSessionId);
|
|
|
- sessionBo.setAgentId(id);
|
|
|
- sessionBo.setStartTime(new Date());
|
|
|
- sessionBo.setUserId(userId);
|
|
|
- if (customerPhone != null) {
|
|
|
- sessionBo.setCustomerPhone(customerPhone);
|
|
|
- }
|
|
|
- talkSessionService.insertByBo(sessionBo);
|
|
|
-
|
|
|
- log.info("客服 {} 开始对话,sessionId: {}, customerPhone: {}, 创建人: {}", id, tempSessionId, customerPhone, userId);
|
|
|
- return Map.of("success", true, "code", 200, "sessionId", tempSessionId);
|
|
|
- } else {
|
|
|
- log.warn("客服 {} 开始对话失败,可能已被占用", id);
|
|
|
- return Map.of("success", false, "code", 400, "msg", "该客服已被其他用户占用,请选择其他客服");
|
|
|
+ Long userId = 0L;
|
|
|
+ try {
|
|
|
+ userId = LoginHelper.getUserId();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("获取登录用户ID失败,使用默认值", e);
|
|
|
}
|
|
|
+
|
|
|
+ String customerPhone = request != null ? request.get("customerPhone") : null;
|
|
|
+ return chatService.startChatSession(id, userId, customerPhone);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -163,42 +120,7 @@ public class ChatController {
|
|
|
String sessionId = (String) request.get("sessionId");
|
|
|
List<Map<String, Object>> chatHistory = (List<Map<String, Object>>) request.get("chatHistory");
|
|
|
|
|
|
- // 更新会话结束时间并保存对话内容
|
|
|
- if (sessionId != null) {
|
|
|
- talkSessionService.updateEndTime(sessionId);
|
|
|
-
|
|
|
- // 保存对话内容
|
|
|
- if (chatHistory != null && !chatHistory.isEmpty()) {
|
|
|
- try {
|
|
|
- // 转换前端格式到后端格式
|
|
|
- List<Map<String, String>> messages = new ArrayList<>();
|
|
|
- for (Map<String, Object> msg : chatHistory) {
|
|
|
- Map<String, String> message = new HashMap<>();
|
|
|
- String type = (String) msg.get("type");
|
|
|
- String content = (String) msg.get("content");
|
|
|
-
|
|
|
- // 转换 type: user/agent 到 role: user/assistant
|
|
|
- message.put("role", "user".equals(type) ? "user" : "assistant");
|
|
|
- message.put("content", content);
|
|
|
- message.put("timestamp", String.valueOf(System.currentTimeMillis()));
|
|
|
- messages.add(message);
|
|
|
- }
|
|
|
-
|
|
|
- String conversationJson = new ObjectMapper().writeValueAsString(messages);
|
|
|
- talkSessionService.saveOrUpdateConversation(sessionId, id, conversationJson, null, null);
|
|
|
- log.info("挂断时保存对话内容,会话ID: {}, 消息数量: {}", sessionId, messages.size());
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("保存对话内容失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 将客服状态改为空闲
|
|
|
- TalkAgentBo bo = new TalkAgentBo();
|
|
|
- bo.setId(id);
|
|
|
- bo.setStatus("0");
|
|
|
- boolean success = talkAgentService.updateByBo(bo);
|
|
|
- log.info("客服 {} 挂断电话,状态已改为空闲中,会话ID: {}", id, sessionId);
|
|
|
+ boolean success = chatService.hangupCall(id, sessionId, chatHistory);
|
|
|
return Map.of("success", success);
|
|
|
}
|
|
|
|