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; }
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); }
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 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 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"); }