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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.T_Call_OutTelNumWX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_OutTelNumWX(");
            strSql.Append("F_PagerId,F_CusID,F_CusName,F_TelPhone,F_Phone,F_CountryId,F_Country,F_Address,F_Openid,F_UserId,F_UserName,F_CreateTime,F_Count,F_AskInfo,F_AskRes,F_DeleteFlag)");
            strSql.Append(" values (");
            strSql.Append("@F_PagerId,@F_CusID,@F_CusName,@F_TelPhone,@F_Phone,@F_CountryId,@F_Country,@F_Address,@F_Openid,@F_UserId,@F_UserName,@F_CreateTime,@F_Count,@F_AskInfo,@F_AskRes,@F_DeleteFlag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_PagerId", SqlDbType.Int,4),
                    new SqlParameter("@F_CusID", SqlDbType.Int,4),
                    new SqlParameter("@F_CusName", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_TelPhone", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Phone", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_CountryId", SqlDbType.Int,4),
                    new SqlParameter("@F_Country", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Address", SqlDbType.NVarChar,2000),
                    new SqlParameter("@F_Openid", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_UserId", SqlDbType.Int,4),
                    new SqlParameter("@F_UserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_Count", SqlDbType.Int,4),
                    new SqlParameter("@F_AskInfo", SqlDbType.Text),
                    new SqlParameter("@F_AskRes", SqlDbType.Text),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4)};
            parameters[0].Value = model.F_PagerId;
            parameters[1].Value = model.F_CusID;
            parameters[2].Value = model.F_CusName;
            parameters[3].Value = model.F_TelPhone;
            parameters[4].Value = model.F_Phone;
            parameters[5].Value = model.F_CountryId;
            parameters[6].Value = model.F_Country;
            parameters[7].Value = model.F_Address;
            parameters[8].Value = model.F_Openid;
            parameters[9].Value = model.F_UserId;
            parameters[10].Value = model.F_UserName;
            parameters[11].Value = model.F_CreateTime;
            parameters[12].Value = model.F_Count;
            parameters[13].Value = model.F_AskInfo;
            parameters[14].Value = model.F_AskRes;
            parameters[15].Value = model.F_DeleteFlag;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_Call_OutTelNumWX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_OutTelNumWX set ");
            strSql.Append("F_PagerId=@F_PagerId,");
            strSql.Append("F_CusID=@F_CusID,");
            strSql.Append("F_CusName=@F_CusName,");
            strSql.Append("F_TelPhone=@F_TelPhone,");
            strSql.Append("F_Phone=@F_Phone,");
            strSql.Append("F_CountryId=@F_CountryId,");
            strSql.Append("F_Country=@F_Country,");
            strSql.Append("F_Address=@F_Address,");
            strSql.Append("F_Openid=@F_Openid,");
            strSql.Append("F_UserId=@F_UserId,");
            strSql.Append("F_UserName=@F_UserName,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_Count=@F_Count,");
            strSql.Append("F_AskInfo=@F_AskInfo,");
            strSql.Append("F_AskRes=@F_AskRes,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_PagerId", SqlDbType.Int,4),
                    new SqlParameter("@F_CusID", SqlDbType.Int,4),
                    new SqlParameter("@F_CusName", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_TelPhone", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Phone", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_CountryId", SqlDbType.Int,4),
                    new SqlParameter("@F_Country", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Address", SqlDbType.NVarChar,2000),
                    new SqlParameter("@F_Openid", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_UserId", SqlDbType.Int,4),
                    new SqlParameter("@F_UserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_Count", SqlDbType.Int,4),
                    new SqlParameter("@F_AskInfo", SqlDbType.Text),
                    new SqlParameter("@F_AskRes", SqlDbType.Text),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.F_PagerId;
            parameters[1].Value = model.F_CusID;
            parameters[2].Value = model.F_CusName;
            parameters[3].Value = model.F_TelPhone;
            parameters[4].Value = model.F_Phone;
            parameters[5].Value = model.F_CountryId;
            parameters[6].Value = model.F_Country;
            parameters[7].Value = model.F_Address;
            parameters[8].Value = model.F_Openid;
            parameters[9].Value = model.F_UserId;
            parameters[10].Value = model.F_UserName;
            parameters[11].Value = model.F_CreateTime;
            parameters[12].Value = model.F_Count;
            parameters[13].Value = model.F_AskInfo;
            parameters[14].Value = model.F_AskRes;
            parameters[15].Value = model.F_DeleteFlag;
            parameters[16].Value = model.F_Id;

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

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

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

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


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

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

            CallCenterApi.Model.T_Call_OutTelNumWX model = new CallCenterApi.Model.T_Call_OutTelNumWX();
            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 CallCenterApi.Model.T_Call_OutTelNumWX DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Call_OutTelNumWX model = new CallCenterApi.Model.T_Call_OutTelNumWX();
            if (row != null)
            {
                if (row["F_Id"] != null && row["F_Id"].ToString() != "")
                {
                    model.F_Id = int.Parse(row["F_Id"].ToString());
                }
                if (row["F_PagerId"] != null && row["F_PagerId"].ToString() != "")
                {
                    model.F_PagerId = int.Parse(row["F_PagerId"].ToString());
                }
                if (row["F_CusID"] != null && row["F_CusID"].ToString() != "")
                {
                    model.F_CusID = int.Parse(row["F_CusID"].ToString());
                }
                if (row["F_CusName"] != null)
                {
                    model.F_CusName = row["F_CusName"].ToString();
                }
                if (row["F_TelPhone"] != null)
                {
                    model.F_TelPhone = row["F_TelPhone"].ToString();
                }
                if (row["F_Phone"] != null)
                {
                    model.F_Phone = row["F_Phone"].ToString();
                }
                if (row["F_CountryId"] != null && row["F_CountryId"].ToString() != "")
                {
                    model.F_CountryId = int.Parse(row["F_CountryId"].ToString());
                }
                if (row["F_Country"] != null)
                {
                    model.F_Country = row["F_Country"].ToString();
                }
                if (row["F_Address"] != null)
                {
                    model.F_Address = row["F_Address"].ToString();
                }
                if (row["F_Openid"] != null)
                {
                    model.F_Openid = row["F_Openid"].ToString();
                }
                if (row["F_UserId"] != null && row["F_UserId"].ToString() != "")
                {
                    model.F_UserId = int.Parse(row["F_UserId"].ToString());
                }
                if (row["F_UserName"] != null)
                {
                    model.F_UserName = row["F_UserName"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_Count"] != null && row["F_Count"].ToString() != "")
                {
                    model.F_Count = int.Parse(row["F_Count"].ToString());
                }
                if (row["F_AskInfo"] != null)
                {
                    model.F_AskInfo = row["F_AskInfo"].ToString();
                }
                if (row["F_AskRes"] != null)
                {
                    model.F_AskRes = row["F_AskRes"].ToString();
                }
                if (row["F_DeleteFlag"] != null && row["F_DeleteFlag"].ToString() != "")
                {
                    model.F_DeleteFlag = int.Parse(row["F_DeleteFlag"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Call_OutTelNumWX ");
            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_Call_OutTelNumWX ");
            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_OutTelNumWX ");
            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_Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_Call_OutTelNumWX 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 CallCenterApi.Model.T_Call_OutTelNumWX GetModelByTel(string tel)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Call_OutTelNumWX ");
            strSql.Append(" where F_TelPhone=@F_TelPhone order by F_CreateTime desc");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TelPhone", SqlDbType.NVarChar,200)
            };
            parameters[0].Value = tel;

            CallCenterApi.Model.T_Call_OutTelNumWX model = new CallCenterApi.Model.T_Call_OutTelNumWX();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        #endregion  ExtensionMethod
    }
}
