Exemplo n.º 1
0
        public async Task <int> Handle(AddProductCommand request, CancellationToken cancellationToken)
        {
            Product product = new Product(request.ShopId, request.ProductName, request.ProductType, request.ProductImg, request.ProductPrice, request.MSellNum, request.PraiseNum, request.IsDisplay);

            _repository.AddProduct(product);

            return(await _repository.Save());
        }
Exemplo n.º 2
0
        public static ImportResult Execute(IShopRepository repository, StreamReader file, string categoryName, int currentLangId)
        {

            Log.DebugFormat("start importing products Execute");
            var res = new ImportResult { ErrorCode = 0, ProductErrors = new Dictionary<string, string>() };
            //var products = new List<ImportedProduct>();
            try
            {
                bool justAdded = false;
                int productCount = 0;

                Log.DebugFormat("read products from file");
                List<ImportedProduct> products = ReadProductsFromFile(file);
                var importedProductsCount = products.Count();
                Log.DebugFormat("total products {0}", importedProductsCount);

                var productStockToDelete = new List<int>();
                foreach (var importedProduct in products)
                {
                    productCount++;
                    if (string.IsNullOrEmpty(importedProduct.ExternalId))
                    {
                        res.ReadProductFailedCount++;
                        continue;
                    }



                    Log.DebugFormat("get product {0} of {1}", productCount, importedProductsCount);
                    var siteProduct = repository.GetProductByExternalId(importedProduct.ExternalId);

                    if (siteProduct == null)
                    {
                        try
                        {
                            // добавляем новый товар
                            var category = repository.GetCategory(categoryName);
                            var newProduct = new Product { Category = category };
                            newProduct.InjectFromImportProduct(importedProduct);
                            if (importedProduct.ImportedProductStocks != null)
                            {
                                foreach (var importedProductStock in importedProduct.ImportedProductStocks)
                                {
                                    newProduct.ProductStocks.Add(new ProductStock
                                    {
                                        StockNumber = importedProductStock.StockNumber,
                                        Color = importedProductStock.Color,
                                        Size = importedProductStock.Size,
                                        IsAvailable = importedProductStock.IsAvailable
                                    });

                                }
                            }
                            newProduct.SearchCriteriaAttributes = "";
                            repository.AddProduct(newProduct);
                            justAdded = true;
                            res.NewProductCount++;

                            //Log.Debug("get site product again");
                            siteProduct = repository.GetProductByExternalId(importedProduct.ExternalId);
                        }
                        catch (Exception ex)
                        {
                            res.ProductErrors.Add("Не удалось добавить " + importedProduct.ExternalId, ex.Message);
                            res.AddProductFailedCount++;

                            //Log.ErrorFormat("cannot add new product externalId:{0} exception message:{1}", importedProduct.ExternalId, ex.Message);
                        }
                    }




                    if (siteProduct != null)
                    {
                        try
                        {
                            siteProduct.InjectFromImportProduct(importedProduct);
                            if (siteProduct.ProductStocks == null)
                            {
                                siteProduct.ProductStocks = new LinkedList<ProductStock>();
                            }


                            //Log.DebugFormat("Updating product stocks started");
                            foreach (var productStock in siteProduct.ProductStocks)
                            {
                                var importedProductStock =
                                    importedProduct.ImportedProductStocks.FirstOrDefault(
                                        ips => ips.StockNumber == productStock.StockNumber && !ips.Imported);
                                if (importedProductStock != null)
                                {
                                    // update productStock in siteProduct.ProductStocks
                                    importedProductStock.Imported = true;
                                    productStock.Size = importedProductStock.Size;
                                    productStock.Color = importedProductStock.Color;
                                    productStock.IsAvailable = importedProductStock.IsAvailable;
                                    res.UpdatedArticles++;
                                }
                                else
                                {
                                    // remove productStock from siteProduct.ProductStocks
                                    //repository.DeleteProductStock(productStock.Id);
                                    productStockToDelete.Add(productStock.Id);
                                    res.DeletedArticles++;
                                }
                            }



                            foreach (
                                var importedProductStock in
                                    importedProduct.ImportedProductStocks.Where(ips => !ips.Imported))
                            {
                                siteProduct.ProductStocks.Add(new ProductStock
                                {
                                    StockNumber = importedProductStock.StockNumber,
                                    Color = importedProductStock.Color,
                                    Size = importedProductStock.Size,
                                    IsAvailable = importedProductStock.IsAvailable
                                });
                                res.AddedArticles++;
                            }
                            //Log.DebugFormat("Updating product stocks finished");


                            //Log.DebugFormat("Updating product attributes started");
                            foreach (var attributeGroup in importedProduct.ImportedProductAttibutes)
                            {
                                var attrToDelete = new List<int>();
                                var exId = attributeGroup.ExternalId;
                                //Log.DebugFormat("get site attr started");
                                var siteAttr =
                                    siteProduct.ProductAttributeValues.Where(
                                        pav => pav.ProductAttribute.ExternalId == exId).Select(a => a.Title).ToList();
                                //Log.DebugFormat("get site attr finished");

                                var xx = siteAttr.Except(attributeGroup.Values).ToList(); //  items to delete
                                var xy = attributeGroup.Values.Except(siteAttr).ToList(); //  items to add

                                List<string> addedAttr = new List<string>();

                                //Log.DebugFormat("get attr to delete started");
                                foreach (
                                    var attr in
                                        siteProduct.ProductAttributeValues.Where(
                                            pav => pav.ProductAttribute.ExternalId == exId))
                                {
                                    if (xx.Contains(attr.Title))
                                    {
                                        attrToDelete.Add(attr.Id);
                                    }
                                }
                                //Log.DebugFormat("get attr to delete finished");

                                //Log.DebugFormat("ProductAttributeValues.Remove started");
                                foreach (var id in attrToDelete)
                                {
                                    var a = repository.GetProductAttributeValue(id);
                                    siteProduct.ProductAttributeValues.Remove(a);
                                }
                                //Log.DebugFormat("ProductAttributeValues.Remove finished");

                                //Log.DebugFormat("repository.GetProductAttributes(siteProduct.CategoryId) started");
                                var productAttributes = repository.GetProductAttributes(siteProduct.CategoryId);
                                //Log.DebugFormat("repository.GetProductAttributes(siteProduct.CategoryId) finished");


                                //Log.DebugFormat("siteProduct.ProductAttributeValues.Add(attributeValue) started");
                                var productAttribute = productAttributes.FirstOrDefault(pa => pa.ExternalId == exId);
                                if (productAttribute == null)
                                {
                                    //Log.ErrorFormat("Атрибут с идентификатором {0} не найден", exId);
                                    throw new Exception("Атрибут с идентификатором " + exId + " не найден");
                                }
                                foreach (var attributeValue in productAttribute.ProductAttributeValues)
                                {
                                    if (xy.Contains(attributeValue.Title))
                                    {
                                        siteProduct.ProductAttributeValues.Add(attributeValue);
                                        addedAttr.Add(attributeValue.Title);
                                    }
                                }
                                //Log.DebugFormat("siteProduct.ProductAttributeValues.Add(attributeValue) finished");


                                //Log.DebugFormat("add new attributes started");
                                var attrNotFoundOnSite = xy.Except(addedAttr);
                                foreach (string s in attrNotFoundOnSite)
                                {
                                    var newProductAttributeValue = new ProductAttributeValue
                                    {
                                        CurrentLang = currentLangId,
                                        Title = s,
                                        ProductAttribute = productAttribute,
                                    };
                                    newProductAttributeValue.Products.Add(siteProduct);

                                    repository.AddProductAttributeValue(newProductAttributeValue);
                                }
                                //Log.DebugFormat("add new attributes finished");

                            }
                            //Log.DebugFormat("Updating product attributes finished");



                            Log.DebugFormat("save product {0} of {1}", productCount, importedProductsCount);
                            repository.SaveProduct(siteProduct);

                            //Log.DebugFormat("product saved {0} of {1}", productCount, importedProductsCount);

                            if (!justAdded)
                            {
                                res.UpdatedProductCount++;
                            }
                            else
                            {
                                justAdded = false;
                            }
                        }
                        catch (OutOfMemoryException ex)
                        {

                            Log.ErrorFormat("cannot update product externalId:{0} exception message:{1} exception{2}", siteProduct.ExternalId, ex.Message, ex);
                            throw new Exception(ex.Message);
                        }
                        catch (Exception ex)
                        {
                            res.ProductErrors.Add("Не удалось обновить " + siteProduct.ExternalId, ex.Message);
                            res.UpdateProductFailedCount++;

                            Log.ErrorFormat("cannot update product externalId:{0} exception message:{1}", siteProduct.ExternalId, ex.Message);

                        }
                    }
                }

                //Log.DebugFormat("deleting product stocks");
                foreach (var id in productStockToDelete)
                {
                    repository.DeleteProductStock(id);
                }
                //Log.DebugFormat("product stocks deleted");

                res.ProductCount = products.Count;
            }
            catch (Exception ex)
            {
                Log.DebugFormat("error while importing products {1}", ex.Message);
                res.ErrorMessage = ex.Message;
                res.ErrorCode = 1;
            }

            //Log.DebugFormat("finish importing products Execute");

            return res;
        }
Exemplo n.º 3
0
 public bool AddProduct(Product product)
 {
     return(shopRepository.AddProduct(product));
 }