Beispiel #1
0
        private PricingSearchResult <webModel.ProductPrice> SearchProductPricesImpl(PricesSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new PricesSearchCriteria();
            }
            var result = _pricingSearchService.SearchPrices(criteria);
            var retVal = new PricingSearchResult <webModel.ProductPrice>
            {
                TotalCount = result.TotalCount,
                Results    = new List <webModel.ProductPrice>()
            };

            var products = _itemService.GetByIds(result.Results.Select(x => x.ProductId).Distinct().ToArray(), Domain.Catalog.Model.ItemResponseGroup.ItemInfo);

            foreach (var productPricesGroup in result.Results.GroupBy(x => x.ProductId))
            {
                var productPrice = new webModel.ProductPrice
                {
                    ProductId = productPricesGroup.Key,
                    Prices    = productPricesGroup.ToList()
                };
                var product = products.FirstOrDefault(x => x.Id == productPricesGroup.Key);
                if (product != null)
                {
                    productPrice.Product = product.ToWebModel(_blobUrlResolver);
                }
                retVal.Results.Add(productPrice);
            }
            return(retVal);
        }
Beispiel #2
0
        private async Task <IList <Price> > GetMergedPriceDefault(IList <CsvPrice> restPrices)
        {
            if (!restPrices.Any())
            {
                return(new List <Price>());
            }

            var criteria = new PricesSearchCriteria
            {
                ProductIds = restPrices.Select(x => x.ProductId).ToArray(),
                Take       = int.MaxValue,
            };

            var result      = new List <Price>();
            var existPrices = (await _pricingSearchService.SearchPricesAsync(criteria)).Results;

            foreach (var price in restPrices)
            {
                var existPrice = existPrices.FirstOrDefault(x => x.Currency.EqualsInvariant(price.Currency) &&
                                                            x.ProductId.EqualsInvariant(price.ProductId));

                if (existPrice != null)
                {
                    price.MergeFrom(existPrice);
                }

                result.Add(price);
            }

            return(result);
        }
        public virtual Task <PriceSearchResult> SearchPricesAsync(PricesSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPricesAsync), criteria.GetCacheKey());

            return(_platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(PricesCacheRegion.CreateChangeToken());
                cacheEntry.AddExpirationToken(PricingSearchCacheRegion.CreateChangeToken());

                var result = AbstractTypeFactory <PriceSearchResult> .TryCreateInstance();

                using (var repository = _repositoryFactory())
                {
                    var query = await BuildQueryAsync(repository, criteria);
                    var sortInfos = BuildSortExpression(criteria);
                    //Try to replace sorting columns names
                    TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);

                    if (criteria.GroupByProducts)
                    {
                        var groupedQuery = query.Select(x => x.ProductId).OrderBy(x => x).Distinct();
                        result.TotalCount = await groupedQuery.CountAsync();

                        if (criteria.Take > 0)
                        {
                            query = query.Where(x => groupedQuery.Contains(x.ProductId));
                        }
                    }
                    else
                    {
                        result.TotalCount = await query.CountAsync();
                    }

                    if (criteria.Take > 0)
                    {
                        var priceIds = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                       .Skip(criteria.Skip).Take(criteria.Take)
                                       .Select(x => x.Id)
                                       .AsNoTracking()
                                       .ToArrayAsync();

                        var unorderedResults = await _pricingService.GetPricesByIdAsync(priceIds);
                        result.Results = unorderedResults.OrderBy(x => Array.IndexOf(priceIds, x.Id)).ToList();
                    }
                }
                return result;
            }));
        }
Beispiel #4
0
        protected virtual IList <SortInfo> BuildSortExpression(PricesSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[]
                {
                    new SortInfo
                    {
                        SortColumn = nameof(PriceEntity.List)
                    }
                };
            }
            return(sortInfos);
        }
