using Common; using CommonInfrastructure; using CustomerService.Customer; using CustomerService.Domain.Entity; using CustomerService.Domain.Response; using Microsoft.AspNetCore.Mvc; namespace CustomerApi.Controllers; [ApiController] [Route("customerapi/[controller]")] public class CustomerController : BaseController { private readonly ICustomerService _customerService; public CustomerController(ICustomerService customerService) { _customerService = customerService; } /// /// 发起会话 - 用户点击客服入口时调用 /// [HttpPost("start")] public async Task> StartConversation([FromBody] StartConversationParam param) { var result = await _customerService.StartConversation(param.UserId, param.UserName); if (result.isOk) return this.ToResult(result.conversation); else return this.ToFailResult(result.msg); } /// /// 发送消息 - 在会话中发送文字或图片消息 /// [HttpPost("message")] public async Task> SendMessage([FromBody] SendMessageParam param) { var result = await _customerService.SendMessage(param.ConversationId, param.SenderType, param.SenderId, param.SenderName, param.ContentType, param.Content, param.SenderAvatar); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } /// /// 获取消息列表 - 获取指定会话的所有聊天记录 /// [HttpGet("messages/{conversationId}")] public async Task> GetMessages(string conversationId) { var result = await _customerService.GetMessages(conversationId); if (result.isOk) return this.ToListResult(result.messages ?? new List()); else return this.ToListFailResult(result.msg); } /// /// 获取会话列表 - 获取所有会话或指定客服的会话 /// [HttpGet("conversations")] public async Task> GetConversations([FromQuery] string? customerId) { var result = await _customerService.GetConversations(customerId); if (result.isOk) return this.ToListResult(result.conversations ?? new List()); else return this.ToListFailResult(result.msg); } /// /// 获取当前用户的会话列表 - 获取当前登录用户的所有会话及聊天记录 /// [HttpGet("my-conversations")] public async Task> GetMyConversations() { var userId = loginUser.UserId; var result = await _customerService.GetUserConversations(userId); if (result.isOk) return this.ToListResult(result.conversations ?? new List()); else return this.ToListFailResult(result.msg); } /// /// 结束会话 - 关闭指定的会话 /// [HttpPost("end")] public async Task> EndConversation([FromBody] EndConversationParam param) { var result = await _customerService.EndConversation(param.ConversationId); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } /// /// 获取在线客服列表 - 获取当前所有在线的客服人员 /// [HttpGet("online")] public async Task> GetOnlineCustomers() { var result = await _customerService.GetOnlineCustomers(); if (result.isOk) return this.ToListResult(result.customers ?? new List()); else return this.ToListFailResult(result.msg); } /// /// 更新客服状态 - 设置客服的在线/离线状态 /// [HttpPost("status")] public async Task> UpdateCustomerStatus([FromBody] UpdateStatusParam param) { var result = await _customerService.UpdateCustomerStatus(param.UserId, param.Status); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } /// /// 标记会话已读 - 客服点击会话时调用,更新该用户所有会话的最后读取时间 /// [HttpPost("mark-read")] public async Task> MarkAsRead([FromBody] MarkReadParam param) { var result = await _customerService.MarkAsRead(param.CustomerId, param.UserId); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } /// /// 获取聊天记录 - 获取指定会话的所有消息 /// [HttpGet("chatlog/{conversationId}")] public async Task> GetChatLog(string conversationId) { var result = await _customerService.GetMessages(conversationId); if (result.isOk) return this.ToListResult(result.messages ?? new List()); else return this.ToListFailResult(result.msg); } } public class StartConversationParam { public string UserId { get; set; } = ""; public string UserName { get; set; } = ""; } public class SendMessageParam { public string ConversationId { get; set; } = ""; public int SenderType { get; set; } public string SenderId { get; set; } = ""; public string SenderName { get; set; } = ""; public string? SenderAvatar { get; set; } public int ContentType { get; set; } public string Content { get; set; } = ""; } public class EndConversationParam { public string ConversationId { get; set; } = ""; } public class UpdateStatusParam { public string UserId { get; set; } = ""; public int Status { get; set; } } public class MarkReadParam { public string CustomerId { get; set; } = ""; public string UserId { get; set; } = ""; }