using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web25175.DBHelper;
using System.Data.SqlClient;

/// <summary>
/// N38_User 的摘要说明
/// </summary>
public class N38_User
{
	public N38_User()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 检查用户是否锁定
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public bool UserLock(string UserName)
    {
        string sqlStr = "select n38_user_state from n38_25175_user where n38_user_name='" + UserName + "'";
        string state = DBManager.Instance().ExecuteScalar(CommandType.Text, sqlStr).ToString();
        if (state.Trim() == "锁定")
        {
            return true;
        }
        else
            return false;
    }


    /// <summary>
    /// 锁定用户
    /// </summary>
    /// <param name="ID"></param>
    public void LockUser(int ID)
    {
        string sqlStr = "update n38_25175_user set n38_user_state='锁定' where n38_user_id=" + ID + "";
        DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
    }

    /// <summary>
    /// 解锁用户
    /// </summary>
    /// <param name="ID"></param>
    public void UnLockUser(int ID)
    {
        string sqlStr = "update n38_25175_user set n38_user_state='正常' where n38_user_id=" + ID + "";
        DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
    }
    /// <summary>
    /// 根据用户名检查用户名是否存在
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public bool UserExit(string UserName)
    {
        string sqlStr = "select * from n38_25175_user where n38_user_name='" + UserName + "'";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 根据真名检查用户名是否存在
    /// </summary>
    /// <param name="RealName"></param>
    /// <returns></returns>
    public bool UserExitByReal(string RealName)
    {
        string sqlStr = "select * from n38_25175_user where n38_user_realname='" + RealName + "'";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }
    /// <summary>
    /// 根据id检查用户名是否存在
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public bool UserExitByID(int ID)
    {
        string sqlStr = "select * from n38_25175_user where n38_user_id=" + ID + "";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }
    /// <summary>
    /// 添加新用户
    /// </summary>
    /// <param name="Name"></param>
    /// <param name="RealName"></param>
    /// <param name="Pwd"></param>
    /// <param name="Type"></param>
    /// <param name="State"></param>
    /// <param name="BorrowAmount"></param>
    /// <returns></returns>
    public bool InsertUser(string Name, string RealName, string Type)
    {
        if (this.UserExit(Name))
        {
            return false;
        }
        else
        {
            string sqlStr = "insert into n38_25175_user (n38_user_name,n38_user_realname,n38_user_pwd,n38_user_type,n38_user_state,n38_user_borrowamount) values('" + Name + "','" + RealName + "','111111','" + Type + "','正常',0)";
            int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
            if (num > 0)
                return true;
            else
                return false;
        }
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public bool DeleteUser(int ID,string UserName)
    {
        if (this.BookBorrow(UserName))
        {
            return false;
        }
        else
        {
            string sqlStr = "delete from n38_25175_user where n38_user_id=" + ID + "";
            int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
            if (num > 0)
                return true;
            else
                return false;
        }
    }

    /// <summary>
    /// 修改用户信息
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Name"></param>
    /// <param name="RealName"></param>
    /// <param name="Pwd"></param>
    /// <param name="Type"></param>
    /// <returns></returns>
    public bool UpdateUser(int ID,string Name, string RealName, string Type)
    {
        
            string sqlStr = "update n38_25175_user set n38_user_name='" + Name + "',n38_user_realname='" + RealName + "',n38_user_type='" + Type + "' where n38_user_id=" + ID + "";
            int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
            if (num > 0)
                return true;
            else
                return false;
    }
    /// <summary>
    /// 检查用户是否挂失图书
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public bool BookLost(string UserName)
    {
        string sqlStr = "select * from n38_25175_lost where n38_lost_username='" + UserName + "'";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 检查用户是否借书
    /// </summary>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool BookBorrow(string UserName)
    {
        string sqlStr = "select * from n38_25175_borrow where n38_borrow_username='" + UserName + "'";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }
    /// <summary>
    /// 获取用户已借书量
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public int GetBorrowAmount(string UserName)
    {
        string sqlStr = "select * from n38_25175_user where n38_user_name='" + UserName + "'";
        int num = 0;
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        while (myRead.Read())
        {
            num= int.Parse(myRead["n38_user_borrowamount"].ToString());
        }
        myRead.Close();
        return num;
    }

    /// <summary>
    /// 通过id获取用户
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public string GetUserName(int ID)
    {
        string sqlStr = "select n38_user_name from n38_25175_user where n38_user_id=" + ID + "";
        string name = DBManager.Instance().ExecuteScalar(CommandType.Text, sqlStr).ToString().Trim();
        return name;
    }

    /// <summary>
    /// 根据真名获取用户名
    /// </summary>
    /// <param name="realname"></param>
    /// <returns></returns>
    public string GetName(string realname)
    {
        string sqlStr = "select n38_user_name from n38_25175_user where n38_user_realname='" + realname + "'";
        string name = DBManager.Instance().ExecuteScalar(CommandType.Text, sqlStr).ToString().Trim();
        return name;
    }
    /// <summary>
    /// 通过id获取用户类型
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public string GetUserType(int ID)
    {
        string sqlStr = "select * from n38_25175_user where n38_user_id=" + ID + "";
        string type = "";
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        while (myRead.Read())
        {
            type = myRead["n38_user_type"].ToString().Trim();
        }
        myRead.Close();
        return type;
    }
    /// <summary>
    /// 读取用户信息到label
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="userid"></param>
    /// <param name="realname"></param>
    /// <param name="type"></param>
    /// <param name="amount"></param>
    public void GetUserData(int ID,Label userid,Label realname,Label type,Label amount)
    {
        if (this.UserExitByID(ID))
        {
            string sqlStr = "select *from n38_25175_user where n38_user_id=" + ID + "";
            SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
            while (myRead.Read())
            {
                userid.Text = "读者编号：" + myRead["n38_user_id"].ToString();
                realname.Text = "读者姓名：" + myRead["n38_user_realname"].ToString();
                type.Text = "读者类型：" + myRead["n38_user_type"].ToString();
                amount.Text = "已借书数量：" + myRead["n38_user_borrowamount"].ToString();
            }
            myRead.Close();
        }
        else
        {
            userid.Text = "该读者不存在";
            realname.Text = type.Text = amount.Text = "";
        }
    }

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="Name"></param>
    /// <param name="Pwd"></param>
    /// <returns></returns>
    public bool UserLogin(string Name, string Pwd)
    {
        string sqlStr = "select * from n38_25175_admin where n38_admin_name='" + Name + "' and n38_admin_pwd='" + Pwd + "'";
        bool result=DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }
}
