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_GongDan
    /// </summary>
    public partial class T_Sys_GongDan
    {
        public T_Sys_GongDan()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_GDId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Sys_GongDan with(nolock)");
            strSql.Append(" where F_GDId=@F_GDId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_GDId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_GDId;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Sys_GongDan model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_GongDan(");
            strSql.Append("F_ParentID,F_Name,F_Remark,F_Sort,F_CreateTime,F_TypeId,F_ImgUrl,F_TimeOut,F_RepairTimeLimit,F_Identification,F_AutoDept,F_CanReplayCount,F_CanUrgeCount)");
            strSql.Append(" values (");
            strSql.Append("@F_ParentID,@F_Name,@F_Remark,@F_Sort,@F_CreateTime,@F_TypeId,@F_ImgUrl,@F_TimeOut,@F_RepairTimeLimit,@F_Identification,@F_AutoDept,@F_CanReplayCount,@F_CanUrgeCount)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ParentID", SqlDbType.Int,4),
                    new SqlParameter("@F_Name", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,225),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_TypeId", SqlDbType.Int,4),
                    new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
                     new SqlParameter("@F_TimeOut", SqlDbType.Float,8),
                        new SqlParameter("@F_RepairTimeLimit", SqlDbType.Float,8),
                          new SqlParameter("@F_Identification", SqlDbType.VarChar,50),
new SqlParameter("@F_AutoDept", SqlDbType.Int,4),
new SqlParameter("@F_CanReplayCount", SqlDbType.Int,4),
new SqlParameter("@F_CanUrgeCount", SqlDbType.Int,4),




            };
            parameters[0].Value = model.F_ParentID;
            parameters[1].Value = model.F_Name;
            parameters[2].Value = model.F_Remark;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_CreateTime;
            parameters[5].Value = model.F_TypeId;
            parameters[6].Value = model.F_ImgUrl;
            parameters[7].Value = model.F_TimeOut;
            parameters[8].Value = model.F_RepairTimeLimit;
            parameters[9].Value = model.F_Identification;

            parameters[10].Value = model.F_AutoDept;
            parameters[11].Value = model.F_CanReplayCount;
            parameters[12].Value = model.F_CanUrgeCount;
            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_GongDan model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_GongDan set ");
            strSql.Append("F_ParentID=@F_ParentID,");
            strSql.Append("F_Name=@F_Name,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_TypeId=@F_TypeId,");
            
                  strSql.Append("F_TimeOut=@F_TimeOut,");
            strSql.Append("F_RepairTimeLimit=@F_RepairTimeLimit,");
            strSql.Append("F_Identification=@F_Identification,");
            strSql.Append("F_ImgUrl=@F_ImgUrl,");
            strSql.Append("F_AutoDept=@F_AutoDept,");
            strSql.Append("F_CanReplayCount=@F_CanReplayCount,");


            strSql.Append("F_IsDelete=@F_IsDelete,");
            strSql.Append("F_CanUrgeCount=@F_CanUrgeCount");
            




            strSql.Append(" where F_GDId=@F_GDId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ParentID", SqlDbType.Int,4),
                    new SqlParameter("@F_Name", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,225),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_TypeId", SqlDbType.Int,4),
                      new SqlParameter("@F_TimeOut", SqlDbType.Float,8),
                        new SqlParameter("@F_RepairTimeLimit", SqlDbType.Float,8),
                       new SqlParameter("@F_Identification", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
new SqlParameter("@F_AutoDept", SqlDbType.Int,4),
new SqlParameter("@F_CanReplayCount", SqlDbType.Int,4),
new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
new SqlParameter("@F_CanUrgeCount", SqlDbType.Int,4),

                    new SqlParameter("@F_GDId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ParentID;
            parameters[1].Value = model.F_Name;
            parameters[2].Value = model.F_Remark;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_CreateTime;
            parameters[5].Value = model.F_TypeId;
            parameters[6].Value = model.F_TimeOut;
            parameters[7].Value = model.F_RepairTimeLimit;
            parameters[8].Value = model.F_Identification;
            parameters[9].Value = model.F_ImgUrl;
            parameters[10].Value = model.F_AutoDept;
            parameters[11].Value = model.F_CanReplayCount;
            parameters[12].Value = model.F_IsDelete;
            parameters[13].Value = model.F_CanUrgeCount;            
            parameters[14].Value = model.F_GDId;

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

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_GongDan set F_IsDelete=1");
            strSql.Append(" where F_GDId=@F_GDId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_GDId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_GDId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_GDIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  T_Sys_GongDan set  F_IsDelete=1");
            strSql.Append(" where F_GDId in (" + F_GDIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Sys_GongDan GetModel(int F_GDId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_GDId,F_ParentID,F_Identification,F_Name,F_Remark,F_Sort,F_CreateTime,F_TypeId,F_ImgUrl ,F_TimeOut,F_RepairTimeLimit,F_AutoDept,F_CanReplayCount,F_CanUrgeCount from T_Sys_GongDan with(nolock)");
            strSql.Append(" where F_GDId=@F_GDId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_GDId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_GDId;

            RMYY_CallCenter_Api.Model.T_Sys_GongDan model = new RMYY_CallCenter_Api.Model.T_Sys_GongDan();
            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>
        /// <param name="F_GDId"></param>
        /// <returns></returns>
        public RMYY_CallCenter_Api.Model.T_Sys_GongDan GetModelbyname(string F_Name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_GDId,F_ParentID,F_Identification,F_Name,F_Remark,F_Sort,F_CreateTime,F_TypeId,F_ImgUrl,F_TimeOut,F_RepairTimeLimit,F_AutoDept,F_CanReplayCount,F_CanUrgeCount  from T_Sys_GongDan with(nolock)");
            strSql.Append(" where F_Name=@F_Name");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Name", SqlDbType.VarChar,100)
            };
            parameters[0].Value = F_Name;

            RMYY_CallCenter_Api.Model.T_Sys_GongDan model = new RMYY_CallCenter_Api.Model.T_Sys_GongDan();
            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_GongDan DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Sys_GongDan model = new RMYY_CallCenter_Api.Model.T_Sys_GongDan();
            if (row != null)
            {
                if (row["F_GDId"] != null && row["F_GDId"].ToString() != "")
                {
                    model.F_GDId = int.Parse(row["F_GDId"].ToString());
                }
                if (row["F_ParentID"] != null && row["F_ParentID"].ToString() != "")
                {
                    model.F_ParentID = int.Parse(row["F_ParentID"].ToString());
                }
                if (row["F_Name"] != null)
                {
                    model.F_Name = row["F_Name"].ToString();
                }
                if (row["F_Identification"] != null)
                {
                    model.F_Identification = row["F_Identification"].ToString();
                }
                
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                

                     if (row["F_CanReplayCount"] != null && row["F_CanReplayCount"].ToString() != "")
                {
                    model.F_CanReplayCount = int.Parse(row["F_CanReplayCount"].ToString());
                }
                if (row["F_CanUrgeCount"] != null && row["F_CanUrgeCount"].ToString() != "")
                {
                    model.F_CanUrgeCount = int.Parse(row["F_CanUrgeCount"].ToString());
                }
                
                if (row["F_TimeOut"] != null && row["F_TimeOut"].ToString() != "")
                {
                    model.F_TimeOut = float.Parse(row["F_TimeOut"].ToString());
                }
                if (row["F_RepairTimeLimit"] != null && row["F_RepairTimeLimit"].ToString() != "")
                {
                    model.F_RepairTimeLimit = float.Parse(row["F_RepairTimeLimit"].ToString());
                }
                

                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_TypeId"] != null && row["F_TypeId"].ToString() != "")
                {
                    model.F_TypeId = int.Parse(row["F_TypeId"].ToString());
                }
                if (row["F_AutoDept"] != null && row["F_AutoDept"].ToString() != "")
                {
                    model.F_AutoDept = int.Parse(row["F_AutoDept"].ToString());
                }
               
                if (row["F_ImgUrl"] != null)
                {
                    model.F_ImgUrl = row["F_ImgUrl"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_GDId,F_ParentID,F_Name,F_Identification,F_Remark,F_Sort,F_CreateTime,F_TypeId,F_ImgUrl,F_TimeOut,F_RepairTimeLimit,F_AutoDept,F_CanReplayCount,F_CanUrgeCount ");
            strSql.Append(" FROM T_Sys_GongDan with(nolock)");
            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_GDId,F_ParentID,F_Name,F_Remark,F_Identification,F_Sort,F_CreateTime,F_TypeId,F_ImgUrl ,F_TimeOut,F_RepairTimeLimit,F_AutoDept,F_CanReplayCount,F_CanUrgeCount");
            strSql.Append(" FROM T_Sys_GongDan with(nolock)");
            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_GongDan 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);
            }
        }
        /// <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_GDId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Sys_GongDan 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());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_Sys_GongDan";
			parameters[1].Value = "F_GDId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

