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; 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> getSessions( @RequestParam(required = false) String status, @RequestParam(required = false) String csId, @RequestParam(required = false) String channelType) { LambdaQueryWrapper 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); } query.orderByDesc(Session::getUpdatedAt); List sessions = sessionMapper.selectList(query); List 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()); info.setStatus(session.getStatus()); info.setManualCsId(session.getManualCsId()); info.setCreatedAt(session.getCreatedAt()); info.setUpdatedAt(session.getUpdatedAt()); info.setMetadata(session.getMetadata()); LambdaQueryWrapper 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 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()); 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> getSessionHistory(@PathVariable String sessionId) { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(Message::getSessionId, sessionId) .orderByAsc(Message::getCreatedAt); List messages = messageMapper.selectList(query); List 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 acceptSession( @PathVariable String sessionId, @Valid @RequestBody AcceptSessionRequest request) { 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()); return ApiResponse.success(null); } @PostMapping("/{sessionId}/message") public ApiResponse sendMessage( @PathVariable String sessionId, @Valid @RequestBody SendMessageRequest request) { 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, "消息发送失败"); } 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 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); } }