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

    /// <summary>
    /// 添加分类
    /// </summary>
    /// <param name="CatalogName"></param>
    /// <returns></returns>
    public bool AddCatalog(string CatalogName)
    {
        if (this.ExitCatalog(CatalogName))
        {
            return false;
        }
        else
        {
            string sqlStr = "insert into n38_25175_catalog(n38_catalog_name) values('" + CatalogName + "')";
            int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
            if (num > 0)
                return true;
            else
                return false;
        }
    }

    /// <summary>
    /// 检查分类是否已经存在
    /// </summary>
    /// <param name="CatalogName"></param>
    /// <returns></returns>
    public bool ExitCatalog(string CatalogName)
    {
        string sqlStr = "select * from n38_25175_catalog where n38_catalog_name='" + CatalogName + "'";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 删除分类
    /// </summary>
    /// <param name="CatalogName"></param>
    /// <returns></returns>
    public bool DeleteCatalog(string CatalogName)
    {
        if (this.ExitBook(CatalogName))
        {
            return false;
        }
        else
        {
            string sqlStr = "delete from n38_25175_catalog where n38_catalog_name='" + CatalogName + "'";
            int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
            if (num > 0)
                return true;
            else
                return false;
        }
    }

    /// <summary>
    /// 根据id获取分类名
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public string GetCatalogName(int ID)
    {
        string sqlStr = "select *from n38_25175_catalog where n38_catalog_id="+ID+"";
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        string name = "";
        while (myRead.Read())
        {
            name = myRead["n38_catalog_name"].ToString();
        }
        myRead.Close();
        return name;
    }

    /// <summary>
    /// 检查该分类下是否还有图书
    /// </summary>
    /// <param name="CatalogName"></param>
    /// <returns></returns>
    public bool ExitBook(string CatalogName)
    {
        string sqlStr = "select * from n38_25175_book where n38_book_catalog='" + CatalogName + "'";
        bool result = DBManager.Instance().CheckData(sqlStr);
        if (result)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 修改分类名称
    /// </summary>
    /// <param name="newCatalogName"></param>
    /// <param name="oldCatalogName"></param>
    /// <returns></returns>
    public bool UpdateCatalog(string newCatalogName, string oldCatalogName)
    {
        if (this.ExitCatalog(newCatalogName))
        {
            return false;
        }
        else
        {
            if (this.ExitBook(oldCatalogName))
            {
                string sqlStr1 = "update n38_25175_catalog set n38_catalog_name='" + newCatalogName + "' where n38_catalog_name='" + oldCatalogName + "'";
                string sqlStr2 = "update n38_25175_book set n38_book_catalog='" + newCatalogName + "' where n38_book_catalog='" + oldCatalogName + "'";
                try
                {
                    DBManager.Instance().BeginTrans();
                    DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr1);
                    DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr2);
                    DBManager.Instance().CommitTrans();
                    return true;
                }
                catch (Exception)
                {
                    DBManager.Instance().RollbackTrans();
                    return false;
                }
            }
            else
            {
                string sqlStr = "update n38_25175_catalog set n38_catalog_name='" + newCatalogName + "' where n38_catalog_name='" + oldCatalogName + "'";
                int num = DBManager.Instance().ExecuteNonQuery(CommandType.Text, sqlStr);
                if (num > 0)
                    return true;
                else
                    return false;
            }
        }
    }

    /// <summary>
    /// 分类绑定到下拉控件
    /// </summary>
    /// <param name="ddl"></param>
    public void rDropDownList(DropDownList ddl)
    {
        DataSet ds = new DataSet();
        string sqlStr="select * from n38_25175_catalog";
        ds = DBManager.Instance().ExecuteDataSet(CommandType.Text, sqlStr);
        ddl.DataSource = ds;
        ddl.DataTextField = "n38_catalog_name";
        ddl.DataValueField = "n38_catalog_name";
        ddl.DataBind();
        ddl.Items.Insert(0,new ListItem("请选择","请选择"));
    }

    /// <summary>
    /// 读取分类名到文本框
    /// </summary>
    /// <param name="CatalogName"></param>
    /// <param name="Name"></param>
    public void GetCatalog(string CatalogName,TextBox Name)
    {
        string sqlStr = "select *from n38_25175_catalog where n38_catalog_name='" + CatalogName + "'";
        SqlDataReader myRead = DBManager.Instance().ExecuteReader(sqlStr);
        while (myRead.Read())
        {
            Name.Text = myRead["n38_catalog_name"].ToString();
        }
        myRead.Close();
    }
}
