namespace Admin.NET.Core.Ai.Services.DataBase;
using Admin.NET.Core.Ai.Entitys;
using Admin.NET.Core.Ai.Enums;
using Admin.NET.Core.Ai.Services.DataBase.Dto;
///
/// 异步对数据库进行操作
///
public class ChatChannelActionService : IScoped
{
private readonly ILogger _logger;
private readonly SqlSugarRepository _chatSummaryHistoryService;
private readonly SqlSugarRepository _chatHistoryService;
///
/// 构造函数
///
///
///
///
public ChatChannelActionService(ILogger logger,
SqlSugarRepository chatSummaryHistoryService,
SqlSugarRepository chatHistoryService)
{
_logger = logger;
_chatSummaryHistoryService = chatSummaryHistoryService;
_chatHistoryService = chatHistoryService;
}
///
/// 追加聊天记录
///
public async Task AppendAsync(LLMChatSummaryHistory chatSummaryHistory)
{
await _chatSummaryHistoryService.Context.CopyNew()
.InsertNav(chatSummaryHistory)
.Include(z => z.Histories)
.ExecuteCommandAsync();
}
///
/// 追加聊天明细
///
public async Task AppendItemAsync(LLMChatSummaryHistory chatSummaryHistory)
{
chatSummaryHistory.Histories.ForEach(item => item.SummaryId = chatSummaryHistory.Id);
await _chatHistoryService.Context.CopyNew()
.Insertable(chatSummaryHistory.Histories)
.ExecuteCommandAsync();
}
///
/// 删除所有聊天记录
///
public async Task DeleteAllAsync(LLMChatSummaryHistory chatSummaryHistory)
{
await _chatSummaryHistoryService.Context.CopyNew()
.DeleteNav(u => u.Id == chatSummaryHistory.Id)
.Include(u => u.Histories)
.ExecuteCommandAsync();
}
///
/// 重命名摘要
///
public async Task RenameSummaryAsync(LLMChatSummaryHistory chatSummaryHistory)
{
var oldChatSummaryHistory = await _chatSummaryHistoryService.CopyNew()
.AsQueryable()
.Where(u => u.Id == chatSummaryHistory.Id)
.FirstAsync();
oldChatSummaryHistory.Summary = chatSummaryHistory.Summary;
await _chatSummaryHistoryService.Context.CopyNew()
.Updateable(oldChatSummaryHistory)
.ExecuteCommandAsync();
}
}