/// <summary> /// 获取页面视图模型 /// </summary> /// <returns></returns> private HomePageViewModel GetIndexPageViewModel() { var postsQuery = PostService.GetQuery <PostDto>(p => (p.Status == Status.Published || CurrentUser.IsAdmin)); //准备文章的查询 var notices = NoticeService.GetPagesFromCache <DateTime, NoticeDto>(1, 5, n => (n.Status == Status.Display || CurrentUser.IsAdmin), n => n.ModifyDate, false); //加载前5条公告 var cats = CategoryService.GetQueryFromCache <string, CategoryDto>(c => c.Status == Status.Available, c => c.Name).ToList(); //加载分类目录 var hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList(); //热词统计 var hot6Post = postsQuery.OrderBy((new Random().Next() % 3) switch { 1 => nameof(OrderBy.VoteUpCount), 2 => nameof(OrderBy.AverageViewCount), _ => nameof(OrderBy.TotalViewCount) } +" desc").Skip(0).Take(5).Cacheable().ToList(); //热门文章 var newdic = new Dictionary <string, int>(); //标签云最终结果 var tagdic = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Distinct().Cacheable().ToList().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count()); //统计标签 if (tagdic.Any()) { var min = tagdic.Values.Min(); foreach (var(key, value) in tagdic) { var fontsize = (int)Math.Floor(value * 1.0 / (min * 1.0) + 12.0); newdic.Add(key, fontsize >= 36 ? 36 : fontsize); } } return(new HomePageViewModel() { Categories = cats, HotSearch = hotSearches, Notices = notices.Data, Tags = newdic, Top6Post = hot6Post, PostsQueryable = postsQuery, SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar), ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.PostList) }); }
/// <summary> /// 获取页面视图模型 /// </summary> /// <param name="page"></param> /// <param name="size"></param> /// <param name="orderBy"></param> /// <param name="user"></param> /// <returns></returns> private IndexPageViewModel GetIndexPageViewModel(int page, int size, OrderBy?orderBy, UserInfoOutputDto user) { IQueryable <PostOutputDto> postsQuery = PostService.GetQuery <PostOutputDto>(p => (p.Status == Status.Pended || user.IsAdmin)); //准备文章的查询 var notices = NoticeService.GetPagesFromCache <DateTime, NoticeOutputDto>(1, 5, out int _, n => (n.Status == Status.Display || user.IsAdmin), n => n.ModifyDate, false).ToList(); //加载前5条公告 var cats = CategoryService.GetQueryFromCache <string, CategoryOutputDto>(c => c.Status == Status.Available, c => c.Name).ToList(); //加载分类目录 var hotSearches = RedisHelper.Get <List <KeywordsRankOutputDto> >("SearchRank:Week").Take(10).ToList(); //热词统计 Expression <Func <PostOutputDto, double> > order = p => p.TotalViewCount; switch (new Random().Next() % 3) { case 1: order = p => p.VoteUpCount; break; case 2: order = p => p.AverageViewCount; break; } var hot6Post = postsQuery.OrderByDescending(order).Skip(0).Take(5).Cacheable().ToList(); //热门文章 var newdic = new Dictionary <string, int>(); //标签云最终结果 var tagdic = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Cacheable().AsEnumerable().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => 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 = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.CommentCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList(); break; case OrderBy.PostDate: posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.PostDate).Skip(size * (page - 1)).Take(size).Cacheable().ToList(); break; case OrderBy.ViewCount: posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.TotalViewCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList(); break; case OrderBy.VoteCount: posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.VoteUpCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList(); break; case OrderBy.AverageViewCount: posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.AverageViewCount).Skip(size * (page - 1)).Take(size).Cacheable().ToList(); break; default: posts = postsQuery.Where(p => !p.IsFixedTop).OrderByDescending(p => p.ModifyDate).Skip(size * (page - 1)).Take(size).Cacheable().ToList(); break; } if (page == 1) { posts = postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate).AsEnumerable().Union(posts).ToList(); } return(new IndexPageViewModel() { Categories = cats, HotSearch = hotSearches, Notices = notices, Posts = posts, Tags = newdic, Top6Post = hot6Post, PostsQueryable = postsQuery }); }