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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Vis_TelRecords model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Vis_TelRecords(");
            strSql.Append("F_HFID,F_HFTelphone,F_HFState,F_HFInfo,F_HFRemark,F_HFUserId,F_HFUserCode,F_HFUserName,F_HFTime,F_CallUserID,F_CallUserName,F_CallDate,F_ISCall,F_CallResID,F_CallRes,F_ISCallRes)");
            strSql.Append(" values (");
            strSql.Append("@F_HFID,@F_HFTelphone,@F_HFState,@F_HFInfo,@F_HFRemark,@F_HFUserId,@F_HFUserCode,@F_HFUserName,@F_HFTime,@F_CallUserID,@F_CallUserName,@F_CallDate,@F_ISCall,@F_CallResID,@F_CallRes,@F_ISCallRes)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_HFID", SqlDbType.Int,4),
                    new SqlParameter("@F_HFTelphone", SqlDbType.VarChar,100),
                    new SqlParameter("@F_HFState", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_HFInfo", SqlDbType.NText),
                    new SqlParameter("@F_HFRemark", SqlDbType.NText),
                    new SqlParameter("@F_HFUserId", SqlDbType.Int,4),
                    new SqlParameter("@F_HFUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_HFUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_HFTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CallUserID", SqlDbType.Int,4),
                    new SqlParameter("@F_CallUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CallDate", SqlDbType.DateTime),
                    new SqlParameter("@F_ISCall", SqlDbType.Bit,1),
                    new SqlParameter("@F_CallResID", SqlDbType.Int,4),
                    new SqlParameter("@F_CallRes", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ISCallRes", SqlDbType.Bit,1)};
            parameters[0].Value = model.F_HFID;
            parameters[1].Value = model.F_HFTelphone;
            parameters[2].Value = model.F_HFState;
            parameters[3].Value = model.F_HFInfo;
            parameters[4].Value = model.F_HFRemark;
            parameters[5].Value = model.F_HFUserId;
            parameters[6].Value = model.F_HFUserCode;
            parameters[7].Value = model.F_HFUserName;
            parameters[8].Value = model.F_HFTime;
            parameters[9].Value = model.F_CallUserID;
            parameters[10].Value = model.F_CallUserName;
            parameters[11].Value = model.F_CallDate;
            parameters[12].Value = model.F_ISCall;
            parameters[13].Value = model.F_CallResID;
            parameters[14].Value = model.F_CallRes;
            parameters[15].Value = model.F_ISCallRes;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Vis_TelRecords model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Vis_TelRecords set ");
            strSql.Append("F_HFID=@F_HFID,");
            strSql.Append("F_HFTelphone=@F_HFTelphone,");
            strSql.Append("F_HFState=@F_HFState,");
            strSql.Append("F_HFInfo=@F_HFInfo,");
            strSql.Append("F_HFRemark=@F_HFRemark,");
            strSql.Append("F_HFUserId=@F_HFUserId,");
            strSql.Append("F_HFUserCode=@F_HFUserCode,");
            strSql.Append("F_HFUserName=@F_HFUserName,");
            strSql.Append("F_HFTime=@F_HFTime,");
            strSql.Append("F_CallUserID=@F_CallUserID,");
            strSql.Append("F_CallUserName=@F_CallUserName,");
            strSql.Append("F_CallDate=@F_CallDate,");
            strSql.Append("F_ISCall=@F_ISCall,");
            strSql.Append("F_CallResID=@F_CallResID,");
            strSql.Append("F_CallRes=@F_CallRes,");
            strSql.Append("F_ISCallRes=@F_ISCallRes");
            strSql.Append(" where F_TRID=@F_TRID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_HFID", SqlDbType.Int,4),
                    new SqlParameter("@F_HFTelphone", SqlDbType.VarChar,100),
                    new SqlParameter("@F_HFState", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_HFInfo", SqlDbType.NText),
                    new SqlParameter("@F_HFRemark", SqlDbType.NText),
                    new SqlParameter("@F_HFUserId", SqlDbType.Int,4),
                    new SqlParameter("@F_HFUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_HFUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_HFTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CallUserID", SqlDbType.Int,4),
                    new SqlParameter("@F_CallUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CallDate", SqlDbType.DateTime),
                    new SqlParameter("@F_ISCall", SqlDbType.Bit,1),
                    new SqlParameter("@F_CallResID", SqlDbType.Int,4),
                    new SqlParameter("@F_CallRes", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ISCallRes", SqlDbType.Bit,1),
                    new SqlParameter("@F_TRID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_HFID;
            parameters[1].Value = model.F_HFTelphone;
            parameters[2].Value = model.F_HFState;
            parameters[3].Value = model.F_HFInfo;
            parameters[4].Value = model.F_HFRemark;
            parameters[5].Value = model.F_HFUserId;
            parameters[6].Value = model.F_HFUserCode;
            parameters[7].Value = model.F_HFUserName;
            parameters[8].Value = model.F_HFTime;
            parameters[9].Value = model.F_CallUserID;
            parameters[10].Value = model.F_CallUserName;
            parameters[11].Value = model.F_CallDate;
            parameters[12].Value = model.F_ISCall;
            parameters[13].Value = model.F_CallResID;
            parameters[14].Value = model.F_CallRes;
            parameters[15].Value = model.F_ISCallRes;
            parameters[16].Value = model.F_TRID;

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

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_TRID,F_HFID,F_HFTelphone,F_HFState,F_HFInfo,F_HFRemark,F_HFUserId,F_HFUserCode,F_HFUserName,F_HFTime,F_CallUserID,F_CallUserName,F_CallDate,F_ISCall,F_CallResID,F_CallRes,F_ISCallRes from T_Vis_TelRecords ");
            strSql.Append(" where F_TRID=@F_TRID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TRID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_TRID;

            Model.T_Vis_TelRecords model = new Model.T_Vis_TelRecords();
            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_Vis_TelRecords DataRowToModel(DataRow row)
        {
            Model.T_Vis_TelRecords model = new Model.T_Vis_TelRecords();
            if (row != null)
            {
                if (row["F_TRID"] != null && row["F_TRID"].ToString() != "")
                {
                    model.F_TRID = int.Parse(row["F_TRID"].ToString());
                }
                if (row["F_HFID"] != null && row["F_HFID"].ToString() != "")
                {
                    model.F_HFID = int.Parse(row["F_HFID"].ToString());
                }
                if (row["F_HFTelphone"] != null)
                {
                    model.F_HFTelphone = row["F_HFTelphone"].ToString();
                }
                if (row["F_HFState"] != null && row["F_HFState"].ToString() != "")
                {
                    model.F_HFState = int.Parse(row["F_HFState"].ToString());
                }
                if (row["F_HFInfo"] != null)
                {
                    model.F_HFInfo = row["F_HFInfo"].ToString();
                }
                if (row["F_HFRemark"] != null)
                {
                    model.F_HFRemark = row["F_HFRemark"].ToString();
                }
                if (row["F_HFUserId"] != null && row["F_HFUserId"].ToString() != "")
                {
                    model.F_HFUserId = int.Parse(row["F_HFUserId"].ToString());
                }
                if (row["F_HFUserCode"] != null)
                {
                    model.F_HFUserCode = row["F_HFUserCode"].ToString();
                }
                if (row["F_HFUserName"] != null)
                {
                    model.F_HFUserName = row["F_HFUserName"].ToString();
                }
                if (row["F_HFTime"] != null && row["F_HFTime"].ToString() != "")
                {
                    model.F_HFTime = DateTime.Parse(row["F_HFTime"].ToString());
                }
                if (row["F_CallUserID"] != null && row["F_CallUserID"].ToString() != "")
                {
                    model.F_CallUserID = int.Parse(row["F_CallUserID"].ToString());
                }
                if (row["F_CallUserName"] != null)
                {
                    model.F_CallUserName = row["F_CallUserName"].ToString();
                }
                if (row["F_CallDate"] != null && row["F_CallDate"].ToString() != "")
                {
                    model.F_CallDate = DateTime.Parse(row["F_CallDate"].ToString());
                }
                if (row["F_ISCall"] != null && row["F_ISCall"].ToString() != "")
                {
                    if ((row["F_ISCall"].ToString() == "1") || (row["F_ISCall"].ToString().ToLower() == "true"))
                    {
                        model.F_ISCall = true;
                    }
                    else
                    {
                        model.F_ISCall = false;
                    }
                }
                if (row["F_CallResID"] != null && row["F_CallResID"].ToString() != "")
                {
                    model.F_CallResID = int.Parse(row["F_CallResID"].ToString());
                }
                if (row["F_CallRes"] != null)
                {
                    model.F_CallRes = row["F_CallRes"].ToString();
                }
                if (row["F_ISCallRes"] != null && row["F_ISCallRes"].ToString() != "")
                {
                    if ((row["F_ISCallRes"].ToString() == "1") || (row["F_ISCallRes"].ToString().ToLower() == "true"))
                    {
                        model.F_ISCallRes = true;
                    }
                    else
                    {
                        model.F_ISCallRes = false;
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_TRID,F_HFID,F_HFTelphone,F_HFState,F_HFInfo,F_HFRemark,F_HFUserId,F_HFUserCode,F_HFUserName,F_HFTime,F_CallUserID,F_CallUserName,F_CallDate,F_ISCall,F_CallResID,F_CallRes,F_ISCallRes ");
            strSql.Append(" FROM T_Vis_TelRecords ");
            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_TRID,F_HFID,F_HFTelphone,F_HFState,F_HFInfo,F_HFRemark,F_HFUserId,F_HFUserCode,F_HFUserName,F_HFTime,F_CallUserID,F_CallUserName,F_CallDate,F_ISCall,F_CallResID,F_CallRes,F_ISCallRes ");
            strSql.Append(" FROM T_Vis_TelRecords ");
            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_Vis_TelRecords ");
            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_TRID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Vis_TelRecords 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
    }
}
