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_CallRecordsLD
    /// </summary>
    public partial class T_Call_CallRecordsLD
    {
        public T_Call_CallRecordsLD()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Call_CallRecordsLD model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_CallRecordsLD(");
            strSql.Append("F_CallRecordsID,F_IsListen,F_IsDownload,F_OptID,F_OptBy,F_OptDate)");
            strSql.Append(" values (");
            strSql.Append("@F_CallRecordsID,@F_IsListen,@F_IsDownload,@F_OptID,@F_OptBy,@F_OptDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CallRecordsID", SqlDbType.Int,4),
                    new SqlParameter("@F_IsListen", SqlDbType.Bit,1),
                    new SqlParameter("@F_IsDownload", SqlDbType.Bit,1),
                    new SqlParameter("@F_OptID", SqlDbType.Int,4),
                    new SqlParameter("@F_OptBy", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_OptDate", SqlDbType.DateTime)};
            parameters[0].Value = model.F_CallRecordsID;
            parameters[1].Value = model.F_IsListen;
            parameters[2].Value = model.F_IsDownload;
            parameters[3].Value = model.F_OptID;
            parameters[4].Value = model.F_OptBy;
            parameters[5].Value = model.F_OptDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Call_CallRecordsLD model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_CallRecordsLD set ");
            strSql.Append("F_CallRecordsID=@F_CallRecordsID,");
            strSql.Append("F_IsListen=@F_IsListen,");
            strSql.Append("F_IsDownload=@F_IsDownload,");
            strSql.Append("F_OptID=@F_OptID,");
            strSql.Append("F_OptBy=@F_OptBy,");
            strSql.Append("F_OptDate=@F_OptDate");
            strSql.Append(" where F_CallRLDID=@F_CallRLDID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CallRecordsID", SqlDbType.Int,4),
                    new SqlParameter("@F_IsListen", SqlDbType.Bit,1),
                    new SqlParameter("@F_IsDownload", SqlDbType.Bit,1),
                    new SqlParameter("@F_OptID", SqlDbType.Int,4),
                    new SqlParameter("@F_OptBy", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_OptDate", SqlDbType.DateTime),
                    new SqlParameter("@F_CallRLDID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_CallRecordsID;
            parameters[1].Value = model.F_IsListen;
            parameters[2].Value = model.F_IsDownload;
            parameters[3].Value = model.F_OptID;
            parameters[4].Value = model.F_OptBy;
            parameters[5].Value = model.F_OptDate;
            parameters[6].Value = model.F_CallRLDID;

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_CallRLDID,F_CallRecordsID,F_IsListen,F_IsDownload,F_OptID,F_OptBy,F_OptDate from T_Call_CallRecordsLD ");
            strSql.Append(" where F_CallRLDID=@F_CallRLDID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CallRLDID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_CallRLDID;

            Model.T_Call_CallRecordsLD model = new Model.T_Call_CallRecordsLD();
            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_Call_CallRecordsLD DataRowToModel(DataRow row)
        {
            Model.T_Call_CallRecordsLD model = new Model.T_Call_CallRecordsLD();
            if (row != null)
            {
                if (row["F_CallRLDID"] != null && row["F_CallRLDID"].ToString() != "")
                {
                    model.F_CallRLDID = int.Parse(row["F_CallRLDID"].ToString());
                }
                if (row["F_CallRecordsID"] != null && row["F_CallRecordsID"].ToString() != "")
                {
                    model.F_CallRecordsID = int.Parse(row["F_CallRecordsID"].ToString());
                }
                if (row["F_IsListen"] != null && row["F_IsListen"].ToString() != "")
                {
                    if ((row["F_IsListen"].ToString() == "1") || (row["F_IsListen"].ToString().ToLower() == "true"))
                    {
                        model.F_IsListen = true;
                    }
                    else
                    {
                        model.F_IsListen = false;
                    }
                }
                if (row["F_IsDownload"] != null && row["F_IsDownload"].ToString() != "")
                {
                    if ((row["F_IsDownload"].ToString() == "1") || (row["F_IsDownload"].ToString().ToLower() == "true"))
                    {
                        model.F_IsDownload = true;
                    }
                    else
                    {
                        model.F_IsDownload = false;
                    }
                }
                if (row["F_OptID"] != null && row["F_OptID"].ToString() != "")
                {
                    model.F_OptID = int.Parse(row["F_OptID"].ToString());
                }
                if (row["F_OptBy"] != null)
                {
                    model.F_OptBy = row["F_OptBy"].ToString();
                }
                if (row["F_OptDate"] != null && row["F_OptDate"].ToString() != "")
                {
                    model.F_OptDate = DateTime.Parse(row["F_OptDate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_CallRLDID,F_CallRecordsID,F_IsListen,F_IsDownload,F_OptID,F_OptBy,F_OptDate ");
            strSql.Append(" FROM T_Call_CallRecordsLD ");
            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_CallRLDID,F_CallRecordsID,F_IsListen,F_IsDownload,F_OptID,F_OptBy,F_OptDate ");
            strSql.Append(" FROM T_Call_CallRecordsLD ");
            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_CallRecordsLD ");
            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_CallRLDID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Call_CallRecordsLD 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
    }
}
