public void GetItems(IMarketApi api, IList <string> marketIds) { var asinsWithError = new List <string>(); var items = api.GetItems(_log, _time, MarketItemFilters.Build(marketIds), ItemFillMode.Defualt, out asinsWithError); }
public void CallUpdateParentItemEBay(IMarketApi api, string parentAsin) { var list = new List <string>() { parentAsin }; var parentASINsWithError = new List <string>(); var parents = api.GetItems(_log, _time, MarketItemFilters.Build(list), ItemFillMode.Defualt, out parentASINsWithError); }
public void ProcessNewParents(IUnitOfWork db, IMarketApi api, IList <ItemDTO> parents, IList <ItemDTO> items) { var parentASINsWithError = new List <string>(); _log.Debug("Begin process new parents, count=" + parents.Count); var parentsDto = api.GetItems(_log, _time, MarketItemFilters.Build(parents.Select(p => p.ParentASIN).ToList()), ItemFillMode.NoAdv, out parentASINsWithError).ToList(); _log.Debug("Error when GetItems, parentASINS: " + String.Join(", ", parentASINsWithError)); //Fill fake parentItem with item info var notAmazonUpdatedParents = parentsDto.Where(p => p.IsAmazonUpdated != true).ToList(); foreach (var parent in notAmazonUpdatedParents) { _log.Warn("Parent item is not filled by Amazon (setting image from first item), ParentASIN=" + parent.ASIN); _syncInfo.AddWarning(parent.ASIN, "Parent item is not filled by Amazon (setting image from first item)"); //NOTE: parent EQUAL to first item (contains the same info) var item = items.FirstOrDefault(r => r.ParentASIN == parent.ASIN); if (item != null) { parent.ImageSource = item.ImageUrl; parent.AmazonName = item.Name; } } foreach (var parent in parentsDto) { //NOTE: prepare ParentSKU if (String.IsNullOrEmpty(parent.SKU)) { var mainStyleString = db.Items.GetAllViewAsDto() .Where(pi => pi.ASIN == parent.ASIN && pi.Market == (int)api.Market && pi.MarketplaceId == api.MarketplaceId) .OrderByDescending(i => i.IsDefault) .ThenBy(i => i.Id) .FirstOrDefault()?.StyleString; parent.SKU = StringHelper.JoinTwo("-", mainStyleString, parent.ASIN); } var dbParent = db.ParentItems.CreateOrUpdateParent(parent, api.Market, api.MarketplaceId, _time.GetAppNowTime()); _log.Warn("Parent item was stored, ParentASIN=" + parent.ASIN); _syncInfo.AddSuccess(parent.ASIN, "Parent item was stored"); } _log.Debug("End process new parents"); }
public void Import(IMarketApi api) { var asinsWithError = new List <string>(); var items = api.GetItems(_log, _time, null, ItemFillMode.Defualt, out asinsWithError); _log.Info("Items count: " + items.Count()); var index = 0; var updatedListingIds = new List <long>(); var processOnlyStyle = true; foreach (var item in items) { _log.Info("item: " + index); CreateOrUpdateListing(_dbFactory, _time, _log, api.Market, api.MarketplaceId, item, canCreate: true, updateQty: true, updateStyleInfo: true, mapByBarcode: true, processOnlyStyle: false); updatedListingIds.AddRange(item.Variations.Where(v => v.ListingEntityId.HasValue).Select(v => v.ListingEntityId.Value).ToList()); index++; } //Mark as deleted not exists if (!processOnlyStyle) { using (var db = _dbFactory.GetRWDb()) { var notExistListings = db.Listings.GetAll().Where(l => !updatedListingIds.Contains(l.Id) && l.Market == (int)api.Market && (l.MarketplaceId == api.MarketplaceId || String.IsNullOrEmpty(api.MarketplaceId))) .ToList(); foreach (var listing in notExistListings) { _log.Info("Listing was removed, SKU=" + listing.SKU); listing.IsRemoved = true; listing.UpdateDate = _time.GetAppNowTime(); } db.Commit(); } } }
public static ItemRankViewModel GetRank(IMarketApi api, IUnitOfWork db, ILogService log, ITime time, string asin) { var model = new ItemRankViewModel(); var soldFrom = time.GetAppNowTime().Date.AddDays(-7); asin = StringHelper.TrimWhitespace(asin); model.ASIN = asin; var asinsWithError = new List <string>(); var parentItems = api.GetItems(log, time, MarketItemFilters.Build(new List <string>() { asin }), ItemFillMode.NoAdv, out asinsWithError) .Where(i => i.IsAmazonUpdated == true) .ToList(); if (parentItems.Any()) { var parentItem = parentItems.First(); if (!String.IsNullOrEmpty(parentItem.TempParentASIN)) { parentItems = api.GetItems(log, time, MarketItemFilters.Build(new List <string>() { parentItem.TempParentASIN }), ItemFillMode.NoAdv, out asinsWithError) .Where(i => i.IsAmazonUpdated == true) .ToList(); } if (parentItems.Any()) { model.Rank = parentItems.First().Rank; model.ClosestListings = GetClosestListings(db, model.Rank, soldFrom); return(model); } } throw new Exception("Not found"); }
public void ProcessExistingParents(IUnitOfWork db, IMarketApi api, ISyncInformer syncInfo, IList <string> parentASINs, IList <ItemDTO> items) { var parentASINsWithError = new List <string>(); _log.Debug("Begin process existing parents, count=" + parentASINs.Count); var parentsDto = api.GetItems(_log, _time, MarketItemFilters.Build(parentASINs), ItemFillMode.NoAdv, out parentASINsWithError).ToList(); _log.Debug("Error when GetItems, parentASINS: " + String.Join(", ", parentASINsWithError)); //Only update fields foreach (var parent in parentsDto) { //NOTE: in case when parent item has "no-img" using child image if (String.IsNullOrEmpty(parent.ImageSource)) { var childImage = items.FirstOrDefault(i => i.ParentASIN == parent.ASIN && !String.IsNullOrEmpty(i.ImageUrl)); if (childImage != null) { parent.ImageSource = childImage.ImageUrl; } } var dbParent = db.ParentItems.CreateOrUpdateParent(parent, api.Market, api.MarketplaceId, _time.GetAppNowTime()); _log.Warn("Parent item was updated, ParentASIN=" + parent.ASIN); syncInfo.AddSuccess(parent.ASIN, "Parent item was updated"); } _log.Debug("End process existing parents"); }
public void UpdateParentsForExistantItems(IUnitOfWork db, IMarketApi api, ISyncInformer syncInfo) { var parentASINsWithError = new List <string>(); var allExistingItems = (from i in db.Items.GetAll() join l in db.Listings.GetAll() on i.Id equals l.ItemId where i.Market == (int)api.Market && (i.MarketplaceId == api.MarketplaceId || String.IsNullOrEmpty(api.MarketplaceId)) && !l.IsRemoved select new { Id = i.Id, ParentASIN = i.ParentASIN, MarketParentASIN = i.MarketParentASIN, StyleString = i.StyleString, IsDefault = l.IsDefault, }) .ToList(); var parentASINs = allExistingItems .Where(i => !String.IsNullOrEmpty(i.ParentASIN)) .Select(i => i.ParentASIN) .Distinct() .ToList(); var marketParentASINs = allExistingItems .Where(i => !String.IsNullOrEmpty(i.MarketParentASIN)) .Select(i => i.MarketParentASIN) .Distinct() .ToList(); _log.Debug("Begin process existing parents, count=" + marketParentASINs.Count); var marketParentsDto = api.GetItems(_log, _time, MarketItemFilters.Build(marketParentASINs), ItemFillMode.NoAdv, out parentASINsWithError).ToList(); _log.Debug("Error when GetItems, parentASINS: " + String.Join(", ", parentASINsWithError)); foreach (var parentASIN in parentASINs) { var existingItems = allExistingItems.Where(i => i.ParentASIN == parentASIN).ToList(); //Select actual ParentASIN info var parentASINCandidates = existingItems.GroupBy(i => i.MarketParentASIN) .Select(i => new { MarketParentASIN = i.Key, Count = i.Count() }) .OrderByDescending(i => i.Count) .ToList(); var parentASINwinner = parentASINCandidates.FirstOrDefault(i => !String.IsNullOrEmpty(i.MarketParentASIN)); if (parentASINwinner != null) { var parentItemDto = marketParentsDto.FirstOrDefault(pi => pi.ASIN == parentASINwinner.MarketParentASIN); //NOTE: TEMP prepare ParentSKU, For first updating Parent SKUs, afterward keep logic only for new if (String.IsNullOrEmpty(parentItemDto.SKU)) { var mainStyleString = existingItems.OrderByDescending(i => i.IsDefault).ThenBy(i => i.Id).FirstOrDefault()?.StyleString; parentItemDto.SKU = StringHelper.JoinTwo("-", mainStyleString, parentItemDto.ASIN); } if (parentItemDto != null && parentItemDto.IsAmazonUpdated == true) { if (parentASIN != parentItemDto.ASIN) { var existNewParent = db.ParentItems.GetByASIN(parentItemDto.ASIN, api.Market, api.MarketplaceId); if (existNewParent == null) { db.ParentItems.UpdateParent(parentASIN, parentItemDto, api.Market, api.MarketplaceId, _time.GetAppNowTime()); } else { var dbParentItem = db.ParentItems.GetByASIN(parentASIN, api.Market, api.MarketplaceId); if (dbParentItem != null) { db.ParentItems.Remove(dbParentItem); db.Commit(); } } //Update child items references var childItemsToUpdate = db.Items.GetAll() .Where(i => i.ParentASIN == parentASIN && i.Market == (int)api.Market && (i.MarketplaceId == api.MarketplaceId || String.IsNullOrEmpty(api.MarketplaceId))) .ToList(); childItemsToUpdate.ForEach(i => i.ParentASIN = parentItemDto.ASIN); db.Commit(); _log.Warn("Parent item was updated, ParentASIN=" + parentASIN + "=>" + parentItemDto.ASIN); } else { //NOTE: just update info or create missing parent item db.ParentItems.CreateOrUpdateParent(parentItemDto, api.Market, api.MarketplaceId, _time.GetAppNowTime()); } } else { //NOTE: fill FAKE parentItem if (parentItemDto.IsAmazonUpdated != true && (String.IsNullOrEmpty(parentItemDto.AmazonName) || String.IsNullOrEmpty(parentItemDto.ImageSource))) { var mainItemId = existingItems.OrderByDescending(i => i.IsDefault).FirstOrDefault()?.Id; if (mainItemId.HasValue) { var mainItem = db.Items.Get(mainItemId.Value); parentItemDto.AmazonName = mainItem?.Title; parentItemDto.ImageSource = mainItem?.PrimaryImage; } } if (parentItemDto != null) { db.ParentItems.CreateOrUpdateParent(parentItemDto, api.Market, api.MarketplaceId, _time.GetAppNowTime()); } } } } _log.Debug("End process existing parents"); }