Files
BuYuHouDuan/AdminSrc/AdminApi/Customer/CustomerController.cs
T

245 lines
8.1 KiB
C#
Raw Normal View History

2026-10-12 16:22:04 +08:00
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<PageResponse<CustomerDto>> GetCustomerList([FromQuery] GetCustomerListParam param)
{
var result = await _customerService.GetCustomerList(param);
var dtos = MapToCustomerDtos(result.customers);
return this.ToPageResult(dtos, result.total);
}
[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);
}
[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);
}
}
[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);
}
}
[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);
}
}
[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);
}
[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);
}
[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);
}
[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);
}
}
[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);
}
}
[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);
}
}
[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;
}
}
}