private void SetItemFieldsIfExistOnAmazon(IUnitOfWork db, ItemDTO dtoItem, Item dbItem)
        {
            //NOTE: Skip any ParentASIN updates
            //NOTE: updates for childs only when we change ParentItem objects
            //NOTE: to keep source relationships
            //if (!String.IsNullOrEmpty(dtoItem.ParentASIN))
            //{
            //    dbItem.ParentASIN = dtoItem.ParentASIN;
            //}

            //NOTE: only when no ParentASIN
            if (String.IsNullOrEmpty(dbItem.ParentASIN) &&
                !String.IsNullOrEmpty(dtoItem.ParentASIN))
            {
                dbItem.ParentASIN = dtoItem.ParentASIN;
            }

            if (dbItem.ASIN != dtoItem.ASIN)
            {
                _log.Info("For SKU=" + dtoItem.SKU + "(id: " + dbItem.Id + ")" + ", ASIN=" + dbItem.ASIN + "=>" + dtoItem.ASIN);
                dbItem.ASIN = dtoItem.ASIN;
            }

            if (dbItem.ItemPublishedStatus == (int)PublishedStatuses.New ||
                dbItem.ItemPublishedStatus == (int)PublishedStatuses.PublishedInactive ||
                dbItem.ItemPublishedStatus == (int)PublishedStatuses.PublishedInProgress ||
                dbItem.ItemPublishedStatus == (int)PublishedStatuses.PublishingErrors ||
                dbItem.ItemPublishedStatus == (int)PublishedStatuses.None ||
                dbItem.ItemPublishedStatus == (int)PublishedStatuses.HasPublishRequest)
            //NOTE: should be excluded HasUnpublishdRequest
            {
                dbItem.ItemPublishedStatus       = (int)PublishedStatuses.Published;
                dbItem.ItemPublishedStatusReason = "";
                dbItem.ItemPublishedStatusDate   = _time.GetAppNowTime();
            }

            dbItem.ItemPublishedStatusFromMarket     = (int)PublishedStatuses.Published;
            dbItem.ItemPublishedStatusFromMarketDate = _time.GetAppNowTime();

            dbItem.IsAmazonParentASIN = dtoItem.ParentASIN == dbItem.ParentASIN;
            dbItem.SourceMarketId     = dtoItem.SourceMarketId;
            dbItem.MarketParentASIN   = dtoItem.ParentASIN;

            if (dbItem.OnMarketTemplateName != dtoItem.OnMarketTemplateName)
            {
                _log.Debug("Template changed: " + dbItem.OnMarketTemplateName + "=>" + dtoItem.OnMarketTemplateName);
            }
            dbItem.OnMarketTemplateName = dtoItem.OnMarketTemplateName;

            //Updates size if came not null
            if (!String.IsNullOrEmpty(dtoItem.Size))
            {
                var newSize = StringHelper.Substring(dtoItem.Size, 50);
                if (dbItem.Size != newSize)
                {
                    if (_itemHistoryService != null)
                    {
                        _itemHistoryService.AddRecord(dbItem.Id,
                                                      ItemHistoryHelper.SizeKey,
                                                      dbItem.Size,
                                                      "ListingLineProcessing.SetItemFieldsIfExistOnAmazon",
                                                      newSize,
                                                      null,
                                                      null);
                    }
                    dbItem.Size = newSize;
                }
            }
            else
            {
                dtoItem.Size = dbItem.Size; //NOTE: item DTO size using in below code (should be actual)
            }

            dbItem.IsExistOnAmazon      = true;
            dbItem.LastUpdateFromAmazon = _time.GetUtcTime();

            //Set style string
            if (String.IsNullOrEmpty(dbItem.StyleString))
            {
                dtoItem.StyleString = SkuHelper.RetrieveStyleIdFromSKU(db,
                                                                       dtoItem.SKU,
                                                                       dtoItem.Name);

                _log.Debug(string.Format("StyleString updated from={0}, to={1}", dbItem.StyleString, dtoItem.StyleString));
                dbItem.StyleString = dtoItem.StyleString;
            }
            else
            {
                dtoItem.StyleString = dbItem.StyleString;
            }

            //Additional Fields
            dbItem.Title = dtoItem.Name;
            if (!String.IsNullOrEmpty(dtoItem.ImageUrl))
            {
                dbItem.PrimaryImage = dtoItem.ImageUrl;
            }
            dbItem.BrandName        = dtoItem.BrandName;
            dbItem.Type             = StringHelper.Substring(dtoItem.Type, 50);
            dbItem.ListPrice        = dtoItem.ListPrice;
            dbItem.Color            = StringHelper.Substring(dtoItem.Color, 50);
            dbItem.Department       = dtoItem.Department;
            dbItem.AdditionalImages = dtoItem.AdditionalImages;
            if (!String.IsNullOrEmpty(dtoItem.Features))
            {
                dbItem.Features = dtoItem.Features;
            }
            if (!String.IsNullOrEmpty(dtoItem.SearchKeywords))
            {
                dbItem.SearchKeywords = dtoItem.SearchKeywords;
            }
            if (!String.IsNullOrEmpty(dtoItem.Barcode))
            {
                dbItem.Barcode = dtoItem.Barcode;
            }

            //Setting styleId, create style, styleItem if not exist
            if (!dbItem.StyleItemId.HasValue)
            {
                //Kepp exists styleId
                dtoItem.StyleId = dbItem.StyleId;

                _log.Info("FindOrCreateStyleItem");
                var styleItem = FindOrCreateStyleItem(db, dtoItem);
                if (styleItem != null)
                {
                    if (styleItem.StyleItemId > 0)
                    {
                        _styleHistoryService.AddRecord(styleItem.StyleId,
                                                       StyleHistoryHelper.AttachListingKey,
                                                       dbItem.StyleItemId,
                                                       dbItem.Market + ":" + dbItem.MarketplaceId,
                                                       styleItem.StyleItemId,
                                                       dbItem.Id.ToString() + ":" + dbItem.ASIN,
                                                       null);

                        dbItem.StyleId     = styleItem.StyleId;
                        dbItem.StyleItemId = styleItem.StyleItemId;
                    }
                    else
                    {
                        if (!dbItem.StyleId.HasValue)
                        {
                            dbItem.StyleId = styleItem.StyleId;
                        }
                    }

                    _log.Debug(String.Format("Set for ASIN={0}, styleId={1}, styleItemId={2}",
                                             dtoItem.ASIN,
                                             styleItem.StyleId,
                                             styleItem.StyleItemId));
                }
                else
                {
                    _log.Info("Empty StyleItem");
                }
            }

            //Update style image
            if (dbItem.StyleId.HasValue &&
                !String.IsNullOrEmpty(dbItem.PrimaryImage))
            {
                _styleManager.UpdateStyleImageIfEmpty(db, dbItem.StyleId.Value, dbItem.PrimaryImage);
            }

            if (dtoItem.Weight.HasValue &&
                dbItem.StyleItemId.HasValue)
            {
                var dbStyleItem = db.StyleItems.Get(dbItem.StyleItemId.Value);
                if (!dbStyleItem.Weight.HasValue)
                {
                    dbStyleItem.Weight = dtoItem.Weight;
                }
            }

            if (!String.IsNullOrEmpty(dtoItem.Barcode) &&
                dbItem.StyleItemId.HasValue)
            {
                _styleManager.StoreOrUpdateBarcode(db,
                                                   dbItem.StyleItemId.Value,
                                                   dtoItem.Barcode);
            }
        }
        public void ProcessNewListingsWithItems(IUnitOfWork db, IMarketApi api, ITime time, IList <ItemDTO> items)
        {
            _log.Debug("Begin process new items");

            foreach (var dtoItem in items)
            {
                try
                {
                    if (dtoItem.IsExistOnAmazon == true)
                    {
                        _syncInfo.AddSuccess(dtoItem.ASIN, "New listing item was filled by Amazon");

                        if (String.IsNullOrEmpty(dtoItem.ParentASIN))
                        {
                            _syncInfo.AddWarning(dtoItem.ASIN, "Empty ParentASIN");
                        }

                        dtoItem.IsAmazonParentASIN   = !String.IsNullOrEmpty(dtoItem.ParentASIN);
                        dtoItem.LastUpdateFromAmazon = time.GetUtcTime();
                        //Add new item, no need to additional check
                        dtoItem.StyleString = SkuHelper.RetrieveStyleIdFromSKU(db,
                                                                               dtoItem.SKU,
                                                                               dtoItem.Name);


                        var dbItem = db.Items.StoreItemIfNotExist(_itemHistoryService,
                                                                  "ListingLineProcessing",
                                                                  dtoItem,
                                                                  api.Market,
                                                                  api.MarketplaceId,
                                                                  _companyId,
                                                                  time.GetAppNowTime());

                        //NOTE: fresh size (for some reason can start came emtpy)
                        dtoItem.Size = dbItem.Size;

                        if (!dbItem.StyleItemId.HasValue)
                        {
                            //Keep exists styleId
                            dtoItem.StyleId = dbItem.StyleId;

                            var styleItem = FindOrCreateStyleItem(db, dtoItem);
                            if (styleItem != null)
                            {
                                if (styleItem.StyleItemId > 0)
                                {
                                    dbItem.StyleId     = styleItem.StyleId;
                                    dbItem.StyleItemId = styleItem.StyleItemId;
                                }
                                else
                                {
                                    if (!dbItem.StyleId.HasValue)
                                    {
                                        dbItem.StyleId = styleItem.StyleId;
                                    }
                                }
                                _log.Debug(String.Format("Set for ASIN={0}, styleId={1}, styleItemId={2}",
                                                         dtoItem.ASIN,
                                                         styleItem.StyleId,
                                                         styleItem.StyleItemId));
                            }
                            db.Commit();
                        }

                        if (!String.IsNullOrEmpty(dtoItem.Barcode) &&
                            dbItem.StyleItemId.HasValue)
                        {
                            _styleManager.StoreOrUpdateBarcode(db,
                                                               dbItem.StyleItemId.Value,
                                                               dtoItem.Barcode);
                        }

                        var dbListing = db.Listings.StoreOrUpdate(dtoItem,
                                                                  dbItem,
                                                                  api.Market,
                                                                  api.MarketplaceId,
                                                                  time.GetAppNowTime());

                        dtoItem.IsDefault = dbListing.IsDefault;

                        _syncInfo.AddSuccess(dtoItem.ASIN, "New listing item was stored");
                        _log.Debug("Store item:" + dbItem.ASIN + ", parentASIN=" + dbItem.ParentASIN + ", SKU=" + dtoItem.SKU + ", StyleString=" + dbItem.StyleString + ", quantity=" + dtoItem.RealQuantity);
                    }
                    else
                    {
                        _syncInfo.AddWarning(dtoItem.ASIN, "Item is not filled by Amazon (new listing item)");
                        _log.Warn("Item is not filled by Amazon (new listing item), item=" + dtoItem.ASIN);
                    }
                }
                catch (Exception ex)
                {
                    _syncInfo.AddError(dtoItem.ASIN, "Error while creating item", ex);
                    _log.Error(string.Format("Error while creating item, asin={0}", dtoItem.ASIN), ex);
                }
            }
            _log.Debug("End process new items");
        }
