예제 #1
0
        public long Save(IUnitOfWork db, DateTime when, long?by)
        {
            if (!InputStyleItemId.HasValue)
            {
                return(0);
            }

            var barcode = new StyleItemBarcode()
            {
                Barcode     = Barcode,
                StyleItemId = InputStyleItemId.Value,
                CreateDate  = when,
                CreatedBy   = by
            };

            db.StyleItemBarcodes.Add(barcode);

            db.Commit();

            var styleItem = db.StyleItems.GetAllAsDto().FirstOrDefault(si => si.StyleItemId == InputStyleItemId);

            if (styleItem != null)
            {
                StyleId     = styleItem.StyleId;
                StyleString = InputStyleString;
                StyleItemId = styleItem.StyleItemId;
                Size        = styleItem.Size;
            }

            return(barcode.Id);
        }
        private void CreateOrUpdateListing(IDbFactory dbFactory,
                                           ITime time,
                                           ILogService log,
                                           MarketType market,
                                           string marketplaceId,
                                           ParentItemDTO product,
                                           bool canCreate,
                                           bool updateQty,
                                           bool updateStyleInfo,
                                           bool mapByBarcode,
                                           bool processOnlyStyle)
        {
            using (var db = dbFactory.GetRWDb())
            {
                //var productImageUrl = product.ImageSource;

                if (!product.Variations.Any())
                {
                    log.Debug("No variations, productId=" + product.SourceMarketId + ", handle=" + product.SourceMarketUrl);
                }

                var skuList  = product.Variations.Select(v => v.SKU).ToList();
                var baseSKU  = product.Variations.FirstOrDefault()?.SKU ?? "";
                var skuParts = baseSKU.Split("-".ToCharArray());

                var mainSKU = (skuList.Distinct().Count() > 1 && skuParts.Count() > 1) ? String.Join("-", skuParts.Take(skuParts.Count() - 1)) : baseSKU;
                _log.Info("Main SKU: " + mainSKU + ", variations: " + String.Join(", ", skuList));
                //var firstMRSP = product.Variations.FirstOrDefault()?.ListPrice;
                //var firstBarcode = product.Variations.FirstOrDefault()?.Barcode;

                //Style

                Style existStyle = null;

                //Get by SKU/Name
                if (existStyle == null)
                {
                    existStyle = db.Styles.GetAll().FirstOrDefault(s => s.StyleID == mainSKU);
                    if (existStyle != null)
                    {
                        _log.Info("Style found by StyleID");
                    }
                }

                var isNewStyle = false;
                //Create new
                if (existStyle == null)
                {
                    if (!canCreate)
                    {
                        _log.Info("Unable to find style for, SKU: " + mainSKU);
                        return;
                    }

                    existStyle = new Style()
                    {
                        StyleID    = mainSKU,
                        CreateDate = time.GetAppNowTime(),
                    };
                    db.Styles.Add(existStyle);

                    isNewStyle = true;
                }
                else
                {
                }

                existStyle.DropShipperId = DSHelper.DefaultDSId;

                if (updateStyleInfo || isNewStyle)
                {
                    existStyle.StyleID     = mainSKU;
                    existStyle.Description = product.Description;
                    existStyle.Name        = product.AmazonName;

                    existStyle.Image        = product.ImageSource;
                    existStyle.Manufacturer = product.Department;
                    existStyle.BulletPoint1 = product.SearchKeywords;
                    existStyle.BulletPoint2 = product.Type;
                    //existStyle.MSRP = firstMRSP;
                    //existStyle.Price = product.Variations.Select(v => v.AmazonCurrentPrice).FirstOrDefault();

                    existStyle.UpdateDate = time.GetAppNowTime();
                }

                db.Commit();

                //if (!processOnlyStyle)
                {
                    //Style Image
                    var existStyleImages = db.StyleImages.GetAll().Where(si => si.StyleId == existStyle.Id).ToList();
                    if (!existStyleImages.Any() && !String.IsNullOrEmpty(product.ImageSource))
                    {
                        var newImage = new StyleImage()
                        {
                            Image      = product.ImageSource,
                            IsDefault  = true,
                            Type       = (int)StyleImageType.HiRes,
                            StyleId    = existStyle.Id,
                            CreateDate = _time.GetAppNowTime()
                        };
                        db.StyleImages.Add(newImage);
                        db.Commit();
                    }
                }

                //StyleFeatures
                if (isNewStyle && !processOnlyStyle)
                {
                    var existFeatures =
                        db.StyleFeatureTextValues.GetAll().Where(tv => tv.StyleId == existStyle.Id).ToList();
                    var existDdlFeatures =
                        db.StyleFeatureValues.GetAll().Where(tv => tv.StyleId == existStyle.Id).ToList();
                    var allFeatureValues = db.FeatureValues.GetAllAsDto().ToList();
                    var newFeatures      = ComposeFeatureList(db,
                                                              allFeatureValues,
                                                              product.Type,
                                                              product.SearchKeywords);
                    newFeatures.Add(ComposeFeatureValue(db, "Product Type", product.Type));
                    //newFeatures.Add(ComposeFeatureValue(db, "Brand Name", product.Department));
                    foreach (var feature in newFeatures)
                    {
                        if (feature == null)
                        {
                            continue;
                        }

                        var existFeature    = existFeatures.FirstOrDefault(f => f.FeatureId == feature.Id);
                        var existDdlFeature = existDdlFeatures.FirstOrDefault(f => f.FeatureId == feature.Id);
                        if (existFeature == null && existDdlFeature == null)
                        {
                            if (feature.Type == (int)FeatureValuesType.TextBox)
                            {
                                db.StyleFeatureTextValues.Add(new StyleFeatureTextValue()
                                {
                                    FeatureId = feature.FeatureId,
                                    Value     = StringHelper.Substring(feature.Value, 512),
                                    StyleId   = existStyle.Id,

                                    CreateDate = _time.GetAppNowTime()
                                });
                            }
                            if (feature.Type == (int)FeatureValuesType.DropDown)
                            {
                                db.StyleFeatureValues.Add(new StyleFeatureValue()
                                {
                                    FeatureId      = feature.FeatureId,
                                    FeatureValueId = feature.FeatureValueId.Value,
                                    StyleId        = existStyle.Id,

                                    CreateDate = _time.GetAppNowTime()
                                });
                            }
                        }
                    }
                    db.Commit();
                }

                ParentItem existParentItem = null;
                //if (!processOnlyStyle)
                {
                    //ParentItem
                    existParentItem = db.ParentItems.GetAll().FirstOrDefault(pi => pi.Market == (int)market
                                                                             &&
                                                                             (pi.MarketplaceId ==
                                                                              marketplaceId ||
                                                                              String.IsNullOrEmpty(
                                                                                  marketplaceId))
                                                                             &&
                                                                             pi.SourceMarketId ==
                                                                             product.SourceMarketId);
                    if (existParentItem == null)
                    {
                        existParentItem = new ParentItem()
                        {
                            Market         = (int)market,
                            MarketplaceId  = marketplaceId,
                            ASIN           = product.ASIN,
                            SourceMarketId = product.SourceMarketId,
                            CreateDate     = time.GetAppNowTime(),
                        };
                        db.ParentItems.Add(existParentItem);
                    }
                    existParentItem.AmazonName           = product.AmazonName;
                    existParentItem.Type                 = product.Type;
                    existParentItem.Description          = product.Description;
                    existParentItem.ImageSource          = product.ImageSource;
                    existParentItem.SourceMarketUrl      = product.SourceMarketUrl;
                    existParentItem.SearchKeywords       = product.SearchKeywords;
                    existParentItem.UpdateDate           = time.GetAppNowTime();
                    existParentItem.IsAmazonUpdated      = true;
                    existParentItem.LastUpdateFromAmazon = time.GetAppNowTime();

                    db.Commit();

                    product.Id = existParentItem.Id;
                }

                //if (!processOnlyStyle)
                {
                    foreach (var variation in product.Variations)
                    {
                        //StyleItem
                        var existStyleItem = db.StyleItems.GetAll()
                                             .FirstOrDefault(si => si.StyleId == existStyle.Id);
                        // && (si.Color == variation.Color || String.IsNullOrEmpty(variation.Color)));

                        var isNewStyleItem = false;
                        if (existStyleItem == null)
                        {
                            if (!canCreate && !isNewStyle)
                            {
                                _log.Info("Unable to find StyleItem for styleId: " + existStyle.StyleID);
                                return;
                            }

                            existStyleItem = new StyleItem()
                            {
                                StyleId = existStyle.Id,
                                Size    = variation.Size,
                                Color   = variation.Color,
                            };
                            db.StyleItems.Add(existStyleItem);

                            isNewStyleItem = true;
                        }

                        //TEMP:
                        //if (updateQty || isNewStyle)
                        //{
                        //    existStyleItem.Quantity = variation.AmazonRealQuantity;
                        //    existStyleItem.QuantitySetDate = time.GetAppNowTime();
                        //}
                        db.Commit();

                        variation.StyleItemId = existStyleItem.Id;

                        //StyleItem Barcode
                        if (!String.IsNullOrEmpty(variation.Barcode))
                        {
                            var existBarcode =
                                db.StyleItemBarcodes.GetAll().FirstOrDefault(b => b.Barcode == variation.Barcode);
                            if (existBarcode == null)
                            {
                                _log.Info("Added new barcode: " + variation.Barcode);
                                existBarcode = new StyleItemBarcode()
                                {
                                    Barcode    = variation.Barcode,
                                    CreateDate = time.GetAppNowTime(),
                                };
                                db.StyleItemBarcodes.Add(existBarcode);
                            }
                            existBarcode.StyleItemId = existStyleItem.Id;
                            existBarcode.UpdateDate  = time.GetAppNowTime();
                            db.Commit();
                        }

                        //if (!processOnlyStyle)
                        {
                            //Item
                            var existItem =
                                db.Items.GetAll().FirstOrDefault(v => v.SourceMarketId == variation.SourceMarketId &&
                                                                 v.Market == (int)market
                                                                 &&
                                                                 (v.MarketplaceId == marketplaceId ||
                                                                  String.IsNullOrEmpty(marketplaceId)));
                            if (existItem == null)
                            {
                                existItem = new Item()
                                {
                                    ParentASIN     = existParentItem.ASIN,
                                    ASIN           = variation.ASIN,
                                    SourceMarketId = variation.SourceMarketId,
                                    Market         = (int)market,
                                    MarketplaceId  = marketplaceId,
                                    CreateDate     = time.GetAppNowTime(),
                                };
                                db.Items.Add(existItem);
                            }

                            existItem.StyleId     = existStyle.Id;
                            existItem.StyleItemId = existStyleItem.Id;

                            existItem.Title   = product.AmazonName;
                            existItem.Barcode = variation.Barcode;
                            existItem.Color   = variation.Color;
                            existItem.Size    = variation.Size;

                            existItem.SourceMarketUrl = variation.SourceMarketUrl;

                            existItem.PrimaryImage = variation.ImageUrl;
                            existItem.ListPrice    = (decimal?)variation.ListPrice;

                            existItem.UpdateDate              = time.GetAppNowTime();
                            existItem.IsExistOnAmazon         = true;
                            existItem.LastUpdateFromAmazon    = time.GetAppNowTime();
                            existItem.ItemPublishedStatus     = variation.PublishedStatus;
                            existItem.ItemPublishedStatusDate = variation.PuclishedStatusDate ?? time.GetAppNowTime();

                            db.Commit();

                            variation.Id = existItem.Id;

                            //Listing
                            var existListing =
                                db.Listings.GetAll().FirstOrDefault(v => v.ListingId == variation.ListingId.ToString() &&
                                                                    v.Market == (int)market
                                                                    &&
                                                                    (v.MarketplaceId == marketplaceId ||
                                                                     String.IsNullOrEmpty(marketplaceId)));
                            if (existListing == null)
                            {
                                existListing = new Listing()
                                {
                                    ItemId = existItem.Id,

                                    ASIN      = variation.ASIN,
                                    ListingId = StringHelper.GetFirstNotEmpty(variation.ASIN, variation.ListingId),

                                    Market        = (int)market,
                                    MarketplaceId = marketplaceId,

                                    CreateDate = time.GetAppNowTime(),
                                };
                                db.Listings.Add(existListing);
                            }

                            existListing.SKU = variation.SKU;

                            existListing.CurrentPrice                 = (decimal)(variation.AmazonCurrentPrice ?? 0);
                            existListing.AmazonCurrentPrice           = (decimal?)variation.AmazonCurrentPrice;
                            existListing.AmazonCurrentPriceUpdateDate = time.GetAmazonNowTime();
                            existListing.PriceFromMarketUpdatedDate   = time.GetAppNowTime();

                            existListing.RealQuantity                 = variation.AmazonRealQuantity ?? 0;
                            existListing.AmazonRealQuantity           = variation.AmazonRealQuantity ?? 0;
                            existListing.AmazonRealQuantityUpdateDate = time.GetAppNowTime();

                            existListing.UpdateDate = time.GetAppNowTime();
                            existListing.IsRemoved  = false;

                            db.Commit();

                            variation.ListingEntityId = existListing.Id;
                        }
                    }
                }
            }
        }
        private void CreateOrUpdateListing(IDbFactory dbFactory,
                                           ITime time,
                                           ILogService log,
                                           MarketType market,
                                           string marketplaceId,
                                           ParentItemDTO product)
        {
            using (var db = dbFactory.GetRWDb())
            {
                //var productImageUrl = product.ImageSource;

                if (!product.Variations.Any())
                {
                    log.Debug("No variations, productId=" + product.SourceMarketId + ", handle=" + product.SourceMarketUrl);
                }

                var firstSKU     = product.Variations.FirstOrDefault()?.SKU;
                var firstMRSP    = product.Variations.FirstOrDefault()?.ListPrice;
                var firstBarcode = product.Variations.FirstOrDefault()?.Barcode;

                //Style

                Style existStyle = null;

                //Get by SKU/Name
                if (existStyle == null)
                {
                    existStyle = db.Styles.GetAll().FirstOrDefault(s => s.StyleID == firstSKU);
                    if (existStyle != null)
                    {
                        _log.Info("Style found by StyleID");
                    }
                }

                //Get by barcode
                if (existStyle == null && !String.IsNullOrEmpty(firstBarcode))
                {
                    var barcode = db.StyleItemBarcodes.GetAll().FirstOrDefault(b => b.Barcode == firstBarcode);
                    if (barcode != null)
                    {
                        var styleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.Id == barcode.StyleItemId);
                        existStyle = db.Styles.GetAll().FirstOrDefault(s => s.Id == styleItem.StyleId);
                    }
                    if (existStyle != null)
                    {
                        _log.Info("Style found by barcode");
                    }
                }


                //ParentItem
                var existParentItem = db.ParentItems.GetAll().FirstOrDefault(pi => pi.Market == (int)market &&
                                                                             (pi.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)) &&
                                                                             pi.SourceMarketId == product.SourceMarketId);
                if (existParentItem == null)
                {
                    existParentItem = new ParentItem()
                    {
                        Market         = (int)market,
                        MarketplaceId  = marketplaceId,
                        ASIN           = product.ASIN,
                        SourceMarketId = product.SourceMarketId,
                        CreateDate     = time.GetAppNowTime(),
                    };
                    db.ParentItems.Add(existParentItem);
                }
                existParentItem.SourceMarketUrl      = product.SourceMarketUrl;
                existParentItem.UpdateDate           = time.GetAppNowTime();
                existParentItem.IsAmazonUpdated      = true;
                existParentItem.LastUpdateFromAmazon = time.GetAppNowTime();

                db.Commit();

                product.Id = existParentItem.Id;

                foreach (var variation in product.Variations)
                {
                    //StyleItem
                    var existStyleItem = existStyle != null?db.StyleItems.GetAll()
                                         .FirstOrDefault(si => si.StyleId == existStyle.Id) : null;

                    variation.StyleItemId = existStyleItem?.Id;

                    //StyleItem Barcode
                    if (!String.IsNullOrEmpty(variation.Barcode) &&
                        existStyleItem != null)
                    {
                        var existBarcode = db.StyleItemBarcodes.GetAll().FirstOrDefault(b => b.StyleItemId == existStyleItem.Id &&
                                                                                        b.Barcode == variation.Barcode);
                        if (existBarcode == null)
                        {
                            _log.Info("Added new barcode: " + variation.Barcode);
                            existBarcode = new StyleItemBarcode()
                            {
                                Barcode    = variation.Barcode,
                                CreateDate = time.GetAppNowTime(),
                            };
                            db.StyleItemBarcodes.Add(existBarcode);
                        }
                        existBarcode.StyleItemId = existStyleItem.Id;
                        existBarcode.UpdateDate  = time.GetAppNowTime();
                        db.Commit();
                    }

                    //Item
                    var existItem = db.Items.GetAll().FirstOrDefault(v => v.SourceMarketId == variation.SourceMarketId &&
                                                                     v.Market == (int)market &&
                                                                     (v.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)));
                    if (existItem == null)
                    {
                        existItem = new Item()
                        {
                            ParentASIN     = existParentItem.ASIN,
                            ASIN           = variation.ASIN,
                            SourceMarketId = variation.SourceMarketId,
                            Market         = (int)market,
                            MarketplaceId  = marketplaceId,
                            CreateDate     = time.GetAppNowTime(),
                        };
                        db.Items.Add(existItem);
                    }

                    existItem.StyleId     = existStyle.Id;
                    existItem.StyleItemId = existStyleItem.Id;

                    //existItem.Title = product.AmazonName;
                    existItem.Barcode = variation.Barcode;
                    //existItem.Color = variation.Color;

                    existItem.SourceMarketUrl = variation.SourceMarketUrl;

                    //existItem.PrimaryImage = variation.ImageUrl;
                    //existItem.ListPrice = (decimal?)variation.ListPrice;

                    existItem.UpdateDate              = time.GetAppNowTime();
                    existItem.IsExistOnAmazon         = true;
                    existItem.LastUpdateFromAmazon    = time.GetAppNowTime();
                    existItem.ItemPublishedStatus     = variation.PublishedStatus;
                    existItem.ItemPublishedStatusDate = variation.PuclishedStatusDate ?? time.GetAppNowTime();

                    db.Commit();

                    variation.Id = existItem.Id;

                    //Listing
                    var existListing = db.Listings.GetAll().FirstOrDefault(v => v.ListingId == variation.ListingId.ToString() &&
                                                                           v.Market == (int)market &&
                                                                           (v.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)));
                    if (existListing == null)
                    {
                        existListing = new Listing()
                        {
                            ItemId = existItem.Id,

                            ASIN      = variation.ASIN,
                            ListingId = variation.ListingId,

                            Market        = (int)market,
                            MarketplaceId = marketplaceId,

                            CreateDate = time.GetAppNowTime(),
                        };
                        db.Listings.Add(existListing);
                    }

                    existListing.SKU = variation.SKU;
                    //existListing.CurrentPrice = (decimal)(variation.AmazonCurrentPrice ?? 0);
                    existListing.AmazonCurrentPrice           = (decimal?)variation.AmazonCurrentPrice;
                    existListing.AmazonCurrentPriceUpdateDate = time.GetAmazonNowTime();
                    existListing.PriceFromMarketUpdatedDate   = time.GetAppNowTime();

                    //existListing.RealQuantity = variation.AmazonRealQuantity ?? 0;
                    existListing.AmazonRealQuantity           = variation.AmazonRealQuantity ?? 0;
                    existListing.AmazonRealQuantityUpdateDate = time.GetAppNowTime();

                    existListing.UpdateDate = time.GetAppNowTime();
                    existListing.IsRemoved  = false;

                    db.Commit();

                    variation.ListingEntityId = existListing.Id;
                }
            }
        }