public CurrencyService(IAsyncRepository <Currency> currencyRepository, IApiProvider apiProvider, IDbCacheService dbCacheService) { _currencyRepository = currencyRepository; _apiProvider = apiProvider; _dbCacheService = dbCacheService; }
//public static ProductSearchResult GetIdListByFilters(IUnitOfWork db, // ProductSearchFilterViewModel filter) //{ // var result = new ProductSearchResult(); // if (filter.NoneSoldPeriod.HasValue && filter.NoneSoldPeriod > 0) // { // //NOTE: using styleId to hide parent item with that style but without sold // //получаем список продающихся styleId // //выводим список item у которых styleId not in Sold Style List // var fromDate = DateTime.Today.AddDays(-filter.NoneSoldPeriod.Value); // var soldStyleIds = db.StyleCaches.GetAll() // .Where(sc => sc.LastSoldDateOnMarket < fromDate) // .GroupBy(sc => sc.Id) // .Select(gsc => gsc.Key).ToList(); // result.StyleIdList = GeneralUtils.IntersectIfNotNull(result.StyleIdList, soldStyleIds); // } // if (filter.Gender.HasValue) // { // var genderStyleIds = db.StyleCaches // .GetAll() // .Where(s => s.Gender == filter.Gender.Value.ToString()) // .Select(s => s.Id).ToList(); // result.StyleIdList = GeneralUtils.IntersectIfNotNull(result.StyleIdList, genderStyleIds); // } // if (filter.MainLicense.HasValue) // { // var mainLicenseStyleIds = db.StyleCaches // .GetAll() // .Where(s => s.MainLicense == filter.MainLicense.Value.ToString()) // .Select(s => s.Id) // .ToList(); // result.StyleIdList = GeneralUtils.IntersectIfNotNull(result.StyleIdList, mainLicenseStyleIds); // } // if (filter.SubLicense.HasValue) // { // var subLicenseStyleIds = db.StyleCaches // .GetAll() // .Where(s => s.SubLicense == filter.SubLicense.Value.ToString()) // .Select(s => s.Id) // .ToList(); // result.StyleIdList = GeneralUtils.IntersectIfNotNull(result.StyleIdList, subLicenseStyleIds); // } // if (filter.MinPrice.HasValue || filter.MaxPrice.HasValue) // { // var priceQuery = db.Items.GetAllViewAsDto().Where(i => i.Market == (int)filter.Market); // if (!String.IsNullOrEmpty(filter.MarketplaceId)) // priceQuery = priceQuery.Where(p => p.MarketplaceId == filter.MarketplaceId); // if (filter.MinPrice.HasValue) // priceQuery = priceQuery.Where(p => p.CurrentPrice >= filter.MinPrice.Value); // if (filter.MaxPrice.HasValue) // priceQuery = priceQuery.Where(p => p.CurrentPrice <= filter.MaxPrice.Value); // result.ChildItemIdList = priceQuery.Select(p => p.Id).ToList(); // } // return result; //} //public static IEnumerable<ListingDefectDTO> GetListingDefects(ILogService log, // IDbFactory dbFactory) //{ // using (var db = dbFactory.GetRDb()) // { // log.Debug("GetAll Info begin"); // var defectList = db.ListingDefects.GetAllAsDto() // .Where() // log.Debug("GetAll Info end"); // } //} public static GridResponse <ParentItemViewModel> GetAll(ILogService log, IDbCacheService cacheService, IDbFactory dbFactory, bool clearCache, ProductSearchFilterViewModel filter) { if (filter.LimitCount == 0) { filter.LimitCount = 50; } using (var db = dbFactory.GetRDb()) { log.Debug("GetAll begin"); var forceStyleImage = filter.MarketplaceId == MarketplaceKeeper.AmazonAuMarketplaceId || filter.MarketplaceId == MarketplaceKeeper.AmazonComMarketplaceId; var gridResponse = db.ParentItems.GetWithChildSizesWithPaging(log, cacheService, clearCache, forceStyleImage, filter.GetDto()); var results = (from p in gridResponse.Items select new ParentItemViewModel { Id = p.Id, Market = (MarketType)p.Market, MarketplaceId = p.MarketplaceId, SourceMarketId = p.SourceMarketId, ImageSource = p.ImageSource, AmazonName = p.AmazonName, FirstStyleString = GetFirstStyle(p.ChildItems, p.ASIN), ASIN = p.ASIN, SKU = p.SKU, OnHold = p.OnHold, LockMarketUpdate = p.LockMarketUpdate, PublishRequestedDate = p.PublishRequestedDate, ChildItems = p.ChildItems .Select(i => new ItemShortInfoViewModel(p.Market, p.MarketplaceId, i)) .ToList(), StyleItems = p.StyleItems.Select(i => new StyleItemInfoViewModel(i)) .ToList(), Rank = p.Rank, Positions = p.PositionsInfo != null ? p.PositionsInfo.Replace(";", ",<br/>") : String.Empty, Comment = p.LastComment?.Message, CommentByName = p.LastComment?.CreatedByName, CommentDate = p.LastComment?.CreateDate, HasPriceDifferencesWithAmazon = p.HasPriceDifferencesWithAmazon, HasQuantityDifferencesWithAmazon = p.HasQuantityDifferencesWithAmazon, HasChildWithFakeParentASIN = p.HasChildWithFakeParentASIN, LastChildOpenDate = p.LastChildOpenDate, MinChildPrice = p.MinChildPrice, MaxChildPrice = p.MaxChildPrice, IsAmazonUpdated = p.IsAmazonUpdated, }).ToList(); log.Debug("GetAll end"); return(new GridResponse <ParentItemViewModel>(results.OrderBy(r => r.FirstStyleString).ToList(), gridResponse.TotalCount)); } }
public GridResponse <ParentItemDTO> GetWithChildSizesWithPaging(ILogService log, IDbCacheService cacheService, bool clearCache, bool useStyleImage, ItemSearchFiltersDTO filters) { log.Debug("begin GetWithChildSizes"); var baseParentQuery = from p in unitOfWork.GetSet <ParentItem>() where p.Market == (int)filters.Market && (String.IsNullOrEmpty(filters.MarketplaceId) || p.MarketplaceId == filters.MarketplaceId) select p; if (!String.IsNullOrEmpty(filters.Keywords) || !String.IsNullOrEmpty(filters.StyleName) || filters.StyleId.HasValue || filters.DropShipperId.HasValue || (filters.Genders != null && filters.Genders.Any()) || !String.IsNullOrEmpty(filters.Brand) || filters.MinPrice.HasValue || filters.MaxPrice.HasValue || filters.PublishedStatus.HasValue || filters.Availability != (int)ProductAvailability.All) { var keywordQuery = from vi in unitOfWork.GetSet <ViewItem>() where vi.Market == (int)filters.Market && (String.IsNullOrEmpty(filters.MarketplaceId) || vi.MarketplaceId == filters.MarketplaceId) select vi; if (!String.IsNullOrEmpty(filters.Keywords)) { keywordQuery = keywordQuery.Where(vi => vi.StyleString.Contains(filters.Keywords) || vi.SKU.Contains(filters.Keywords) || vi.ASIN.Contains(filters.Keywords) || vi.ParentASIN.Contains(filters.Keywords) || vi.Title.Contains(filters.Keywords)); } if (filters.StyleId.HasValue) { keywordQuery = keywordQuery.Where(vi => vi.StyleId == filters.StyleId); } if (!String.IsNullOrEmpty(filters.StyleName)) { keywordQuery = keywordQuery.Where(vi => vi.StyleString.Contains(filters.StyleName)); } if (filters.DropShipperId.HasValue) { keywordQuery = keywordQuery.Where(vi => vi.DropShipperId == filters.DropShipperId); } if (filters.MinPrice.HasValue) { keywordQuery = keywordQuery.Where(vi => vi.CurrentPrice >= filters.MinPrice); } if (filters.MaxPrice.HasValue) { keywordQuery = keywordQuery.Where(vi => vi.CurrentPrice <= filters.MaxPrice); } if (filters.PublishedStatus.HasValue) { keywordQuery = keywordQuery.Where(vi => vi.ItemPublishedStatus == filters.PublishedStatus); } if (filters.Availability == (int)ProductAvailability.InStock) { keywordQuery = keywordQuery.Where(vi => vi.RealQuantity > 0); } if ((filters.Genders != null && filters.Genders.Any()) || !String.IsNullOrEmpty(filters.Brand)) { var featureQuery = from sc in unitOfWork.GetSet <StyleCache>() select sc; if (filters.Genders != null && filters.Genders.Any()) { var genderIds = filters.Genders.Select(g => g.ToString()).ToList(); featureQuery = from f in featureQuery where genderIds.Contains(f.Gender) select f; } if (!String.IsNullOrEmpty(filters.Brand)) { featureQuery = from f in featureQuery where f.Brand == filters.Brand select f; } if (filters.MainLicense.HasValue) { var mainLicenseStr = filters.MainLicense.ToString(); featureQuery = from f in featureQuery where f.MainLicense == mainLicenseStr select f; } keywordQuery = from k in keywordQuery join f in featureQuery on k.StyleId equals f.Id select k; } var parentFilterQuery = keywordQuery.Select(vi => vi.ParentASIN).Distinct(); baseParentQuery = from p in baseParentQuery join k in parentFilterQuery on p.ASIN equals k select p; } var totalCount = baseParentQuery.Count(); baseParentQuery = baseParentQuery .OrderBy(p => p.Id) .Skip(filters.StartIndex) .Take(filters.LimitCount); var dropShipperInfo = from vi in unitOfWork.GetSet <ViewItem>() group vi by vi.ParentASIN into byParent select new { ASIN = byParent.Key, DropShipperId = byParent.Max(i => i.DropShipperId) }; var parentItemQuery = from pi in baseParentQuery join dsInfo in dropShipperInfo on pi.ASIN equals dsInfo.ASIN into withDsInfo from dsInfo in withDsInfo.DefaultIfEmpty() join pCache in unitOfWork.GetSet <ParentItemCache>() on pi.Id equals pCache.Id into withPCache from pCache in withPCache.DefaultIfEmpty() join c in unitOfWork.GetSet <ViewActualProductComment>() on pi.Id equals c.ProductId into withC from c in withC.DefaultIfEmpty() join ds in unitOfWork.GetSet <DropShipper>() on dsInfo.DropShipperId equals ds.Id into withDs from ds in withDs.DefaultIfEmpty() select new ParentItemDTO { Id = pi.Id, Market = pi.Market, MarketplaceId = pi.MarketplaceId, DropShipperId = ds.Id, DropShipperName = ds.Name, AmazonName = pi.AmazonName, ImageSource = pi.ImageSource, SourceMarketId = pi.SourceMarketId, SourceMarketUrl = pi.SourceMarketUrl, ASIN = pi.ASIN, SKU = pi.SKU, OnHold = pi.OnHold, Rank = pi.Rank, LastComment = new CommentDTO() { Message = c.Message, CreateDate = c.CreateDate, CreatedBy = c.CreatedBy, CreatedByName = c.CreatedByName, }, HasListings = pCache == null || pCache.HasListings, IsAmazonUpdated = pi.IsAmazonUpdated, HasPriceDifferencesWithAmazon = pCache != null ? pCache.HasPriceDifferences : false, HasQuantityDifferencesWithAmazon = pCache != null ? pCache.HasQtyDifferences : false, HasChildWithFakeParentASIN = pCache != null ? pCache.HasChildWithFakeParentASIN : false, LastChildOpenDate = pCache != null ? pCache.LastOpenDate : null, MinChildPrice = pCache != null ? pCache.MinPrice : null, MaxChildPrice = pCache != null ? pCache.MaxPrice : null, PositionsInfo = pCache != null ? pCache.PositionsInfo : null, }; var parentItems = parentItemQuery.AsNoTracking().ToList(); //if (filters.Market == (int)MarketType.Amazon // || filters.Market == (int)MarketType.AmazonEU) //{ // parentItemQuery = parentItemQuery.Where(pi => pi.HasListings); //} log.Debug("Get ParentItems end"); var parentASINList = parentItems.Select(pi => pi.ASIN).ToList(); var childItemsResult = GetChildItemInfoList(parentASINList, (MarketType)filters.Market, filters.MarketplaceId); var childItems = childItemsResult.Select(i => new ItemShortInfoDTO() { Id = i.Id, AmazonRealQuantity = i.AmazonRealQuantity, RealQuantity = i.RealQuantity, DisplayQuantity = i.DisplayQuantity, ASIN = i.ASIN, //NOTE: for search filters ParentASIN = i.ParentASIN, SKU = i.SKU, Barcode = i.Barcode, SourceMarketId = i.SourceMarketId, StyleItemId = i.StyleItemId, StyleId = i.StyleId, StyleString = i.StyleString, StyleOnHold = i.StyleOnHold, StyleItemOnHold = i.StyleItemOnHold, StyleColor = i.StyleColor, StyleSize = i.StyleSize, IsFBA = i.IsFBA, IsDefault = i.IsDefault, OnHold = i.OnHold, PublishedStatus = i.PublishedStatus, ListingDefects = i.ListingDefects, }).ToList(); log.Debug("Get ChildItems end"); var styleIdList = childItems.Where(ci => ci.StyleId.HasValue) .Select(ci => ci.StyleId.Value) .Distinct().ToList(); var styleItemResult = GetStyleItemInfoList(styleIdList, true); var forceStyleImage = filters.Market == (int)MarketType.Walmart || filters.Market == (int)MarketType.WalmartCA || filters.Market == (int)MarketType.Shopify || filters.Market == (int)MarketType.Magento || filters.Market == (int)MarketType.OverStock || filters.Market == (int)MarketType.Jet || filters.Market == (int)MarketType.eBay || filters.Market == (int)MarketType.Groupon || filters.Market == (int)MarketType.DropShipper || filters.Market == (int)MarketType.OfflineOrders || filters.Market == (int)MarketType.WooCommerce; var styleItems = styleItemResult.Where(si => si.StyleId.HasValue && styleIdList.Contains(si.StyleId.Value)) .Select(si => new ItemShortInfoDTO() { Id = si.Id, StyleId = si.StyleId, StyleString = si.StyleString, StyleOnHold = si.StyleOnHold, StyleItemOnHold = si.OnHold, StyleSize = si.StyleSize, StyleColor = si.StyleColor, StyleItemId = si.StyleItemId, RemainingQuantity = si.RemainingQuantity, Image = forceStyleImage ? si.Image : null, LinkedListingCount = si.MarketplacesInfo != null ? (si.MarketplacesInfo.Contains(filters.MarketCode) ? 1 : 0) : 0, }).ToList(); var publishRequestInfoes = unitOfWork.GetSet <SystemAction>().Where(i => i.Type == (int)SystemActionType.UpdateOnMarketProductData && i.Status == (int)SystemActionStatus.None) .Select(i => new { ItemId = i.Tag, CreateDate = i.CreateDate, }) .ToList(); log.Debug("Get StyleItems end"); foreach (var parentItem in parentItems) { var item = parentItem; item.ChildItems = childItems .Where(ch => ch.ParentASIN == item.ASIN) .ToList(); var childItemIdStrs = item.ChildItems.Select(i => i.Id.ToString()).ToList(); var publishRequestInfo = publishRequestInfoes.FirstOrDefault(i => childItemIdStrs.Contains(i.ItemId)); item.PublishRequestedDate = publishRequestInfo?.CreateDate; var itemStyleIdList = item.ChildItems.Select(ch => ch.StyleId).ToList(); item.StyleItems = styleItems .Where(si => itemStyleIdList.Contains(si.StyleId)) .ToList(); if (item.StyleItems.Any() && (forceStyleImage || useStyleImage)) { var defaultItem = item.ChildItems.FirstOrDefault(ch => ch.IsDefault); ItemShortInfoDTO mainStyleItem = null; if (defaultItem != null) { mainStyleItem = item.StyleItems.FirstOrDefault(si => si.Id == defaultItem.StyleItemId); } if (mainStyleItem == null) { mainStyleItem = item.StyleItems[0]; } item.ImageSource = mainStyleItem.Image; } foreach (var childItem in item.ChildItems) { var styleItem = item.StyleItems.FirstOrDefault(si => si.Id == childItem.StyleItemId); childItem.RemainingQuantity = styleItem != null ? styleItem.RemainingQuantity : 0; } } log.Debug("Get Positions end"); return(new GridResponse <ParentItemDTO>(parentItems, totalCount)); }