Esempio n. 1
0
        private int AddNewProduct(int countInsert, SellProductModel product, SmartBuyEntities db, string productNameFirst, Dictionary dupProductDictionary)
        {
            #region Comments
            // Check excell với Dictionary
            //List<SellProduct> newCorrectProduct = (List<SellProduct>)Session["CorrectProducts"];
            //List<SellProduct> sellProductCompare = db.SellProducts.ToList();
            //List<List<SellProduct>> results = new List<List<SellProduct>>();
            //for (int i = 0; i < newCorrectProduct.Count; i++)
            //{
            //    List<SellProduct> result = new List<SellProduct>();
            //    for (int j = i + 1; j < sellProductCompare.Count; j++)
            //    {
            //        // var sellProMarket = db.SellProducts.Where(m => m.Market.Equals(sellProductCompare[j].MarketId))
            //        if (newCorrectProduct[i].Market.Name == sellProductCompare[j].Market.Name)
            //        {
            //            var percentage =
            //                CompareStringHelper.CompareString(newCorrectProduct[i].Product.Name.Split(';').First(), sellProductCompare[j].Product.Name);
            //            if (percentage > 0.7 && percentage < 1)
            //            {
            //                // var productSimilarDB = db.Products.Where(p => p.Dictionaries.Equals(dictionaryCompare[j]));
            //                if (result.Count() == 0)
            //                {
            //                    result.Add(newCorrectProduct[i]);
            //                }
            //                result.Add(sellProductCompare[j]);
            //                newCorrectProduct.Remove(newCorrectProduct[i]);
            //            }
            //        }
            //    }
            //    if (result.Count() != 0)
            //    {
            //        i = i - 1;
            //        results.Add(result);
            //    }
            //}
            #endregion

            var market = new Market
            {
                Name = product.MarketName,
                IsActive = true,
            };
            var newMarket = db.Markets.Add(market); //add market

            var newProduct = new SmartB.UI.Models.EntityFramework.Product
            {
                Name = productNameFirst,
                IsActive = true,
            };
            var addedProduct = db.Products.Add(newProduct); // add product

            var sellProduct = new SmartB.UI.Models.EntityFramework.SellProduct
            {
                Market = newMarket,
                Product = addedProduct,
                SellPrice = product.Price,
                LastUpdatedTime = DateTime.Now
            };
            var addedSellProduct = db.SellProducts.Add(sellProduct); // add sellProduct
            db.SaveChanges(); // Save to database

            //add product Attribute
            PriceHelper helper = new PriceHelper();
            helper.CalculatePriceRange(addedProduct.Id);

            countInsert++;
            // add Product Dictionary
            var dictionaries = product.Name.Split(';').ToList();
            foreach (string dictionary in dictionaries)
            {
                if (dupProductDictionary == null && dictionary != "")
                {
                    var ProductDic = new SmartB.UI.Models.EntityFramework.Dictionary
                    {
                        Name = dictionary,
                        ProductId = addedProduct.Id
                    };
                    var addProductDic = db.Dictionaries.Add(ProductDic);
                }
            }
            db.SaveChanges(); // Save to database
            return countInsert;
        }
