diff --git a/ZR.LiveForum.Model/Liveforum/Dto/T_PostsDto.cs b/ZR.LiveForum.Model/Liveforum/Dto/T_PostsDto.cs index 1214df3..aab9d36 100644 --- a/ZR.LiveForum.Model/Liveforum/Dto/T_PostsDto.cs +++ b/ZR.LiveForum.Model/Liveforum/Dto/T_PostsDto.cs @@ -27,6 +27,10 @@ namespace ZR.LiveForum.Model.Liveforum.Dto [ExcelColumnName("用户ID")] public long UserId { get; set; } + [ExcelColumn(Name = "用户昵称")] + [ExcelColumnName("用户昵称")] + public string? NickName { get; set; } + [Required(ErrorMessage = "标题不能为空")] [ExcelColumn(Name = "标题")] [ExcelColumnName("标题")] diff --git a/ZR.Repository/BaseRepository.cs b/ZR.Repository/BaseRepository.cs index f78e075..4c8c5f4 100644 --- a/ZR.Repository/BaseRepository.cs +++ b/ZR.Repository/BaseRepository.cs @@ -1,4 +1,4 @@ -using Infrastructure; +using Infrastructure; using Infrastructure.Extensions; using Mapster; using SqlSugar; diff --git a/ZR.Service/Liveforum/T_PostsService.cs b/ZR.Service/Liveforum/T_PostsService.cs index a7d3927..493a9c9 100644 --- a/ZR.Service/Liveforum/T_PostsService.cs +++ b/ZR.Service/Liveforum/T_PostsService.cs @@ -22,14 +22,104 @@ namespace ZR.Service.Liveforum { var predicate = QueryExp(parm); - var response = Queryable() - //.OrderBy("Id desc") + var query = Queryable() .Where(predicate.ToExpression()) - .ToPage(parm); + .LeftJoin((p, u) => p.UserId == u.Id); - return response; + // 在 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); + + return resp; } + /// + /// 应用排序(在 Join 查询中使用表别名) + /// + private ISugarQueryable ApplyOrderBy(ISugarQueryable 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) // 默认排序 + }; + } + + /// + /// 读取列表 + /// + /// + /// 查询表单式 + /// 分页参数 + /// + public PagedInfo ToPage(ISugarQueryable source, PagerInfo parm) + { + var page = new PagedInfo(); + 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; + } + + /// /// 获取详情 @@ -76,16 +166,41 @@ namespace ZR.Service.Liveforum parm.PageSize = 100000; var predicate = QueryExp(parm); - var response = Queryable() + var query = Queryable() .Where(predicate.ToExpression()) - .Select((it) => new T_PostsDto() - { - //IsTopLabel = it.IsTop.GetConfigValue("liveforum_action_bool"), - //StatusLabel = it.Status.GetConfigValue("liveforum_posts_status"), - }, true) - .ToPage(parm); + .LeftJoin((p, u) => p.UserId == u.Id); - return response; + // 导出时使用默认排序 + 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("liveforum_action_bool"), + //StatusLabel = p.Status.GetConfigValue("liveforum_posts_status"), + }, true); + var resp = ToPage(response, parm); + + return resp; } /// @@ -98,7 +213,7 @@ namespace ZR.Service.Liveforum var predicate = Expressionable.Create(); predicate = predicate.AndIF(parm.UserId != null, it => it.UserId == parm.UserId); - predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Title), it => it.Title == parm.Title); + 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; diff --git a/ZR.Vue/src/views/liveforum/tposts.vue b/ZR.Vue/src/views/liveforum/tposts.vue index f751225..ee69b1e 100644 --- a/ZR.Vue/src/views/liveforum/tposts.vue +++ b/ZR.Vue/src/views/liveforum/tposts.vue @@ -16,7 +16,7 @@ {{ item.dictLabel }} - {{ item.dictValue }} + {{ item.dictValue }} @@ -69,6 +69,7 @@ +