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; }
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; }
private static List<SellProductModel> TrungMarket(ref int countInsert, SellProductModel product, SmartBuyEntities db, string productNameFirst, Market dupMarket, Dictionary dupProductDictionary) { //Kiem tra productName voi dictionary List<SellProductModel> sellProducts = null; List<Product> listProduct = CheckProductNameWithDictionary(productNameFirst, db.Dictionaries); if (listProduct != null) { sellProducts = new List<SellProductModel>(); foreach (var productId in listProduct) { var existedSellProduct = db.SellProducts.FirstOrDefault(x => x.ProductId == productId.Id && x.MarketId == dupMarket.Id); var existedSellProductModel = SellProductModel.MapToSellProductEntity(existedSellProduct); sellProducts.Add(existedSellProductModel); } sellProducts.Add(product); } else { var newProduct = new SmartB.UI.Models.EntityFramework.Product // add Product { Name = productNameFirst, IsActive = true, }; var addedProduct = db.Products.Add(newProduct); var sellProduct = new SmartB.UI.Models.EntityFramework.SellProduct //add SellProduct { Market = dupMarket, Product = addedProduct, SellPrice = product.Price, LastUpdatedTime = DateTime.Now }; var addedSellProduct = db.SellProducts.Add(sellProduct); 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(); 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 sellProducts; }
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 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); } }
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); } }
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); } }