using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcBookStore.Models
{
public partial class ShoppingCart
{
BookStoreEntities storeDB = new BookStoreEntities();
string ShoppingCartId { get; set; }
public const string CartSessionKey = "CartId";
public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}
//使用帮助器方法来简化购物车的访问
public static ShoppingCart GetCart(Controller controller)
{
return GetCart(controller.HttpContext);
}
public void AddToCart(Book book)
{
//获取对应的购物车对象的实例与书籍对象的实例
var cartItem = storeDB.Carts.SingleOrDefault(c => c.CartId ==
ShoppingCartId && c.BookId == book.Id);
if (cartItem == null)
{
//如果购物车中不存在该书籍则添加该书籍
cartItem = new Cart
{
BookId = book.Id,
CartId = ShoppingCartId,
Count = 1,
DateCreated = DateTime.Now
};
storeDB.Carts.Add(cartItem);
}
else
{
//如果在购物车中该书籍已存在,则添加一个数量
cartItem.Count++;
}
// 保存改变
storeDB.SaveChanges();
}
public int RemoveFromCart(int id)
{
//获取购物车对象实例
var cartItem = storeDB.Carts.Single(cart => cart.CartId ==
ShoppingCartId && cart.RecordId == id);
int itemCount = 0;
if (cartItem != null)
{
if (cartItem.Count > 1)
{
cartItem.Count--;
itemCount = cartItem.Count;
}
else
{
storeDB.Carts.Remove(cartItem);
}
// 保存修改
storeDB.SaveChanges();
}
return itemCount;
}
public void EmptyCart()
{
var cartItems = storeDB.Carts.Where(cart => cart.CartId ==
ShoppingCartId);
foreach (var cartItem in cartItems)
{
storeDB.Carts.Remove(cartItem);
}
// 保存修改
storeDB.SaveChanges();
}
public List<Cart> GetCartItems()
{
return storeDB.Carts.Where(cart => cart.CartId ==
ShoppingCartId).ToList();
}
public int GetCount()
{
//获取每本书的数量并相加在一起
int? count = (from cartItems in storeDB.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count).Sum();
// 如果实例为null,则返回0
return count ?? 0;
}
public decimal GetTotal()
{
//将书籍数量与单价相乘得到每本书的总价,然后相加得到购物车中的总金额
decimal? total = (from cartItems in storeDB.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count *
cartItems.Book.Price).Sum();
return total ?? decimal.Zero;
}
public int CreateOrder(Order order)
{
decimal orderTotal = 0;
var cartItems = GetCartItems();
// 在购物车中进行遍历,将每本书放入订单细节中
foreach (var item in cartItems)
{
var orderDetails = new OrderDetail
{
BookId = item.BookId,
OrderId = order.OrderId,
UnitPrice = item.Book.Price,
Quantity = item.Count
};
// 将购物车中每本书的价格加入购物车总金额中
orderTotal += (item.Count * item.Book.Price);
}
//用购物车的总金额来设置订单的Total(总金额)属性值
order.Total = orderTotal;
// 保存订单
storeDB.SaveChanges();
// 清空购物车
EmptyCart();
// 将OrderId属性值作为订单编号来返回
return order.OrderId;
}
//使用HttpContextBase类来允许用户使用cookies.
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
//使用System.Guid class创建一个随机GUID
Guid tempCartId = Guid.NewGuid();
//将tempCartId作为一个cookie来发送回客户端
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
//当用户登陆之后,使用他们的用户名来修改购物车的CartId
public void MigrateCart(string userName)
{
var shoppingCart = storeDB.Carts.Where(c => c.CartId ==
ShoppingCartId);
foreach (Cart item in shoppingCart)
{
item.CartId = userName;
}
storeDB.SaveChanges();
}
}
}
请发表评论