Exemplo n.º 1
0
 public static SellProductModel MapToSellProductEntity(SellProduct sellProduct)
 {
     SellProductModel model = new SellProductModel();
     model.Id = sellProduct.Id;
     model.MarketName = sellProduct.Market.Name;
     model.Name = sellProduct.Product.Name;
     model.Price = (int)sellProduct.SellPrice;
     return model;
 }
Exemplo 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;
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
        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");
        }