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>
///UserClass 的摘要说明
/// </summary>
public class UserClass
{
    SqlConnection con = ConSql.ConnectionSql();
    DataSet ds = new DataSet();

    /// <summary>
    /// 管理员登录查询
    /// </summary>
    /// <param name="ust"></param>
    /// <returns>用户角色</returns>
    public string UserLoginByManager(UserST ust)
    {
        string result = "";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select role from [user] where [uid]=@userid and pwd=@pwd ";
        cmd.Connection = con;
        cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = ust.Uid;
        cmd.Parameters.Add("@pwd", SqlDbType.VarChar).Value = ust.Pwd;
        try
        {
            con.Open();
            result = Convert.ToString(cmd.ExecuteScalar());
        }
        catch (Exception ex)
        {
            result = "error";
        }
        finally
        {
            con.Close();
        }
        return result;

    }



    /// <summary>
    /// 用于注册时重名检查
    /// </summary>
    /// <param name="userId">登录名</param>
    /// <returns>返回数据库记录或“error”</returns>
    public string SelectUserById(string userId)
    {
        string result = "";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select [uid] from [user] where [uid]=@userid";
        cmd.Connection = con;
        cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = userId;
        try
        {
            con.Open();
            result =Convert.ToString (cmd.ExecuteScalar());
        }
        catch (Exception ex)
        {
            result = "error";
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    /// <summary>
    /// 注册用户信息User表
    /// </summary>
    /// <param name="userST"></param>
    /// <returns>“ok”成功，其他错误</returns>
    public string InsertUser(UserST userST)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Insert into [user] values(@uid,@pwd,@role)";
        cmd.Parameters.Add("@uid", SqlDbType.VarChar).Value = userST.Uid;
        cmd.Parameters.Add("@pwd", SqlDbType.VarChar).Value = userST.Pwd;
        cmd.Parameters.Add("@role", SqlDbType.VarChar).Value = userST.Role;
        string result = "";
        try
        {
            con.Open();
            if (cmd.ExecuteNonQuery() > 0)
            {
                result = "ok";
            }
            else
            {
                result="error";
            }

        }
        catch (Exception ex)
        {
            result="error";           
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    /// <summary>
    /// 注册用户信息UserInfo表
    /// </summary>
    /// <param name="userinfoST"></param>
    /// <returns>“ok”成功，其他错误</returns>
    public string InsertUserInfo(UserInfoST userinfoST)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Insert into userinfo values(@uid,@uname,@sex,@email,@addr1,@code,@phone,@xueli,@userimage)";
        cmd.Parameters.Add("@uid", SqlDbType.VarChar).Value = userinfoST.Uid;
        cmd.Parameters.Add("@uname", SqlDbType.VarChar).Value = userinfoST.Uname;
        cmd.Parameters.Add("@sex", SqlDbType.VarChar).Value = userinfoST.Sex;
        cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = userinfoST.Email;
        cmd.Parameters.Add("@addr1", SqlDbType.VarChar).Value = userinfoST.Addr1;
        cmd.Parameters.Add("@code", SqlDbType.VarChar).Value = userinfoST.Code;
        cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = userinfoST.Phone;
        cmd.Parameters.Add("@xueli", SqlDbType.VarChar).Value = userinfoST.Xueli;
        cmd.Parameters.Add("@userimage", SqlDbType.VarChar).Value = userinfoST.Userimage;
        string result = "";
        try
        {
            con.Open();
            if (cmd.ExecuteNonQuery() > 0)
            {
                result = "ok";
            }
            else
            {
                result = "error";
            }

        }
        catch (Exception ex)
        {
            result = "error";
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    /// <summary>
    /// 按uid查询用户详细信息
    /// </summary>
    /// <param name="ust"></param>
    /// <returns></returns>
    public DataSet UserInfoByUid(UserInfoST uist)
    {
        SqlDataAdapter sdr = new SqlDataAdapter();
        sdr.SelectCommand = new SqlCommand();
        sdr.SelectCommand.CommandText = "select * from userinfo where [uid]=@userid";
        sdr.SelectCommand.Connection = con;
        sdr.SelectCommand.Parameters.Add("@userid", SqlDbType.VarChar).Value = uist.Uid;
        sdr.Fill(ds, "UserInfoByUid");
        return ds;
    }

    /// <summary>
    /// 按uid查询基本信息
    /// </summary>
    /// <param name="ust"></param>
    /// <returns></returns>
    public DataSet UserByUid(UserST ust)
    {
        SqlDataAdapter sdr = new SqlDataAdapter();
        sdr.SelectCommand = new SqlCommand();
        sdr.SelectCommand.CommandText = "select * from [user] where [uid]=@userid";
        sdr.SelectCommand.Connection = con;
        sdr.SelectCommand.Parameters.Add("@userid", SqlDbType.VarChar).Value = ust.Uid;
        sdr.Fill(ds, "UserByUid");
        return ds;
    }




   


}
