281 lines
9.1 KiB
C#
281 lines
9.1 KiB
C#
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客服列表
|
|
/// </summary>
|
|
[HttpGet("list")]
|
|
public async Task<PageResponse<CustomerDto>> GetCustomerList([FromQuery] GetCustomerListParam param)
|
|
{
|
|
var result = await _customerService.GetCustomerList(param);
|
|
var dtos = MapToCustomerDtos(result.customers);
|
|
return this.ToPageResult(dtos, result.total);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取客服详情
|
|
/// </summary>
|
|
[HttpGet("{id}")]
|
|
public async Task<BaseResponse<CustomerDto>> GetCustomerById(string id)
|
|
{
|
|
var result = await _customerService.GetCustomerById(id);
|
|
var dto = MapToCustomerDto(result.customer);
|
|
return this.ToResult(dto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加新客服
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse<CustomerDto>> 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<CustomerDto>(result.msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新客服信息
|
|
/// </summary>
|
|
[HttpPut]
|
|
public async Task<BaseResponse<string>> UpdateCustomer([FromBody] UpdateCustomerParam param)
|
|
{
|
|
var result = await _customerService.UpdateCustomer(param);
|
|
if (result.isOk)
|
|
{
|
|
return this.ToResult(result.msg);
|
|
}
|
|
else
|
|
{
|
|
return this.ToFailResult<string>(result.msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除客服
|
|
/// </summary>
|
|
[HttpDelete("{id}")]
|
|
public async Task<BaseResponse<string>> DeleteCustomer(string id)
|
|
{
|
|
var result = await _customerService.DeleteCustomer(id);
|
|
if (result.isOk)
|
|
{
|
|
return this.ToResult(result.msg);
|
|
}
|
|
else
|
|
{
|
|
return this.ToFailResult<string>(result.msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取会话列表
|
|
/// </summary>
|
|
[HttpGet("conversations")]
|
|
public async Task<PageResponse<ConversationDto>> GetConversationList([FromQuery] GetConversationListParam param)
|
|
{
|
|
var result = await _customerService.GetConversationList(param);
|
|
var dtos = MapToConversationDtos(result.conversations);
|
|
return this.ToPageResult(dtos, result.total);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取会话详情
|
|
/// </summary>
|
|
[HttpGet("conversation/{id}")]
|
|
public async Task<BaseResponse<ConversationDto>> GetConversationById(string id)
|
|
{
|
|
var result = await _customerService.GetConversationById(id);
|
|
var dto = MapToConversationDto(result.conversation);
|
|
return this.ToResult(dto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除会话及全部关联聊天记录。
|
|
/// </summary>
|
|
[HttpDelete("conversation/{id}")]
|
|
public async Task<BaseResponse<string>> DeleteConversation(string id)
|
|
{
|
|
var result = await _customerService.DeleteConversation(id);
|
|
return result.isOk
|
|
? this.ToResult(result.msg)
|
|
: this.ToFailResult<string>(result.msg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取会话聊天记录
|
|
/// </summary>
|
|
[HttpGet("chatlog/{conversationId}")]
|
|
public async Task<BaseResponse<List<MessageDto>>> 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<List<MessageDto>>(result.msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除单条消息
|
|
/// </summary>
|
|
[HttpDelete("message/{id}")]
|
|
public async Task<BaseResponse<string>> DeleteMessage(string id)
|
|
{
|
|
var result = await _customerService.DeleteMessage(id);
|
|
if (result.isOk)
|
|
{
|
|
return this.ToResult(result.msg);
|
|
}
|
|
else
|
|
{
|
|
return this.ToFailResult<string>(result.msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除会话所有消息
|
|
/// </summary>
|
|
[HttpDelete("messages/{conversationId}")]
|
|
public async Task<BaseResponse<string>> DeleteAllMessages(string conversationId)
|
|
{
|
|
var result = await _customerService.DeleteAllMessages(conversationId);
|
|
if (result.isOk)
|
|
{
|
|
return this.ToResult(result.msg);
|
|
}
|
|
else
|
|
{
|
|
return this.ToFailResult<string>(result.msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除消息
|
|
/// </summary>
|
|
[HttpPost("messages/delete-batch")]
|
|
public async Task<BaseResponse<string>> DeleteMessagesBatch([FromBody] DeleteMessagesBatchRequest param)
|
|
{
|
|
var result = await _customerService.DeleteMessagesBatch(param.Ids);
|
|
if (result.isOk)
|
|
{
|
|
return this.ToResult(result.msg);
|
|
}
|
|
else
|
|
{
|
|
return this.ToFailResult<string>(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<CustomerDto> MapToCustomerDtos(IEnumerable<CustomerService.Domain.Entity.CustomerEntity> entities)
|
|
{
|
|
var dtos = new List<CustomerDto>();
|
|
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<ConversationDto> MapToConversationDtos(IEnumerable<CustomerService.Domain.Entity.ConversationEntity> entities)
|
|
{
|
|
var dtos = new List<ConversationDto>();
|
|
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<MessageDto> MapToMessageDtos(IEnumerable<CustomerService.Domain.Entity.MessageEntity> entities)
|
|
{
|
|
var dtos = new List<MessageDto>();
|
|
foreach (var entity in entities)
|
|
{
|
|
dtos.Add(MapToMessageDto(entity));
|
|
}
|
|
return dtos;
|
|
}
|
|
}
|
|
}
|