Exemplo n.º 1
0
        private IQueryable <CatalogItemEntity> GetEdvanceItems()
        {
            IQueryable <CatalogItemEntity> items = null;

            if (SearchCriteria != null)
            {
                Func <string, string[]> prepareArray =
                    x =>
                {
                    List <string> results = new List <string>();

                    if (!string.IsNullOrWhiteSpace(x))
                    {
                        results = x.Split(',', ' ').ToList();
                        results.RemoveAll(string.IsNullOrWhiteSpace);
                    }

                    return(results.ToArray());
                };

                string[]       codes                = prepareArray(SearchCriteria.Code);
                string[]       lexemes              = prepareArray(SearchCriteria.Name);
                string[]       articles             = prepareArray(SearchCriteria.Article);
                DateTimeOffset dateForNew           = DateTimeOffset.Now.AddDays(-14);
                DateTimeOffset dateForPrice         = DateTimeOffset.Now.AddDays(-7);
                List <long>    directoryIds         = SearchCriteria?.GetDirectoryIds().ToList();
                List <long>    selectedDirectoryIds = SearchCriteria?.GetSelectedDirectoryIds().ToList();
                List <long>    brandIds             = SearchCriteria?.GetBrandIds() ?? new List <long>();
                List <long>    selectedBrandIds     = SearchCriteria?.GetSelectedBrandIds() ?? new List <long>();

                items = DataService.Select <CatalogItemEntity>()
                        .Include(x => x.BasketItems)
                        .Where(x => !codes.Any() || codes.Contains(x.Code))
                        .Where(n => !lexemes.Any() || lexemes.All(s => n.Name.Contains(s)))
                        .Where(x => !articles.Any() || articles.Contains(x.Article))
                        .Where(x => (!SearchCriteria.IsNew && !SearchCriteria.PriceIsDown && !SearchCriteria.PriceIsUp) ||
                               (SearchCriteria.IsNew && x.Status == CatalogItemStatus.New && x.DateOfCreation >= dateForNew) ||
                               (SearchCriteria.PriceIsDown && x.Status == CatalogItemStatus.PriceIsDown && x.LastUpdatedStatus >= dateForPrice) ||
                               (SearchCriteria.PriceIsUp && x.Status == CatalogItemStatus.PriceIsUp && x.LastUpdatedStatus >= dateForPrice))
                        .Where(x => (!selectedDirectoryIds.Any() && directoryIds.Contains(x.Directory.Id)) || selectedDirectoryIds.Contains(x.Directory.Id))
                        .Where(x => (!selectedBrandIds.Any() && brandIds.Contains(x.Brand.Id)) || selectedBrandIds.Contains(x.Brand.Id))
                        .Distinct();
            }

            return(items);
        }