Files
BuYuHouDuan/CustomerSrc/CustomerApi/Controllers/CustomerController.cs
T
2026-10-12 16:22:04 +08:00

158 lines
5.2 KiB
C#

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<BaseResponse<ConversationEntity>> 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<ConversationEntity>(result.msg);
}
[HttpPost("message")]
public async Task<BaseResponse<string>> 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<string>(result.msg);
}
[HttpGet("messages/{conversationId}")]
public async Task<ListResponse<MessageEntity>> GetMessages(string conversationId)
{
var result = await _customerService.GetMessages(conversationId);
if (result.isOk)
return this.ToListResult(result.messages ?? new List<MessageEntity>());
else
return this.ToListFailResult<MessageEntity>(result.msg);
}
[HttpGet("conversations")]
public async Task<ListResponse<ConversationResponse>> GetConversations([FromQuery] string? customerId)
{
var result = await _customerService.GetConversations(customerId);
if (result.isOk)
return this.ToListResult(result.conversations ?? new List<ConversationResponse>());
else
return this.ToListFailResult<ConversationResponse>(result.msg);
}
[HttpGet("my-conversations")]
public async Task<ListResponse<ConversationResponse>> GetMyConversations()
{
var userId = loginUser.UserId;
var result = await _customerService.GetUserConversations(userId);
if (result.isOk)
return this.ToListResult(result.conversations ?? new List<ConversationResponse>());
else
return this.ToListFailResult<ConversationResponse>(result.msg);
}
[HttpPost("end")]
public async Task<BaseResponse<string>> EndConversation([FromBody] EndConversationParam param)
{
var result = await _customerService.EndConversation(param.ConversationId);
if (result.isOk)
return this.ToResult(result.msg);
else
return this.ToFailResult<string>(result.msg);
}
[HttpGet("online")]
public async Task<ListResponse<CustomerEntity>> GetOnlineCustomers()
{
var result = await _customerService.GetOnlineCustomers();
if (result.isOk)
return this.ToListResult(result.customers ?? new List<CustomerEntity>());
else
return this.ToListFailResult<CustomerEntity>(result.msg);
}
[HttpPost("status")]
public async Task<BaseResponse<string>> 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<string>(result.msg);
}
[HttpPost("mark-read")]
public async Task<BaseResponse<string>> 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<string>(result.msg);
}
[HttpGet("chatlog/{conversationId}")]
public async Task<ListResponse<MessageEntity>> GetChatLog(string conversationId)
{
var result = await _customerService.GetMessages(conversationId);
if (result.isOk)
return this.ToListResult(result.messages ?? new List<MessageEntity>());
else
return this.ToListFailResult<MessageEntity>(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; } = "";
}