Пример #1
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;
        }
Пример #2
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;
        }
Пример #3
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;
        }
Пример #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);
 }
Пример #5
0
        public JsonResult MergeProductTraining(String productJson, string productName, int productId, int position)
        {
            var check = false;

            JavaScriptSerializer ser = new JavaScriptSerializer();
            List<DictionaryModel> parseJson = ser.Deserialize<List<DictionaryModel>>(productJson);
            try
            {
                //var pName = parseJson[parseJson.Count - 1].Name;
                //var dupDictionary = db.Dictionaries.Where(d => d.Name == pName).FirstOrDefault();

                //pName = parseJson[0].Name;
                //var dupProductName = db.Products.Where(p => p.Name == pName).FirstOrDefault();
                //if (dupProductName == null)
                //{
                //    var newProduct = new Product();
                //    newProduct.Name = pName;
                //    newProduct.IsActive = true;
                //    db.Products.Add(newProduct);
                //    dupDictionary = db.Dictionaries.Where(d => d.Name == pName && d.ProductId == newProduct.Id).FirstOrDefault();

                //    if (dupDictionary == null)
                //    {
                //        var newDictionary = new Dictionary();
                //        newDictionary.Name = pName;
                //        newDictionary.Product = newProduct;
                //        db.Dictionaries.Add(newDictionary);
                //    }
                //}
                //else
                //{
                //    dupDictionary = db.Dictionaries.Where(d => d.Name == pName && d.ProductId == dupProductName.Id).FirstOrDefault();

                //    if (dupDictionary == null)
                //    {
                //        var newDictionary = new Dictionary();
                //        newDictionary.Name = pName;
                //        newDictionary.ProductId = dupProductName.Id;
                //        db.Dictionaries.Add(newDictionary);
                //    }
                //}
                //db.SaveChanges();

                //var pId = db.Products.Where(p => p.Name == pName).FirstOrDefault().Id;
                //for (int i = 1; i < parseJson.Count; i++)
                //{
                //    pName = parseJson[i].Name;
                //    dupDictionary = db.Dictionaries.Where(d => d.Name == pName).FirstOrDefault();
                //    if (dupDictionary == null)
                //    {
                //        var newDictionary = new Dictionary();
                //        newDictionary.Name = parseJson[i].Name;
                //        newDictionary.ProductId = pId;
                //        db.Dictionaries.Add(newDictionary);
                //        db.SaveChanges();
                //    }
                //}

                if (productId != 0)
                {
                    for (int i = 0; i < parseJson.Count; i++)
                    {
                        string pName = parseJson[i].Name;
                        var dupDictionary = db.Dictionaries.Where(d => d.Name == pName).FirstOrDefault();
                        if (dupDictionary == null)
                        {
                            var newDictionary = new Dictionary();
                            newDictionary.Name = parseJson[i].Name;
                            newDictionary.ProductId = productId;
                            db.Dictionaries.Add(newDictionary);
                        }
                        else
                        {
                            dupDictionary.ProductId = productId;
                        }
                        db.SaveChanges();
                    }
                }
                else
                {
                    var proName = parseJson[position].Name;
                    var checkDupPName = db.Products.Where(p => p.Name == proName).FirstOrDefault();
                    var pId = 0;
                    var product = new Product();
                    if (checkDupPName == null)
                    {
                        var newProduct = new Product();
                        newProduct.Name = proName;
                        newProduct.IsActive = true;
                        db.Products.Add(newProduct);
                        pId = newProduct.Id;
                        product = newProduct;
                    }
                    else
                    {
                        pId = checkDupPName.Id;
                        product = checkDupPName;
                    }
                        var dictionary = db.Dictionaries.Where(d => d.Name == proName && d.ProductId == pId).FirstOrDefault();

                        if (dictionary == null)
                        {
                            var newDictionary = new Dictionary();
                            newDictionary.Name = proName;
                            newDictionary.Product = product;
                            db.Dictionaries.Add(newDictionary);
                        }
                        else
                        {
                            dictionary.ProductId = pId;
                        }
                        for (int i = 0; i < parseJson.Count; i++)
                        {
                            if (i != position)
                            {
                                string pName = parseJson[i].Name;
                                var dupDictionary = db.Dictionaries.Where(d => d.Name == pName).FirstOrDefault();
                                if (dupDictionary == null)
                                {
                                    var newDictionary = new Dictionary();
                                    newDictionary.Name = parseJson[i].Name;
                                    newDictionary.ProductId = pId;
                                    db.Dictionaries.Add(newDictionary);
                                }
                                else
                                {
                                    dupDictionary.ProductId = pId;
                                }
                            }
                            else { continue; }
                            //db.SaveChanges();
                        }

                    db.SaveChanges();
                }

                writeToTxt(productName);
                TempData["MergeProduct"] = "Success";
                check = true;
                return Json(check, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                TempData["MergeProduct"] = "Failed";
                return Json(check, JsonRequestBehavior.AllowGet);
            }
        }
Пример #6
0
        public JsonResult SplitProductTraining(String productJson, string productName)
        {
            var check = false;
            JavaScriptSerializer ser = new JavaScriptSerializer();
            List<DictionaryModel> parseJson = ser.Deserialize<List<DictionaryModel>>(productJson);
            try
            {
                for (int i = 0; i < parseJson.Count; i++)
                {
                    var pName = parseJson[i].Name;
                    var dupProductName = db.Products.Where(p => p.Name == pName).FirstOrDefault();
                    var pId = 0;
                    var product = new Product();

                    if (dupProductName == null)
                    {
                        var newProduct = new Product();
                        newProduct.Name = pName;
                        newProduct.IsActive = true;
                        db.Products.Add(newProduct);
                        pId = newProduct.Id;
                        product = newProduct;
                    }
                    else
                    {
                        pId = dupProductName.Id;
                        product = dupProductName;
                    }
                        var dupDictionaryName = db.Dictionaries.Where(d => d.Name == pName && d.ProductId == pId).FirstOrDefault();
                        if (dupDictionaryName == null)
                        {

                            var newDictionary = new Dictionary();
                            newDictionary.Name = pName;
                            newDictionary.Product = product;
                            db.Dictionaries.Add(newDictionary);
                        }
                        db.SaveChanges();
                }

                writeToTxt(productName);
                TempData["SplitProduct"] = "Success";
                check = true;
                return Json(check, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                TempData["SplitProduct"] = "Failed";
                return Json(check, JsonRequestBehavior.AllowGet);
            }
        }
Пример #7
0
        public ActionResult SetActive(int id)
        {
            var product = context.Products.FirstOrDefault(x => x.Id == id);
            bool statusFlag = false;
            if (ModelState.IsValid)
            {
                if (product.IsActive == true)
                {
                    product.IsActive = false;
                    statusFlag = false;
                }
                else
                {
                    product.IsActive = true;
                    statusFlag = true;
                }
                context.SaveChanges();
            }

            // Display the confirmation message
            var results = new Product
            {
                IsActive = statusFlag
            };

            return Json(results);
        }
Пример #8
0
        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);
            }
        }