2025-06-21 13:02:38 +08:00
|
|
|
|
using Mapster;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2025-06-21 13:15:14 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2025-06-21 13:02:38 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using Volo.Abp.Application.Dtos;
|
|
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
|
using Volo.Abp.Users;
|
|
|
|
|
|
using Yi.Framework.AiHub.Application.Contracts.Dtos;
|
|
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities;
|
2025-06-27 22:13:26 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
2025-06-21 13:02:38 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.AiHub.Application.Services;
|
|
|
|
|
|
|
|
|
|
|
|
public class MessageService : ApplicationService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ISqlSugarRepository<MessageAggregateRoot> _repository;
|
|
|
|
|
|
|
|
|
|
|
|
public MessageService(ISqlSugarRepository<MessageAggregateRoot> repository)
|
|
|
|
|
|
{
|
|
|
|
|
|
_repository = repository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询消息
|
|
|
|
|
|
/// 需要会话id
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Authorize]
|
2025-06-21 13:15:14 +08:00
|
|
|
|
public async Task<PagedResultDto<MessageDto>> GetListAsync([FromQuery]MessageGetListInput input)
|
2025-06-21 13:02:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
RefAsync<int> total = 0;
|
|
|
|
|
|
var userId = CurrentUser.GetId();
|
|
|
|
|
|
var entities = await _repository._DbQueryable
|
|
|
|
|
|
.Where(x => x.SessionId == input.SessionId)
|
|
|
|
|
|
.Where(x=>x.UserId == userId)
|
2026-01-29 14:40:03 +08:00
|
|
|
|
.Where(x => !x.IsHidden)
|
2025-06-27 22:13:26 +08:00
|
|
|
|
.OrderBy(x => x.Id)
|
2025-06-21 13:02:38 +08:00
|
|
|
|
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
|
|
|
|
|
return new PagedResultDto<MessageDto>(total, entities.Adapt<List<MessageDto>>());
|
|
|
|
|
|
}
|
2026-01-29 14:40:03 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除消息(软删除,标记为隐藏)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">删除参数,包含消息Id列表和是否删除后续消息的开关</param>
|
|
|
|
|
|
[Authorize]
|
2026-01-31 16:07:30 +08:00
|
|
|
|
public async Task DeleteAsync([FromQuery] MessageDeleteInput input)
|
2026-01-29 14:40:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
var userId = CurrentUser.GetId();
|
|
|
|
|
|
|
|
|
|
|
|
// 获取要删除的消息
|
|
|
|
|
|
var messages = await _repository._DbQueryable
|
|
|
|
|
|
.Where(x => input.Ids.Contains(x.Id))
|
|
|
|
|
|
.Where(x => x.UserId == userId)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (messages.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 标记当前消息为隐藏
|
|
|
|
|
|
var idsToHide = messages.Select(x => x.Id).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// 如果需要删除后续消息
|
|
|
|
|
|
if (input.IsDeleteSubsequent)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var message in messages)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取同一会话中时间大于当前消息的所有消息Id
|
|
|
|
|
|
var subsequentIds = await _repository._DbQueryable
|
|
|
|
|
|
.Where(x => x.SessionId == message.SessionId)
|
|
|
|
|
|
.Where(x => x.UserId == userId)
|
|
|
|
|
|
.Where(x => x.CreationTime > message.CreationTime)
|
|
|
|
|
|
.Where(x => !x.IsHidden)
|
|
|
|
|
|
.Select(x => x.Id)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
idsToHide.AddRange(subsequentIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
idsToHide = idsToHide.Distinct().ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量更新为隐藏状态
|
|
|
|
|
|
await _repository._Db.Updateable<MessageAggregateRoot>()
|
|
|
|
|
|
.SetColumns(x => x.IsHidden == true)
|
|
|
|
|
|
.Where(x => idsToHide.Contains(x.Id))
|
|
|
|
|
|
.ExecuteCommandAsync();
|
|
|
|
|
|
}
|
2025-06-21 13:02:38 +08:00
|
|
|
|
}
|