当前文件路径:MyShop/BLL/Cart.cs 
1
using System;
2
using System.Collections.Generic;
3
using System.Data;
4
//该源码下载自www.51aspx.com(51aspx.com)
5
6
7
using MyShop.DALFactory;
8
using MyShop.IDAL;
9
using MyShop.Model;
10
11
namespace MyShop.BLL
12
...{
13
public class Cart
14
...{
15
private ConfigInfo configInfo = new ConfigInfo();
16
private string tableName = "Ljh_Cart";
17
18
private ICart dal = DataAccess.CreateCart();
19
public Cart()
20
...{
21
Config config = new Config();
22
configInfo = config.GetModel();
23
24
if (!string.IsNullOrEmpty(configInfo.TablePrefix.Trim()))
25
tableName = configInfo.TablePrefix + "Cart";
26
}
27
28
ICart member#region ICart member
29
30
protected int Add(CartInfo model)
31
...{
32
if (model == null)
33
return 0;
34
return dal.Add(model);
35
}
36
public int Delete(string filter)
37
...{
38
if (string.IsNullOrEmpty(filter))
39
return 0;
40
return dal.Delete(filter);
41
}
42
43
public bool Exist(string filter)
44
...{
45
filter = filter.Trim();
46
if (string.IsNullOrEmpty(filter))
47
return false;
48
return dal.Exist(filter);
49
}
50
51
public DataSet GetDataSet()
52
...{
53
return dal.GetDataSet();
54
}
55
public DataSet GetDataSet(string filter)
56
...{
57
filter = filter.Trim();
58
if (string.IsNullOrEmpty(filter))
59
return null;
60
return dal.GetDataSet(filter);
61
}
62
63
public CartInfo GetModel(DataRow dr)
64
...{
65
if (dr == null)
66
return null;
67
return dal.GetModel(dr);
68
}
69
70
private DataSet Query(string sql)
71
...{
72
sql = sql.Trim();
73
if (string.IsNullOrEmpty(sql))
74
return null;
75
return dal.Query(sql);
76
}
77
78
protected int Update(CartInfo model, string filter)
79
...{
80
if (model == null)
81
return 0;
82
filter = filter.Trim();
83
if (string.IsNullOrEmpty(filter))
84
return 0;
85
return dal.Update(model, filter);
86
}
87
//-------------------------
88
#endregion
89
90
common#region common
91
92
protected int Add(CartInfo model, out string msg)
93
...{
94
msg = "";
95
if (model == null)
96
...{
97
msg = "<li>数据不能为空</li>";
98
return 0;
99
}
100
if (model.Quantity == 0)
101
...{
102
msg = "<li>数量不能为0</li>";
103
return 0;
104
}
105
106
Product product = new Product();
107
ProductInfo productModel = new ProductInfo();
108
productModel = product.GetModel(model.ProductId);
109
model.Subtotal = model.Quantity * productModel.Price;
110
111
return Add(model);
112
}
113
114
/**//// <summary>
115
/// 购物车的添加
116
/// </summary>
117
/// <param name="model"></param>
118
/// <param name="msg"></param>
119
/// <param name="isMember">会员为true游客为false</param>
120
/// <returns></returns>
121
public int Add(CartInfo model, out string msg,bool isMember)
122
...{
123
msg = "";
124
if (model == null)
125
...{
126
msg = "<li>数据不能为空</li>";
127
return 0;
128
}
129
if (model.Quantity == 0)
130
...{
131
msg = "<li>数量不能为0</li>";
132
return 0;
133
}
134
if (model.Quantity > Int32.MaxValue)
135
model.Quantity = Int32.MaxValue;
136
137
//判断同一种商品是否存在
138
if( Exist( " cartId='" + model.CartId + "' and productId=" + model.ProductId ) )
139
...{
140
DataSet dataset = new DataSet();
141
dataset = GetDataSet(" cartId='" + model.CartId + "' and productId=" + model.ProductId);
142
int quantity = model.Quantity;
143
model = GetModel(dataset.Tables[0].Rows[0]);
144
model.Quantity = quantity + model.Quantity;
145
return Update(model.CartItemId, model.Quantity, isMember);
146
}
147
148
Product product = new Product();
149
ProductInfo productModel = new ProductInfo();
150
productModel = product.GetModel(model.ProductId);
151
if (isMember)
152
...{
153
decimal price= productModel.Price;
154
switch( productModel.ProductType)
155
156
...{
157
//正常销售 涨价商品
158
case 1:
159
case 2:
160
if ( productModel.PriceMember != 0 )
161
price = productModel.PriceMember;
162
else
163
price = productModel.Price ;
164
break;
165
//降价商品
166
case 3:
167
if (productModel.PriceMember != 0)
168
...{
169
if (productModel.PriceMember <= productModel.Price)
170
price = productModel.PriceMember;
171
else
172
price = productModel.Price;
173
}
174
else
175
...{
176
if (productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount ) >= productModel.Price)
177
price = productModel.Price;
178
else
179
price = productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount);
180
}
181
break;
182
case 4:
183
break;
184
default:
185
break;
186
187
}
188
model.Subtotal = model.Quantity * price;
189
}
190
else
191
...{
192
model.Subtotal = model.Quantity * productModel.Price;
193
}
194
return Add(model);
195
}
196
197
198
199
200
public int Delete(int cartItemId)
201
...{
202
if ( string.IsNullOrEmpty( cartItemId.ToString()) )
203
return 0;
204
string filer;
205
filer = " cartItemId =" + cartItemId;
206
return Delete(filer);
207
}
208
protected int Update(CartInfo model)
209
...{
210
211
if (model == null)
212
...{
213
return 0;
214
}
215
string filter;
216
filter = " cartItemId=" + model.CartItemId;
217
return Update(model, filter);
218
}
219
220
221
/**//// <summary>
222
/// 购物车数量更新
223
/// </summary>
224
/// <param name="cartItemId"></param>
225
/// <param name="quantity"></param>
226
/// <param name="isMember">会员为true游客为false</param>
227
/// <returns></returns>
228
public int Update(int cartItemId, int quantity,bool isMember)
229
...{
230
if (quantity <= 0)
231
return 0;
232
CartInfo model = new CartInfo();
233
Cart cart = new Cart();
234
model = cart.GetModel(cartItemId);
235
model.Quantity = quantity;
236
237
Product product = new Product();
238
ProductInfo productModel = new ProductInfo();
239
productModel = product.GetModel(model.ProductId);
240
if (isMember )
241
...{
242
decimal price = productModel.Price;
243
switch (productModel.ProductType)
244
...{
245
//正常销售 涨价商品
246
case 1:
247
case 2:
248
if (productModel.PriceMember != 0)
249
price = productModel.PriceMember;
250
else
251
price = productModel.Price;
252
break;
253
//降价商品
254
case 3:
255
if (productModel.PriceMember != 0)
256
...{
257
if (productModel.PriceMember <= productModel.Price)
258
price = productModel.PriceMember;
259
else
260
price = productModel.Price;
261
}
262
else
263
...{
264
if (productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount) >= productModel.Price)
265
price = productModel.Price;
266
else
267
price = productModel.PriceOriginal * Convert.ToDecimal( productModel.Discount);
268
}
269
break;
270
case 4:
271
break;
272
default:
273
break;
274
275
}
276
model.Subtotal = model.Quantity * price;
277
}
278
else
279
...{
280
model.Subtotal = model.Quantity * productModel.Price;
281
}
282
283
return Update(model);
284
}
285
286
287
288
public CartInfo GetModel(int cartItemId)
289
...{
290
DataSet dataset = new DataSet();
291
dataset = GetDataSet(" cartItemId=" + cartItemId);
292
if (dataset != null && dataset.Tables[0].Rows.Count > 0)
293
return GetModel(dataset.Tables[0].Rows[0]);
294
return null;
295
296
}
297
298
#endregion
299
300
301
/**//// <summary>
302
/// 更新当前会员的购物车的总价
303
/// 用于会员登录后,正常更新要用Add方法
304
/// </summary>
305
/// <param name="cartId"></param>
306
/// <param name="isMember">会员为true游客为false</param>
307
/// <returns></returns>
308
public bool Update(string cartId, bool isMember)
309
...{
310
if (string.IsNullOrEmpty(cartId))
311
return false;
312
CartInfo model = new CartInfo();
313
Cart cart = new Cart();
314
DataSet dataset = new DataSet();
315
dataset = cart.GetDataSet(" cartId='" + cartId + "'");
316
if (dataset.Tables[0].Rows.Count == 0)
317
return false;
318
foreach (DataRow d