249 lines
9.4 KiB
C#
249 lines
9.4 KiB
C#
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_PostsService), ServiceLifetime = LifeTime.Transient)]
|
|
public class T_PostsService : BaseService<T_Posts>, IT_PostsService
|
|
{
|
|
/// <summary>
|
|
/// 查询论坛帖子列表
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
public PagedInfo<T_PostsDto> GetList(T_PostsQueryDto parm)
|
|
{
|
|
var predicate = QueryExp(parm);
|
|
|
|
var query = Queryable()
|
|
.Where(predicate.ToExpression())
|
|
.LeftJoin<T_Users>((p, u) => p.UserId == u.Id);
|
|
|
|
// 在 Select 之前进行排序,使用表别名
|
|
if (parm.Sort.IsNotEmpty())
|
|
{
|
|
var orderType = parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc;
|
|
query = ApplyOrderBy(query, parm.Sort, orderType);
|
|
}
|
|
else
|
|
{
|
|
// 默认排序
|
|
query = query.OrderBy((p, u) => p.Id, OrderByType.Desc);
|
|
}
|
|
|
|
var response = query.Select((p, u) => new T_PostsDto()
|
|
{
|
|
Id = p.Id,
|
|
UserId = p.UserId,
|
|
NickName = u.NickName,
|
|
Title = p.Title,
|
|
CoverImage = p.CoverImage,
|
|
Content = p.Content,
|
|
CategoryId = p.CategoryId,
|
|
ViewCount = p.ViewCount,
|
|
LikeCount = p.LikeCount,
|
|
CommentCount = p.CommentCount,
|
|
ShareCount = p.ShareCount,
|
|
IsTop = p.IsTop,
|
|
IsHot = p.IsHot,
|
|
IsEssence = p.IsEssence,
|
|
Status = p.Status,
|
|
PublishTime = p.PublishTime,
|
|
IsDeleted = p.IsDeleted,
|
|
DeletedAt = p.DeletedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
UpdatedAt = p.UpdatedAt,
|
|
}, true);
|
|
|
|
var resp = ToPage(response, parm);
|
|
|
|
// 批量查询并填充图片列表
|
|
if (resp.Result != null && resp.Result.Any())
|
|
{
|
|
var postIds = resp.Result.Select(x => x.Id).ToList();
|
|
var images = Context.Queryable<T_PostImages>()
|
|
.Where(x => postIds.Contains(x.PostId))
|
|
.OrderBy(x => x.SortOrder)
|
|
.ToList();
|
|
|
|
var imagesDict = images
|
|
.GroupBy(x => x.PostId)
|
|
.ToDictionary(
|
|
g => g.Key,
|
|
g => g.Select(x => new PostImageDto
|
|
{
|
|
Id = x.Id,
|
|
ImageUrl = x.ImageUrl,
|
|
SortOrder = x.SortOrder
|
|
}).ToList()
|
|
);
|
|
|
|
Context.ThenMapper(resp.Result, item =>
|
|
{
|
|
item.Images = imagesDict.GetValueOrDefault(item.Id, new List<PostImageDto>());
|
|
});
|
|
}
|
|
|
|
return resp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 应用排序(在 Join 查询中使用表别名)
|
|
/// </summary>
|
|
private ISugarQueryable<T_Posts, T_Users> ApplyOrderBy(ISugarQueryable<T_Posts, T_Users> query, string sortField, OrderByType orderType)
|
|
{
|
|
// 将 DTO 属性名映射到表字段表达式
|
|
return sortField.ToLower() switch
|
|
{
|
|
"id" => query.OrderBy((p, u) => p.Id, orderType),
|
|
"userid" => query.OrderBy((p, u) => p.UserId, orderType),
|
|
"nickname" => query.OrderBy((p, u) => u.NickName, orderType),
|
|
"title" => query.OrderBy((p, u) => p.Title, orderType),
|
|
"coverimage" => query.OrderBy((p, u) => p.CoverImage, orderType),
|
|
"content" => query.OrderBy((p, u) => p.Content, orderType),
|
|
"categoryid" => query.OrderBy((p, u) => p.CategoryId, orderType),
|
|
"viewcount" => query.OrderBy((p, u) => p.ViewCount, orderType),
|
|
"likecount" => query.OrderBy((p, u) => p.LikeCount, orderType),
|
|
"commentcount" => query.OrderBy((p, u) => p.CommentCount, orderType),
|
|
"sharecount" => query.OrderBy((p, u) => p.ShareCount, orderType),
|
|
"istop" => query.OrderBy((p, u) => p.IsTop, orderType),
|
|
"ishot" => query.OrderBy((p, u) => p.IsHot, orderType),
|
|
"isessence" => query.OrderBy((p, u) => p.IsEssence, orderType),
|
|
"status" => query.OrderBy((p, u) => p.Status, orderType),
|
|
"publishtime" => query.OrderBy((p, u) => p.PublishTime, orderType),
|
|
"isdeleted" => query.OrderBy((p, u) => p.IsDeleted, orderType),
|
|
"deletedat" => query.OrderBy((p, u) => p.DeletedAt, orderType),
|
|
"createdat" => query.OrderBy((p, u) => p.CreatedAt, orderType),
|
|
"updatedat" => query.OrderBy((p, u) => p.UpdatedAt, orderType),
|
|
_ => query.OrderBy((p, u) => p.Id, OrderByType.Desc) // 默认排序
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取列表
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="source">查询表单式</param>
|
|
/// <param name="parm">分页参数</param>
|
|
/// <returns></returns>
|
|
public 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_Posts GetInfo(long Id)
|
|
{
|
|
var response = Queryable()
|
|
.Where(x => x.Id == Id)
|
|
.First();
|
|
|
|
return response;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加论坛帖子
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public T_Posts AddT_Posts(T_Posts model)
|
|
{
|
|
return Insertable(model).ExecuteReturnEntity();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改论坛帖子
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public int UpdateT_Posts(T_Posts model)
|
|
{
|
|
return Update(model, true, "修改论坛帖子");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出论坛帖子
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
public PagedInfo<T_PostsDto> ExportList(T_PostsQueryDto parm)
|
|
{
|
|
parm.PageNum = 1;
|
|
parm.PageSize = 100000;
|
|
var predicate = QueryExp(parm);
|
|
|
|
var query = Queryable()
|
|
.Where(predicate.ToExpression())
|
|
.LeftJoin<T_Users>((p, u) => p.UserId == u.Id);
|
|
|
|
// 导出时使用默认排序
|
|
query = query.OrderBy((p, u) => p.Id, OrderByType.Desc);
|
|
|
|
var response = query.Select((p, u) => new T_PostsDto()
|
|
{
|
|
Id = p.Id,
|
|
UserId = p.UserId,
|
|
NickName = u.NickName,
|
|
Title = p.Title,
|
|
CoverImage = p.CoverImage,
|
|
Content = p.Content,
|
|
CategoryId = p.CategoryId,
|
|
ViewCount = p.ViewCount,
|
|
LikeCount = p.LikeCount,
|
|
CommentCount = p.CommentCount,
|
|
ShareCount = p.ShareCount,
|
|
IsTop = p.IsTop,
|
|
IsHot = p.IsHot,
|
|
IsEssence = p.IsEssence,
|
|
Status = p.Status,
|
|
PublishTime = p.PublishTime,
|
|
IsDeleted = p.IsDeleted,
|
|
DeletedAt = p.DeletedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
UpdatedAt = p.UpdatedAt,
|
|
//IsTopLabel = p.IsTop.GetConfigValue<Model.System.SysDictData>("liveforum_action_bool"),
|
|
//StatusLabel = p.Status.GetConfigValue<Model.System.SysDictData>("liveforum_posts_status"),
|
|
}, true);
|
|
var resp = ToPage(response, parm);
|
|
|
|
return resp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询导出表达式
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
private static Expressionable<T_Posts> QueryExp(T_PostsQueryDto parm)
|
|
{
|
|
var predicate = Expressionable.Create<T_Posts>();
|
|
|
|
predicate = predicate.AndIF(parm.UserId != null, it => it.UserId == parm.UserId);
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Title), it => it.Title == parm.Title || it.Content.Contains(parm.Title));
|
|
predicate = predicate.AndIF(parm.Status != null, it => it.Status == parm.Status);
|
|
predicate = predicate.AndIF(parm.IsDeleted != null, it => it.IsDeleted == parm.IsDeleted);
|
|
return predicate;
|
|
}
|
|
}
|
|
} |