using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RMYY_CallCenter_Api.DB;

namespace RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Cus_Customer
    /// </summary>
    public partial class T_Cus_Customer
    {
        public T_Cus_Customer()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_CustomerId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Cus_Customer with(nolock)");
            strSql.Append(" where F_CustomerId=@F_CustomerId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CustomerId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_CustomerId;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Cus_Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Cus_Customer(");
            strSql.Append("F_CustomerName,F_CustomerCode,F_Layer,F_Province,F_City,F_County,F_Address,F_Telephone,F_Mobile,F_Fax,F_Email,F_PostCode,F_Sex,F_IdCard,F_CreateBy,F_CreatedOn,F_ModifyBy,F_ModifiedOn,F_DeleteFlag,F_DeleteBy,F_DeleteOn,F_Remark,F_AboutLinkMan,F_RelationShip)");
            strSql.Append(" values (");
            strSql.Append("@F_CustomerName,@F_CustomerCode,@F_Layer,@F_Province,@F_City,@F_County,@F_Address,@F_Telephone,@F_Mobile,@F_Fax,@F_Email,@F_PostCode,@F_Sex,@F_IdCard,@F_CreateBy,@F_CreatedOn,@F_ModifyBy,@F_ModifiedOn,@F_DeleteFlag,@F_DeleteBy,@F_DeleteOn,@F_Remark,@F_AboutLinkMan,@F_RelationShip)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CustomerName", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_CustomerCode", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Layer", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_Province", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_City", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_County", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Address", SqlDbType.NVarChar,300),
                    new SqlParameter("@F_Telephone", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Mobile", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Fax", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Email", SqlDbType.VarChar,200),
                    new SqlParameter("@F_PostCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Sex", SqlDbType.VarChar,50),
                    new SqlParameter("@F_IdCard", SqlDbType.VarChar,18),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreatedOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifyBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ModifiedOn", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                      new SqlParameter("@F_AboutLinkMan", SqlDbType.VarChar,100),
                       new SqlParameter("@F_RelationShip", SqlDbType.NVarChar,50)
                      

            };
            parameters[0].Value = model.F_CustomerName;
            parameters[1].Value = model.F_CustomerCode;
            parameters[2].Value = model.F_Layer;
            parameters[3].Value = model.F_Province;
            parameters[4].Value = model.F_City;
            parameters[5].Value = model.F_County;
            parameters[6].Value = model.F_Address;
            parameters[7].Value = model.F_Telephone;
            parameters[8].Value = model.F_Mobile;
            parameters[9].Value = model.F_Fax;
            parameters[10].Value = model.F_Email;
            parameters[11].Value = model.F_PostCode;
            parameters[12].Value = model.F_Sex;
            parameters[13].Value = model.F_IdCard;
            parameters[14].Value = model.F_CreateBy;
            parameters[15].Value = model.F_CreatedOn;
            parameters[16].Value = model.F_ModifyBy;
            parameters[17].Value = model.F_ModifiedOn;
            parameters[18].Value = model.F_DeleteFlag;
            parameters[19].Value = model.F_DeleteBy;
            parameters[20].Value = model.F_DeleteOn;
            parameters[21].Value = model.F_Remark;
            parameters[22].Value = model.F_AboutLinkMan;
            parameters[23].Value = model.F_RelationShip; 


            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_Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Cus_Customer set ");
            strSql.Append("F_CustomerName=@F_CustomerName,");
            strSql.Append("F_CustomerCode=@F_CustomerCode,");
            strSql.Append("F_Layer=@F_Layer,");
            strSql.Append("F_Province=@F_Province,");
            strSql.Append("F_City=@F_City,");
            strSql.Append("F_County=@F_County,");
            strSql.Append("F_Address=@F_Address,");
            strSql.Append("F_Telephone=@F_Telephone,");
            strSql.Append("F_Mobile=@F_Mobile,");
            strSql.Append("F_Fax=@F_Fax,");
            strSql.Append("F_Email=@F_Email,");
            strSql.Append("F_PostCode=@F_PostCode,");
            strSql.Append("F_Sex=@F_Sex,");
            strSql.Append("F_IdCard=@F_IdCard,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreatedOn=@F_CreatedOn,");
            strSql.Append("F_ModifyBy=@F_ModifyBy,");
            strSql.Append("F_ModifiedOn=@F_ModifiedOn,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag,");
            strSql.Append("F_DeleteBy=@F_DeleteBy,");
            strSql.Append("F_DeleteOn=@F_DeleteOn,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_AboutLinkMan=@F_AboutLinkMan");
            

            strSql.Append(" where F_CustomerId=@F_CustomerId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CustomerName", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_CustomerCode", SqlDbType.VarChar,100),
                    new SqlParameter("@F_Layer", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_Province", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_City", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_County", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Address", SqlDbType.NVarChar,300),
                    new SqlParameter("@F_Telephone", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Mobile", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Fax", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Email", SqlDbType.VarChar,200),
                    new SqlParameter("@F_PostCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Sex", SqlDbType.VarChar,50),
                    new SqlParameter("@F_IdCard", SqlDbType.VarChar,18),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreatedOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifyBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ModifiedOn", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                      new SqlParameter("@F_AboutLinkMan", SqlDbType.VarChar,100),
                    

                    new SqlParameter("@F_CustomerId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_CustomerName;
            parameters[1].Value = model.F_CustomerCode;
            parameters[2].Value = model.F_Layer;
            parameters[3].Value = model.F_Province;
            parameters[4].Value = model.F_City;
            parameters[5].Value = model.F_County;
            parameters[6].Value = model.F_Address;
            parameters[7].Value = model.F_Telephone;
            parameters[8].Value = model.F_Mobile;
            parameters[9].Value = model.F_Fax;
            parameters[10].Value = model.F_Email;
            parameters[11].Value = model.F_PostCode;
            parameters[12].Value = model.F_Sex;
            parameters[13].Value = model.F_IdCard;
            parameters[14].Value = model.F_CreateBy;
            parameters[15].Value = model.F_CreatedOn;
            parameters[16].Value = model.F_ModifyBy;
            parameters[17].Value = model.F_ModifiedOn;
            parameters[18].Value = model.F_DeleteFlag;
            parameters[19].Value = model.F_DeleteBy;
            parameters[20].Value = model.F_DeleteOn;
            parameters[21].Value = model.F_Remark;
            parameters[22].Value = model.F_AboutLinkMan;
            
            parameters[23].Value = model.F_CustomerId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_CustomerId)
        {

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

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



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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_CustomerId,F_CustomerName,F_CustomerCode,F_Layer,F_Province,F_City,F_County,F_Address,F_Telephone,F_Mobile,F_Fax,F_Email,F_PostCode,F_Sex,F_IdCard,F_CreateBy,F_CreatedOn,F_ModifyBy,F_ModifiedOn,F_DeleteFlag,F_DeleteBy,F_DeleteOn,F_Remark,F_AboutLinkMan from T_Cus_Customer ");
            strSql.Append(" where F_CustomerId=@F_CustomerId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CustomerId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_CustomerId;

            RMYY_CallCenter_Api.Model.T_Cus_Customer model = new RMYY_CallCenter_Api.Model.T_Cus_Customer();
            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_Customer DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Cus_Customer model = new RMYY_CallCenter_Api.Model.T_Cus_Customer();
            if (row != null)
            {

                if (row["F_AboutLinkMan"] != null)
                {
                    model.F_AboutLinkMan = row["F_AboutLinkMan"].ToString();
                }
                if (row["F_CustomerId"] != null && row["F_CustomerId"].ToString() != "")
                {
                    model.F_CustomerId = int.Parse(row["F_CustomerId"].ToString());
                }
                if (row["F_CustomerName"] != null)
                {
                    model.F_CustomerName = row["F_CustomerName"].ToString();
                }
                if (row["F_CustomerCode"] != null)
                {
                    model.F_CustomerCode = row["F_CustomerCode"].ToString();
                }
                if (row["F_Layer"] != null && row["F_Layer"].ToString() != "")
                {
                    model.F_Layer = int.Parse(row["F_Layer"].ToString());
                }
                if (row["F_Province"] != null)
                {
                    model.F_Province = row["F_Province"].ToString();
                }
                if (row["F_City"] != null)
                {
                    model.F_City = row["F_City"].ToString();
                }
                if (row["F_County"] != null)
                {
                    model.F_County = row["F_County"].ToString();
                }
                if (row["F_Address"] != null)
                {
                    model.F_Address = row["F_Address"].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_Fax"] != null)
                {
                    model.F_Fax = row["F_Fax"].ToString();
                }
                if (row["F_Email"] != null)
                {
                    model.F_Email = row["F_Email"].ToString();
                }
                if (row["F_PostCode"] != null)
                {
                    model.F_PostCode = row["F_PostCode"].ToString();
                }
                if (row["F_Sex"] != null)
                {
                    model.F_Sex = row["F_Sex"].ToString();
                }
                if (row["F_IdCard"] != null)
                {
                    model.F_IdCard = row["F_IdCard"].ToString();
                }
                if (row["F_CreateBy"] != null)
                {
                    model.F_CreateBy = row["F_CreateBy"].ToString();
                }
                if (row["F_CreatedOn"] != null && row["F_CreatedOn"].ToString() != "")
                {
                    model.F_CreatedOn = DateTime.Parse(row["F_CreatedOn"].ToString());
                }
                if (row["F_ModifyBy"] != null)
                {
                    model.F_ModifyBy = row["F_ModifyBy"].ToString();
                }
                if (row["F_ModifiedOn"] != null && row["F_ModifiedOn"].ToString() != "")
                {
                    model.F_ModifiedOn = DateTime.Parse(row["F_ModifiedOn"].ToString());
                }
                if (row["F_DeleteFlag"] != null && row["F_DeleteFlag"].ToString() != "")
                {
                    model.F_DeleteFlag = int.Parse(row["F_DeleteFlag"].ToString());
                }
                if (row["F_DeleteBy"] != null)
                {
                    model.F_DeleteBy = row["F_DeleteBy"].ToString();
                }
                if (row["F_DeleteOn"] != null && row["F_DeleteOn"].ToString() != "")
                {
                    model.F_DeleteOn = DateTime.Parse(row["F_DeleteOn"].ToString());
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_CustomerId,F_CustomerName,F_CustomerCode,F_Layer,F_Province,F_City,F_County,F_Address,F_Telephone,F_Mobile,F_Fax,F_Email,F_PostCode,F_Sex,F_IdCard,F_CreateBy,F_CreatedOn,F_ModifyBy,F_ModifiedOn,F_DeleteFlag,F_DeleteBy,F_DeleteOn,F_Remark ");
            strSql.Append(" FROM T_Cus_Customer ");
            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_CustomerId,F_CustomerName,F_CustomerCode,F_Layer,F_Province,F_City,F_County,F_Address,F_Telephone,F_Mobile,F_Fax,F_Email,F_PostCode,F_Sex,F_IdCard,F_CreateBy,F_CreatedOn,F_ModifyBy,F_ModifiedOn,F_DeleteFlag,F_DeleteBy,F_DeleteOn,F_Remark ");
            strSql.Append(" FROM T_Cus_Customer ");
            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_Customer 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);
            }
        }

        #endregion  BasicMethod
    }
}

