public List <HomeCategorySet> GetHomeCategorySets() { var homeCategorySet = new HomeCategorySet[HOME_CATEGORY_SET_COUNT]; if (Cache.Exists(CacheKeyCollection.HomeCategory)) { homeCategorySet = Core.Cache.Get <HomeCategorySet[]>(CacheKeyCollection.HomeCategory); } else { var cc = DbFactory.Default.Get <HomeCategoryInfo>() .LeftJoin <CategoryInfo>((hci, ci) => hci.CategoryId == ci.Id) .Where <CategoryInfo>(p => p.IsDeleted == false && p.IsShow == true) .OrderBy <CategoryInfo>(x => x.DisplaySequence) .ToList(); foreach (var group in cc.GroupBy(p => p.RowId)) { homeCategorySet[group.Key - 1] = new HomeCategorySet() { RowNumber = group.Key, HomeCategories = group.ToList() }; } var _HomeCategoryRowInfo = DbFactory.Default.Get <HomeCategoryRowInfo>().ToList(); for (int i = 0; i < HOME_CATEGORY_SET_COUNT; i++) { if (homeCategorySet[i] == null) { homeCategorySet[i] = new HomeCategorySet() { RowNumber = i + 1, HomeCategories = new List <HomeCategoryInfo>() }; } var homeCategoryRows = new List <HomeCategorySet.HomeCategoryTopic>(); var categoryRowInfo = _HomeCategoryRowInfo.Where(item => item.RowId == i + 1).FirstOrDefault(); if (categoryRowInfo != null) { homeCategoryRows.Add(new HomeCategorySet.HomeCategoryTopic() { Url = categoryRowInfo.Url1, ImageUrl = categoryRowInfo.Image1 }); homeCategoryRows.Add(new HomeCategorySet.HomeCategoryTopic() { Url = categoryRowInfo.Url2, ImageUrl = categoryRowInfo.Image2 }); } homeCategorySet[i].HomeCategoryTopics = homeCategoryRows; } var brandService = ObjectContainer.Current.Resolve <IBrandService>(); var categories = ObjectContainer.Current.Resolve <ICategoryService>().GetCategories(); for (int i = 0; i < homeCategorySet.Count(); i++) { homeCategorySet[i].HomeCategories = homeCategorySet[i].HomeCategories.Select(t => { return(new HomeCategoryInfo() { CategoryId = t.CategoryId, Depth = t.Depth, Id = t.Id, RowId = t.RowId, }); }).ToList(); homeCategorySet[i].HomeCategoryTopics = homeCategorySet[i].HomeCategoryTopics.Select(t => new HomeCategorySet.HomeCategoryTopic() { ImageUrl = t.ImageUrl, Url = t.Url }).ToList(); const int MAX_DISPLAY_BRANDS = 8;//最多显示推荐品牌个数 //获取推荐的品牌 var singleRowBrand = brandService.GetBrandsByCategoryIds(homeCategorySet[i].HomeCategories.Where(item => item.Depth == 1).Select(item => item.CategoryId).ToArray()).Take(MAX_DISPLAY_BRANDS); homeCategorySet[i].HomeBrand = singleRowBrand.Select(item => new BrandInfo { Id = item.Id, Name = item.Name, Logo = item.Logo, }).ToList(); } Cache.Insert <HomeCategorySet[]>(CacheKeyCollection.HomeCategory, homeCategorySet, 5); } return(homeCategorySet.ToList()); }
public IEnumerable <HomeCategorySet> GetHomeCategorySets() { var homeCategorySet = new HomeCategorySet[HOME_CATEGORY_SET_COUNT]; if (Cache.Exists(CacheKeyCollection.HomeCategory)) { homeCategorySet = Core.Cache.Get <HomeCategorySet[]>(CacheKeyCollection.HomeCategory); } else { //var homeCategoryGroups = context.HomeCategoryInfo.FindAll().GroupBy(item => item.RowNumber); var cc = (from C in Context.HomeCategoryInfo .Include("CategoryInfo") where C.CategoryInfo.IsDeleted == false select C).ToList(); var homeCategoryGroups = (from C in cc group C by C.RowNumber into G select G).ToList(); foreach (var group in homeCategoryGroups) { homeCategorySet[group.Key - 1] = new HomeCategorySet() { RowNumber = group.Key, HomeCategories = group }; } var _HomeCategoryRowInfo = Context.HomeCategoryRowInfo.FindAll().ToList(); for (int i = 0; i < HOME_CATEGORY_SET_COUNT; i++) { if (homeCategorySet[i] == null) { homeCategorySet[i] = new HomeCategorySet() { RowNumber = i + 1, HomeCategories = new List <HomeCategoryInfo>() }; } var homeCategoryRows = new List <HomeCategorySet.HomeCategoryTopic>(); var categoryRowInfo = _HomeCategoryRowInfo.Where(item => item.RowId == i + 1).FirstOrDefault(); if (categoryRowInfo != null) { homeCategoryRows.Add(new HomeCategorySet.HomeCategoryTopic() { Url = categoryRowInfo.Url1, ImageUrl = categoryRowInfo.Image1 }); homeCategoryRows.Add(new HomeCategorySet.HomeCategoryTopic() { Url = categoryRowInfo.Url2, ImageUrl = categoryRowInfo.Image2 }); } homeCategorySet[i].HomeCategoryTopics = homeCategoryRows; } var brandService = ObjectContainer.Current.Resolve <IBrandService>(); for (int i = 0; i < homeCategorySet.Count(); i++) { homeCategorySet[i].HomeCategories = homeCategorySet[i].HomeCategories.Select(t => new HomeCategoryInfo() { CategoryId = t.CategoryId, Depth = t.Depth, Id = t.Id, RowNumber = t.RowNumber, CategoryInfo = new CategoryInfo() { Depth = t.CategoryInfo.Depth, Name = t.CategoryInfo.Name, RewriteName = t.CategoryInfo.RewriteName, TypeId = t.CategoryInfo.TypeId, CommisRate = t.CategoryInfo.CommisRate, DisplaySequence = t.CategoryInfo.DisplaySequence, HasChildren = t.CategoryInfo.HasChildren, Icon = t.CategoryInfo.Icon, Path = t.CategoryInfo.Path, Meta_Title = t.CategoryInfo.Meta_Title, ParentCategoryId = t.CategoryInfo.ParentCategoryId, Meta_Keywords = t.CategoryInfo.Meta_Keywords, Meta_Description = t.CategoryInfo.Meta_Description, Id = t.CategoryInfo.Id } }).ToList(); homeCategorySet[i].HomeCategoryTopics = homeCategorySet[i].HomeCategoryTopics.Select(t => new HomeCategorySet.HomeCategoryTopic() { ImageUrl = t.ImageUrl, Url = t.Url }).ToList(); const int MAX_DISPLAY_BRANDS = 8;//最多显示推荐品牌个数 //获取推荐的品牌 var singleRowBrand = brandService.GetBrandsByCategoryIds(homeCategorySet[i].HomeCategories.Where(item => item.Depth == 1).Select(item => item.CategoryId).ToArray()).Take(MAX_DISPLAY_BRANDS); BrandInfo[] brands = new BrandInfo[singleRowBrand.Count()]; for (int k = 0; k < singleRowBrand.Count(); k++) { brands[k] = new BrandInfo(); brands[k].Id = singleRowBrand.ElementAt(k).Id; brands[k].Name = singleRowBrand.ElementAt(k).Name; brands[k].Logo = singleRowBrand.ElementAt(k).Logo; } homeCategorySet[i].HomeBrand = brands; } Cache.Insert <HomeCategorySet[]>(CacheKeyCollection.HomeCategory, homeCategorySet, 5); } return(homeCategorySet); }