Beispiel #5
0
        public virtual PricingSearchResult <coreModel.Price> SearchPrices(PricesSearchCriteria criteria)
        {
            var result = new PricingSearchResult <coreModel.Price>();

            ICollection <CatalogProduct> products;

            using (var repository = _repositoryFactory())
            {
                repository.DisableChangesTracking();

                var query = GetPricesQuery(repository, criteria, out products);

                var sortInfos = criteria.SortInfos;
                if (sortInfos.IsNullOrEmpty())
                {
                    sortInfos = new[] { new SortInfo {
                                            SortColumn = ReflectionUtility.GetPropertyName <coreModel.Price>(x => x.List)
                                        } };
                }
                //Try to replace sorting columns names
                TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);

                query = query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id);

                if (criteria.GroupByProducts)
                {
                    var groupedQuery = query.GroupBy(x => x.ProductId).OrderBy(x => 1);
                    result.TotalCount = groupedQuery.Count();
                    query             = groupedQuery.Skip(criteria.Skip).Take(criteria.Take).SelectMany(x => x);
                }
                else
                {
                    result.TotalCount = query.Count();
                    query             = query.Skip(criteria.Skip).Take(criteria.Take);
                }

                var pricesIds = query.Select(x => x.Id).ToList();
                result.Results = _pricingService.GetPricesById(pricesIds.ToArray())
                                 .OrderBy(x => pricesIds.IndexOf(x.Id))
                                 .ToList();
            }

            return(result);
        }
Beispiel #6
0
        public async Task <IActionResult> SearchProductPrices(PricesSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new PricesSearchCriteria();
            }
            var result = await _pricingSearchService.SearchPricesAsync(criteria);

            var retVal = new GenericSearchResult <ProductPrice>
            {
                TotalCount = result.TotalCount,
                Results    = new List <ProductPrice>()
            };

            var products = await _itemService.GetByIdsAsync(result.Results.Select(x => x.ProductId).Distinct().ToArray(), ItemResponseGroup.ItemInfo.ToString());

            foreach (var productPricesGroup in result.Results.GroupBy(x => x.ProductId))
            {
                var productPrice = new ProductPrice
                {
                    ProductId = productPricesGroup.Key,
                    Prices    = productPricesGroup.ToList()
                };
                var product = products.FirstOrDefault(x => x.Id == productPricesGroup.Key);
                if (product != null)
                {
                    if (!product.Images.IsNullOrEmpty())
                    {
                        foreach (var image in product.Images)
                        {
                            image.RelativeUrl = image.Url;
                            image.Url         = _blobUrlResolver.GetAbsoluteUrl(image.Url);
                        }
                    }

                    productPrice.Product = product;
                }
                retVal.Results.Add(productPrice);
            }

            return(Ok(retVal));
        }
Beispiel #7
0
        private async Task <ProductPriceSearchResult> InnerSearchProductPrices(PricesSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new PricesSearchCriteria();
            }

            var result = AbstractTypeFactory <ProductPriceSearchResult> .TryCreateInstance();

            var searchResult = await _pricingSearchService.SearchPricesAsync(criteria);

            result.TotalCount = searchResult.TotalCount;
            result.Results    = new List <ProductPrice>();

            var products = await _itemService.GetByIdsAsync(searchResult.Results.Select(x => x.ProductId).Distinct().ToArray(), ItemResponseGroup.ItemInfo.ToString());

            foreach (var productPricesGroup in searchResult.Results.GroupBy(x => x.ProductId))
            {
                var productPrice = new ProductPrice
                {
                    ProductId = productPricesGroup.Key,
                    Prices    = productPricesGroup.ToList()
                };
                var product = products.FirstOrDefault(x => x.Id == productPricesGroup.Key);
                if (product != null)
                {
                    if (!product.Images.IsNullOrEmpty())
                    {
                        foreach (var image in product.Images)
                        {
                            image.RelativeUrl = image.Url;
                            image.Url         = _blobUrlResolver.GetAbsoluteUrl(image.Url);
                        }
                    }

                    productPrice.Product = product;
                }
                result.Results.Add(productPrice);
            }

            return(result);
        }
Beispiel #8
0
        private async Task <IList <Price> > GetMergedPriceByPriceList(IList <CsvPrice> pricesWithPriceListIds)
        {
            if (!pricesWithPriceListIds.Any())
            {
                return(new List <Price>());
            }

            var existentPrices = new List <Price>();

            var dictionary = pricesWithPriceListIds.GroupBy(x => x.PricelistId).ToDictionary(g => g.Key, g => g.ToArray());

            foreach (var priceListId in dictionary.Keys)
            {
                var criteria = new PricesSearchCriteria
                {
                    PriceListId = priceListId,
                    ProductIds  = dictionary[priceListId].Select(x => x.ProductId).ToArray(),
                    Take        = int.MaxValue,
                };

                var searchResult = await _pricingSearchService.SearchPricesAsync(criteria);

                existentPrices.AddRange(searchResult.Results);
            }

            var result = new List <Price>();

            foreach (var price in pricesWithPriceListIds)
            {
                var existPrice = existentPrices.FirstOrDefault(x => x.ProductId.EqualsInvariant(price.ProductId) && x.PricelistId.EqualsInvariant(price.PricelistId));

                if (existPrice != null)
                {
                    price.MergeFrom(existPrice);
                }

                result.Add(price);
            }

            return(result);
        }
