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>
/// 图书操作类
/// </summary>
public class N38_BookOperate
{
	public N38_BookOperate()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 借书方法
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="UserType"></param>
    /// <param name="BookName"></param>
    /// <param name="State"></param>
    /// <returns></returns>
    public bool BorrowBook(string UserName, string UserType, string BookName)
    {
        N38_Config myConfig = new N38_Config();
        N38_User myUser = new N38_User();
        int amount = myConfig.GetBookAmount(UserType);
        int num = myUser.GetBorrowAmount(UserName);
        if (num < amount)
        {
            if (myUser.UserLock(UserName))
            {
                return false;
            }
            else
            {
                string sqlStr1 = "update n38_25175_user set n38_user_borrowamount=n38_user_borrowamount+1 where n38_user_name='" + UserName + "'";
                string sqlStr2 = "update n38_25175_book set n38_book_amount=n38_book_amount-1 where n38_book_name='" + BookName + "'";
                string sqlStr3 = "insert into n38_25175_borrow (n38_borrow_bookname,n38_borrow_username,n38_borrow_date,n38_borrow_state) values('"+BookName+"','"+UserName+"','"+DateTime.Now+"','正常')";
                try
                {
                    DBManager.Instance().BeginTrans();
                    DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr1);
                    DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr2);
                    DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr3);
                    DBManager.Instance().CommitTrans();
                    return true;
                }
                catch(Exception)
                {
                    DBManager.Instance().RollbackTrans();
                    return false;
                }
            }
        }
        else
            return false;
    }

    /// <summary>
    /// 检查图书是否超期
    /// </summary>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool BookOverTime(string BookName,string UserName)
    {
        string sqlStr = "select n38_borrow_state from n38_25175_borrow where n38_borrow_bookname='" + BookName + "' and n38_borrow_username='" + UserName + "'";
        string state=DBManager.Instance().ExecuteScalar(CommandType.Text,sqlStr).ToString();
        if(state.Trim()=="超期")
            return true ;
        else 
            return false ;
    }

    /// <summary>
    /// 还书方法
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool BackBook(string UserName, string BookName)
    {
        if (this.BookOverTime(BookName,UserName))
        {
            return false;
        }
        else
        {
            string sqlStr1 = "update n38_25175_book set n38_book_amount=n38_book_amount+1 where n38_book_name='" + BookName + "'";
            string sqlStr2 = "update n38_25175_user set n38_user_borrowamount=n38_user_borrowamount-1 where n38_user_name='" + UserName + "'";
            string sqlStr3 = "insert into n38_25175_back (n38_back_bookname,n38_back_username,n38_back_date) values('" + BookName + "','" + UserName + "','" + DateTime.Now + "')";
            string sqlStr4 = "delete from n38_25175_borrow where n38_borrow_username='" + UserName + "' and n38_borrow_bookname='" + BookName + "'";
            try
            {
                DBManager.Instance().BeginTrans();
                DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr1);
                DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr2);
                DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr3);
                DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr4);
                DBManager.Instance().CommitTrans();
                return true;
            }
            catch (Exception)
            {
                DBManager.Instance().RollbackTrans();
                return false;
            }
        }
    }

    /// <summary>
    /// 续借图书方法
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool RenewBook(string UserName, string BookName)
    {
        if (this.BookOverTime(BookName,UserName))
        {
            return false;
        }
        else
        {
            N38_User myUser=new N38_User();
            if (myUser.UserLock(UserName))
            {
                return false;
            }
            else
            {
                string sqlStr = "update n38_25175_borrow set n38_borrow_date='" + DateTime.Now + "' where n38_borrow_bookname='" + BookName + "' and n38_borrow_username='" + UserName + "'";
                int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
                if (num > 0)
                    return true;
                else
                    return false;
            }
        }
    }

    /// <summary>
    /// 修改借书表中借书状态
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="BookName"></param>
    /// <param name="State"></param>
    /// <returns></returns>
    public bool UpdateBorrow(string UserName, string BookName,string State)
    {
        string sqlStr = "update n38_25175_borrow set n38_borrow_state='" + State + "' where n38_borrow_bookname='" + BookName + "' and n38_borrow_username='" + UserName + "'";
        int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
        if (num > 0)
        {
            return true;
        }
        else
            return false;
    }

    /// <summary>
    /// 图书报废方法
    /// </summary>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool BookBreak(string BookName)
    {
        N38_Book myBook=new N38_Book();
        if (myBook.ExitBook(BookName))
        {
            string sqlStr = "insert into n38_25175_break (n38_break_bookname,n38_break_date) values('" + BookName + "','" + DateTime.Now + "')";
            string sqlStr1 = "update n38_25175_book set n38_book_amount=n38_book_amount-1 where n38_book_name='" + BookName + "'";
            try
            {
                DBManager.Instance().BeginTrans();
                DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
                DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr1);
                DBManager.Instance().CommitTrans();
                return true;
            }
            catch (Exception)
            {
                DBManager.Instance().RollbackTrans();
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 图书挂失方法
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool BookLost(string UserName, string BookName)
    {
        string sqlStr2 = "update n38_25175_user set n38_user_borrowamount=n38_user_borrowamount-1 where n38_user_name='" + UserName + "'";
        string sqlStr3 = "insert into n38_25175_lost (n38_lost_bookname,n38_lost_username,n38_lost_date) values('" + BookName + "','" + UserName + "','" + DateTime.Now + "')";
        string sqlStr4 = "delete from n38_25175_borrow where n38_borrow_bookname='"+BookName+"' and n38_borrow_username='"+UserName+"'";
        try
        {
            DBManager.Instance().BeginTrans();
            DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr2);
            DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr3);
            DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr4);
            DBManager.Instance().CommitTrans();
            return true;
        }
        catch (Exception)
        {
            DBManager.Instance().RollbackTrans();
            return false;
        }
    }


    /// <summary>
    /// 图书找回，取消挂失
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="BookName"></param>
    /// <returns></returns>
    public bool BookFind(string UserName,string BookName)
    {
        string sqlStr3 = "delete from n38_25175_lost where n38_lost_bookname='" + BookName + "'and n38_lost_username='" + UserName + "'";
        string sqlStr4 = "update n38_25175_book set n38_book_amount=n38_book_amount+1 where n38_book_name='" + BookName + "'";
        try
        {
            DBManager.Instance().BeginTrans();
            DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr3);
            DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr4);
            DBManager.Instance().CommitTrans();
            return true;
        }
        catch (Exception)
        {
            DBManager.Instance().RollbackTrans();
            return false;
        }
    }

    /*public bool ExitOverTime(string UserName, string BookName,DateTime BorrowDate)
    {
 string sqlStr="select count(*) from n38_25175_overtime where "
    }*/

}
