using RMYY_CallCenter_Api.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 RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Cus_AddressBook
    /// </summary>
    public partial class T_Cus_AddressBook
    {
        public T_Cus_AddressBook()
        { }
        #region  BasicMethod


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Cus_AddressBook model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Cus_AddressBook(");
            strSql.Append("F_Name,F_Sex,F_Birthday,F_IDCard,F_Address,F_Education,F_Email,F_Telephone,F_Mobile,F_ElseMobile,F_CompanyId,F_DepartmentId,F_PositionId,F_Note,F_CreateDate,F_IsDelete,F_Company,F_Department,F_Position)");
            strSql.Append(" values (");
            strSql.Append("@F_Name,@F_Sex,@F_Birthday,@F_IDCard,@F_Address,@F_Education,@F_Email,@F_Telephone,@F_Mobile,@F_ElseMobile,@F_CompanyId,@F_DepartmentId,@F_PositionId,@F_Note,@F_CreateDate,@F_IsDelete,@F_Company,@F_Department,@F_Position)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Name", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Sex", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Birthday", SqlDbType.DateTime),
                    new SqlParameter("@F_IDCard", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Address", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Education", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Email", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Telephone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Mobile", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ElseMobile", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CompanyId", SqlDbType.Int,4),
                    new SqlParameter("@F_DepartmentId", SqlDbType.Int,4),
                    new SqlParameter("@F_PositionId", SqlDbType.Int,4),
                    new SqlParameter("@F_Note", SqlDbType.VarChar,500),
                    new SqlParameter("@F_CreateDate", SqlDbType.DateTime),
                    new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@F_Company", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Department", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Position", SqlDbType.VarChar,100)};
            parameters[0].Value = model.F_Name;
            parameters[1].Value = model.F_Sex;
            parameters[2].Value = model.F_Birthday;
            parameters[3].Value = model.F_IDCard;
            parameters[4].Value = model.F_Address;
            parameters[5].Value = model.F_Education;
            parameters[6].Value = model.F_Email;
            parameters[7].Value = model.F_Telephone;
            parameters[8].Value = model.F_Mobile;
            parameters[9].Value = model.F_ElseMobile;
            parameters[10].Value = model.F_CompanyId;
            parameters[11].Value = model.F_DepartmentId;
            parameters[12].Value = model.F_PositionId;
            parameters[13].Value = model.F_Note;
            parameters[14].Value = model.F_CreateDate;
            parameters[15].Value = model.F_IsDelete;
            parameters[16].Value = model.F_Company;
            parameters[17].Value = model.F_Department;
            parameters[18].Value = model.F_Position;

            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_Cus_AddressBook model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Cus_AddressBook set ");
            strSql.Append("F_Name=@F_Name,");
            strSql.Append("F_Sex=@F_Sex,");
            strSql.Append("F_Birthday=@F_Birthday,");
            strSql.Append("F_IDCard=@F_IDCard,");
            strSql.Append("F_Address=@F_Address,");
            strSql.Append("F_Education=@F_Education,");
            strSql.Append("F_Email=@F_Email,");
            strSql.Append("F_Telephone=@F_Telephone,");
            strSql.Append("F_Mobile=@F_Mobile,");
            strSql.Append("F_ElseMobile=@F_ElseMobile,");
            strSql.Append("F_CompanyId=@F_CompanyId,");
            strSql.Append("F_DepartmentId=@F_DepartmentId,");
            strSql.Append("F_PositionId=@F_PositionId,");
            strSql.Append("F_Note=@F_Note,");
            strSql.Append("F_CreateDate=@F_CreateDate,");
            strSql.Append("F_IsDelete=@F_IsDelete,");
            strSql.Append("F_Company=@F_Company,");
            strSql.Append("F_Department=@F_Department,");
            strSql.Append("F_Position=@F_Position");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Name", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Sex", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Birthday", SqlDbType.DateTime),
                    new SqlParameter("@F_IDCard", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Address", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Education", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Email", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Telephone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Mobile", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ElseMobile", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CompanyId", SqlDbType.Int,4),
                    new SqlParameter("@F_DepartmentId", SqlDbType.Int,4),
                    new SqlParameter("@F_PositionId", SqlDbType.Int,4),
                    new SqlParameter("@F_Note", SqlDbType.VarChar,500),
                    new SqlParameter("@F_CreateDate", SqlDbType.DateTime),
                    new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@F_Company", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Department", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Position", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.F_Name;
            parameters[1].Value = model.F_Sex;
            parameters[2].Value = model.F_Birthday;
            parameters[3].Value = model.F_IDCard;
            parameters[4].Value = model.F_Address;
            parameters[5].Value = model.F_Education;
            parameters[6].Value = model.F_Email;
            parameters[7].Value = model.F_Telephone;
            parameters[8].Value = model.F_Mobile;
            parameters[9].Value = model.F_ElseMobile;
            parameters[10].Value = model.F_CompanyId;
            parameters[11].Value = model.F_DepartmentId;
            parameters[12].Value = model.F_PositionId;
            parameters[13].Value = model.F_Note;
            parameters[14].Value = model.F_CreateDate;
            parameters[15].Value = model.F_IsDelete;
            parameters[16].Value = model.F_Company;
            parameters[17].Value = model.F_Department;
            parameters[18].Value = model.F_Position;
            parameters[19].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_Cus_AddressBook ");
            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_Cus_AddressBook ");
            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 RMYY_CallCenter_Api.Model.T_Cus_AddressBook GetModel(int F_Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_Id,F_Name,F_Sex,F_Birthday,F_IDCard,F_Address,F_Education,F_Email,F_Telephone,F_Mobile,F_ElseMobile,F_CompanyId,F_DepartmentId,F_PositionId,F_Note,F_CreateDate,F_IsDelete,F_Company,F_Department,F_Position from T_Cus_AddressBook ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

            RMYY_CallCenter_Api.Model.T_Cus_AddressBook model = new RMYY_CallCenter_Api.Model.T_Cus_AddressBook();
            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_Cus_AddressBook DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Cus_AddressBook model = new RMYY_CallCenter_Api.Model.T_Cus_AddressBook();
            if (row != null)
            {
                if (row["F_Id"] != null && row["F_Id"].ToString() != "")
                {
                    model.F_Id = int.Parse(row["F_Id"].ToString());
                }
                if (row["F_Name"] != null)
                {
                    model.F_Name = row["F_Name"].ToString();
                }
                if (row["F_Sex"] != null)
                {
                    model.F_Sex = row["F_Sex"].ToString();
                }
                if (row["F_Birthday"] != null && row["F_Birthday"].ToString() != "")
                {
                    model.F_Birthday = DateTime.Parse(row["F_Birthday"].ToString());
                }
                if (row["F_IDCard"] != null)
                {
                    model.F_IDCard = row["F_IDCard"].ToString();
                }
                if (row["F_Address"] != null)
                {
                    model.F_Address = row["F_Address"].ToString();
                }
                if (row["F_Education"] != null)
                {
                    model.F_Education = row["F_Education"].ToString();
                }
                if (row["F_Email"] != null)
                {
                    model.F_Email = row["F_Email"].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_ElseMobile"] != null)
                {
                    model.F_ElseMobile = row["F_ElseMobile"].ToString();
                }
                if (row["F_CompanyId"] != null && row["F_CompanyId"].ToString() != "")
                {
                    model.F_CompanyId = int.Parse(row["F_CompanyId"].ToString());
                }
                if (row["F_DepartmentId"] != null && row["F_DepartmentId"].ToString() != "")
                {
                    model.F_DepartmentId = int.Parse(row["F_DepartmentId"].ToString());
                }
                if (row["F_PositionId"] != null && row["F_PositionId"].ToString() != "")
                {
                    model.F_PositionId = int.Parse(row["F_PositionId"].ToString());
                }
                if (row["F_Note"] != null)
                {
                    model.F_Note = row["F_Note"].ToString();
                }
                if (row["F_CreateDate"] != null && row["F_CreateDate"].ToString() != "")
                {
                    model.F_CreateDate = DateTime.Parse(row["F_CreateDate"].ToString());
                }
                if (row["F_IsDelete"] != null && row["F_IsDelete"].ToString() != "")
                {
                    model.F_IsDelete = int.Parse(row["F_IsDelete"].ToString());
                }
                if (row["F_Company"] != null)
                {
                    model.F_Company = row["F_Company"].ToString();
                }
                if (row["F_Department"] != null)
                {
                    model.F_Department = row["F_Department"].ToString();
                }
                if (row["F_Position"] != null)
                {
                    model.F_Position = row["F_Position"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_Id,F_Name,F_Sex,F_Birthday,F_IDCard,F_Address,F_Education,F_Email,F_Telephone,F_Mobile,F_ElseMobile,F_CompanyId,F_DepartmentId,F_PositionId,F_Note,F_CreateDate,F_IsDelete,F_Company,F_Department,F_Position ");
            strSql.Append(" FROM T_Cus_AddressBook ");
            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_Id,F_Name,F_Sex,F_Birthday,F_IDCard,F_Address,F_Education,F_Email,F_Telephone,F_Mobile,F_ElseMobile,F_CompanyId,F_DepartmentId,F_PositionId,F_Note,F_CreateDate,F_IsDelete,F_Company,F_Department,F_Position ");
            strSql.Append(" FROM T_Cus_AddressBook ");
            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_Cus_AddressBook ");
            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_Cus_AddressBook 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
    }
}
