温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:MyShop网络商城源码(mvc开发)
当前文件:
MyShop/BLL/Cart.cs,打开代码结构图
MyShop/BLL/Cart.cs,打开代码结构图
using System;
using System.Collections.Generic;
using System.Data;
//该源码下载自www.51aspx.com(51aspx.com)
using MyShop.DALFactory;
using MyShop.IDAL;
using MyShop.Model;
namespace MyShop.BLL
{
public class Cart
{
private ConfigInfo configInfo = new ConfigInfo();
private string tableName = "Ljh_Cart";
private ICart dal = DataAccess.CreateCart();
public Cart()
{
Config config = new Config();
configInfo = config.GetModel();
if (!string.IsNullOrEmpty(configInfo.TablePrefix.Trim()))
tableName = configInfo.TablePrefix + "Cart";
}
#region ICart member
protected int Add(CartInfo model)
{
if (model == null)
return 0;
return dal.Add(model);
}
public int Delete(string filter)
{
if (string.IsNullOrEmpty(filter))
return 0;
return dal.Delete(filter);
}
public bool Exist(string filter)
{
filter = filter.Trim();
if (string.IsNullOrEmpty(filter))
return false;
return dal.Exist(filter);
}
public DataSet GetDataSet()
{
return dal.GetDataSet();
}
public DataSet GetDataSet(string filter)
{
filter = filter.Trim();
if (string.IsNullOrEmpty(filter))
return null;
return dal.GetDataSet(filter);
}
public CartInfo GetModel(DataRow dr)
{
if (dr == null)
return null;
return dal.GetModel(dr);
}
private DataSet Query(string sql)
{
sql = sql.Trim();
if (string.IsNullOrEmpty(sql))
return null;
return dal.Query(sql);
}
protected int Update(CartInfo model, string filter)
{
if (model == null)
return 0;
filter = filter.Trim();
if (string.IsNullOrEmpty(filter))
return 0;
return dal.Update(model, filter);
}
//-------------------------
#endregion
#region common
protected int Add(CartInfo model, out string msg)
{
msg = "";
if (model == null)
{
msg = "<li>数据不能为空</li>";
return 0;
}
if (model.Quantity == 0)
{
msg = "<li>数量不能为0</li>";
return 0;
}
Product product = new Product();
ProductInfo productModel = new ProductInfo();
productModel = product.GetModel(model.ProductId);
model.Subtotal = model.Quantity * productModel.Price;
return Add(model);
}
/// <summary>
/// 购物车的添加
/// </summary>
/// <param name="model"></param>
/// <param name="msg"></param>
/// <param name="isMember">会员为true游客为false</param>
/// <returns></returns>
public int Add(CartInfo model, out string msg,bool isMember)
{
msg = "";
if (model == null)
{
msg = "<li>数据不能为空</li>";
return 0;
}
if (model.Quantity == 0)
{
msg = "<li>数量不能为0</li>";
return 0;
}
if (model.Quantity > Int32.MaxValue)
model.Quantity = Int32.MaxValue;
//判断同一种商品是否存在
if( Exist( " cartId='" + model.CartId + "' and productId=" + model.ProductId ) )
{
DataSet dataset = new DataSet();
dataset = GetDataSet(" cartId='" + model.CartId + "' and productId=" + model.ProductId);
int quantity = model.Quantity;
model = GetModel(dataset.Tables[0].Rows[0]);
model.Quantity = quantity + model.Quantity;
return Update(model.CartItemId, model.Quantity, isMember);
}
Product product = new Product();
ProductInfo productModel = new ProductInfo();
productModel = product.GetModel(model.ProductId);
if (isMember)
{
decimal price= productModel.Price;
switch( productModel.ProductType)
{
//正常销售 涨价商品
case 1:
case 2:
if ( productModel.PriceMember != 0 )
price = productModel.PriceMember;
else
price = productModel.Price ;
break;
//降价商品
case 3:
if (productModel.PriceMember != 0)
{
if (productModel.PriceMember <= productModel.Price)
price = productModel.PriceMember;
else
price = productModel.Price;
}
else
{
if (productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount ) >= productModel.Price)
price = productModel.Price;
else
price = productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount);
}
break;
case 4:
break;
default:
break;
}
model.Subtotal = model.Quantity * price;
}
else
{
model.Subtotal = model.Quantity * productModel.Price;
}
return Add(model);
}
public int Delete(int cartItemId)
{
if ( string.IsNullOrEmpty( cartItemId.ToString()) )
return 0;
string filer;
filer = " cartItemId =" + cartItemId;
return Delete(filer);
}
protected int Update(CartInfo model)
{
if (model == null)
{
return 0;
}
string filter;
filter = " cartItemId=" + model.CartItemId;
return Update(model, filter);
}
/// <summary>
/// 购物车数量更新
/// </summary>
/// <param name="cartItemId"></param>
/// <param name="quantity"></param>
/// <param name="isMember">会员为true游客为false</param>
/// <returns></returns>
public int Update(int cartItemId, int quantity,bool isMember)
{
if (quantity <= 0)
return 0;
CartInfo model = new CartInfo();
Cart cart = new Cart();
model = cart.GetModel(cartItemId);
model.Quantity = quantity;
Product product = new Product();
ProductInfo productModel = new ProductInfo();
productModel = product.GetModel(model.ProductId);
if (isMember )
{
decimal price = productModel.Price;
switch (productModel.ProductType)
{
//正常销售 涨价商品
case 1:
case 2:
if (productModel.PriceMember != 0)
price = productModel.PriceMember;
else
price = productModel.Price;
break;
//降价商品
case 3:
if (productModel.PriceMember != 0)
{
if (productModel.PriceMember <= productModel.Price)
price = productModel.PriceMember;
else
price = productModel.Price;
}
else
{
if (productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount) >= productModel.Price)
price = productModel.Price;
else
price = productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount);
}
break;
case 4:
break;
default:
break;
}
model.Subtotal = model.Quantity * price;
}
else
{
model.Subtotal = model.Quantity * productModel.Price;
}
return Update(model);
}
public CartInfo GetModel(int cartItemId)
{
DataSet dataset = new DataSet();
dataset = GetDataSet(" cartItemId=" + cartItemId);
if (dataset != null && dataset.Tables[0].Rows.Count > 0)
return GetModel(dataset.Tables[0].Rows[0]);
return null;
}
#endregion
/// <summary>
/// 更新当前会员的购物车的总价
/// 用于会员登录后,正常更新要用Add方法
/// </summary>
/// <param name="cartId"></param>
/// <param name="isMember">会员为true游客为false</param>
/// <returns></returns>
public bool Update(string cartId, bool isMember)
{
if (string.IsNullOrEmpty(cartId))
return false;
CartInfo model = new CartInfo();
Cart cart = new Cart();
DataSet dataset = new DataSet();
dataset = cart.GetDataSet(" cartId='" + cartId + "'");
if (dataset.Tables[0].Rows.Count == 0)
return false;
foreach (DataRow dr in dataset.Tables[0].Rows)
{
Update(Convert.ToInt32(dr["cartItemId"].ToString()), Convert.ToInt32(dr["quantity"].ToString()), isMember);
}
return true;
}
public DataSet GetCartDataset(string cartId)
{
cartId = Utils.ReplaceBadSQL(cartId.Trim().ToLower());
if (string.IsNullOrEmpty(cartId))
return null;
return GetDataSet( " cartId='" + cartId + "'");
}
/// <summary>
/// 返回指定编号的购物车集合的记录总数,购物车为空时返回0
/// </summary>
/// <param name="cartId">购物车编号</param>
/// <returns>记录总数</returns>
public int RowsCount(string cartId)
{
DataSet dataSet = new DataSet();
dataSet = GetCartDataset(cartId);
if (dataSet.Tables[0].Rows.Count == 0)
return 0;
return dataSet.Tables[0].Rows.Count;
}
#region 后台管理
/// <summary>
/// 快速搜索
/// </summary>
/// <param name="searchType"></param>
/// <returns></returns>
public DataSet QuickSearch(int searchType)
{
return dal.QuickSearch(searchType);
}
public DataSet GetDateSetOrderByIdDesc()
{
return GetDataSet(" cartItemId > 0 order by CartItemId Desc ");
}
public int Admin_Shop_Cart_Delete(int type)
{
return dal.Admin_Shop_Cart_Delete(type);
}
#endregion
}
}
//该源码下载自www.51aspx.com(51aspx.com)

