ai-robot-channel/src/main/java/com/wecom/robot/controller/SessionController.java

198 lines
7.1 KiB
Java
Raw Normal View History

2026-02-23 01:45:23 +00:00
package com.wecom.robot.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wecom.robot.dto.*;
import com.wecom.robot.entity.Message;
import com.wecom.robot.entity.Session;
import com.wecom.robot.mapper.MessageMapper;
import com.wecom.robot.mapper.SessionMapper;
import com.wecom.robot.service.SessionManagerService;
import com.wecom.robot.service.WecomApiService;
import com.wecom.robot.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
2026-02-23 01:45:23 +00:00
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/api/sessions")
@RequiredArgsConstructor
public class SessionController {
private final SessionMapper sessionMapper;
private final MessageMapper messageMapper;
private final SessionManagerService sessionManagerService;
private final WecomApiService wecomApiService;
private final WebSocketService webSocketService;
@GetMapping
public ApiResponse<List<SessionInfo>> getSessions(
@RequestParam(required = false) String status,
@RequestParam(required = false) String csId,
@RequestParam(required = false) String channelType) {
2026-02-23 01:45:23 +00:00
LambdaQueryWrapper<Session> query = new LambdaQueryWrapper<>();
if (status != null) {
query.eq(Session::getStatus, status);
}
if (csId != null) {
query.eq(Session::getManualCsId, csId);
}
if (channelType != null) {
query.eq(Session::getChannelType, channelType);
}
2026-02-23 01:45:23 +00:00
query.orderByDesc(Session::getUpdatedAt);
List<Session> sessions = sessionMapper.selectList(query);
List<SessionInfo> sessionInfos = sessions.stream().map(session -> {
SessionInfo info = new SessionInfo();
info.setSessionId(session.getSessionId());
info.setCustomerId(session.getCustomerId());
info.setKfId(session.getKfId());
info.setChannelType(session.getChannelType());
2026-02-23 01:45:23 +00:00
info.setStatus(session.getStatus());
info.setManualCsId(session.getManualCsId());
info.setCreatedAt(session.getCreatedAt());
info.setUpdatedAt(session.getUpdatedAt());
info.setMetadata(session.getMetadata());
LambdaQueryWrapper<Message> msgQuery = new LambdaQueryWrapper<>();
msgQuery.eq(Message::getSessionId, session.getSessionId())
.orderByDesc(Message::getCreatedAt)
.last("LIMIT 1");
Message lastMsg = messageMapper.selectOne(msgQuery);
if (lastMsg != null) {
info.setLastMessage(lastMsg.getContent());
info.setLastMessageTime(lastMsg.getCreatedAt());
}
int msgCount = sessionManagerService.getMessageCount(session.getSessionId());
info.setMessageCount(msgCount);
return info;
}).collect(Collectors.toList());
return ApiResponse.success(sessionInfos);
}
@GetMapping("/{sessionId}")
public ApiResponse<SessionInfo> getSession(@PathVariable String sessionId) {
Session session = sessionMapper.selectById(sessionId);
if (session == null) {
return ApiResponse.error(404, "会话不存在");
}
SessionInfo info = new SessionInfo();
info.setSessionId(session.getSessionId());
info.setCustomerId(session.getCustomerId());
info.setKfId(session.getKfId());
info.setChannelType(session.getChannelType());
2026-02-23 01:45:23 +00:00
info.setStatus(session.getStatus());
info.setManualCsId(session.getManualCsId());
info.setCreatedAt(session.getCreatedAt());
info.setUpdatedAt(session.getUpdatedAt());
info.setMetadata(session.getMetadata());
return ApiResponse.success(info);
}
@GetMapping("/{sessionId}/history")
public ApiResponse<List<MessageInfo>> getSessionHistory(@PathVariable String sessionId) {
LambdaQueryWrapper<Message> query = new LambdaQueryWrapper<>();
query.eq(Message::getSessionId, sessionId)
.orderByAsc(Message::getCreatedAt);
List<Message> messages = messageMapper.selectList(query);
List<MessageInfo> messageInfos = messages.stream().map(msg -> {
MessageInfo info = new MessageInfo();
info.setMsgId(msg.getMsgId());
info.setSessionId(msg.getSessionId());
info.setSenderType(msg.getSenderType());
info.setSenderId(msg.getSenderId());
info.setContent(msg.getContent());
info.setMsgType(msg.getMsgType());
info.setCreatedAt(msg.getCreatedAt());
return info;
}).collect(Collectors.toList());
return ApiResponse.success(messageInfos);
}
@PostMapping("/{sessionId}/accept")
public ApiResponse<Void> acceptSession(
@PathVariable String sessionId,
@Valid @RequestBody AcceptSessionRequest request) {
2026-02-23 01:45:23 +00:00
Session session = sessionMapper.selectById(sessionId);
if (session == null) {
return ApiResponse.error(404, "会话不存在");
}
if (!Session.STATUS_PENDING.equals(session.getStatus())) {
return ApiResponse.error(400, "会话状态不正确");
}
sessionManagerService.acceptTransfer(sessionId, request.getCsId());
webSocketService.notifySessionAccepted(sessionId, request.getCsId());
2026-02-23 01:45:23 +00:00
return ApiResponse.success(null);
}
@PostMapping("/{sessionId}/message")
public ApiResponse<Void> sendMessage(
@PathVariable String sessionId,
@Valid @RequestBody SendMessageRequest request) {
2026-02-23 01:45:23 +00:00
Session session = sessionMapper.selectById(sessionId);
if (session == null) {
return ApiResponse.error(404, "会话不存在");
}
if (!Session.STATUS_MANUAL.equals(session.getStatus())) {
return ApiResponse.error(400, "会话状态不正确");
}
boolean success = wecomApiService.sendTextMessage(
session.getCustomerId(),
session.getKfId(),
request.getContent()
);
if (!success) {
return ApiResponse.error(500, "消息发送失败");
2026-02-23 01:45:23 +00:00
}
sessionManagerService.saveMessage(
"manual_" + System.currentTimeMillis(),
sessionId,
Message.SENDER_TYPE_MANUAL,
session.getManualCsId(),
request.getContent(),
request.getMsgType() != null ? request.getMsgType() : "text",
null
);
return ApiResponse.success(null);
}
@PostMapping("/{sessionId}/close")
public ApiResponse<Void> closeSession(@PathVariable String sessionId) {
Session session = sessionMapper.selectById(sessionId);
if (session == null) {
return ApiResponse.error(404, "会话不存在");
}
sessionManagerService.closeSession(sessionId);
webSocketService.notifySessionClosed(sessionId);
return ApiResponse.success(null);
}
}