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

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZXDT.CallCenter.Model.T_Sys_Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_Department(");
            strSql.Append("F_DeptCode,F_ParentName,F_ParentId,F_ParentCode,F_DeptName,F_State,F_Layer,F_Type,F_Sort,F_Remark,F_Telephone,F_Mobile,F_OtherPhone,F_Email,F_Header)");
            strSql.Append(" values (");
            strSql.Append("@F_DeptCode,@F_ParentName,@F_ParentId,@F_ParentCode,@F_DeptName,@F_State,@F_Layer,@F_Type,@F_Sort,@F_Remark,@F_Telephone,@F_Mobile,@F_OtherPhone,@F_Email,@F_Header)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50),
					new SqlParameter("@F_ParentName", SqlDbType.VarChar,50),
					new SqlParameter("@F_ParentId", SqlDbType.Int,4),
					new SqlParameter("@F_ParentCode", SqlDbType.VarChar,50),
					new SqlParameter("@F_DeptName", SqlDbType.VarChar,60),
					new SqlParameter("@F_State", SqlDbType.Int,4),
					new SqlParameter("@F_Layer", SqlDbType.Int,4),
					new SqlParameter("@F_Type", SqlDbType.Int,4),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_Remark", SqlDbType.VarChar,200),
					new SqlParameter("@F_Telephone", SqlDbType.VarChar,50),
					new SqlParameter("@F_Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@F_OtherPhone", SqlDbType.VarChar,50),
					new SqlParameter("@F_Email", SqlDbType.VarChar,50),
					new SqlParameter("@F_Header", SqlDbType.Int,4)};
            parameters[0].Value = model.F_DeptCode;
            parameters[1].Value = model.F_ParentName;
            parameters[2].Value = model.F_ParentId;
            parameters[3].Value = model.F_ParentCode;
            parameters[4].Value = model.F_DeptName;
            parameters[5].Value = model.F_State;
            parameters[6].Value = model.F_Layer;
            parameters[7].Value = model.F_Type;
            parameters[8].Value = model.F_Sort;
            parameters[9].Value = model.F_Remark;
            parameters[10].Value = model.F_Telephone;
            parameters[11].Value = model.F_Mobile;
            parameters[12].Value = model.F_OtherPhone;
            parameters[13].Value = model.F_Email;
            parameters[14].Value = model.F_Header;

            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_Sys_Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_Department set ");
            strSql.Append("F_DeptCode=@F_DeptCode,");
            strSql.Append("F_ParentName=@F_ParentName,");
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_ParentCode=@F_ParentCode,");
            strSql.Append("F_DeptName=@F_DeptName,");
            strSql.Append("F_State=@F_State,");
            strSql.Append("F_Layer=@F_Layer,");
            strSql.Append("F_Type=@F_Type,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_Telephone=@F_Telephone,");
            strSql.Append("F_Mobile=@F_Mobile,");
            strSql.Append("F_OtherPhone=@F_OtherPhone,");
            strSql.Append("F_Email=@F_Email,");
            strSql.Append("F_Header=@F_Header");
            strSql.Append(" where F_DeptId=@F_DeptId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50),
					new SqlParameter("@F_ParentName", SqlDbType.VarChar,50),
					new SqlParameter("@F_ParentId", SqlDbType.Int,4),
					new SqlParameter("@F_ParentCode", SqlDbType.VarChar,50),
					new SqlParameter("@F_DeptName", SqlDbType.VarChar,60),
					new SqlParameter("@F_State", SqlDbType.Int,4),
					new SqlParameter("@F_Layer", SqlDbType.Int,4),
					new SqlParameter("@F_Type", SqlDbType.Int,4),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_Remark", SqlDbType.VarChar,200),
					new SqlParameter("@F_Telephone", SqlDbType.VarChar,50),
					new SqlParameter("@F_Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@F_OtherPhone", SqlDbType.VarChar,50),
					new SqlParameter("@F_Email", SqlDbType.VarChar,50),
					new SqlParameter("@F_Header", SqlDbType.Int,4),
					new SqlParameter("@F_DeptId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_DeptCode;
            parameters[1].Value = model.F_ParentName;
            parameters[2].Value = model.F_ParentId;
            parameters[3].Value = model.F_ParentCode;
            parameters[4].Value = model.F_DeptName;
            parameters[5].Value = model.F_State;
            parameters[6].Value = model.F_Layer;
            parameters[7].Value = model.F_Type;
            parameters[8].Value = model.F_Sort;
            parameters[9].Value = model.F_Remark;
            parameters[10].Value = model.F_Telephone;
            parameters[11].Value = model.F_Mobile;
            parameters[12].Value = model.F_OtherPhone;
            parameters[13].Value = model.F_Email;
            parameters[14].Value = model.F_Header;
            parameters[15].Value = model.F_DeptId;

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

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_DeptId,F_DeptCode,F_ParentName,F_ParentId,F_ParentCode,F_DeptName,F_State,F_Layer,F_Type,F_Sort,F_Remark,F_Telephone,F_Mobile,F_OtherPhone,F_Email,F_Header from T_Sys_Department ");
            strSql.Append(" where F_DeptId=@F_DeptId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_DeptId", SqlDbType.Int,4)
			};
            parameters[0].Value = F_DeptId;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.T_Sys_Department GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_DeptId,F_DeptCode,F_ParentName,F_ParentId,F_ParentCode,F_DeptName,F_State,F_Layer,F_Type,F_Sort,F_Remark,F_Telephone,F_Mobile,F_OtherPhone,F_Email,F_Header from T_Sys_Department ");
            strSql.Append(" where "+ where);


            ZXDT.CallCenter.Model.T_Sys_Department model = new ZXDT.CallCenter.Model.T_Sys_Department();
            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_Sys_Department DataRowToModel(DataRow row)
        {
            ZXDT.CallCenter.Model.T_Sys_Department model = new ZXDT.CallCenter.Model.T_Sys_Department();
            if (row != null)
            {
                if (row["F_DeptId"] != null && row["F_DeptId"].ToString() != "")
                {
                    model.F_DeptId = int.Parse(row["F_DeptId"].ToString());
                }
                if (row["F_DeptCode"] != null)
                {
                    model.F_DeptCode = row["F_DeptCode"].ToString();
                }
                if (row["F_ParentName"] != null)
                {
                    model.F_ParentName = row["F_ParentName"].ToString();
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_ParentCode"] != null)
                {
                    model.F_ParentCode = row["F_ParentCode"].ToString();
                }
                if (row["F_DeptName"] != null)
                {
                    model.F_DeptName = row["F_DeptName"].ToString();
                }
                if (row["F_State"] != null && row["F_State"].ToString() != "")
                {
                    model.F_State = int.Parse(row["F_State"].ToString());
                }
                if (row["F_Layer"] != null && row["F_Layer"].ToString() != "")
                {
                    model.F_Layer = int.Parse(row["F_Layer"].ToString());
                }
                if (row["F_Type"] != null && row["F_Type"].ToString() != "")
                {
                    model.F_Type = int.Parse(row["F_Type"].ToString());
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_Telephone"] != null)
                {
                    model.F_Telephone = row["F_Telephone"].ToString();
                }
                if (row["F_Mobile"] != null)
                {
                    model.F_Mobile = row["F_Mobile"].ToString();
                }
                if (row["F_OtherPhone"] != null)
                {
                    model.F_OtherPhone = row["F_OtherPhone"].ToString();
                }
                if (row["F_Email"] != null)
                {
                    model.F_Email = row["F_Email"].ToString();
                }
                if (row["F_Header"] != null && row["F_Header"].ToString() != "")
                {
                    model.F_Header = int.Parse(row["F_Header"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_DeptId,F_DeptCode,F_ParentName,F_ParentId,F_ParentCode,F_DeptName,F_State,F_Layer,F_Type,F_Sort,F_Remark,F_Telephone,F_Mobile,F_OtherPhone,F_Email,F_Header ");
            strSql.Append(" FROM T_Sys_Department ");
            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(" F_DeptId,F_DeptCode,F_ParentName,F_ParentId,F_ParentCode,F_DeptName,F_State,F_Layer,F_Type,F_Sort,F_Remark,F_Telephone,F_Mobile,F_OtherPhone,F_Email,F_Header ");
            strSql.Append(" FROM T_Sys_Department ");
            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_Sys_Department ");
            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.F_DeptId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Sys_Department 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  ExtensionMethod

        #endregion  ExtensionMethod


        #region 获取树结构
        /// <summary>
        ///  获得当前节点下的所有子集以tree的格式
        /// </summary>
        public TreeNodeModel GetDepartmentJsonModel(int parentId,bool AddUser=false)
        {
            TreeNodeModel returnModel = new TreeNodeModel();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Sys_Department  order by F_Sort asc");

            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {                
                TreeNodeModel model = new TreeNodeModel();
                if (!false)
                {
                    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_dclose.png";
                    model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_dopen.png";
                    model.open = "true";
                    model.children = modelList;
                }
                 returnModel = GetTreeModel(ds.Tables[0], model, AddUser);
                
            }
            return returnModel;
        }


        /// <summary>
        /// 获取chlid
        /// </summary>
        /// <param name="table">数据结合</param>
        /// <param name="model">当前model</param>
        /// <returns></returns>
        public TreeNodeModel GetTreeModel(DataTable table, TreeNodeModel model, bool AddUser,int level=2)
        {
            DataRow[] rows = table.Select("F_ParentId=" + model.code);
            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["F_DeptId"].ToString());
                    childModel.name = row["F_DeptName"].ToString();
                    childModel.code = int.Parse(row["F_DeptId"].ToString());
                    //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.iconClose = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dclose.png", level);
                    childModel.iconOpen = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dopen.png", level);
                    //childModel.iconClose = row["F_Header"].ToMyString();
                    //childModel.iconOpen = childModel.iconClose + ".png";
                    childModel.open = "true";
                    childModel.children = modelList;
                    model.children.Add(childModel);
                    GetTreeModel(table, childModel, AddUser, level+1);
                }
            }
            else if(AddUser)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select * ");
                strSql.Append(" FROM T_Sys_UserAccount where F_DeptId="+ model.code);

                DataSet ds = DbHelperSQL.Query(strSql.ToString());

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                        TreeNodeModel childModel = new TreeNodeModel();
                        childModel.id = int.Parse(row["F_UserId"].ToString());
                        childModel.name = row["F_UserName"].ToString();
                        childModel.code = int.Parse(row["F_UserId"].ToString());
                        childModel.iconClose = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dclose.png", level);
                        childModel.iconOpen = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dopen.png", level);
                        childModel.open = "false";
                        childModel.IsOrPrivate = 1;
                        childModel.children = modelList;
                        model.children.Add(childModel);
                    }

                }
            }
            return model;

        }


        /// <summary>
        ///  获得当前节点下的所有子集以tree的格式
        /// </summary>
        public TreeNodeModel GetDepartmentJsonModelTurn(int parentId, string key = "")
        {
            TreeNodeModel returnModel = new TreeNodeModel();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM T_Sys_Department ");
            if (!string.IsNullOrEmpty(key))
            {
                strSql.AppendFormat("WHERE (F_DeptName  LIKE '%{0}%' OR F_DeptId IN(SELECT F_DeptId FROM dbo.T_Sys_UserAccount WHERE F_UserName LIKE '%{0}%'))", key);
            }
            strSql.Append(" order by F_Sort asc");

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0&& !string.IsNullOrEmpty(key))
            {

                string strlist = "0";
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    strlist += ",";
                    strlist += row["F_DeptId"];
                    strlist += ",";
                    strlist += row["F_ParentId"];
                }
                strSql.Clear();
                strSql.Append("SELECT * FROM T_Sys_Department ");
                strSql.AppendFormat("WHERE  F_DeptId IN({0})", strlist);

                strSql.Append(" order by F_Sort asc");
                ds = DbHelperSQL.Query(strSql.ToString());
            }

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                TreeNodeModel model = new TreeNodeModel();
                if (!false)
                {
                    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_dclose.png";
                    model.iconOpen =  "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_dopen.png";
                    model.open = "true";
                    model.children = modelList;
                }
                returnModel = GetTreeModelTurnDept(ds.Tables[0], model, key);

            }

            return returnModel;
        }


        /// <summary>
        /// 获取chlid
        /// </summary>
        /// <param name="table">数据结合</param>
        /// <param name="model">当前model</param>
        /// <returns></returns>
        public TreeNodeModel GetTreeModelTurnDept(DataTable table, TreeNodeModel model, string key = "", int level = 2)
        {
            DataRow[] rows = table.Select("F_ParentId=" + model.code);
            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["F_DeptId"].ToString());
                    childModel.name = row["F_DeptName"].ToString();
                    childModel.code = int.Parse(row["F_DeptId"].ToString());
                    childModel.iconClose = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dclose.png", level);
                    childModel.iconOpen = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dopen.png", level);


                    //childModel.iconClose = row["F_Header"].ToMyString();
                    //childModel.iconOpen = childModel.iconClose + ".png";
                    //if (row["F_DeptName"].ToString() == "郑州地铁" || row["F_DeptName"].ToString() == "运营分公司")
                    //{
                       childModel.open = "true";
                    //}
                    //else {
                    //    childModel.open = "false";
                    //}
                    childModel.children = modelList;
                    model.children.Add(childModel);
                    GetTreeModelTurnDept(table, childModel, key,level+1);
                }
            }
            else if (model.name.Contains(key) || string.IsNullOrEmpty(key))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select * ");
                strSql.Append(" FROM T_Sys_UserAccount where F_DeptId=" + model.code);

                DataSet ds = DbHelperSQL.Query(strSql.ToString());

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                        TreeNodeModel childModel = new TreeNodeModel();
                        childModel.id = int.Parse(row["F_UserId"].ToString());
                        childModel.name = row["F_UserName"].ToString();
                        childModel.code = int.Parse(row["F_UserId"].ToString());
                        childModel.iconClose = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dclose.png", level);
                        childModel.iconOpen = string.Format("../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/{0}_dopen.png", level);
                        childModel.open = "false";
                        childModel.IsOrPrivate = 1;
                        childModel.children = modelList;
                        model.children.Add(childModel);
                    }

                }
            }
            else
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select * ");
                strSql.AppendFormat(" FROM T_Sys_UserAccount where F_DeptId={0} AND F_UserName LIKE '%{1}%'", model.code,key);

                DataSet ds = DbHelperSQL.Query(strSql.ToString());

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                        TreeNodeModel childModel = new TreeNodeModel();
                        childModel.id = int.Parse(row["F_UserId"].ToString());
                        childModel.name = row["F_UserName"].ToString();
                        childModel.code = int.Parse(row["F_UserId"].ToString());
                        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);
                    }

                }
            }
            return model;

        }

        #endregion

    }
}
