根据 asp.net mvc5 中的多供应商电子商务商店中的商店订单限制计算购物车的商店折扣

calculate shop discount from cart based on shop order limit in a multi-vendor e-commerce store in asp.net mvc5

我有一个多供应商电子商务商店,每个商店经理都可以管理他们的商店订单折扣,items.the购物车模型如下:

    namespace myapp.Models
     public class Cart
     {
        [Key]
        public int RecordId { get; set; }
        public string CartId { get; set; }
        public Guid ItemId { get; set; }
        public Guid StoreId { get; set; }

        public Decimal? ShopDiscount { get; set; }
        public long ShopId { get; set; }
        [Display(Name = "Discount Time")]
        public string Duration { get; set; }
        [StringLength(100, ErrorMessage = "Must be less than 100 characters")]
        public string StoreName { get; set; }
        public decimal? Giftback { get; set; }
        [Display(Name = "Min order For Disc")]
        public Decimal? OrderLimit { get; set; }
        public int Count { get; set; }
        public decimal? Discount { get; set; }
        public System.DateTime DateCreated { get; set; }
        public virtual Item Item { get; set; }
      }
    }

我使用了带有代码优先脚手架的实体框架。 此外,在我的购物车模型 class 中,我使用了 shopdeal() 方法来计算店铺折扣,但它 returns 始终为 0 并且确实如此。我觉得这个方法有问题。

    // shopdeal method to calculate shopdiscount for all items in cart 
     // 

            public decimal? ShopDeal()
        {
            decimal? shopdeal = 0;
           //get record with different shops so that calculate shop total 
     //separatly
            var results = db.Carts.Select(m => new { m.CartId, m.StoreId, 
    m.OrderLimit, m.ShopDiscount, m.Duration }).Distinct().ToList();

              // calculate total price of all items for a particular shop  
            foreach (var item in results)
            {
                decimal? shoptotal = (from cartItems in db.Carts
                                      where cartItems.CartId == ShoppingCartId
                                      && cartItems.Item.StoreId == 
    item.StoreId
                                      select 
    (decimal?)cartItems.Item.Price).Sum();
                if (shoptotal >= item.OrderLimit && item.Duration == 
    "Started")
                {
                    shopdeal = shopdeal + shoptotal * item.ShopDiscount / 100;
                }

            }

            return shopdeal;
        }

在上述方法中,我们尝试计算特定商店所有商品的总价,而不是将 shoptotal 与该商店的 shopdiscount (orderlimit) 进行比较,并检查其时间段(是否开始)如果两个条件都适用该商店的商店折扣。如果用户从商店购买的商品多于对所有商店应用相同的商品(使用 foreach 循环)。 如果有解决方法可以获取每个商店的总价并应用 shopdiscount 并获得总的 shopdeal(total shopdiscount),请提供帮助。总功能如下:

        {
            // Multiply album price by count of that album to get 
            // the current price for each of those albums in the cart
            // sum all album price totals to get the cart total
            decimal? total = (from cartItems in db.Carts
                              where cartItems.CartId == ShoppingCartId
                              select (int?)cartItems.Count *
                              cartItems.Item.Price * (100 - 
                              cartItems.Item.Discount) / 100).Sum();
             // update total if shopdiscount available
            // why we need to apply shopdeal again? how to save result of 
             //shopdeal()
            total = total - ShopDeal(); 
            return total ?? decimal.Zero;
        }

 the above code works and no error is shown but the shopdiscount is always zero.any workaround to calculate and apply shopdiscount and reduce database queries.

谢谢大家,经过大量的努力后,我的预期效果很好,在 https://smartbook.pk/Items/DiscountItems

中实现了

     public decimal? ShopDeal()
        {
            decimal? shopdeal = 0;

            var results = (from cartItems in db.Carts
                           where cartItems.CartId == ShoppingCartId
                           select new { cartItems.StoreId,cartItems.OrderLimit,cartItems.Duration,cartItems.ShopDiscount }).Distinct().ToList();
                //db.Carts.Select(m => new { m.StoreId, m.OrderLimit, m.ShopDiscount, m.Giftback, m.Duration }.where()).Distinct().ToList();


            foreach (var item in results)
            {
                decimal? shoptotal = (from cartItems in db.Carts
                                      where cartItems.CartId == ShoppingCartId
                                      && cartItems.Item.StoreId == item.StoreId
                                      select (decimal?)cartItems.Item.Price).Sum();
                if (shoptotal >= item.OrderLimit && item.Duration == "Started")
                {
                    shopdeal = shopdeal + shoptotal * item.ShopDiscount / 100;
                }

            }

            return shopdeal;
        }

regards 
fiaz ahmed ranjha