public void CalculatePriceRange(int productId) { using (var context = new SmartBuyEntities()) { // Calculate min, max price for today var priceList = context.SellProducts .Where(x => x.ProductId == productId) .ToList(); priceList = priceList .Where(x => x.LastUpdatedTime.Date == DateTime.Today) .ToList(); var minPrice = priceList.Min(x => x.SellPrice); var maxPrice = priceList.Max(x => x.SellPrice); if (minPrice != null && maxPrice != null) { var attribute = new ProductAttribute { ProductId = productId, MinPrice = minPrice, MaxPrice = maxPrice, LastUpdatedTime = DateTime.Now }; context.ProductAttributes.Add(attribute); context.SaveChanges(); } } }
public void UpdateItem(ProductAttribute product, float quantity) { CartLine line = lineCollection.Where(p => p.Product.ProductId == product.ProductId).FirstOrDefault(); if (line != null) { line.Quantity = quantity; } }
public void AddItem(ProductAttribute product, float quantity) { CartLine line = lineCollection.Where(p => p.Product.ProductId == product.ProductId).FirstOrDefault(); if (line == null) { lineCollection.Add(new CartLine { Product = product, Quantity = quantity }); } else { line.Quantity += quantity; } }
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; }
public JsonResult UpdateUserPrice(int id, int productId, int price, DateTime lastUpdatedTime, int marketId) { var check = false; string message = ""; try { ProductAttribute product = db.ProductAttributes .Where(p => p.ProductId == productId) .OrderByDescending(p => p.LastUpdatedTime).FirstOrDefault(); //var getSellProduct = db.SellProducts.Where(s => s.Product.Id == productId & s.MarketId == marketId).FirstOrDefault(); var sellProduct = new SellProduct(); sellProduct.MarketId = marketId; sellProduct.ProductId = productId; sellProduct.SellPrice = price; sellProduct.LastUpdatedTime = lastUpdatedTime; db.SellProducts.Add(sellProduct); if (product != null && price < product.MinPrice.GetValueOrDefault()) { //if (getSellProduct != null) //{ // getSellProduct.SellPrice = price; //update Price //} var proAtt = new ProductAttribute(); proAtt.ProductId = productId; proAtt.MinPrice = price; proAtt.MaxPrice = product.MaxPrice; proAtt.LastUpdatedTime = lastUpdatedTime; db.ProductAttributes.Add(proAtt); } else if (product != null && price > product.MaxPrice.GetValueOrDefault()) { //if (getSellProduct != null) //{ // getSellProduct.SellPrice = price; //update Price //} var proAtt = new ProductAttribute(); proAtt.ProductId = productId; proAtt.MinPrice = product.MinPrice; proAtt.MaxPrice = price; proAtt.LastUpdatedTime = lastUpdatedTime; db.ProductAttributes.Add(proAtt); } UserPrice up = db.UserPrices.Find(id); if (up != null) { up.isApprove = true; } db.SaveChanges(); UpdateUserPriceModel.checkApprove = true; check = true; message = "Success"; TempData["UpdateUserPrice"] = message; return Json(check, JsonRequestBehavior.AllowGet); } catch { message = "Failed"; TempData["UpdateUserPrice"] = message; return Json(check, JsonRequestBehavior.AllowGet); } }
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 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 void RemoveLine(ProductAttribute product) { lineCollection.RemoveAll(l => l.Product.ProductId == product.ProductId); }