using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CitySunlight
{
    public static class ProductClassManager
    {
        public static SqlHelper sql = new SqlHelper("Data Source=.;Initial Catalog=myApplication;Persist Security Info=True;User ID=sa;Password=325325");
        //分类表名
        public const String tbname = "tb_proclass";

        /// <summary>
        /// 获得分类列表的Html代码,默认选择第一个
        /// </summary>
        /// <param name="name">列表名</param>
        /// <returns>分类列表的Html代码</returns>
        public static String getOptionString(String name)
        {
            return getOptionString(name, "0");
        }
        /// <summary>
        /// 获得分类列表的Html代码,可配置选项(数据库部分)
        /// </summary>
        /// <param name="name">列表名</param>
        /// <param name="df">列表选项</param>
        /// <returns>分类列表的Html代码</returns>
        public static String getOptionString(String name, String df)
        {
            String html = "<select style=\"width: 120px\" Name=\"" + name + "\">";
            html += "<option value=\"0\">请选择分类</option>";
            DataTable option = sql.ExecuteDataTable("select * from " + tbname, CommandType.Text);
            foreach (DataRow item in option.Rows)
            {
                String nid = item["Nid"].ToString();
                String classname = item["ProClassName"].ToString();
                html += String.Format("<option value=\"{0}\"{1}>{2}</option>", nid, df == nid ? " selected=\"selected\"" : "", classname);
            }
            html += "</select>";
            return html;
        }
        /// <summary>
        /// 通过ID获得分类名称(数据库部分)
        /// </summary>
        /// <param name="Nid">分类ID</param>
        /// <returns>分类名称</returns>
        public static String getClassName(int Nid)
        {
            DataTable dt = sql.ExecuteDataTable("select * from " + tbname + " where nid='" + Nid + "'", CommandType.Text);
            if (dt.Rows.Count > 0)
                return dt.Rows[0]["ProClassName"].ToString();
            else
                return "分类已被删除";
        }
        /// <summary>
        /// 判断分类是否存在(数据库部分)
        /// </summary>
        /// <param name="name">分类名称</param>
        /// <returns>是否存在</returns>
        public static bool isExist(String name)
        {
            return sql.ExecuteDataTable("select * from " + tbname + " where ProClassName='" + name + "'", CommandType.Text).Rows.Count > 0;
        }
        /// <summary>
        /// 通过Id判断分类是否存在(数据库部分)
        /// </summary>
        /// <param name="nid">分类ID</param>
        /// <returns>是否存在</returns>
        public static bool isExist(int nid)
        {
            return sql.ExecuteDataTable("select * from " + tbname + " where Nid='" + nid + "'", CommandType.Text).Rows.Count > 0;
        }
        /// <summary>
        /// 添加分类(数据库部分)
        /// </summary>
        /// <param name="Pid"></param>
        /// <param name="classname"></param>
        /// <returns>是否成功</returns>
        public static bool addclass(int Pid, String classname)
        {
            int j = sql.ExecuteNonQuery("insert into " + tbname + "(Pid,ProClassName) values('" + Pid + "','" + classname + "')", CommandType.Text);
            if (j > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 删除分类(数据库部分)
        /// </summary>
        /// <param name="Nid"></param>
        /// <returns>是否成功</returns>
        public static bool removeclass(int Nid)
        {
            if (Nid != 0)
            {
                int j = sql.ExecuteNonQuery("delete from " + tbname + " where Nid='" + Nid + "'", CommandType.Text);
                if (j > 0)
                    return true;
            }
            return false;
        }
        /// <summary>
        /// 修改分类(数据库部分)
        /// </summary>
        /// <param name="Nid">分类ID</param>
        /// <param name="cname">修改后的名称</param>
        /// <returns>是否成功</returns>
        public static bool editclass(int Nid, String cname)
        {
            if (Nid != 0)
            {
                int j = sql.ExecuteNonQuery("update " + tbname + " set ProClassName='" + cname + "' where Nid='" + Nid + "'", CommandType.Text);
                if (j > 0)
                    return true;
            }
            return false;
        }
    }
}