using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using DBUtility;

namespace ZXDT.CallCenter.DAL
{
    /// <summary>
    /// 数据访问类:rec_file_info
    /// </summary>
    public partial class rec_file_info
    {
        public rec_file_info()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from rec_file_info");
            strSql.Append(" where ID=@ID ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@ID", MySqlDbType.Int32,20)            };
            parameters[0].Value = ID;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ZXDT.CallCenter.Model.rec_file_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into rec_file_info(");
            strSql.Append("ID,RecTime,AgentID,AgentName,AgentExt,GroupID,RemoteNum,TimeLen,CallType,AppID,CallID,EndType,TrunkIP,FileName)");
            strSql.Append(" values (");
            strSql.Append("@ID,@RecTime,@AgentID,@AgentName,@AgentExt,@GroupID,@RemoteNum,@TimeLen,@CallType,@AppID,@CallID,@EndType,@TrunkIP,@FileName)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@ID", MySqlDbType.Int32,11),
                    new MySqlParameter("@RecTime", MySqlDbType.DateTime),
                    new MySqlParameter("@AgentID", MySqlDbType.VarChar,30),
                    new MySqlParameter("@AgentName", MySqlDbType.VarChar,64),
                    new MySqlParameter("@AgentExt", MySqlDbType.VarChar,32),
                    new MySqlParameter("@GroupID", MySqlDbType.VarChar,30),
                    new MySqlParameter("@RemoteNum", MySqlDbType.VarChar,32),
                    new MySqlParameter("@TimeLen", MySqlDbType.Int32,11),
                    new MySqlParameter("@CallType", MySqlDbType.Int32,11),
                    new MySqlParameter("@AppID", MySqlDbType.VarChar,64),
                    new MySqlParameter("@CallID", MySqlDbType.VarChar,64),
                    new MySqlParameter("@EndType", MySqlDbType.Int32,11),
                    new MySqlParameter("@TrunkIP", MySqlDbType.VarChar,40),
                    new MySqlParameter("@FileName", MySqlDbType.VarChar,256)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RecTime;
            parameters[2].Value = model.AgentID;
            parameters[3].Value = model.AgentName;
            parameters[4].Value = model.AgentExt;
            parameters[5].Value = model.GroupID;
            parameters[6].Value = model.RemoteNum;
            parameters[7].Value = model.TimeLen;
            parameters[8].Value = model.CallType;
            parameters[9].Value = model.AppID;
            parameters[10].Value = model.CallID;
            parameters[11].Value = model.EndType;
            parameters[12].Value = model.TrunkIP;
            parameters[13].Value = model.FileName;

            int rows = DbHelperMYSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZXDT.CallCenter.Model.rec_file_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update rec_file_info set ");
            strSql.Append("RecTime=@RecTime,");
            strSql.Append("AgentID=@AgentID,");
            strSql.Append("AgentName=@AgentName,");
            strSql.Append("AgentExt=@AgentExt,");
            strSql.Append("GroupID=@GroupID,");
            strSql.Append("RemoteNum=@RemoteNum,");
            strSql.Append("TimeLen=@TimeLen,");
            strSql.Append("CallType=@CallType,");
            strSql.Append("AppID=@AppID,");
            strSql.Append("CallID=@CallID,");
            strSql.Append("EndType=@EndType,");
            strSql.Append("TrunkIP=@TrunkIP,");
            strSql.Append("FileName=@FileName");
            strSql.Append(" where ID=@ID ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@RecTime", MySqlDbType.DateTime),
                    new MySqlParameter("@AgentID", MySqlDbType.VarChar,30),
                    new MySqlParameter("@AgentName", MySqlDbType.VarChar,64),
                    new MySqlParameter("@AgentExt", MySqlDbType.VarChar,32),
                    new MySqlParameter("@GroupID", MySqlDbType.VarChar,30),
                    new MySqlParameter("@RemoteNum", MySqlDbType.VarChar,32),
                    new MySqlParameter("@TimeLen", MySqlDbType.Int32,11),
                    new MySqlParameter("@CallType", MySqlDbType.Int32,11),
                    new MySqlParameter("@AppID", MySqlDbType.VarChar,64),
                    new MySqlParameter("@CallID", MySqlDbType.VarChar,64),
                    new MySqlParameter("@EndType", MySqlDbType.Int32,11),
                    new MySqlParameter("@TrunkIP", MySqlDbType.VarChar,40),
                    new MySqlParameter("@FileName", MySqlDbType.VarChar,256),
                    new MySqlParameter("@ID", MySqlDbType.Int32,11)};
            parameters[0].Value = model.RecTime;
            parameters[1].Value = model.AgentID;
            parameters[2].Value = model.AgentName;
            parameters[3].Value = model.AgentExt;
            parameters[4].Value = model.GroupID;
            parameters[5].Value = model.RemoteNum;
            parameters[6].Value = model.TimeLen;
            parameters[7].Value = model.CallType;
            parameters[8].Value = model.AppID;
            parameters[9].Value = model.CallID;
            parameters[10].Value = model.EndType;
            parameters[11].Value = model.TrunkIP;
            parameters[12].Value = model.FileName;
            parameters[13].Value = model.ID;

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

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from rec_file_info ");
            strSql.Append(" where ID=@ID ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@ID", MySqlDbType.Int32,11)            };
            parameters[0].Value = ID;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.rec_file_info GetModel(long ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,RecTime,AgentID,AgentName,AgentExt,GroupID,RemoteNum,TimeLen,CallType,AppID,CallID,EndType,TrunkIP,FileName from rec_file_info ");
            strSql.Append(" where ID=@ID ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@ID", MySqlDbType.Int32,11)            };
            parameters[0].Value = ID;

            ZXDT.CallCenter.Model.rec_file_info model = new ZXDT.CallCenter.Model.rec_file_info();
            DataSet ds = DbHelperMYSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public ZXDT.CallCenter.Model.rec_file_info GetModel(string appID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,RecTime,AgentID,AgentName,AgentExt,GroupID,RemoteNum,TimeLen,CallType,AppID,CallID,EndType,TrunkIP,FileName from rec_file_info ");
            strSql.Append(" where AppID=@AppID  limit 1");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@AppID", appID)            };


            ZXDT.CallCenter.Model.rec_file_info model = new ZXDT.CallCenter.Model.rec_file_info();
            DataSet ds = DbHelperMYSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.rec_file_info DataRowToModel(DataRow row)
        {
            ZXDT.CallCenter.Model.rec_file_info model = new ZXDT.CallCenter.Model.rec_file_info();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = long.Parse(row["ID"].ToString());
                }
                if (row["RecTime"] != null && row["RecTime"].ToString() != "")
                {
                    model.RecTime = DateTime.Parse(row["RecTime"].ToString());
                }
                if (row["AgentID"] != null)
                {
                    model.AgentID = row["AgentID"].ToString();
                }
                if (row["AgentName"] != null)
                {
                    model.AgentName = row["AgentName"].ToString();
                }
                if (row["AgentExt"] != null)
                {
                    model.AgentExt = row["AgentExt"].ToString();
                }
                if (row["GroupID"] != null)
                {
                    model.GroupID = row["GroupID"].ToString();
                }
                if (row["RemoteNum"] != null)
                {
                    model.RemoteNum = row["RemoteNum"].ToString();
                }
                if (row["TimeLen"] != null && row["TimeLen"].ToString() != "")
                {
                    model.TimeLen = int.Parse(row["TimeLen"].ToString());
                }
                if (row["CallType"] != null && row["CallType"].ToString() != "")
                {
                    model.CallType = int.Parse(row["CallType"].ToString());
                }
                if (row["AppID"] != null)
                {
                    model.AppID = row["AppID"].ToString();
                }
                if (row["CallID"] != null)
                {
                    model.CallID = row["CallID"].ToString();
                }
                if (row["EndType"] != null && row["EndType"].ToString() != "")
                {
                    model.EndType = int.Parse(row["EndType"].ToString());
                }
                if (row["TrunkIP"] != null)
                {
                    model.TrunkIP = row["TrunkIP"].ToString();
                }
                if (row["FileName"] != null)
                {
                    model.FileName = row["FileName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,RecTime,AgentID,AgentName,AgentExt,GroupID,RemoteNum,TimeLen,CallType,AppID,CallID,EndType,TrunkIP,FileName ");
            strSql.Append(" FROM rec_file_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMYSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM rec_file_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMYSQL.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.ID desc");
            }
            strSql.Append(")AS Row, T.*  from rec_file_info 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 DbHelperMYSQL.Query(strSql.ToString());
        }

       

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

