Exemplo n.º 1
0
        public JsonResult SaveUserPrice(String userPriceJson)
        {
            var check = false;

            // define epsilon
            var ep = 0.1;

            JavaScriptSerializer ser = new JavaScriptSerializer();
            UserPrice parseJson = ser.Deserialize<UserPrice>(userPriceJson);
            try
            {
                var pId = parseJson.ProductId;
                var updatedPrice = parseJson.UpdatedPrice;

                var minPrice = db.ProductAttributes.Where(p => p.ProductId == pId)
                    .OrderByDescending(x => x.LastUpdatedTime)
                    .Select(x => x.MinPrice)
                    .FirstOrDefault();

                var maxPrice = db.ProductAttributes.Where(p => p.ProductId == pId)
                    .OrderByDescending(x => x.LastUpdatedTime)
                    .Select(x => x.MaxPrice)
                    .FirstOrDefault();

                var averagePrice = (minPrice + maxPrice) / 2;
                var rangeFrom = minPrice - ep * averagePrice;
                var rangeTo = maxPrice + ep * averagePrice;

                if ((updatedPrice >= rangeFrom && updatedPrice <= minPrice) || ( updatedPrice >= maxPrice && updatedPrice <= rangeTo))
                {
                    var userPrice = new UserPrice();
                    userPrice.Username = User.Identity.Name;
                    userPrice.MarketId = parseJson.MarketId;
                    userPrice.ProductId = pId;
                    userPrice.UpdatedPrice = updatedPrice;
                    userPrice.LastUpdatedTime = DateTime.Now;
                    userPrice.isApprove = false;
                    db.UserPrices.Add(userPrice);
                    db.SaveChanges();
                }

                check = true;
                return Json(check, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(check, JsonRequestBehavior.AllowGet);
            }
        }
Exemplo n.º 2
0
        public bool SaveUserPrice(List<UserPrice> parseJson)
        {
            var check = false;

            // define epsilon
            var ep = 0.1;
            try
            {
                for (int i = 0; i < parseJson.Count; i++)
                {
                    var pId = parseJson[i].ProductId;
                    var updatedPrice = parseJson[i].UpdatedPrice;

                    var minPrice = context.ProductAttributes.Where(p => p.ProductId == pId)
                        .OrderByDescending(x => x.LastUpdatedTime)
                        .Select(x => x.MinPrice)
                        .FirstOrDefault();

                    var maxPrice = context.ProductAttributes.Where(p => p.ProductId == pId)
                        .OrderByDescending(x => x.LastUpdatedTime)
                        .Select(x => x.MaxPrice)
                        .FirstOrDefault();

                    var averagePrice = (minPrice + maxPrice) / 2;
                    var rangeFrom = minPrice - ep * averagePrice;
                    var rangeTo = maxPrice + ep * averagePrice;

                    if ((updatedPrice >= rangeFrom && updatedPrice <= minPrice) || (updatedPrice >= maxPrice && updatedPrice <= rangeTo))
                    {
                        var userPrice = new UserPrice();
                        userPrice.Username = parseJson[i].Username;
                        userPrice.MarketId = parseJson[i].MarketId;
                        userPrice.ProductId = pId;
                        userPrice.UpdatedPrice = updatedPrice;
                        userPrice.LastUpdatedTime = DateTime.Now;
                        userPrice.isApprove = false;
                        context.UserPrices.Add(userPrice);
                        context.SaveChanges();
                    }
                }
                check = true;
                return check;
            }
            catch
            {
                return check;
            }
        }