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

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


        /// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CallCenterApi.Model.T_Cus_ContactPersonNew model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Cus_ContactPersonNew(");
            strSql.Append("F_CustomerId,F_IsMain,F_Name,F_Telephone,F_Email,F_Duties,F_QQ,F_MSN,F_Remark,F_CreateBy,F_CreateOn,F_DeleteFlag)");
            strSql.Append(" values (");
            strSql.Append("@F_CustomerId,@F_IsMain,@F_Name,@F_Telephone,@F_Email,@F_Duties,@F_QQ,@F_MSN,@F_Remark,@F_CreateBy,@F_CreateOn,@F_DeleteFlag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CustomerId", SqlDbType.Int,4),
                    new SqlParameter("@F_IsMain", SqlDbType.Bit,1),
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Telephone", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_Email", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Duties", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_QQ", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_MSN", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CreateBy", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2)};
            parameters[0].Value = model.F_CustomerId;
            parameters[1].Value = model.F_IsMain;
            parameters[2].Value = model.F_Name;
            parameters[3].Value = model.F_Telephone;
            parameters[4].Value = model.F_Email;
            parameters[5].Value = model.F_Duties;
            parameters[6].Value = model.F_QQ;
            parameters[7].Value = model.F_MSN;
            parameters[8].Value = model.F_Remark;
            parameters[9].Value = model.F_CreateBy;
            parameters[10].Value = model.F_CreateOn;
            parameters[11].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_Cus_ContactPersonNew model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Cus_ContactPersonNew set ");
            strSql.Append("F_CustomerId=@F_CustomerId,");
            strSql.Append("F_IsMain=@F_IsMain,");
            strSql.Append("F_Name=@F_Name,");
            strSql.Append("F_Telephone=@F_Telephone,");
            strSql.Append("F_Email=@F_Email,");
            strSql.Append("F_Duties=@F_Duties,");
            strSql.Append("F_QQ=@F_QQ,");
            strSql.Append("F_MSN=@F_MSN,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag");
            strSql.Append(" where F_ManId=@F_ManId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CustomerId", SqlDbType.Int,4),
                    new SqlParameter("@F_IsMain", SqlDbType.Bit,1),
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Telephone", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_Email", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Duties", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_QQ", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_MSN", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CreateBy", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_ManId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_CustomerId;
            parameters[1].Value = model.F_IsMain;
            parameters[2].Value = model.F_Name;
            parameters[3].Value = model.F_Telephone;
            parameters[4].Value = model.F_Email;
            parameters[5].Value = model.F_Duties;
            parameters[6].Value = model.F_QQ;
            parameters[7].Value = model.F_MSN;
            parameters[8].Value = model.F_Remark;
            parameters[9].Value = model.F_CreateBy;
            parameters[10].Value = model.F_CreateOn;
            parameters[11].Value = model.F_DeleteFlag;
            parameters[12].Value = model.F_ManId;

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

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

            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Cus_ContactPersonNew ");
            strSql.Append("update T_Cus_ContactPersonNew set F_DeleteFlag=1 ");
            strSql.Append(" where F_ManId=@F_ManId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ManId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ManId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_ManIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Cus_ContactPersonNew ");
            strSql.Append("update T_Cus_ContactPersonNew set F_DeleteFlag=1 ");
            strSql.Append(" where F_ManId in (" + F_ManIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据客户id删除联系人信息
        /// </summary>
        public bool DeleteListByCusid(string cusid)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Cus_ContactPersonNew ");
            strSql.Append("update T_Cus_ContactPersonNew set F_DeleteFlag=1 ");
            strSql.Append(" where F_CustomerId in (" + cusid + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


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

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

            CallCenterApi.Model.T_Cus_ContactPersonNew model = new CallCenterApi.Model.T_Cus_ContactPersonNew();
            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_Cus_ContactPersonNew DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Cus_ContactPersonNew model = new CallCenterApi.Model.T_Cus_ContactPersonNew();
            if (row != null)
            {
                if (row["F_ManId"] != null && row["F_ManId"].ToString() != "")
                {
                    model.F_ManId = int.Parse(row["F_ManId"].ToString());
                }
                if (row["F_CustomerId"] != null && row["F_CustomerId"].ToString() != "")
                {
                    model.F_CustomerId = int.Parse(row["F_CustomerId"].ToString());
                }
                if (row["F_IsMain"] != null && row["F_IsMain"].ToString() != "")
                {
                    if ((row["F_IsMain"].ToString() == "1") || (row["F_IsMain"].ToString().ToLower() == "true"))
                    {
                        model.F_IsMain = true;
                    }
                    else
                    {
                        model.F_IsMain = false;
                    }
                }
                if (row["F_Name"] != null)
                {
                    model.F_Name = row["F_Name"].ToString();
                }
                if (row["F_Telephone"] != null)
                {
                    model.F_Telephone = row["F_Telephone"].ToString();
                }
                if (row["F_Email"] != null)
                {
                    model.F_Email = row["F_Email"].ToString();
                }
                if (row["F_Duties"] != null)
                {
                    model.F_Duties = row["F_Duties"].ToString();
                }
                if (row["F_QQ"] != null)
                {
                    model.F_QQ = row["F_QQ"].ToString();
                }
                if (row["F_MSN"] != null)
                {
                    model.F_MSN = row["F_MSN"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_CreateBy"] != null)
                {
                    model.F_CreateBy = row["F_CreateBy"].ToString();
                }
                if (row["F_CreateOn"] != null && row["F_CreateOn"].ToString() != "")
                {
                    model.F_CreateOn = DateTime.Parse(row["F_CreateOn"].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_Cus_ContactPersonNew ");
            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_Cus_ContactPersonNew ");
            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_ContactPersonNew ");
            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_ManId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Cus_ContactPersonNew 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
    }
}
