【发布时间】:2019-08-27 04:29:01
【问题描述】:
我有一个多供应商电子商务商店,每个商店经理都可以管理他们对商店订单和商品的折扣。购物车模型如下:
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; }
}
}
我使用了实体框架和代码优先脚手架。 此外,在我的购物车模型类中,我使用了 shopdeal() 方法来计算商店级别的折扣,但它总是返回 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) 进行比较,并检查其时间段(开始与否),如果这两个条件都为真,则适用 shopdiscount那家商店。如果用户从多个商店购买而不是适用于所有商店(使用 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.
【问题讨论】:
-
亲爱的,请检查 shopdeal() 函数为什么它返回 0。这个函数的目的是从购物车中获取属于特定商店的商品,将它们的价格相加并与 orderlimit 进行比较那家商店的。(还检查持续时间是否开始)。
-
对购物车中的所有商店重复 shoptotal(如果购物车包含属于不同商店的商品)。如果 shoptotal >=orderlimit 则应用 shopdiscount
标签: c# asp.net-mvc entity-framework ef-code-first shopping-cart