using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
///OrderItemClass 的摘要说明
/// </summary>
public class OrderItemClass
{
    DataSet ds = new DataSet();
    SqlConnection con = ConSql.ConnectionSql();

    public string AddOrderItem(OrderItemST orderitem)
    {
        string messgae = "";
        SqlCommand cmd = new SqlCommand();                           //创建SqlCommand对象
        cmd.Connection = con;
        cmd.CommandText = "insert into orderItem(orderid,proname,listprice,qty,totalprice,productid)values(@orderid,@proname,@listprice,@qty,@totalprice,@productid)";
        cmd.Parameters.Add("@orderid", SqlDbType.Int).Value = orderitem.OrderId;
        cmd.Parameters.Add("@proname", SqlDbType.VarChar).Value = orderitem.ProName;
        cmd.Parameters.Add("@listprice", SqlDbType.Decimal ).Value = orderitem.ListPrice;
        cmd.Parameters.Add("@qty", SqlDbType.Int).Value = orderitem.Qty;
        cmd.Parameters.Add("@totalprice", SqlDbType.Decimal).Value = orderitem.TotalPrice;
        cmd.Parameters.Add("@productid", SqlDbType.Int).Value = orderitem.ProductId;
      
        try
        {
            con.Open();            //打开数据库
            if (cmd.ExecuteNonQuery() > 0)              //执行SQL语句
            {
                messgae = "订单详情添加成功！";
            }
            else
            {
                messgae = "订单详情添加失败！";
            }
        }
        catch
        {
            messgae = "数据库打开出错！";
        }
        finally
        {
            con.Close();      //关闭数据库           
        }
        return messgae;
    }

    public DataSet OrderItemByItemid(int itemId)
    {
        SqlDataAdapter sdr = new SqlDataAdapter();
        sdr.SelectCommand = new SqlCommand();   //创建数据适配集的SelectCommand对象
        sdr.SelectCommand.CommandText = "select * from orderItem where itemid=@itemid";
        sdr.SelectCommand.Connection = con; //直接调用数据库连接对象
        sdr.SelectCommand.Parameters.Add("@itemid", SqlDbType.Int).Value = itemId ;
        sdr.Fill(ds, "OrderItemByItemid");  //填充数据集ds，表名为OrderItemByItemid
        return ds;  //返回数据集
    }

    /// <summary>
    /// 查询用户订单详情
    /// </summary>
    /// <param name="uid"></param>
    /// <returns></returns>
    public DataSet OrderItemByUid(string uid)
    {
        SqlDataAdapter sdr = new SqlDataAdapter();
        sdr.SelectCommand = new SqlCommand();   //创建数据适配集的SelectCommand对象
        sdr.SelectCommand.CommandText = "select orderitem.*, [status] from orderItem inner join [order] on orderitem.orderid=[order].orderid where [uid]=@uid";
        sdr.SelectCommand.Connection = con; //直接调用数据库连接对象
        sdr.SelectCommand.Parameters.Add("@uid", SqlDbType.VarChar).Value = uid;
        sdr.Fill(ds, "OrderItemByUid");  //填充数据集ds，表名为OrderItemByUid
        return ds;  //返回数据集
    }


  
}
