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

308 lines
14 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 ShoppingCart2
{
static user_Goods daoGoods = new user_Goods();
static order_info daoorder = new order_info();
public static int TotalNum(HttpContext context)
{
int intTotalNum = 0;
if(context.Request.Cookies["0x120xxx2"]!=null)
{
HttpCookie myCookie = context.Request.Cookies["0x120xxx2"];
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 == 2)
{
if (strA[1] != "")
{
intTotalNum = intTotalNum + Convert.ToInt32(strA[1]);
}
}
}
}
return intTotalNum;
}
//该过程用来返回购物车中的所有商品注意这里是将HashTable中的数据转换到了一个DataTable中最后返回一个DataView对象
//转换成DataTable的原因是为了显示购物车时方便的帮定到DataGrid控件
public static DataView Getprod(HttpContext context)
{
//从Cookie中获取购物车中的货物栏目信息
DataTable dt=new DataTable();
if (context.Request.Cookies["0x120xxx2"] != null)
{
HttpCookie myCookie = context.Request.Cookies["0x120xxx2"];
string strCookie=myCookie.Value;
if (strCookie.Trim() != "")
{
string[] strACookie;
dt = new DataTable("0x120xxx2");
strACookie = strCookie.Split('|');
dt = new DataTable("0x120xxx2"); //声明并建立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列,商品图片prodimg
dc = new DataColumn("prodimg", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第3列,商品IDprodid
dc = new DataColumn("prodid", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第4列,商品单价prodPrice
dc = new DataColumn("prodPrice", System.Type.GetType("System.Double"));
dt.Columns.Add(dc);
//建立第4列,商品PVprodPrice2
dc = new DataColumn("prodPrice2", System.Type.GetType("System.Double"));
dt.Columns.Add(dc);
//建立第5列,购商品数量prodNum
dc = new DataColumn("prodNum", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第6列,购商品请允许购买数量TotalNum
dc = new DataColumn("TotalNum", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//建立第7列这是一个计算列为商品的价格 * 商品数量
dc = new DataColumn("SubTotal", System.Type.GetType("System.Double"), "prodPrice*prodNum");
dt.Columns.Add(dc);
//建立第7列这是一个计算列为商品的PV * 商品数量
dc = new DataColumn("SubTotal2", System.Type.GetType("System.Double"), "prodPrice2*prodNum");
dt.Columns.Add(dc);
//建立第8列这是一个计算列为是否买家承担运费
dc = new DataColumn("IFMJCDYH", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第9列这是一个计算列为商品运费1
dc = new DataColumn("SPYH1", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第10列这是一个计算列为商品运费2
dc = new DataColumn("SPYH2", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第11列这是一个计算列为商品运费3
dc = new DataColumn("SPYH3", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//建立第12列,商品所有者
dc = new DataColumn("SUserID", System.Type.GetType("System.String"));
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 == 2)
{
dr = dt.NewRow(); //新建1行注意和新建列的区别
if (strA[0] != "")
{
dr["id"] = strA[0]; //商品编号
//context.Response.Write(strA[0]);
Mtxfw.Model.user_Goods ModelGoods = daoGoods.GetModel(int.Parse(strA[0]));
if (ModelGoods != null)
{
dr["prodName"] = ModelGoods.GoodsName; //返回商品名称
dr["prodimg"] = ModelGoods.GoodsPic; //返回商品名称
dr["prodid"] = ModelGoods.Id; //返回商品名称
dr["SUserID"] = 0; //返回商品所有者
dr["TotalNum"] = 10000; //返回商品剩余数量
dr["prodPrice"] = ModelGoods.GoodsPrices;//返回商品折扣价格
dr["prodPrice2"] = ModelGoods.GoodsPrices2;//返回商品折扣价格
string[] strAuthor = "1|0|0|0".Split('|');; //返回商品运费
dr["IFMJCDYH"] = strAuthor[0];
dr["SPYH1"] = 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 bool IsExist(HttpContext context,string strProdId)
{
//从Cookie中获取购物车中的货物栏目信息
string strCookie = "";
bool b = false;
if (context.Request.Cookies["0x120xxx2"] != null)
{
HttpCookie myCookie = context.Request.Cookies["0x120xxx2"];
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 == 2)
{
if (strA[0] != "")
{
if (strA[0] == strProdId)
{
b = true; //表示该商品已经存在
}
}
}
}
}
return b;
}
//向购物车中添加一商品,如果该商品已经存在,只要修改数量即可,如果不存在,则需要添加一项
public static void Addprod(HttpContext context, string strProdId, int num)
{
//从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("0x120xxx2");
if (IsExist(context, AProdId[j]))
{
if (context.Request.Cookies["0x120xxx2"] != null)
{
myCookie = context.Request.Cookies["0x120xxx2"];
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 == 2)
{
if (strA[0] != "")
{
if (strA[0] == AProdId[j])
{
strCookie = strCookie.Replace(strA[0] + "," + strA[1], strA[0] + "," + (Convert.ToInt32(strA[1]) + num).ToString());
}
}
}
}
}
}
else
{
if (context.Request.Cookies["0x120xxx2"] != null)
{
myCookie = context.Request.Cookies["0x120xxx2"];
strCookie = myCookie.Value;
}
strCookie += AProdId[j] + ",1|";
}
//最后将新的购物栏目信息保存到Cookie中
myCookie.Value = strCookie;
myCookie.Expires = DateTime.Now.AddYears(1);
context.Response.Cookies.Add(myCookie);
}
}
}
}
//更改购物车中的货物数量
public static void UpdateProd(HttpContext context, string strProdId, string intprodNum)
{
HttpCookie myCookie = new HttpCookie("0x120xxx2");
//从Cookie中获取购物车中的货物栏目信息
string[] AProdId = strProdId.Split(',');
for (int j = 0; j < AProdId.Length; j++)
{
string strCookie = "";
if (AProdId.Length > 0)
{
if (AProdId[j] != "")
{
if (context.Request.Cookies["0x120xxx2"] != null)
{
myCookie = context.Request.Cookies["0x120xxx2"];
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 == 2)
{
if (strA[0] != "")
{
if (strA[0] == AProdId[j])
{
if (intprodNum == "" || intprodNum == "0")
{
strCookie = strCookie.Replace(strACookie[i] + "|", "");
}
else
{
strCookie = strCookie.Replace(strA[0] + "," + strA[1] + "|", strA[0] + "," + intprodNum + "|");
}
}
}
}
}
}
//最后将新的购物栏目信息保存到Cookie中
myCookie.Value = strCookie;
myCookie.Expires = DateTime.Now.AddYears(1);
context.Response.Cookies.Add(myCookie);
}
}
}
}
}
}