Esempio n. 2
0
        private static int InsertProductToDb(IEnumerable<KeyValuePair<string, string>> data, int marketId)
        {
            int success = 0;
            var helper = new PriceHelper();

            using (var context = new SmartBuyEntities())
            {
                foreach (var pair in data)
                {
                    // Convert price to integer
                    int price = ConvertPrice(pair.Value);

                    // Check product existent
                    var goodMatch = new List<int>();
                    var averageMatch = new List<int>();
                    int pId = -1;
                    bool wholeMatch = false;
                    foreach (var dictionary in context.Dictionaries)
                    {
                        if (pair.Key == dictionary.Name)
                        {
                            wholeMatch = true;
                            pId = dictionary.ProductId.Value;
                            break;
                        }
                        double match = CompareStringHelper.CompareString(pair.Key, dictionary.Name);

                        if (match > 0.9)
                        {
                            // Good match
                            goodMatch.Add(dictionary.ProductId.Value);
                        }
                        else if (match > 0.7)
                        {
                            // Average match
                            averageMatch.Add(dictionary.ProductId.Value);
                        }
                    }

                    if (!wholeMatch)
                    {
                        if (goodMatch.Count == 1)
                        {
                            // Match well with only 1 product, take it
                            pId = goodMatch[0];
                        }
                        else if (goodMatch.Count > 1)
                        {
                            // Match well with more than 1 product, admin decide
                            ExportTrainingFile(goodMatch, pair.Key);
                            continue;
                        }
                        else if (averageMatch.Count > 0 && pId == -1)
                        {
                            // Only average match, admin decide
                            ExportTrainingFile(averageMatch, pair.Key);
                            continue;
                        }
                    }

                    // Already existed?
                    if (pId != -1)
                    {
                        // Insert new price
                        var sell = new SellProduct
                                       {
                                           MarketId = marketId,
                                           ProductId = pId,
                                           SellPrice = price,
                                           LastUpdatedTime = DateTime.Now
                                       };
                        context.SellProducts.Add(sell);
                        try
                        {
                            context.SaveChanges();
                            helper.CalculatePriceRange(pId);
                            success++;
                        }
                        catch (DbUpdateException)
                        {
                            // Do nothing
                        }
                    }
                    else
                    {
                        // Add new product to database
                        var newProduct = new Product {Name = pair.Key, IsActive = true};

                        // Create new attribute
                        var attribute = new ProductAttribute
                        {
                            MinPrice = price,
                            MaxPrice = price,
                            LastUpdatedTime = DateTime.Now
                        };
                        newProduct.ProductAttributes.Add(attribute);

                        // Add product selling information
                        var sell = new SellProduct
                                       {
                                           MarketId = marketId,
                                           SellPrice = price,
                                           LastUpdatedTime = DateTime.Now
                                       };
                        newProduct.SellProducts.Add(sell);

                        context.Products.Add(newProduct);

                        try
                        {
                            context.SaveChanges();
                            success++;

                            // Add to dictionary
                            var dictionary = new Dictionary
                                                 {
                                                     Name = newProduct.Name,
                                                     ProductId = newProduct.Id
                                                 };
                            context.Dictionaries.Add(dictionary);
                            context.SaveChanges();
                        }
                        catch (DbUpdateException)
                        {
                            // Do nothing
                        }
                    }
                }
            }
            return success;
        }
Esempio n. 3
0
        private static int TrungTenSanPham(int countInsert, SellProductModel product, SmartBuyEntities db, Product dupProduct)
        {
            var dupProductAtt = db.ProductAttributes.Where(p => p.ProductId.Equals(dupProduct.Id)).FirstOrDefault();
            // Cập nhật giá Min, Max ProductAttribute
            PriceHelper helper = new PriceHelper();
            helper.CalculatePriceRange(dupProductAtt.Product.Id);
            var market = new Market
            {
                Name = product.MarketName,
                IsActive = true,
            };
            var newMarket = db.Markets.Add(market); //add market

            var sellProduct = new SmartB.UI.Models.EntityFramework.SellProduct
            {
                Market = newMarket,
                Product = dupProduct,
                SellPrice = product.Price,
                LastUpdatedTime = DateTime.Now
            };
            var addedSellProduct = db.SellProducts.Add(sellProduct); // Add SellProduct
            countInsert++;
            db.SaveChanges(); // Save to database
            return countInsert;
        }
Esempio n. 4
0
 private static void TrungHoanToan(ref int countUpdate, ref int countInsert, SellProductModel product, SmartBuyEntities db, Market dupMarket, Product dupProduct)
 {
     var sellProduct = db.SellProducts.Where(s => s.ProductId == dupProduct.Id && s.MarketId == dupMarket.Id).FirstOrDefault();
     var sellProduct1 = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct
     {
         Market = dupMarket,
         Product = dupProduct,
         SellPrice = product.Price,
         LastUpdatedTime = DateTime.Now
     };
     var addedSellProduct = db.SellProducts.Add(sellProduct1);
     db.SaveChanges(); // Save to database
     // Check sellProduct có trùng không??
     if (sellProduct == null)
     {
         countInsert++;
     }
     else
     {
         countUpdate++;
     }
     // Cập nhật giá Min, Max ProductAttribute
     PriceHelper helper = new PriceHelper();
     helper.CalculatePriceRange(dupProduct.Id);
 }
