using System.Collections.Generic; using System.Threading.Tasks; using AdminApplication.DTOs; using AdminDomain.Customer; using AdminDomain.Customer.Param; using Common; using CommonInfrastructure; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace AdminApi.Customer { [ApiController] [Route("adminapi/[controller]")] [Authorize] public class CustomerController : BaseController { private readonly ICustomerService _customerService; public CustomerController(ICustomerService customerService) { _customerService = customerService; } /// /// 获取客服列表 /// [HttpGet("list")] public async Task> GetCustomerList([FromQuery] GetCustomerListParam param) { var result = await _customerService.GetCustomerList(param); var dtos = MapToCustomerDtos(result.customers); return this.ToPageResult(dtos, result.total); } /// /// 根据ID获取客服详情 /// [HttpGet("{id}")] public async Task> GetCustomerById(string id) { var result = await _customerService.GetCustomerById(id); var dto = MapToCustomerDto(result.customer); return this.ToResult(dto); } /// /// 添加新客服 /// [HttpPost] public async Task> AddCustomer([FromBody] AddCustomerParam param) { var result = await _customerService.AddCustomer(param); if (result.isOk) { var dto = MapToCustomerDto(result.customer); return this.ToResult(dto); } else { return this.ToFailResult(result.msg); } } /// /// 更新客服信息 /// [HttpPut] public async Task> UpdateCustomer([FromBody] UpdateCustomerParam param) { var result = await _customerService.UpdateCustomer(param); if (result.isOk) { return this.ToResult(result.msg); } else { return this.ToFailResult(result.msg); } } /// /// 删除客服 /// [HttpDelete("{id}")] public async Task> DeleteCustomer(string id) { var result = await _customerService.DeleteCustomer(id); if (result.isOk) { return this.ToResult(result.msg); } else { return this.ToFailResult(result.msg); } } /// /// 获取会话列表 /// [HttpGet("conversations")] public async Task> GetConversationList([FromQuery] GetConversationListParam param) { var result = await _customerService.GetConversationList(param); var dtos = MapToConversationDtos(result.conversations); return this.ToPageResult(dtos, result.total); } /// /// 根据ID获取会话详情 /// [HttpGet("conversation/{id}")] public async Task> GetConversationById(string id) { var result = await _customerService.GetConversationById(id); var dto = MapToConversationDto(result.conversation); return this.ToResult(dto); } /// /// 删除会话及全部关联聊天记录。 /// [HttpDelete("conversation/{id}")] public async Task> DeleteConversation(string id) { var result = await _customerService.DeleteConversation(id); return result.isOk ? this.ToResult(result.msg) : this.ToFailResult(result.msg); } /// /// 获取会话聊天记录 /// [HttpGet("chatlog/{conversationId}")] public async Task>> GetChatLog(string conversationId) { var result = await _customerService.GetChatLog(conversationId); if (result.isOk) { var dtos = MapToMessageDtos(result.messages); return this.ToResult(dtos); } else { return this.ToFailResult>(result.msg); } } /// /// 删除单条消息 /// [HttpDelete("message/{id}")] public async Task> DeleteMessage(string id) { var result = await _customerService.DeleteMessage(id); if (result.isOk) { return this.ToResult(result.msg); } else { return this.ToFailResult(result.msg); } } /// /// 删除会话所有消息 /// [HttpDelete("messages/{conversationId}")] public async Task> DeleteAllMessages(string conversationId) { var result = await _customerService.DeleteAllMessages(conversationId); if (result.isOk) { return this.ToResult(result.msg); } else { return this.ToFailResult(result.msg); } } /// /// 批量删除消息 /// [HttpPost("messages/delete-batch")] public async Task> DeleteMessagesBatch([FromBody] DeleteMessagesBatchRequest param) { var result = await _customerService.DeleteMessagesBatch(param.Ids); if (result.isOk) { return this.ToResult(result.msg); } else { return this.ToFailResult(result.msg); } } private static CustomerDto MapToCustomerDto(CustomerService.Domain.Entity.CustomerEntity? entity) { if (entity == null) return null!; return new CustomerDto { Id = entity.Id, UserId = entity.UserId, UserName = entity.UserName, Status = entity.Status, Avatar = entity.Avatar, CreateTime = entity.CreateTime }; } private static List MapToCustomerDtos(IEnumerable entities) { var dtos = new List(); foreach (var entity in entities) { dtos.Add(MapToCustomerDto(entity)); } return dtos; } private static ConversationDto MapToConversationDto(CustomerService.Domain.Entity.ConversationEntity? entity) { if (entity == null) return null!; return new ConversationDto { Id = entity.Id, UserId = entity.UserId, UserName = entity.UserName, UserAvatar = entity.UserAvatar, CustomerId = entity.CustomerId, CustomerName = entity.CustomerName, CustomerAvatar = entity.CustomerAvatar, Status = entity.Status, CreateTime = entity.CreateTime, EndTime = entity.EndTime }; } private static List MapToConversationDtos(IEnumerable entities) { var dtos = new List(); foreach (var entity in entities) { dtos.Add(MapToConversationDto(entity)); } return dtos; } private static MessageDto MapToMessageDto(CustomerService.Domain.Entity.MessageEntity entity) { return new MessageDto { Id = entity.Id, ConversationId = entity.ConversationId, SenderType = entity.SenderType, SenderId = entity.SenderId, SenderName = entity.SenderName, SenderAvatar = entity.SenderAvatar, ContentType = entity.ContentType, Content = entity.Content, CreateTime = entity.CreateTime }; } private static List MapToMessageDtos(IEnumerable entities) { var dtos = new List(); foreach (var entity in entities) { dtos.Add(MapToMessageDto(entity)); } return dtos; } } }