Beispiel #9
0
 public IHttpActionResult SearchProductPricesPost([FromBody] PricesSearchCriteria criteria)
 {
     return(Ok(SearchProductPricesImpl(criteria)));
 }
Beispiel #10
0
        public virtual PricingSearchResult <coreModel.Price> SearchPrices(PricesSearchCriteria criteria)
        {
            var retVal = new PricingSearchResult <coreModel.Price>();
            ICollection <CatalogProduct> products = new List <CatalogProduct>();

            using (var repository = _repositoryFactory())
            {
                var query = repository.Prices;

                if (!criteria.PriceListIds.IsNullOrEmpty())
                {
                    query = query.Where(x => criteria.PriceListIds.Contains(x.PricelistId));
                }

                if (!criteria.ProductIds.IsNullOrEmpty())
                {
                    query = query.Where(x => criteria.ProductIds.Contains(x.ProductId));
                }

                if (!string.IsNullOrEmpty(criteria.Keyword))
                {
                    var catalogSearchResult = _catalogSearchService.Search(new Domain.Catalog.Model.SearchCriteria {
                        Keyword = criteria.Keyword, Skip = criteria.Skip, Take = criteria.Take, Sort = criteria.Sort.Replace("product.", string.Empty), ResponseGroup = Domain.Catalog.Model.SearchResponseGroup.WithProducts
                    });
                    var productIds = catalogSearchResult.Products.Select(x => x.Id).ToArray();
                    query = query.Where(x => productIds.Contains(x.ProductId));
                    //preserve resulting products for future assignment to prices
                    products = catalogSearchResult.Products;
                }

                if (criteria.ModifiedSince.HasValue)
                {
                    query = query.Where(x => x.ModifiedDate >= criteria.ModifiedSince);
                }

                var sortInfos = criteria.SortInfos;
                if (sortInfos.IsNullOrEmpty())
                {
                    sortInfos = new[] { new SortInfo {
                                            SortColumn = ReflectionUtility.GetPropertyName <coreModel.Price>(x => x.List)
                                        } };
                }
                //Try to replace sorting columns names
                TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);


                query = query.OrderBySortInfos(sortInfos);

                if (criteria.GroupByProducts)
                {
                    var groupedQuery = query.GroupBy(x => x.ProductId).OrderBy(x => 1);
                    retVal.TotalCount = groupedQuery.Count();
                    query             = groupedQuery.Skip(criteria.Skip).Take(criteria.Take).SelectMany(x => x);
                }
                else
                {
                    retVal.TotalCount = query.Count();
                    query             = query.Skip(criteria.Skip).Take(criteria.Take);
                }

                var pricesIds = query.Select(x => x.Id).ToList();
                retVal.Results = _pricingService.GetPricesById(pricesIds.ToArray())
                                 .OrderBy(x => pricesIds.IndexOf(x.Id))
                                 .ToList();
            }
            return(retVal);
        }