예제 #3
0
        public void ReadItemsInfo(SupplieroasisApi api)
        {
            _log.Info("Begin ReadItemsInfo");
            var itemsWithError = new List <string>();
            var items          = api.GetItems(_log,
                                              _time,
                                              null,
                                              ItemFillMode.Defualt,
                                              out itemsWithError);

            using (var db = _dbFactory.GetRWDb())
            {
                _log.Info("Total items=" + items.Count());
                //Create
                var allItems = db.Items.GetAllViewAsDto().Where(m => m.Market == (int)api.Market &&
                                                                (m.MarketplaceId == api.MarketplaceId ||
                                                                 String.IsNullOrEmpty(api.MarketplaceId))).ToList();
                foreach (var item in items)
                {
                    foreach (var variation in item.Variations)
                    {
                        var existItem = allItems.FirstOrDefault(i => StringHelper.IsEqualNoCase(i.SKU, variation.SKU));

                        if (existItem == null)
                        {
                            _log.Info("Add new item, MarketItemId=" + variation.SourceMarketId);

                            var exampleItem = db.Items.GetAllViewAsDto().FirstOrDefault(i => i.Market == (int)MarketType.Amazon &&
                                                                                        i.MarketplaceId == MarketplaceKeeper.AmazonComMarketplaceId &&
                                                                                        i.SKU == variation.SKU);
                            if (exampleItem == null)
                            {
                                exampleItem = db.Items.GetAllViewAsDto().OrderByDescending(i => i.CreateDate).FirstOrDefault(i => i.SKU == variation.SKU);
                            }

                            var styleString = SkuHelper.RetrieveStyleIdFromSKU(db, variation.SKU, null);
                            if (String.IsNullOrEmpty(styleString))
                            {
                                styleString = variation.SKU;
                            }

                            if (String.IsNullOrEmpty(styleString))
                            {
                                _log.Info("StyleString is empty for: " + variation.SKU + ", itemSKU=" + item.SKU);
                                continue;
                            }

                            var parentItem = db.ParentItems.GetAll().FirstOrDefault(pi => pi.ASIN == styleString &&
                                                                                    pi.Market == (int)MarketType.OverStock);
                            if (parentItem == null)
                            {
                                parentItem = new ParentItem()
                                {
                                    ASIN           = styleString,
                                    SourceMarketId = styleString,
                                    Market         = item.Market,
                                    MarketplaceId  = item.MarketplaceId,

                                    CreateDate = _time.GetAmazonNowTime()
                                };

                                db.ParentItems.Add(parentItem);
                                db.Commit();
                            }

                            if (exampleItem == null)
                            {
                                exampleItem = new DTO.ItemDTO();
                                var style = db.Styles.GetAll().FirstOrDefault(st => st.StyleID == styleString);
                                if (style != null)
                                {
                                    exampleItem.StyleId = style.Id;
                                }
                                var size = SkuHelper.RetrieveSizeFromSKU(variation.SKU);

                                if (style != null)
                                {
                                    var styleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.StyleId == style.Id &&
                                                                                          si.Size == size);
                                    if (styleItem != null)
                                    {
                                        exampleItem.StyleItemId = styleItem.Id;
                                    }
                                }
                            }

                            var newItem = new Item()
                            {
                                ASIN          = item.ASIN,
                                ParentASIN    = parentItem.ASIN,
                                Market        = item.Market,
                                MarketplaceId = item.MarketplaceId,

                                Barcode = variation.Barcode,

                                SourceMarketId      = item.SourceMarketId,
                                ItemPublishedStatus = variation.PublishedStatus,

                                StyleId     = exampleItem?.StyleId,
                                StyleItemId = exampleItem?.StyleItemId,

                                CreateDate = _time.GetAmazonNowTime()
                            };
                            db.Items.Add(newItem);
                            db.Commit();

                            var newListing = new Listing()
                            {
                                ItemId        = newItem.Id,
                                SKU           = variation.SKU,
                                ListingId     = variation.SKU,
                                Market        = variation.Market,
                                MarketplaceId = variation.MarketplaceId,

                                CreateDate         = _time.GetAmazonNowTime(),
                                AmazonRealQuantity = variation.AmazonRealQuantity,
                            };
                            db.Listings.Add(newListing);
                            db.Commit();
                        }
                    }
                }


                //Update
                var updatedItemIds = new List <long>();
                foreach (var item in items)
                {
                    foreach (var variation in item.Variations)
                    {
                        _log.Info("Read info for SKU=" + variation.SKU);
                        var dbListing = db.Listings.GetAll().FirstOrDefault(m => m.Market == (int)api.Market &&
                                                                            (m.MarketplaceId == api.MarketplaceId ||
                                                                             String.IsNullOrEmpty(api.MarketplaceId)) &&
                                                                            m.SKU == variation.SKU);
                        if (dbListing == null)
                        {
                            _log.Info("Unable to find item for, SKU=" + variation.SKU);
                            continue;
                        }

                        var dbItem = db.Items.GetAll().FirstOrDefault(i => i.Id == dbListing.ItemId);
                        if (dbItem == null)
                        {
                            _log.Info("Unable to find item for itemId=" + dbListing.ItemId);
                            continue;
                        }

                        updatedItemIds.Add(dbItem.Id);

                        if (dbItem.ItemPublishedStatus != variation.PublishedStatus)
                        {
                            dbItem.ItemPublishedStatus       = variation.PublishedStatus;
                            dbItem.ItemPublishedStatusDate   = _time.GetAppNowTime();
                            dbItem.ItemPublishedStatusReason = "Derived from market";
                        }

                        dbItem.Barcode = variation.Barcode;

                        //dbListing.AmazonCurrentPrice = variation.AmazonCurrentPrice;
                        //dbListing.AmazonCurrentPriceUpdateDate = _time.GetAppNowTime();

                        dbListing.AmazonRealQuantity           = variation.AmazonRealQuantity;
                        dbListing.AmazonRealQuantityUpdateDate = _time.GetAppNowTime();
                    }
                }
                db.Commit();

                //Remove not exists
                var toRemoveItems = db.Items.GetAll().Where(i => i.Market == (int)api.Market &&
                                                            (i.MarketplaceId == api.MarketplaceId ||
                                                             String.IsNullOrEmpty(api.MarketplaceId)) &&
                                                            !updatedItemIds.Contains(i.Id) &&
                                                            i.ItemPublishedStatus == (int)PublishedStatuses.Published);
                _log.Info("Items to unpublish, count=" + toRemoveItems.Count());
                foreach (var toRemoveItem in toRemoveItems)
                {
                    toRemoveItem.ItemPublishedStatusBeforeRepublishing = toRemoveItem.ItemPublishedStatus;
                    toRemoveItem.ItemPublishedStatus     = (int)PublishedStatuses.Unpublished;
                    toRemoveItem.ItemPublishedStatusDate = _time.GetUtcTime();
                }
                db.Commit();
            }
            _log.Info("End ReadItemsInfo");
        }