208 lines
7.5 KiB
C#
208 lines
7.5 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using AdminDomain.Customer;
|
||
|
|
using AdminDomain.Customer.Param;
|
||
|
|
using CustomerService.Domain.Entity;
|
||
|
|
using MongoDB.Driver;
|
||
|
|
using MongoRepository;
|
||
|
|
|
||
|
|
namespace AdminService.Customer
|
||
|
|
{
|
||
|
|
public class CustomerService : ICustomerService
|
||
|
|
{
|
||
|
|
private readonly IMongoBaseRepository<CustomerEntity> _customerRepo;
|
||
|
|
private readonly IMongoBaseRepository<ConversationEntity> _conversationRepo;
|
||
|
|
private readonly IMongoBaseRepository<MessageEntity> _messageRepo;
|
||
|
|
|
||
|
|
public CustomerService(
|
||
|
|
IMongoBaseRepository<CustomerEntity> customerRepo,
|
||
|
|
IMongoBaseRepository<ConversationEntity> conversationRepo,
|
||
|
|
IMongoBaseRepository<MessageEntity> messageRepo)
|
||
|
|
{
|
||
|
|
_customerRepo = customerRepo;
|
||
|
|
_conversationRepo = conversationRepo;
|
||
|
|
_messageRepo = messageRepo;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg, CustomerEntity? customer)> AddCustomer(AddCustomerParam param)
|
||
|
|
{
|
||
|
|
var allCustomers = await _customerRepo.FilterByAsync(x => true);
|
||
|
|
var existCustomer = allCustomers.FirstOrDefault(x => x.UserId == param.UserId);
|
||
|
|
if (existCustomer != null)
|
||
|
|
{
|
||
|
|
return (false, "该用户已是客服", null);
|
||
|
|
}
|
||
|
|
|
||
|
|
var customer = new CustomerEntity
|
||
|
|
{
|
||
|
|
UserId = param.UserId,
|
||
|
|
UserName = param.UserName,
|
||
|
|
Avatar = param.Avatar,
|
||
|
|
Status = 0,
|
||
|
|
CreateTime = DateTime.Now
|
||
|
|
};
|
||
|
|
|
||
|
|
await _customerRepo.InsertOneAsync(customer);
|
||
|
|
return (true, "添加成功", customer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg)> UpdateCustomer(UpdateCustomerParam param)
|
||
|
|
{
|
||
|
|
var customer = await _customerRepo.FindByIdAsync(param.Id);
|
||
|
|
if (customer == null)
|
||
|
|
{
|
||
|
|
return (false, "客服不存在");
|
||
|
|
}
|
||
|
|
|
||
|
|
var filter = Builders<CustomerEntity>.Filter.Eq(x => x.Id, param.Id);
|
||
|
|
var update = Builders<CustomerEntity>.Update
|
||
|
|
.Set(x => x.UserName, param.UserName)
|
||
|
|
.Set(x => x.Avatar, param.Avatar)
|
||
|
|
.Set(x => x.Status, param.Status);
|
||
|
|
|
||
|
|
await _customerRepo.UpdateByIdAsync(param.Id, update);
|
||
|
|
return (true, "更新成功");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg)> DeleteCustomer(string id)
|
||
|
|
{
|
||
|
|
var customer = await _customerRepo.FindByIdAsync(id);
|
||
|
|
if (customer == null)
|
||
|
|
{
|
||
|
|
return (false, "客服不存在");
|
||
|
|
}
|
||
|
|
|
||
|
|
await _customerRepo.DeleteByIdAsync(id);
|
||
|
|
return (true, "删除成功");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg, List<CustomerEntity>? customers, int total)> GetCustomerList(GetCustomerListParam param)
|
||
|
|
{
|
||
|
|
var list = await _customerRepo.FilterByAsync(x => true);
|
||
|
|
|
||
|
|
if (!string.IsNullOrEmpty(param.UserName))
|
||
|
|
{
|
||
|
|
list = list.Where(x => x.UserName != null && x.UserName.Contains(param.UserName)).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (param.Status.HasValue)
|
||
|
|
{
|
||
|
|
list = list.Where(x => x.Status == param.Status.Value).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
var total = list.Count;
|
||
|
|
var pagedList = list.Skip((param.PageIndex - 1) * param.PageSize).Take(param.PageSize).ToList();
|
||
|
|
|
||
|
|
return (true, "获取成功", pagedList, total);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg, CustomerEntity? customer)> GetCustomerById(string id)
|
||
|
|
{
|
||
|
|
var customer = await _customerRepo.FindByIdAsync(id);
|
||
|
|
if (customer == null)
|
||
|
|
{
|
||
|
|
return (false, "客服不存在", null);
|
||
|
|
}
|
||
|
|
return (true, "获取成功", customer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg, ConversationEntity? conversation)> GetConversationById(string id)
|
||
|
|
{
|
||
|
|
var conversation = await _conversationRepo.FindByIdAsync(id);
|
||
|
|
if (conversation == null)
|
||
|
|
{
|
||
|
|
return (false, "会话不存在", null);
|
||
|
|
}
|
||
|
|
return (true, "获取成功", conversation);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg, List<ConversationEntity>? conversations, int total)> GetConversationList(GetConversationListParam param)
|
||
|
|
{
|
||
|
|
var list = await _conversationRepo.FilterByAsync(x => true);
|
||
|
|
|
||
|
|
if (!string.IsNullOrEmpty(param.UserId))
|
||
|
|
{
|
||
|
|
list = list.Where(x => x.UserId == param.UserId).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!string.IsNullOrEmpty(param.CustomerId))
|
||
|
|
{
|
||
|
|
list = list.Where(x => x.CustomerId == param.CustomerId).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (param.Status.HasValue)
|
||
|
|
{
|
||
|
|
list = list.Where(x => x.Status == param.Status.Value).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
var total = list.Count;
|
||
|
|
var pagedList = list.Skip((param.PageIndex - 1) * param.PageSize).Take(param.PageSize).ToList();
|
||
|
|
|
||
|
|
return (true, "获取成功", pagedList, total);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg)> DeleteConversation(string conversationId)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(conversationId))
|
||
|
|
return (false, "会话ID不能为空");
|
||
|
|
|
||
|
|
var conversation = await _conversationRepo.FindByIdAsync(conversationId);
|
||
|
|
if (conversation == null)
|
||
|
|
return (false, "会话不存在");
|
||
|
|
|
||
|
|
var messages = await _messageRepo.FilterByAsync(x => x.ConversationId == conversationId);
|
||
|
|
foreach (var message in messages)
|
||
|
|
await _messageRepo.DeleteByIdAsync(message.Id);
|
||
|
|
|
||
|
|
await _conversationRepo.DeleteByIdAsync(conversationId);
|
||
|
|
return (true, "删除成功");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg, List<MessageEntity>? messages)> GetChatLog(string conversationId)
|
||
|
|
{
|
||
|
|
var conversation = await _conversationRepo.FindByIdAsync(conversationId);
|
||
|
|
if (conversation == null)
|
||
|
|
{
|
||
|
|
return (false, "会话不存在", null);
|
||
|
|
}
|
||
|
|
|
||
|
|
var allMessages = await _messageRepo.FilterByAsync(x => true);
|
||
|
|
var messages = allMessages.Where(x => x.ConversationId == conversationId).ToList();
|
||
|
|
return (true, "获取成功", messages);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg)> DeleteMessage(string id)
|
||
|
|
{
|
||
|
|
var message = await _messageRepo.FindByIdAsync(id);
|
||
|
|
if (message == null)
|
||
|
|
{
|
||
|
|
return (false, "消息不存在");
|
||
|
|
}
|
||
|
|
|
||
|
|
await _messageRepo.DeleteByIdAsync(id);
|
||
|
|
return (true, "删除成功");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg)> DeleteAllMessages(string conversationId)
|
||
|
|
{
|
||
|
|
var messages = await _messageRepo.FilterByAsync(x => x.ConversationId == conversationId);
|
||
|
|
foreach (var msg in messages)
|
||
|
|
{
|
||
|
|
await _messageRepo.DeleteByIdAsync(msg.Id);
|
||
|
|
}
|
||
|
|
return (true, "删除成功");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(bool isOk, string msg)> DeleteMessagesBatch(List<string> ids)
|
||
|
|
{
|
||
|
|
foreach (var id in ids)
|
||
|
|
{
|
||
|
|
await _messageRepo.DeleteByIdAsync(id);
|
||
|
|
}
|
||
|
|
return (true, "批量删除成功");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|