コード例 #1
0
        public void PrintPdfFromBatch(long batchId)
        {
            var labelService = new LabelService(GetShipmentProviders(_company), _log, _time, _dbFactory, _emailService, new PdfMakerByIText(_log), AddressService.Default);

            using (var db = new UnitOfWork(_log))
            {
                var orderIds = db.OrderBatches.GetOrderIdsForBatch(
                    batchId,
                    OrderStatusEnumEx.AllUnshippedWithShipped //for composing pdf file with all
                    );

                var batchDto = db.OrderBatches.Get(batchId);

                var shippingList = db.OrderShippingInfos.GetOrderInfoWithItems(_weightService, orderIds.ToList(), (SortMode)SortMode.ByLocation, unmaskReferenceStyle: false, includeSourceItems: false)
                                   .ToList();
                shippingList = SortHelper.Sort(shippingList, SortMode.ByShippingMethodThenLocation).Take(9).ToList().ToList();

                var result = new PrintLabelResult();
                labelService.BuildPdfFile(shippingList,
                                          new string[] { },
                                          new BatchInfoToPrint()
                {
                    BatchId          = batchId,
                    BatchName        = batchDto.Name,
                    NumberOfPackages = shippingList.Count,
                    Date             = _time.GetAmazonNowTime(),
                    Carriers         = shippingList.GroupBy(sh => sh.ShippingMethod.CarrierName)
                                       .Select(c => new {
                        Key   = c.Key,
                        Value = c.Count()
                    })
                                       .ToDictionary(d => d.Key, d => d.Value)
                },
                                          AppSettings.LabelDirectory,
                                          ref result);
            }
        }
コード例 #2
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");
        }
コード例 #3
0
        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;
                        }
                    }
                }
            }
        }
コード例 #4
0
        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;
                }
            }
        }