Esempio n. 5
0
        public ActionResult SaveDupProducts(ListSellProductModel model)
        {
            model.CorrectSellProducts = (List<SellProductModel>)Session["CorrectProducts"];
            var errors = ModelState.Values.Where(x => x.Errors.Count > 0);
            //Trạng thái khi lưu xuống db
            int countUpdate = 0;
            int countInsert = 0;
            List<List<SellProductModel>> dupSellProduct = new List<List<SellProductModel>>();
            foreach (var product in model.CorrectSellProducts)
            {
                var productNameFirst = product.Name.Split(';').First(); // Cắt chuỗi
                var dupProduct = db.Products.Where(p => p.Name.Equals(productNameFirst)).FirstOrDefault();
                if (dupProduct != null)
                {
                    var dupMarket = db.Markets.Where(m => m.Name.Equals(product.MarketName)).FirstOrDefault();
                    var sellProduct = db.SellProducts.Where(s => s.ProductId == dupProduct.Id && s.MarketId == dupMarket.Id).FirstOrDefault();

                    if (sellProduct != null)
                    {

                        var newSellProduct = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct
                        {
                            Market = dupMarket,
                            Product = dupProduct,
                            SellPrice = product.Price,
                            LastUpdatedTime = DateTime.Now
                        };
                        var addedSellProduct = db.SellProducts.Add(newSellProduct);
                        db.SaveChanges(); // Save to database
                        countUpdate++;

                        //add new product Attribute
                        PriceHelper helper = new PriceHelper();
                        helper.CalculatePriceRange(dupProduct.Id);

                        // add Product Dictionary
                        var dictionaries = product.Name.Split(';').ToList();
                        var dupProductDictionary = db.Dictionaries.Where(p => p.Name.Equals(productNameFirst)).FirstOrDefault();
                        foreach (string dictionary in dictionaries)
                        {
                            if (dictionary != productNameFirst.ToString() && dictionary != "")
                            {
                                var dictionaryDB = db.Dictionaries.Where(d => d.Name.Equals(dictionary)).FirstOrDefault();
                                if (dictionaryDB != null)
                                {
                                  //  var getProduct = db.Products.Where(p => p.Name.Equals(dictionaryDB.Name)).FirstOrDefault();
                                    var getProductId = Convert.ToInt32(dictionaryDB.ProductId);
                                    var listDic = db.Dictionaries.Where(p => p.ProductId == getProductId).ToList();
                                    foreach (var item in listDic)
                                    {
                                        item.ProductId = dupProduct.Id;
                                    }
                                }
                                else
                                {
                                    var ProductDic = new SmartB.UI.Models.EntityFramework.Dictionary
                                    {
                                        Name = dictionary,
                                        ProductId = dupProduct.Id
                                    };
                                    var addProductDic = db.Dictionaries.Add(ProductDic);
                                }
                            }
                        }
                        db.SaveChanges(); // Save to database
                    }
                } else
                {
                    var dupMarket = db.Markets.Where(m => m.Name.Equals(product.MarketName)).FirstOrDefault();
                    var newProduct = new SmartB.UI.Models.EntityFramework.Product // add Product
                    {
                        Name = productNameFirst,
                        IsActive = true,
                    };
                    var addedProduct = db.Products.Add(newProduct);
                    var sellProduct2 = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct
                    {
                        Market = dupMarket,
                        Product = addedProduct,
                        SellPrice = product.Price,
                        LastUpdatedTime = DateTime.Now
                    };
                    var addedSellProduct = db.SellProducts.Add(sellProduct2);
                    countInsert++;
                    db.SaveChanges(); // Save to database
                    //add new product Attribute

                    var productAttribute = new SmartB.UI.Models.EntityFramework.ProductAttribute
                    {
                        ProductId = addedProduct.Id,
                        MinPrice = product.Price,
                        MaxPrice = product.Price,
                        LastUpdatedTime = DateTime.Now,
                    };
                    var addedProductAtt = db.ProductAttributes.Add(productAttribute);
                    db.SaveChanges(); // Save to database

                    // add Product Dictionary
                    var dictionaries = product.Name.Split(';').ToList();
                    var dupProductDictionary = db.Dictionaries.Where(p => p.Name.Equals(productNameFirst)).FirstOrDefault();
                    foreach (string dictionary in dictionaries)
                    {
                        if (dupProductDictionary == null && dictionary != "")
                        {
                            var dictionaryDB = db.Dictionaries.Where(d => d.Name.Equals(dictionary)).FirstOrDefault();
                            if (dictionaryDB == null)
                            {
                                var ProductDic = new SmartB.UI.Models.EntityFramework.Dictionary
                                {
                                    Name = dictionary,
                                    ProductId = newProduct.Id
                                };
                                var addProductDic = db.Dictionaries.Add(ProductDic);
                            }
                            else
                            {
                                //var getProductId = db.Products.Where(p => p.Name.Equals(dictionary)).FirstOrDefault();
                                //var listDic = db.Dictionaries.Where(d => d.ProductId.Equals(getProductId.Id)).ToList();
                                var getProductId = Convert.ToInt32(dictionaryDB.ProductId);
                                var listDic = db.Dictionaries.Where(p => p.ProductId == getProductId).ToList();
                                foreach (var item in listDic)
                                {
                                    item.ProductId = addedProduct.Id;
                                }
                            }
                        }
                    }
                    db.SaveChanges(); // Save to database
                }
            }

            TempData["UpdateMessage"] = "Có " + countUpdate + " sản phẩm được cập nhật giá.";
            TempData["InsertMessage"] = "Có " + countInsert + " sản phẩm được lưu mới.";
            return RedirectToAction("UploadProduct");
        }
        public JsonResult SaveSellProduct(string productName, int marketId, int sellPrice)
        {
            try
            {
                // Not exist
                Product product = context.Products.FirstOrDefault(x => x.Name.Equals(productName));
                var market = context.Markets.Where(m => m.Id.Equals(marketId)).FirstOrDefault();
                if (product != null)
                {
                    var sellProduct = context.SellProducts
                        .FirstOrDefault(x => x.ProductId == product.Id && x.MarketId == marketId);
                    if (sellProduct != null)
                    {
                        TempData["create"] = "Duplicate";
                    }
                    else
                    {
                        var newSellProduct = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct
                        {
                            Market = market,
                            Product = product,
                            SellPrice = sellPrice,
                            LastUpdatedTime = DateTime.Now
                        };
                        var addedSellProduct = context.SellProducts.Add(newSellProduct);
                        context.SaveChanges(); // Save to database

                        PriceHelper helper = new PriceHelper();
                        helper.CalculatePriceRange(product.Id);

                        context.SaveChanges(); // Save to database
                        TempData["create"] = "Success";
                    }
                }
                else
                {
                    // add product
                    var newProduct = new SmartB.UI.Models.EntityFramework.Product
                    {
                        Name = productName,
                        IsActive = true,
                    };
                    var addedProduct = context.Products.Add(newProduct);

                    var newSellProduct = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct
                    {
                        Market = market,
                        Product = addedProduct,
                        SellPrice = sellPrice,
                        LastUpdatedTime = DateTime.Now
                    };
                    var addedSellProduct = context.SellProducts.Add(newSellProduct);
                    context.SaveChanges(); // Save to database

                    var productAttribute = new SmartB.UI.Models.EntityFramework.ProductAttribute
                    {
                        ProductId = addedProduct.Id,
                        MinPrice = sellPrice,
                        MaxPrice = sellPrice,
                        LastUpdatedTime = DateTime.Now,
                    };
                    var addedProductAtt = context.ProductAttributes.Add(productAttribute);
                    context.SaveChanges(); // Save to database
                    // add Product Dictionary
                    var dictionaries = productName.Split(';').ToList();
                    var dupProductDictionary = context.Dictionaries.Where(p => p.Name.Equals(productName)).FirstOrDefault();
                    foreach (string dictionary in dictionaries)
                    {
                        if (dupProductDictionary == null && dictionary != "")
                        {
                            var ProductDic = new SmartB.UI.Models.EntityFramework.Dictionary
                            {
                                Name = dictionary,
                                ProductId = addedProduct.Id
                            };
                            var addProductDic = context.Dictionaries.Add(ProductDic);
                        }
                    }

                    context.SaveChanges(); // Save to database

                    TempData["create"] = "Success";
                }
                return Json(JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(JsonRequestBehavior.AllowGet);
            }
        }
        public RedirectToRouteResult Edit(SellProduct model)
        {
            var product = context.Products.Where(x => x.Name == model.Product.Name).FirstOrDefault();
            var sellProduct = context.SellProducts.FirstOrDefault(x => x.ProductId == product.Id && x.Market.Id == model.MarketId);
            string message;
            if (sellProduct != null)
            {
                //sellProduct.MarketId = model.MarketId;
                //sellProduct.SellPrice = model.SellPrice;
                //sellProduct.LastUpdatedTime = System.DateTime.Now;
                //context.SaveChanges();
                var Market = context.Markets.Where(x => x.Id == model.MarketId).FirstOrDefault();
                var newSellProduct = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct
                {
                    Market = Market,
                    Product = product,
                    SellPrice = model.SellPrice,
                    LastUpdatedTime = System.DateTime.Now
                };
                var addedSellProduct = context.SellProducts.Add(newSellProduct);
                context.SaveChanges(); // Save to database
                //add new product Attribute
                PriceHelper helper = new PriceHelper();
                helper.CalculatePriceRange(product.Id);

                context.SaveChanges(); // Save to database
                message = "Success";
            }
            else
            {
                message = "Failed";
            }
            TempData["edit"] = message;
            return RedirectToAction("Index");
        }