using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RMYY_CallCenter_Api.DB;

namespace RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Sys_Department
    /// </summary>
    public partial class T_Sys_Department
    {
        public T_Sys_Department()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string F_DeptCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Sys_Department with(nolock)");
            strSql.Append(" where F_DeptCode=@F_DeptCode");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50)
            };
            parameters[0].Value = F_DeptCode;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Sys_Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_Department(");
            strSql.Append("F_ParentId,F_DeptCode,F_DeptName,F_Sort,F_Remark,F_State,F_CreateUser,F_CreateTime,T_Woid,  F_Location,F_FloorDistribution,F_OfficeTelephone,F_TransferType)");
            strSql.Append(" values (");
            strSql.Append("@F_ParentId,@F_DeptCode,@F_DeptName,@F_Sort,@F_Remark,@F_State,@F_CreateUser,@F_CreateTime,@T_Woid,  @F_Location,@F_FloorDistribution,@F_OfficeTelephone,@F_TransferType)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeptName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_State", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@T_Woid", SqlDbType.Int,4),
                    new SqlParameter("@F_Location", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_FloorDistribution", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_OfficeTelephone", SqlDbType.NVarChar,50),
                    
                     new SqlParameter("@F_TransferType", SqlDbType.Int,4)

            };
            parameters[0].Value = model.F_ParentId;
            parameters[1].Value = model.F_DeptCode;
            parameters[2].Value = model.F_DeptName;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_Remark;
            parameters[5].Value = model.F_State;
            parameters[6].Value = model.F_CreateUser;
            parameters[7].Value = model.F_CreateTime;
            parameters[8].Value = model.T_Woid;

            parameters[9].Value = model.F_Location;
            parameters[10].Value = model.F_FloorDistribution;
            parameters[11].Value = model.F_OfficeTelephone;
            parameters[12].Value = model.F_TransferType;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RMYY_CallCenter_Api.Model.T_Sys_Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_Department set ");
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_DeptCode=@F_DeptCode,");
            strSql.Append("F_DeptName=@F_DeptName,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_State=@F_State,");
            strSql.Append("F_CreateUser=@F_CreateUser,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("T_Woid=@T_Woid,");
            strSql.Append("F_Location=@F_Location,");
            strSql.Append("F_FloorDistribution=@F_FloorDistribution,");
            strSql.Append("F_OfficeTelephone=@F_OfficeTelephone,");
            strSql.Append("F_TransferType=@F_TransferType ");
            


            strSql.Append(" where F_DeptId=@F_DeptId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeptName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_State", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@T_Woid", SqlDbType.Int,4),
                       new SqlParameter("@F_Location", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_FloorDistribution", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_OfficeTelephone", SqlDbType.NVarChar,50),
         new SqlParameter("@F_TransferType", SqlDbType.Int,4),
                    
                    new SqlParameter("@F_DeptId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ParentId;
            parameters[1].Value = model.F_DeptCode;
            parameters[2].Value = model.F_DeptName;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_Remark;
            parameters[5].Value = model.F_State;
            parameters[6].Value = model.F_CreateUser;
            parameters[7].Value = model.F_CreateTime;
            parameters[8].Value = model.T_Woid;

            parameters[9].Value = model.F_Location;
            parameters[10].Value = model.F_FloorDistribution;
            parameters[11].Value = model.F_OfficeTelephone;
            
                 parameters[12].Value = model.F_TransferType;
            parameters[13].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 RMYY_CallCenter_Api.Model.T_Sys_Department GetModel(int F_DeptId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Sys_Department with(nolock) ");
            strSql.Append(" where F_DeptId=@F_DeptId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeptId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_DeptId;

            RMYY_CallCenter_Api.Model.T_Sys_Department model = new RMYY_CallCenter_Api.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 RMYY_CallCenter_Api.Model.T_Sys_Department GetModel(string F_DeptCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Sys_Department with(nolock)");
            strSql.Append(" where F_DeptCode=@F_DeptCode");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeptCode", SqlDbType.VarChar,50)
            };
            parameters[0].Value = F_DeptCode;

            RMYY_CallCenter_Api.Model.T_Sys_Department model = new RMYY_CallCenter_Api.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 RMYY_CallCenter_Api.Model.T_Sys_Department DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Sys_Department model = new RMYY_CallCenter_Api.Model.T_Sys_Department();
            if (row != null)
            {
                
 if (row["F_TransferType"] != null && row["F_TransferType"].ToString() != "")
                {
                    model.F_TransferType = int.Parse(row["F_TransferType"].ToString());
                }
                if (row["F_DeptId"] != null && row["F_DeptId"].ToString() != "")
                {
                    model.F_DeptId = int.Parse(row["F_DeptId"].ToString());
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_DeptCode"] != null)
                {
                    model.F_DeptCode = row["F_DeptCode"].ToString();
                }
                if (row["F_DeptName"] != null)
                {
                    model.F_DeptName = row["F_DeptName"].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_State"] != null && row["F_State"].ToString() != "")
                {
                    model.F_State = int.Parse(row["F_State"].ToString());
                }
                if (row["F_CreateUser"] != null)
                {
                    model.F_CreateUser = row["F_CreateUser"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["T_Woid"] != null && row["T_Woid"].ToString() != "")
                {
                    model.T_Woid = int.Parse(row["T_Woid"].ToString());
                }
                if (row["F_Location"] != null)
                {
                    model.F_Location = row["F_Location"].ToString();
                }
                if (row["F_FloorDistribution"] != null)
                {
                    model.F_FloorDistribution = row["F_FloorDistribution"].ToString();
                }
                if (row["F_OfficeTelephone"] != null)
                {
                    model.F_OfficeTelephone = row["F_OfficeTelephone"].ToString();
                }
            }
            return model;
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            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(" * ");
            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 with(nolock)");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        #endregion  BasicMethod
    }
}

