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>
///OrderClass 的摘要说明
/// </summary>
public class OrderClass
{
    DataSet ds = new DataSet();
    SqlConnection con = ConSql.ConnectionSql();
    public string AddOrder(OrderST order)
    {
        string messgae = "";
        SqlCommand cmd = new SqlCommand();                           //创建SqlCommand对象
        cmd.Connection = con;
        cmd.CommandText = "insert into [order]([uid],username,orderDate,addr1,city,[state],zip,phone,[status])values(@uid,@username,@orderDate,@addr1,@city,@state,@zip,@phone,@status)";
        cmd.Parameters.Add("@uid", SqlDbType.VarChar).Value = order.Uid;
        cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = order.UserName;
        cmd.Parameters.Add("@orderDate", SqlDbType.DateTime).Value = order.OrderDate;
        cmd.Parameters.Add("@addr1", SqlDbType.VarChar).Value =order .Addr1 ;
        cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = order .City;
        cmd.Parameters.Add("@state", SqlDbType.VarChar).Value =order .State ;
        cmd.Parameters.Add("@zip", SqlDbType.VarChar).Value = order.Zip;
        cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = order.Phone;
        cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = order.Status;
        try
        {
            con.Open();            //打开数据库
            if (cmd.ExecuteNonQuery() > 0)              //执行SQL语句
            {
                messgae = "订单添加成功！";
            }
            else
            {
                messgae = "订单添加失败！";
            }
        }
        catch(Exception ex)
        {
            messgae = "数据库打开出错！";
        }
        finally
        {
            con.Close();      //关闭数据库           
        }
        return messgae;
    }
    public DataSet OrderByUid(string uid)
    {
        SqlDataAdapter sdr = new SqlDataAdapter();
        sdr.SelectCommand = new SqlCommand();   //创建数据适配集的SelectCommand对象
        sdr.SelectCommand.CommandText = "select * from [order] where [uid]=@uid";
        sdr.SelectCommand.Connection = con; //直接调用数据库连接对象
        sdr.SelectCommand.Parameters.Add("@uid", SqlDbType.VarChar).Value = uid;
        sdr.Fill(ds, "OrderByUid");  //填充数据集ds，表名为OrderByUid
        return ds;  //返回数据集
    }

    /// <summary>
    /// 查询orderID（提供添加订单详细信息）
    /// </summary>
    /// <param name="uid"></param>
    /// <returns></returns>
    public int OrderIdByUid(string uid)
    {
        int orderId = -1;
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select max(orderid)from [order] where [uid]=@uid";
        cmd.Parameters.Add("@uid", SqlDbType.VarChar).Value = uid;
        cmd.Connection = con; //直接调用数据库连接对象
        try
        {
            con.Open();            //打开数据库
            orderId=Convert.ToInt32 (cmd.ExecuteScalar());
        }
        catch (Exception ex)
        {
            orderId = -1;
        }
        finally
        {
            con.Close();      //关闭数据库           
        }
        return orderId ;

       
    }

    // 支付成功更新为已审核

    public string UpdateOrder(OrderST order)
    {
        string messgae = "";
        SqlCommand cmd = new SqlCommand();                           //创建SqlCommand对象
        cmd.Connection = con;
        cmd.CommandText = "Update [order] set [status]='已审核' where orderid=@orderid";
        cmd.Parameters.Add("@orderid", SqlDbType.VarChar).Value = order.OrderId;    
        try
        {
            con.Open();            //打开数据库
            if (cmd.ExecuteNonQuery() > 0)              //执行SQL语句
            {
                messgae = "审核成功！";
            }
            else
            {
                messgae = "审核失败！";
            }
        }
        catch (Exception ex)
        {
            messgae = "数据库打开出错！";
        }
        finally
        {
            con.Close();      //关闭数据库           
        }
        return messgae;
    }
}
