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_Config 的摘要说明
/// </summary>
public class N38_Config
{
	public N38_Config()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 返回老师或学生允许借书的最大数量
    /// </summary>
    /// <param name="UserType"></param>
    /// <returns></returns>
    public int GetBookAmount(string UserType)
    {
        string sqlStr = "select * from n38_25175_config";
        int num = 5;
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        while (myRead.Read())
        {
            if (UserType == "教师")
                num= int.Parse(myRead["n38_config_teacheramount"].ToString());
            if (UserType == "学生")
                num= int.Parse(myRead["n38_config_studentamount"].ToString());
        }
        myRead.Close();
        return num;
    }

    /// <summary>
    /// 返回老师或学生允许借书的最长时间
    /// </summary>
    /// <param name="UserType"></param>
    /// <returns></returns>
    public int GetBookDay(string UserType)
    {
        string sqlStr = "select * from n38_25175_config where n38_config_id=1";
        int num = 15;
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        while (myRead.Read())
        {
            if (UserType == "教师")
                num= int.Parse(myRead["n38_config_teacherday"].ToString());
            if (UserType == "学生")
                num= int.Parse(myRead["n38_config_studentday"].ToString());
        }
        myRead.Close();
        return num;
    }
    /// <summary>
    /// 检查是否有配置
    /// </summary>
    /// <returns></returns>
    public bool ExitConfig()
    {
        string sqlStr = "select * from n38_25175_config";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 添加系统配置
    /// </summary>
    /// <param name="TchAmount"></param>
    /// <param name="TchDay"></param>
    /// <param name="StuAmount"></param>
    /// <param name="StuDay"></param>
    /// <returns></returns>
    public bool InsertConfig(int TchAmount, int TchDay, int StuAmount, int StuDay)
    {
        string sqlStr = "insert into n38_25175_config (n38_config_teacheramount,n38_config_teacherday,n38_config_studentamount,n38_config_studentday) values(" + TchAmount + "," + TchDay + "," + StuAmount + "," + StuDay + ")";
        int result = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
        if (result > 0)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 修改配置方法
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="TchAmount"></param>
    /// <param name="TchDay"></param>
    /// <param name="StuAmount"></param>
    /// <param name="StuDay"></param>
    /// <returns></returns>
    public bool UpdateConfig(int TchAmount, int TchDay, int StuAmount, int StuDay)
    {
        string sqlStr = "update n38_25175_config set n38_config_teacheramount=" + TchAmount + ",n38_config_teacherday=" + TchDay + ",n38_config_studentamount=" + StuAmount + ",n38_config_studentday=" + StuDay + " where n38_config_id=1";
        int result = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
        if (result > 0)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 读取配置信息到文本框
    /// </summary>
    /// <param name="TchAmount"></param>
    /// <param name="TchDay"></param>
    /// <param name="StuAmount"></param>
    /// <param name="StuDay"></param>
    public void GetConfigData(TextBox TchAmount,TextBox TchDay,TextBox StuAmount,TextBox StuDay)
    {
        string sqlStr = "select * from n38_25175_config where n38_config_id=1";
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        while (myRead.Read())
        {
            TchAmount.Text = myRead["n38_config_teacheramount"].ToString();
            TchDay.Text = myRead["n38_config_teacherday"].ToString();
            StuAmount.Text = myRead["n38_config_studentamount"].ToString();
            StuDay.Text = myRead["n38_config_studentday"].ToString();
        }
        myRead.Close();
    }
}
