using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenterApi.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(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(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("update T_Sys_Department set F_State=0 ");
            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 Model.T_Sys_Department GetModel(string DeptName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TOP 1 F_DeptId,F_DeptCode,(SELECT B.F_DeptName FROM T_Sys_Department B WHERE B.F_DeptId=A.F_ParentId) AS 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  A");
            strSql.Append(" where F_DeptName=@F_DeptName");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeptName", SqlDbType.VarChar,50)         };
            parameters[0].Value = DeptName;

            Model.T_Sys_Department model = new 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 Model.T_Sys_Department GetModel(int F_DeptId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TOP 1 F_DeptId,F_DeptCode,(SELECT B.F_DeptName FROM T_Sys_Department B WHERE B.F_DeptId=A.F_ParentId) AS 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  A");
            strSql.Append(" where F_DeptId=@F_DeptId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeptId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_DeptId;

            Model.T_Sys_Department model = new 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 Model.T_Sys_Department DataRowToModel(DataRow row)
        {
            Model.T_Sys_Department model = new 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);
            }
            strSql.Append("  ORDER BY F_Sort ASC ");
            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
        /// <summary>
        /// 更新部门编号
        /// </summary>
        public bool UpdateDeptCode(string deptcode,int deptid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_Department set ");
            strSql.Append("F_DeptCode=@F_DeptCode");
            strSql.Append(" where F_DeptId=@F_DeptId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeptId", SqlDbType.Int,4)};
            parameters[0].Value = deptcode;
            parameters[1].Value = deptid;

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