using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZXDT.DBUtility;
using ZXDT.CallCenter.Model;
using System.Collections.Generic;

namespace ZXDT.CallCenter.DAL
{
    /// <summary>
    /// 数据访问类:T_Com_Class
    /// </summary>
    public partial class T_Com_Class
    {
        public T_Com_Class()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Classid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Com_Class");
            strSql.Append(" where Classid=@Classid");
            SqlParameter[] parameters = {
                    new SqlParameter("@Classid", SqlDbType.Int,4)
            };
            parameters[0].Value = Classid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZXDT.CallCenter.Model.T_Com_Class model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Com_Class(");
            strSql.Append("Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid)");
            strSql.Append(" values (");
            strSql.Append("@Classcode,@Classname,@Parentid,@Parentcode,@ParentName,@Sort,@iconClose,@iconOpen,@Describe,@Remark,@typeid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@Classcode", SqlDbType.VarChar,50),
                    new SqlParameter("@Classname", SqlDbType.VarChar,50),
                    new SqlParameter("@Parentid", SqlDbType.Int,4),
                    new SqlParameter("@Parentcode", SqlDbType.VarChar,50),
                    new SqlParameter("@ParentName", SqlDbType.VarChar,50),
                    new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@iconClose", SqlDbType.VarChar,200),
                    new SqlParameter("@iconOpen", SqlDbType.VarChar,200),
                    new SqlParameter("@Describe", SqlDbType.VarChar,250),
                    new SqlParameter("@Remark", SqlDbType.VarChar,50),
                    new SqlParameter("@typeid", SqlDbType.Int,4)};
            parameters[0].Value = model.Classcode;
            parameters[1].Value = model.Classname;
            parameters[2].Value = model.Parentid;
            parameters[3].Value = model.Parentcode;
            parameters[4].Value = model.ParentName;
            parameters[5].Value = model.Sort;
            parameters[6].Value = model.iconClose;
            parameters[7].Value = model.iconOpen;
            parameters[8].Value = model.Describe;
            parameters[9].Value = model.Remark;
            parameters[10].Value = model.typeid;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZXDT.CallCenter.Model.T_Com_Class model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Com_Class set ");
            strSql.Append("Classcode=@Classcode,");
            strSql.Append("Classname=@Classname,");
            strSql.Append("Parentid=@Parentid,");
            strSql.Append("Parentcode=@Parentcode,");
            strSql.Append("ParentName=@ParentName,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("iconClose=@iconClose,");
            strSql.Append("iconOpen=@iconOpen,");
            strSql.Append("Describe=@Describe,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("typeid=@typeid");
            strSql.Append(" where Classid=@Classid");
            SqlParameter[] parameters = {
                    new SqlParameter("@Classcode", SqlDbType.VarChar,50),
                    new SqlParameter("@Classname", SqlDbType.VarChar,50),
                    new SqlParameter("@Parentid", SqlDbType.Int,4),
                    new SqlParameter("@Parentcode", SqlDbType.VarChar,50),
                    new SqlParameter("@ParentName", SqlDbType.VarChar,50),
                    new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@iconClose", SqlDbType.VarChar,200),
                    new SqlParameter("@iconOpen", SqlDbType.VarChar,200),
                    new SqlParameter("@Describe", SqlDbType.VarChar,250),
                    new SqlParameter("@Remark", SqlDbType.VarChar,50),
                    new SqlParameter("@typeid", SqlDbType.Int,4),
                    new SqlParameter("@Classid", SqlDbType.Int,4)};
            parameters[0].Value = model.Classcode;
            parameters[1].Value = model.Classname;
            parameters[2].Value = model.Parentid;
            parameters[3].Value = model.Parentcode;
            parameters[4].Value = model.ParentName;
            parameters[5].Value = model.Sort;
            parameters[6].Value = model.iconClose;
            parameters[7].Value = model.iconOpen;
            parameters[8].Value = model.Describe;
            parameters[9].Value = model.Remark;
            parameters[10].Value = model.typeid;
            parameters[11].Value = model.Classid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Classid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Com_Class ");
            strSql.Append(" where Classid=@Classid");
            SqlParameter[] parameters = {
                    new SqlParameter("@Classid", SqlDbType.Int,4)
            };
            parameters[0].Value = Classid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Classidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Com_Class ");
            strSql.Append(" where Classid in (" + Classidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.T_Com_Class GetModel(int Classid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid from T_Com_Class ");
            strSql.Append(" where Classid=@Classid");
            SqlParameter[] parameters = {
                    new SqlParameter("@Classid", SqlDbType.Int,4)
            };
            parameters[0].Value = Classid;

            ZXDT.CallCenter.Model.T_Com_Class model = new ZXDT.CallCenter.Model.T_Com_Class();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public ZXDT.CallCenter.Model.T_Com_Class GetModelByWhere(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid from T_Com_Class ");
            if (!string.IsNullOrEmpty(where))
            {
                strSql.Append(" where " + where);
            }

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.T_Com_Class DataRowToModel(DataRow row)
        {
            ZXDT.CallCenter.Model.T_Com_Class model = new ZXDT.CallCenter.Model.T_Com_Class();
            if (row != null)
            {
                if (row["Classid"] != null && row["Classid"].ToString() != "")
                {
                    model.Classid = int.Parse(row["Classid"].ToString());
                }
                if (row["Classcode"] != null)
                {
                    model.Classcode = row["Classcode"].ToString();
                }
                if (row["Classname"] != null)
                {
                    model.Classname = row["Classname"].ToString();
                }
                if (row["Parentid"] != null && row["Parentid"].ToString() != "")
                {
                    model.Parentid = int.Parse(row["Parentid"].ToString());
                }
                if (row["Parentcode"] != null)
                {
                    model.Parentcode = row["Parentcode"].ToString();
                }
                if (row["ParentName"] != null)
                {
                    model.ParentName = row["ParentName"].ToString();
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["iconClose"] != null)
                {
                    model.iconClose = row["iconClose"].ToString();
                }
                if (row["iconOpen"] != null)
                {
                    model.iconOpen = row["iconOpen"].ToString();
                }
                if (row["Describe"] != null)
                {
                    model.Describe = row["Describe"].ToString();
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["typeid"] != null && row["typeid"].ToString() != "")
                {
                    model.typeid = int.Parse(row["typeid"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid ");
            strSql.Append(" FROM T_Com_Class ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid ");
            strSql.Append(" FROM T_Com_Class ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Com_Class ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Classid desc");
            }
            strSql.Append(")AS Row, T.*  from T_Com_Class T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  BasicMethod
        #region 获取树结构
        /// <summary>
        ///  获得当前节点下的所有子集以tree的格式
        /// </summary>
        public TreeNodeModel GetTreeJsonModel(int parentId, int type = 0)
        {
            TreeNodeModel returnModel = new TreeNodeModel();
            DataSet ds = null;
            TreeNodeModel model = new TreeNodeModel();
            if (parentId == 0)
            {
                ds = GetList(string.Format( " 1=1 Order by sort asc", parentId));
                List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                model.id = 0;
                model.name = "通用字典";
                model.code = 0;
                model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                model.open = "true";                
                model.children = modelList;
            }
            else
            {
                ds = GetList(string.Format("typeid={0} Or Classid={0} Order by sort asc", parentId));
                Model.T_Com_Class modelclass = GetModel(parentId);
                if (modelclass == null)
                {
                    return returnModel;
                }
                else
                {
                    List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                    model.id = modelclass.Classid;
                    model.name = modelclass.Classname;
                    model.code = modelclass.Classid;
                    model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                    model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                    model.open = "true";
                    model.IsOrPrivate = 0;
                    model.children = modelList;
                }
            }

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                returnModel = GetTreeModel(ds.Tables[0], model, type);
            }
            return returnModel;
        }


        /// <summary>
        /// 获取chlid
        /// </summary>
        /// <param name="table">数据结合</param>
        /// <param name="model">当前model</param>
        /// <returns></returns>
        public TreeNodeModel GetTreeModel(DataTable table, TreeNodeModel model, int type)
        {
            DataRow[] rows = table.Select("Parentid=" + model.id);
            if (rows != null && rows.Length > 0)
            {
                foreach (DataRow row in rows)
                {
                    
                    List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                    TreeNodeModel childModel = new TreeNodeModel();
                    childModel.id = int.Parse(row["Classid"].ToString());
                    childModel.name = row["Classname"].ToString();
                    childModel.code = row["Remark"].ToInt32();
                    childModel.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                    childModel.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                    childModel.open = "false";
                    childModel.IsOrPrivate = 1;
                    childModel.children = modelList;
                    model.children.Add(childModel);
                    GetTreeModel(table, childModel, type);
                }
            }

            return model;

        }





        /// <summary>
        ///  获得当前节点下的所有子集以tree的格式
        /// </summary>
        public TreeNodeModel GetTreeJsonModelTurn(string usercode)
        {
            // Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid
            TreeNodeModel returnModel = new TreeNodeModel();
            DataSet ds = null;
            TreeNodeModel model = new TreeNodeModel();
            Model.T_Com_Class modelclass = GetModelByWhere(string.Format("parentid=(select Classid from T_Com_Class where  Classname='常用人员' and parentid=0) and Remark='{0}'",usercode));
            if (modelclass != null)
            {
                ds = GetList(string.Format("typeid={0} Or parentid={0} Order by sort desc", modelclass.Classid));

                List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                model.id = modelclass.Classid;
                model.name = "常用名单";
                model.code = modelclass.Classid;
                model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                model.open = "true";
                model.IsOrPrivate = 0;
                model.children = modelList;

            }
            else
            {
                List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                model.id = 0;
                model.name = "常用名单";
                model.code = 0;
                model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                model.open = "true";
                model.IsOrPrivate = 0;
                model.children = null;
              
            }

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                returnModel = GetTreeModelTurn(ds.Tables[0], model);
            }
            else
            {
                returnModel = model;
            }

            return returnModel;
        }


        /// <summary>
        /// 获取chlid
        /// </summary>
        /// <param name="table">数据结合</param>
        /// <param name="model">当前model</param>
        /// <returns></returns>
        public TreeNodeModel GetTreeModelTurn(DataTable table, TreeNodeModel model)
        {
            DataRow[] rows = table.Select("Parentid=" + model.id);
            if (rows != null && rows.Length > 0)
            {
                foreach (DataRow row in rows)
                {

                    List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                    TreeNodeModel childModel = new TreeNodeModel();
                    childModel.id = int.Parse(row["Classid"].ToString());
                    childModel.name = row["Classname"].ToString();
                    childModel.code = row["Remark"].ToInt32();
                    childModel.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                    childModel.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                    childModel.open = "false";
                    childModel.IsOrPrivate = 1;
                    childModel.children = modelList;
                    model.children.Add(childModel);
                    GetTreeModelTurn(table, childModel);
                }
            }

            return model;

        }




        public bool UpdateRate(string usercode,int userid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  T_Com_Class ");
            strSql.Append(" set Sort=Sort+1   ");
            strSql.Append(string.Format(" where parentid=(select Classid  from  T_Com_Class where parentid=(select Classid from T_Com_Class where  Classname='常用人员' and parentid=0) and Remark='{0}') and Remark='{1}'", usercode,userid));
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}

