ZrAdminNetCore/ZR.Service/Liveforum/T_CommentsService.cs
2025-11-16 20:18:15 +08:00

128 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.LiveForum.Model.Liveforum.Dto;
using ZR.LiveForum.Model.Liveforum;
using ZR.Repository;
using ZR.Service.Liveforum.ILiveforumService;
namespace ZR.Service.Liveforum
{
/// <summary>
/// 帖子评论记录Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IT_CommentsService), ServiceLifetime = LifeTime.Transient)]
public class T_CommentsService : BaseService<T_Comments>, IT_CommentsService
{
/// <summary>
/// 查询帖子评论记录列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<T_CommentsDto> GetList(T_CommentsQueryDto parm)
{
var predicate = QueryExp(parm);
var query = Queryable()
.Where(predicate.ToExpression())
.LeftJoin<T_Users>((c, u) => c.UserId == u.Id)
.LeftJoin<T_Users>((c, u, ru) => c.ReplyToUserId == ru.Id)
.LeftJoin<T_Posts>((c, u, ru, p) => c.PostId == p.Id)
.LeftJoin<T_Comments>((c, u, ru, p, pc) => c.ParentCommentId == pc.Id);
// 排序:顶级评论在前,回复评论在后,同层级按创建时间倒序
// 使用多个OrderBy组合排序
query = query.OrderBy((c, u, ru, p, pc) => c.ParentCommentId == null ? 0 : 1, OrderByType.Asc)
.OrderBy((c, u, ru, p, pc) => c.CreatedAt, OrderByType.Desc);
var response = query.Select((c, u, ru, p, pc) => new T_CommentsDto()
{
Id = c.Id,
PostId = c.PostId,
UserId = c.UserId,
ParentCommentId = c.ParentCommentId,
ReplyToUserId = c.ReplyToUserId,
Content = c.Content,
LikeCount = c.LikeCount,
ReplyCount = c.ReplyCount,
Status = c.Status,
IpAddress = c.IpAddress,
CreatedAt = c.CreatedAt,
UpdatedAt = c.UpdatedAt,
IsDeleted = c.IsDeleted,
DeletedAt = c.DeletedAt,
UserName = u.NickName,
ReplyToUserName = ru.NickName,
PostTitle = p.Title,
ParentCommentContent = pc.Content,
}, true);
var resp = ToPage(response, parm);
return resp;
}
/// <summary>
/// 分页查询排序已在Select之前完成
/// </summary>
private PagedInfo<T> ToPage<T>(ISugarQueryable<T> source, PagerInfo parm)
{
var page = new PagedInfo<T>();
var total = 0;
page.PageSize = parm.PageSize;
page.PageIndex = parm.PageNum;
// 注意:排序已经在 Select 之前完成了,这里不再需要排序
page.Result = source
.ToPageList(parm.PageNum, parm.PageSize, ref total);
page.TotalNum = total;
return page;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public T_Comments GetInfo(long Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加帖子评论记录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public T_Comments AddT_Comments(T_Comments model)
{
return Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改帖子评论记录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateT_Comments(T_Comments model)
{
return Update(model, true);
}
/// <summary>
/// 查询导出表达式
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
private static Expressionable<T_Comments> QueryExp(T_CommentsQueryDto parm)
{
var predicate = Expressionable.Create<T_Comments>();
predicate = predicate.AndIF(parm.PostId != null, it => it.PostId == parm.PostId);
predicate = predicate.AndIF(parm.UserId != null, it => it.UserId == parm.UserId);
return predicate;
}
}
}