Beispiel #11
0
        public virtual Task <PriceSearchResult> SearchPricesAsync(PricesSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPricesAsync), criteria.GetCacheKey());

            return(_platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(PricesCacheRegion.CreateChangeToken());
                cacheEntry.AddExpirationToken(PricingSearchCacheRegion.CreateChangeToken());

                var retVal = AbstractTypeFactory <PriceSearchResult> .TryCreateInstance();

                using (var repository = _repositoryFactory())
                {
                    var query = repository.Prices;

                    if (!criteria.PriceListIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => criteria.PriceListIds.Contains(x.PricelistId));
                    }

                    if (!criteria.ProductIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => criteria.ProductIds.Contains(x.ProductId));
                    }

                    if (criteria.ModifiedSince.HasValue)
                    {
                        query = query.Where(x => x.ModifiedDate >= criteria.ModifiedSince);
                    }

                    var sortInfos = criteria.SortInfos.ToArray();
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <Price>(x => x.List)
                                            } };
                    }
                    //Try to replace sorting columns names
                    TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);


                    query = query.OrderBySortInfos(sortInfos);

                    // TODO: add checks for criteria.Take being greater than 0
                    if (criteria.GroupByProducts)
                    {
                        var groupedQuery = query.GroupBy(x => x.ProductId).OrderBy(x => 1);
                        retVal.TotalCount = await groupedQuery.CountAsync();
                        query = groupedQuery.Skip(criteria.Skip).Take(criteria.Take).SelectMany(x => x);
                    }
                    else
                    {
                        retVal.TotalCount = await query.CountAsync();
                        query = query.Skip(criteria.Skip).Take(criteria.Take);
                    }

                    if (criteria.Take > 0)
                    {
                        var pricesIds = await query.Select(x => x.Id).ToListAsync();
                        retVal.Results = (await _pricingService.GetPricesByIdAsync(pricesIds.ToArray()))
                                         .OrderBy(x => pricesIds.IndexOf(x.Id))
                                         .ToList();
                    }
                }
                return retVal;
            }));
        }
Beispiel #12
0
        protected virtual async Task <IQueryable <PriceEntity> > BuildQueryAsync(IPricingRepository repository, PricesSearchCriteria criteria)
        {
            var query = repository.Prices;

            if (!criteria.PriceListIds.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.PriceListIds.Contains(x.PricelistId));
            }

            if (!criteria.ProductIds.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.ProductIds.Contains(x.ProductId));
            }

            if (criteria.ModifiedSince.HasValue)
            {
                query = query.Where(x => x.ModifiedDate >= criteria.ModifiedSince);
            }

            if (!string.IsNullOrEmpty(criteria.Keyword))
            {
                var searchCriteria = AbstractTypeFactory <ProductIndexedSearchCriteria> .TryCreateInstance();

                searchCriteria.Keyword       = criteria.Keyword;
                searchCriteria.Skip          = criteria.Skip;
                searchCriteria.Take          = criteria.Take;
                searchCriteria.Sort          = criteria.Sort.Replace("product.", string.Empty);
                searchCriteria.ResponseGroup = ItemResponseGroup.ItemInfo.ToString();
                var searchResult = await _productIndexedSearchService.SearchAsync(searchCriteria);

                var productIds = searchResult.Items.Select(x => x.Id).ToArray();

                query = query.Where(x => productIds.Contains(x.ProductId));
            }

            return(query);
        }
Beispiel #13
0
        protected virtual IQueryable <PriceEntity> GetPricesQuery(IPricingRepository repository, PricesSearchCriteria criteria, out ICollection <CatalogProduct> products)
        {
            products = new List <CatalogProduct>();

            var query = repository.Prices;

            if (!criteria.PriceListIds.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.PriceListIds.Contains(x.PricelistId));
            }

            if (!criteria.ProductIds.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.ProductIds.Contains(x.ProductId));
            }

            if (!string.IsNullOrEmpty(criteria.Keyword))
            {
                var catalogSearchCriteria = new SearchCriteria
                {
                    Keyword       = criteria.Keyword,
                    Skip          = criteria.Skip,
                    Take          = criteria.Take,
                    Sort          = criteria.Sort.Replace("product.", string.Empty),
                    ResponseGroup = SearchResponseGroup.WithProducts,
                };
                var catalogSearchResult = _catalogSearchService.Search(catalogSearchCriteria);

                var productIds = catalogSearchResult.Products.Select(x => x.Id).ToArray();
                //preserve resulting products for future assignment to prices
                products = catalogSearchResult.Products;

                query = query.Where(x => productIds.Contains(x.ProductId));
            }

            if (criteria.ModifiedSince.HasValue)
            {
                query = query.Where(x => x.ModifiedDate >= criteria.ModifiedSince);
            }

            return(query);
        }
Beispiel #14
0
 public async Task <ActionResult <ProductPriceSearchResult> > SearchProductPricesPost([FromBody] PricesSearchCriteria criteria)
 {
     return(Ok(await InnerSearchProductPrices(criteria)));
 }