Files
g.hnyhua.cn/Mtxfw.DAL/ShoppingCart.cs
2026-02-07 15:48:27 +08:00

535 lines
25 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace Mtxfw.DAL
{
//公共函数
public class ShoppingCart
{
static P_Product daoProduct = new P_Product();
static order_info daoorder = new order_info();
public static int TotalNum()
{
int intTotalNum = 0;
if(HttpContext.Current.Request.Cookies["0x120xxx"]!=null)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
string strCookie=myCookie.Value;
string[] strACookie;
strACookie=strCookie.Split('|');
intTotalNum = 0;
for(int i=0;i<strACookie.LongLength-1;i++)
{
string[] strA=strACookie[i].Split(',');
if (strA.LongLength == 3)
{
if (strA[1] != "")
{
intTotalNum = intTotalNum + Convert.ToInt32(strA[1]);
}
}
}
}
return intTotalNum;
}
public static int BTotalNum()
{
int intTotalNum = 0;
if (HttpContext.Current.Request.Cookies["0x120xxx"] != null)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
string strCookie = myCookie.Value;
string[] strACookie = strCookie.Split('|');
intTotalNum = strACookie.Length - 1;
}
return intTotalNum;
}
//定义一个静态函数,用来返回购物总金额
public static Double TotalMoney()
{
Double sglTotalMoney = 0;
if(HttpContext.Current.Request.Cookies["0x120xxx"]!=null)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
string strCookie=myCookie.Value;
string[] strACookie;
strACookie=strCookie.Split('|');
for(int i=0;i<strACookie.LongLength-1;i++)
{
string[] strA=strACookie[i].Split(',');
if (strA.LongLength == 3)
{
if (strA[0] != "")
{
Mtxfw.Model.P_Product ModelProduct = daoProduct.GetModel(int.Parse(strA[0]));
if (ModelProduct != null)
{
sglTotalMoney = sglTotalMoney + ModelProduct.P_VIP_P * Convert.ToInt32(strA[1]);
}
}
}
}
}
return sglTotalMoney;
}
//该过程用来返回购物车中的所有商品注意这里是将HashTable中的数据转换到了一个DataTable中最后返回一个DataView对象
//转换成DataTable的原因是为了显示购物车时方便的帮定到DataGrid控件
public static DataView Getprod(int IFStores)
{
//从Cookie中获取购物车中的货物栏目信息
DataTable dt=new DataTable();
if(HttpContext.Current.Request.Cookies["0x120xxx"]!=null)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
string strCookie=myCookie.Value;
if (strCookie.Trim() != "")
{
string[] strACookie;
dt = new DataTable("0x120xxx");
strACookie = strCookie.Split('|');
dt = new DataTable("0x120xxx"); //声明并建立DataTable对象表的名称为 Books
DataColumn dc; //声明DataColumn对象变量
DataRow dr; //声明DataRow对象变量
//下面开始建立表格的结构
//建立第0列,商品编号id
dc = new DataColumn("id", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第1列,商品名称prodName
dc = new DataColumn("prodName", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第2列,商品颜色selcolor
dc = new DataColumn("selcolor", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第3列,商品图片prodimg
dc = new DataColumn("prodimg", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第4列,商品IDprodid
dc = new DataColumn("prodid", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第5列,商品单价prodPrice
dc = new DataColumn("prodPrice", System.Type.GetType("System.Double"));
dt.Columns.Add(dc);
//建立第6列,购商品数量prodNum
dc = new DataColumn("prodNum", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第7列,购商品请允许购买数量TotalNum
dc = new DataColumn("TotalNum", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第8列这是一个计算列为商品的价格 * 商品数量
dc = new DataColumn("SubTotal", System.Type.GetType("System.Double"), "prodPrice*prodNum");
dt.Columns.Add(dc);
//建立第9列这是一个计算列为是否买家承担运费
dc = new DataColumn("IFMJCDYH", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第10列这是一个计算列为商品运费1
dc = new DataColumn("SPYH1", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第11列这是一个计算列为商品运费2
dc = new DataColumn("SPYH2", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第12列这是一个计算列为商品运费3
dc = new DataColumn("SPYH3", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第13列,商品所有者
dc = new DataColumn("SUserID", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第14列,商品类型
dc = new DataColumn("utype", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第15列,套餐商品列表
dc = new DataColumn("P_CATEGORYs", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第16列,商品成本
dc = new DataColumn("TotalCB", System.Type.GetType("System.Double"));
dt.Columns.Add(dc);
//建立第17列,积分
dc = new DataColumn("prodPrice0", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//下面根据HashTable中的商品栏目信息添加每一行
if (strACookie.LongLength > 0)
{
for (int i = 0; i < strACookie.Length - 1; i++)
{
//逐行赋值
string[] strA = strACookie[i].Split(',');
if (strA.Length == 3)
{
dr = dt.NewRow(); //新建1行注意和新建列的区别
if (strA[0] != "")
{
dr["id"] = strA[0]; //商品编号
//HttpContext.Current.Response.Write(strA[0]);
int TNum = 0;
Mtxfw.Model.P_Product ModelProduct = daoProduct.GetModel(int.Parse(strA[0]));
if (ModelProduct != null)
{
TNum = ModelProduct.P_Orders; //返回已经购买总数
dr["prodName"] = ModelProduct.P_NAME; //返回商品名称
dr["selcolor"] = strA[2]; //返回商品颜色
dr["prodimg"] = ModelProduct.P_images; //返回商品名称
dr["prodid"] = ModelProduct.P_ID; //返回商品名称
dr["SUserID"] = ModelProduct.P_UserID; //返回商品所有者
dr["utype"] = ModelProduct.utype; //返回商品类型
dr["P_CATEGORYs"] = ModelProduct.P_CATEGORYs; //返回套餐商品列表
dr["TotalCB"] = ModelProduct.P_CB_P; //返回商品成本
int intNum = ModelProduct.P_Inventory;
if (intNum == 0)
{
dr["TotalNum"] = 0; //返回商品剩余数量
}
else
{
dr["TotalNum"] = intNum - TNum; //返回商品剩余数量
}
dr["prodPrice"] = ModelProduct.P_VIP_P;//返回商品折扣价格
dr["prodPrice0"] = ModelProduct.P_JiangLi;//返回商品积分
/*if (IFStores == 1)
{
dr["prodPrice"] = ModelProduct.P_VIP_P1;//返回同行商品价格
}
else if (IFStores == 2)
{
dr["prodPrice"] = ModelProduct.P_VIP_P2;//返回同行商品价格
}
else if (IFStores == 3)
{
dr["prodPrice"] = ModelProduct.P_VIP_P3;//返回同行商品价格
}*/
//string[] strAuthor = dv.Table.Rows[0]["N_Author"].ToString().Split('|');//返回商品运费
string[] strAuthor = "1|0|0|0".Split('|');; //返回商品运费
dr["IFMJCDYH"] = strAuthor[0];
dr["SPYH1"] = ModelProduct.P_YH_P;//返回商品运费strAuthor[1]
dr["SPYH2"] = strAuthor[2];
dr["SPYH3"] = strAuthor[3];
}
//dv.Table.Clear();
}
dr["prodNum"] = 0;
if (strA[1] != "")//商品数量
{
dr["prodNum"] = Convert.ToInt32(strA[1]);
}
dt.Rows.Add(dr); //将该行加到表dt中
}
}
}
}
//返回函数值注意返回的是DataView
}
return dt.DefaultView;
}
//返回浏览过的商品
public static DataTable GetViewedProd()
{
//从Cookie中获取购物车中的货物栏目信息
DataTable dt = new DataTable();
if (HttpContext.Current.Request.Cookies["view_id"] != null)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["view_id"];
string strCookie = myCookie.Value;
if (strCookie.Trim() != "")
{
string[] strACookie;
strACookie = strCookie.Split(',');
dt = new DataTable("view_id"); //声明并建立DataTable对象表的名称为 Books
DataColumn dc; //声明DataColumn对象变量
DataRow dr; //声明DataRow对象变量
//下面开始建立表格的结构
//建立第0列,商品编号id
dc = new DataColumn("id", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第1列,商品名称prodName
dc = new DataColumn("prodName", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第2列,商品颜色selcolor
dc = new DataColumn("selcolor", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第3列,商品图片prodimg
dc = new DataColumn("prodimg", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第4列,商品IDprodid
dc = new DataColumn("prodid", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第5列,商品单价prodPrice
dc = new DataColumn("prodPrice", System.Type.GetType("System.Double"));
dt.Columns.Add(dc);
//建立第6列,购商品数量prodNum
dc = new DataColumn("prodNum", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第7列,购商品请允许购买数量TotalNum
dc = new DataColumn("TotalNum", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第8列这是一个计算列为商品的价格 * 商品数量
dc = new DataColumn("SubTotal", System.Type.GetType("System.Double"), "prodPrice*prodNum");
dt.Columns.Add(dc);
//建立第9列这是一个计算列为是否买家承担运费
dc = new DataColumn("IFMJCDYH", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第10列这是一个计算列为商品运费1
dc = new DataColumn("SPYH1", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第11列这是一个计算列为商品运费2
dc = new DataColumn("SPYH2", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第12列这是一个计算列为商品运费3
dc = new DataColumn("SPYH3", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第13列,商品所有者
dc = new DataColumn("SUserID", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第14列,商品类型
dc = new DataColumn("utype", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第15列,套餐商品列表
dc = new DataColumn("P_CATEGORYs", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//下面根据HashTable中的商品栏目信息添加每一行
if (strACookie.LongLength > 0)
{
int jj = 0;
for (int i = strACookie.Length - 1; i > 0; i--)
{
//逐行赋值
if (strACookie[i] != "" && jj<5)
{
dr = dt.NewRow(); //新建1行注意和新建列的区别
dr["id"] = strACookie[i]; //商品编号
//HttpContext.Current.Response.Write(strA[0]);
int TNum = 0;
Mtxfw.Model.P_Product ModelProduct = daoProduct.GetModel(int.Parse(strACookie[i]));
if (ModelProduct != null)
{
TNum = ModelProduct.P_Orders; //返回已经购买总数
dr["prodName"] = ModelProduct.P_NAME; //返回商品名称
dr["selcolor"] = ""; //返回商品颜色
dr["prodimg"] = ModelProduct.P_images; //返回商品名称
dr["prodid"] = ModelProduct.P_ID; //返回商品名称
dr["SUserID"] = ModelProduct.P_UserID; //返回商品所有者
dr["utype"] = ModelProduct.utype; //返回商品类型
dr["P_CATEGORYs"] = ModelProduct.P_CATEGORYs; //返回套餐商品列表
int intNum = ModelProduct.P_Inventory;
if (intNum == 0)
{
dr["TotalNum"] = 0; //返回商品剩余数量
}
else
{
dr["TotalNum"] = intNum - TNum; //返回商品剩余数量
}
dr["prodPrice"] = ModelProduct.P_VIP_P;//返回商品折扣价格
//string[] strAuthor = dv.Table.Rows[0]["N_Author"].ToString().Split('|');//返回商品运费
string[] strAuthor = "1|0|0|0".Split('|'); ; //返回商品运费
dr["IFMJCDYH"] = strAuthor[0];
dr["SPYH1"] = strAuthor[1];
dr["SPYH2"] = strAuthor[2];
dr["SPYH3"] = strAuthor[3];
}
dr["prodNum"] = 0;
dt.Rows.Add(dr);
jj += 1;
}
//将该行加到表dt中
}
}
}
//返回函数值注意返回的是DataView
}
return dt;
}
//该函数用来判断该商品是否已经在购物车中
public static bool IsExist(string strProdId, string selcolor)
{
//从Cookie中获取购物车中的货物栏目信息
string strCookie = "";
bool b = false;
if (HttpContext.Current.Request.Cookies["0x120xxx"] != null)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
strCookie = myCookie.Value;
}
string[] strACookie = strCookie.Split('|');
if (strACookie.LongLength > 0)
{
for (int i = 0; i < strACookie.Length - 1; i++)
{
string[] strA = strACookie[i].Split(',');
if (strA.Length == 3)
{
if (strA[0] != "")
{
string stra = strA[2];
if (strA[0] == strProdId && selcolor == stra)
{
b = true; //表示该商品已经存在
}
}
}
}
}
return b;
}
//向购物车中添加一商品,如果该商品已经存在,只要修改数量即可,如果不存在,则需要添加一项
public static void Addprod(string strProdId, int num, string selcolor)
{
//从Cookie中获取购物车中的货物栏目信息
string[] AProdId = strProdId.Split(',');
for (int j = 0; j < AProdId.Length; j++)
{
string strCookie = "";
if (AProdId.Length > 0)
{
if (AProdId[j] != "")
{
HttpCookie myCookie = new HttpCookie("0x120xxx");
if (IsExist(AProdId[j], selcolor))
{
if (HttpContext.Current.Request.Cookies["0x120xxx"] != null)
{
myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
strCookie = myCookie.Value;
string[] strACookie;
strACookie = strCookie.Split('|');
//下面根据HashTable中的商品栏目信息添加每一行
for (int i = 0; i < strACookie.Length - 1; i++)
{
string[] strA = strACookie[i].Split(',');
if (strA.Length == 3)
{
if (strA[0] != "")
{
if (strA[0] == AProdId[j] && strA[2] == selcolor)
{
strCookie = strCookie.Replace(strA[0] + "," + strA[1] + "," + strA[2], strA[0] + "," + (Convert.ToInt32(strA[1]) + num).ToString() + "," + strA[2]);
}
}
}
}
}
}
else
{
if (HttpContext.Current.Request.Cookies["0x120xxx"] != null)
{
myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
strCookie = myCookie.Value;
}
strCookie += AProdId[j] + ",1," + selcolor + "|";
}
//最后将新的购物栏目信息保存到Cookie中
myCookie.Value = strCookie;
myCookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
}
}
//从购物车中删除一本书
public static void RemoveProd(string strProdId, string selcolor)
{
HttpCookie myCookie=new HttpCookie("0x120xxx");
string strCookie="";
//从Cookie中获取购物车中的货物栏目信息
if (HttpContext.Current.Request.Cookies["0x120xxx"] != null)
{
myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
strCookie = myCookie.Value;
string[] strACookie = strCookie.Split('|');
string[] ProdIdA = strProdId.Split(',');
string[] selcolorA = selcolor.Split(',');
for (int j = 0; j < ProdIdA.Length - 1; j++)
{
if (ProdIdA[j] != "")
{
for (int i = 0; i < strACookie.Length - 1; i++)
{
string[] strA = strACookie[i].Split(',');
if (strA.Length == 3)
{
if (strA[0] != "")
{
string stra = strA[2];
if (strA[0] == ProdIdA[j] && stra == selcolorA[j])
{
//下面删除该书
strCookie = strCookie.Replace(strACookie[i] + "|", "");
}
}
}
}
}
}
}
//最后将新的购物栏目信息保存到Cookie中
myCookie.Value = strCookie;
myCookie.Expires=DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
//更改购物车中的货物数量
public static void UpdateProd(string strProdId, string intprodNum, string selcolor)
{
HttpCookie myCookie=new HttpCookie("0x120xxx");
string strCookie="";
//从Cookie中获取购物车中的货物栏目信息
if(HttpContext.Current.Request.Cookies["0x120xxx"]!=null)
{
myCookie = HttpContext.Current.Request.Cookies["0x120xxx"];
strCookie=myCookie.Value;
string[] strACookie;
strACookie=strCookie.Split('|');
for(int i=0;i<strACookie.Length-1;i++)
{
//下面更改数量
string[] strA = strACookie[i].Split(',');
if (strA.Length == 3)
{
if (strA[0] != "")
{
string stra = strA[2];
if (strA[0] == strProdId && stra == selcolor)
{
if (intprodNum == "" || intprodNum == "0")
{
strCookie = strCookie.Replace(strACookie[i] + "|", "");
}
else
{
strCookie = strCookie.Replace(strA[0] + "," + strA[1] + "," + strA[2], strA[0] + "," + intprodNum + "," + strA[2]);
}
}
}
}
}
}
//最后将新的购物栏目信息保存到Cookie中
myCookie.Value = strCookie;
myCookie.Expires=DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
}