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_Call_ZBDH
	/// </summary>
	public partial class T_Call_ZBDH
    {
        public T_Call_ZBDH()
        { }
        #region  BasicMethod

        /// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.T_Call_ZBDH model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_ZBDH(");
            strSql.Append("F_ZBDH,F_GroupName,F_Groupid,F_Remark,F_CallinTime,F_CreateBy,F_CreateTime,F_CreateID)");
            strSql.Append(" values (");
            strSql.Append("@F_ZBDH,@F_GroupName,@F_Groupid,@F_Remark,@F_CallinTime,@F_CreateBy,@F_CreateTime,@F_CreateID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ZBDH", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_GroupName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Groupid", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CallinTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateBy", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ZBDH;
            parameters[1].Value = model.F_GroupName;
            parameters[2].Value = model.F_Groupid;
            parameters[3].Value = model.F_Remark;
            parameters[4].Value = model.F_CallinTime;
            parameters[5].Value = model.F_CreateBy;
            parameters[6].Value = model.F_CreateTime;
            parameters[7].Value = model.F_CreateID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Call_ZBDH model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_ZBDH set ");
            strSql.Append("F_ZBDH=@F_ZBDH,");
            strSql.Append("F_GroupName=@F_GroupName,");
            strSql.Append("F_Groupid=@F_Groupid,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_CallinTime=@F_CallinTime,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_CreateID=@F_CreateID");
            strSql.Append(" where F_ZBID=@F_ZBID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ZBDH", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_GroupName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Groupid", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CallinTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateBy", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateID", SqlDbType.Int,4),
                    new SqlParameter("@F_ZBID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ZBDH;
            parameters[1].Value = model.F_GroupName;
            parameters[2].Value = model.F_Groupid;
            parameters[3].Value = model.F_Remark;
            parameters[4].Value = model.F_CallinTime;
            parameters[5].Value = model.F_CreateBy;
            parameters[6].Value = model.F_CreateTime;
            parameters[7].Value = model.F_CreateID;
            parameters[8].Value = model.F_ZBID;

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

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ZBID,F_ZBDH,F_GroupName,F_Groupid,F_Remark,F_CallinTime,F_CreateBy,F_CreateTime,F_CreateID from T_Call_ZBDH ");
            strSql.Append(" where F_ZBID=@F_ZBID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ZBID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ZBID;

            Model.T_Call_ZBDH model = new Model.T_Call_ZBDH();
            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_Call_ZBDH DataRowToModel(DataRow row)
        {
            Model.T_Call_ZBDH model = new Model.T_Call_ZBDH();
            if (row != null)
            {
                if (row["F_ZBID"] != null && row["F_ZBID"].ToString() != "")
                {
                    model.F_ZBID = int.Parse(row["F_ZBID"].ToString());
                }
                if (row["F_ZBDH"] != null)
                {
                    model.F_ZBDH = row["F_ZBDH"].ToString();
                }
                if (row["F_GroupName"] != null)
                {
                    model.F_GroupName = row["F_GroupName"].ToString();
                }
                if (row["F_Groupid"] != null && row["F_Groupid"].ToString() != "")
                {
                    model.F_Groupid = int.Parse(row["F_Groupid"].ToString());
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_CallinTime"] != null && row["F_CallinTime"].ToString() != "")
                {
                    model.F_CallinTime = DateTime.Parse(row["F_CallinTime"].ToString());
                }
                if (row["F_CreateBy"] != null)
                {
                    model.F_CreateBy = row["F_CreateBy"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_CreateID"] != null && row["F_CreateID"].ToString() != "")
                {
                    model.F_CreateID = int.Parse(row["F_CreateID"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_ZBID,F_ZBDH,F_GroupName,F_Groupid,F_Remark,F_CallinTime,F_CreateBy,F_CreateTime,F_CreateID ");
            strSql.Append(" FROM T_Call_ZBDH ");
            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_ZBID,F_ZBDH,F_GroupName,F_Groupid,F_Remark,F_CallinTime,F_CreateBy,F_CreateTime,F_CreateID ");
            strSql.Append(" FROM T_Call_ZBDH ");
            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_Call_ZBDH ");
            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_ZBID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Call_ZBDH 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
    }
}
