网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
namespace Dpz.Core.Service.RepositoryServiceImpl;

public class ChatRecordService(
    IRepository<ChatRecord> repository,
    IMapper mapper,
    IConfiguration configuration) : IChatRecordService
{
    public async Task SaveFriendRecordAsync(VmUserInfo sender, string message, VmUserInfo receiver)
    {
        var viewModel = new VmChatRecord
        {
            Type = "friend",
            Message = message,
            Receiver = receiver,
            ReceivGroup = null,
            Sender = sender
        };
        var entity = mapper.Map<ChatRecord>(viewModel);
        await repository.InsertAsync(entity);
    }

    public async Task SaveGroupRecordAsync(VmUserInfo sender, string groupId, string message)
    {
        var viewModel = new VmChatRecord
        {
            Type = "group",
            Message = message,
            Receiver = null,
            Sender = sender,
            ReceivGroup = groupId
        };
        var entity = mapper.Map<ChatRecord>(viewModel);
        await repository.InsertAsync(entity);
    }

    public async Task SaveRobotRecordAsync(VmUserInfo? sender, string message, string receiveMessage)
    {
        var receiver = new VmUserInfo
        {
            Avatar = $"{configuration["CDNBaseAddress"]}/images/ai.jpg",
            Id = "kefu",
            Sign = "星火认知大模型 Spark3.5 Max",
            Name = "星火认知大模型"
        };
        var send = new VmChatRecord
        {
            Type = "robot",
            Message = message,
            Receiver = receiver,
            Sender = sender,
            ReceivGroup = null
        };
        var receive = new VmChatRecord
        {
            Type = "robot",
            Message = receiveMessage,
            Receiver = sender,
            Sender = receiver,
            ReceivGroup = null
        };
        var entity = mapper.Map<ChatRecord>(send);
        await repository.InsertAsync(entity);
        var entity2 = mapper.Map<ChatRecord>(receive);
        await repository.InsertAsync(entity2);
    }

    public async Task<IPagedList<VmChatRecord>> GetFriendRecordAsync(string sender, string receiver, int pageIndex,
        int pageSize)
    {
        var source = await repository.SearchFor(x =>
                x.Sender != null && x.Receiver != null &&
                ((x.Sender.Id == sender && x.Receiver.Id == receiver) ||
                 (x.Sender.Id == receiver && x.Receiver.Id == sender)) && x.Type == "friend")
            .OrderByDescending(x => x.SendTime)
            .ToPagedListAsync<ChatRecord, VmChatRecord>(pageIndex, pageSize);
        return source;
    }

    public async Task<IPagedList<VmChatRecord>> GetRobotRecordAsync(string sender, int pageIndex, int pageSize)
    {
        var source = await repository
            .SearchFor(x =>
                x.Sender != null && x.Receiver != null &&
                ((x.Sender.Id == sender && x.Receiver.Id == "kefu") ||
                 (x.Sender.Id == "kefu" && x.Receiver.Id == sender)) && x.Type == "robot")
            .OrderByDescending(x => x.SendTime)
            .ToPagedListAsync<ChatRecord, VmChatRecord>(pageIndex, pageSize);
        return source;
    }

    public async Task<IPagedList<VmChatRecord>> GetGroupRecordAsync(string groupId, int pageIndex, int pageSize)
    {
        if (!ObjectId.TryParse(groupId, out var objGroupId))
        {
            return new PagedList<VmChatRecord>(new List<VmChatRecord>(), pageIndex, pageSize);
        }

        var source = await repository
            .SearchFor(x => x.ReceivGroup != null && x.ReceivGroup.Value == objGroupId)
            .OrderByDescending(x => x.SendTime)
            .ToPagedListAsync<ChatRecord, VmChatRecord>(pageIndex, pageSize);
        return source;
    }
}
loading