/// <summary> /// 获取页面视图模型 /// </summary> /// <param name="page"></param> /// <param name="size"></param> /// <param name="orderBy"></param> /// <param name="user"></param> /// <returns></returns> private async Task <IndexPageViewModel> GetIndexPageViewModelAsync(int page, int size, OrderBy orderBy, UserInfoOutputDto user) { IQueryable <PostOutputDto> postList = PostBll.LoadEntitiesNoTracking <PostOutputDto>(p => (p.Status == Status.Pended || user.IsAdmin)); //准备文章的查询 var notices = NoticeBll.LoadPageEntitiesFromL2CacheNoTracking <DateTime, NoticeOutputDto>(1, 5, out int _, n => (n.Status == Status.Display || user.IsAdmin), n => n.ModifyDate, false).ToList(); //加载前5条公告 var cats = await CategoryBll.LoadEntitiesFromL2CacheNoTrackingAsync <string, CategoryOutputDto>(c => c.Status == Status.Available, c => c.Name).ConfigureAwait(true); //加载分类目录 var comments = CommentBll.LoadPageEntitiesFromL2CacheNoTracking <DateTime, CommentOutputDto>(1, 10, out int _, c => c.Status == Status.Pended && c.Post.Status == Status.Pended || user.IsAdmin, c => c.CommentDate, false).ToList(); //加在新评论 var start = DateTime.Today.AddDays(-7); var hotSearches = await SearchDetailsBll.LoadEntitiesNoTracking(s => s.SearchTime > start, s => s.SearchTime, false).GroupBy(s => s.KeyWords.ToLower()).OrderByDescending(g => g.Count()).Take(10).Select(g => new KeywordsRankOutputDto() { KeyWords = g.FirstOrDefault().KeyWords, SearchCount = g.Count() }).ToListAsync(); //热词统计 var hot6Post = await(new Random().Next() % 2 == 0 ? postList.OrderByDescending(p => p.ViewCount) : postList.OrderByDescending(p => p.VoteUpCount)).Skip(0).Take(5).Cacheable().ToListAsync(); //热门文章 var topPostWeek = await PostBll.SqlQuery <SimplePostModel>("SELECT [Id],[Title] from Post WHERE Id in (SELECT top 10 PostId FROM [dbo].[PostAccessRecord] where DATEDIFF(week,AccessTime,getdate())<=1 GROUP BY PostId ORDER BY sum(ClickCount) desc)").ToListAsync(); //文章周排行 var topPostMonth = await PostBll.SqlQuery <SimplePostModel>("SELECT [Id],[Title] from Post WHERE Id in (SELECT top 10 PostId FROM [dbo].[PostAccessRecord] where DATEDIFF(month,AccessTime,getdate())<=1 GROUP BY PostId ORDER BY sum(ClickCount) desc)").ToListAsync(); //文章月排行 var topPostYear = await PostBll.SqlQuery <SimplePostModel>("SELECT [Id],[Title] from Post WHERE Id in (SELECT top 10 PostId FROM [dbo].[PostAccessRecord] where DATEDIFF(year,AccessTime,getdate())<=1 GROUP BY PostId ORDER BY sum(ClickCount) desc)").ToListAsync(); //文章年度排行 var tags = new List <string>(); //标签云 var tagdic = new Dictionary <string, int>(); var newdic = new Dictionary <string, int>(); //标签云最终结果 postList.Select(p => p.Label).ToList().ForEach(m => { if (!string.IsNullOrEmpty(m)) { tags.AddRange(m.Split(',', ',')); } });//统计标签 tags.GroupBy(s => s).ForEach(g => { tagdic.Add(g.Key, g.Count()); }); //将标签分组 if (tagdic.Any()) { int min = tagdic.Values.Min(); tagdic.ForEach(kv => { var fontsize = (int)Math.Floor(kv.Value * 1.0 / (min * 1.0) + 12.0); newdic.Add(kv.Key, fontsize >= 36 ? 36 : fontsize); }); } IList <PostOutputDto> posts; switch (orderBy)//文章排序 { case OrderBy.CommentCount: posts = postList.OrderByDescending(p => p.IsFixedTop).ThenByDescending(p => p.Comment.Count).Skip(size * (page - 1)).Take(size).ToList(); break; case OrderBy.PostDate: posts = postList.OrderByDescending(p => p.IsFixedTop).ThenByDescending(p => p.PostDate).Skip(size * (page - 1)).Take(size).ToList(); break; case OrderBy.ViewCount: posts = postList.OrderByDescending(p => p.IsFixedTop).ThenByDescending(p => p.ViewCount).Skip(size * (page - 1)).Take(size).ToList(); break; case OrderBy.VoteCount: posts = postList.OrderByDescending(p => p.IsFixedTop).ThenByDescending(p => p.VoteUpCount).Skip(size * (page - 1)).Take(size).ToList(); break; default: posts = postList.OrderByDescending(p => p.IsFixedTop).ThenByDescending(p => p.ModifyDate).Skip(size * (page - 1)).Take(size).ToList(); break; } return(new IndexPageViewModel() { Categories = cats.ToList(), Comments = comments, HotSearch = hotSearches, Notices = notices, Posts = posts, Tags = newdic, Top6Post = hot6Post, TopPostByMonth = topPostMonth, TopPostByWeek = topPostWeek, TopPostByYear = topPostYear, PostsQueryable = postList }); }