using RMYY_CallCenter_Api.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 RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Call_CallRecords
    /// </summary>
    public partial class T_Call_CallRecords
    {
        public T_Call_CallRecords()
        { }
        #region  BasicMethod

        public static object Global_Lock = new object();

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CallRecordsId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Call_CallRecords with(nolock)");
            strSql.Append(" where CallRecordsId=@CallRecordsId");
            SqlParameter[] parameters = {
                    new SqlParameter("@CallRecordsId", SqlDbType.Int,4)
            };
            parameters[0].Value = CallRecordsId;

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



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Call_CallRecords model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_CallRecords(");
            strSql.Append("CallId,CallNumber,CusId,CusCode,CallType,CallState,DealType,BeginTime,IvrStartTime,IvrEndTime,RingStartTime,RingEndTime,TalkStartTime,TalkEndTime,EndTime,LongTime,IvrLongTime,RingLongTime,TalkLongTime,UserId,UserCode,UserName,ExtNumber,FilePath,IsExitWorkOrder,IsDeal,OperateType,OperateObject,OperateTime,Remark,MYD,BusinessType,Location,WaitStartTime,WaitEndTime,WaitLongTime,NoCallState,WorkTimesDiff,F_QCState,F_QCScore,F_QCRemark,F_QCUserId,F_QCUserName,F_QCTime,F_QCIsSelect,ActionID,ActionType,WorkOrderId,F_CallInType,F_CallOutType,PhoneType,TaskType)");
            strSql.Append(" values (");
            strSql.Append("@CallId,@CallNumber,@CusId,@CusCode,@CallType,@CallState,@DealType,@BeginTime,@IvrStartTime,@IvrEndTime,@RingStartTime,@RingEndTime,@TalkStartTime,@TalkEndTime,@EndTime,@LongTime,@IvrLongTime,@RingLongTime,@TalkLongTime,@UserId,@UserCode,@UserName,@ExtNumber,@FilePath,@IsExitWorkOrder,@IsDeal,@OperateType,@OperateObject,@OperateTime,@Remark,@MYD,@BusinessType,@Location,@WaitStartTime,@WaitEndTime,@WaitLongTime,@NoCallState,@WorkTimesDiff,@F_QCState,@F_QCScore,@F_QCRemark,@F_QCUserId,@F_QCUserName,@F_QCTime,@F_QCIsSelect,@ActionID,@ActionType,@WorkOrderId,@F_CallInType,@F_CallOutType,@PhoneType,@TaskType)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50),
                    new SqlParameter("@CallNumber", SqlDbType.VarChar,100),
                    new SqlParameter("@CusId", SqlDbType.VarChar,50),
                    new SqlParameter("@CusCode", SqlDbType.VarChar,50),
                    new SqlParameter("@CallType", SqlDbType.Int,4),
                    new SqlParameter("@CallState", SqlDbType.Int,4),
                    new SqlParameter("@DealType", SqlDbType.Int,4),
                    new SqlParameter("@BeginTime", SqlDbType.DateTime),
                    new SqlParameter("@IvrStartTime", SqlDbType.DateTime),
                    new SqlParameter("@IvrEndTime", SqlDbType.DateTime),
                    new SqlParameter("@RingStartTime", SqlDbType.DateTime),
                    new SqlParameter("@RingEndTime", SqlDbType.DateTime),
                    new SqlParameter("@TalkStartTime", SqlDbType.DateTime),
                    new SqlParameter("@TalkEndTime", SqlDbType.DateTime),
                    new SqlParameter("@EndTime", SqlDbType.DateTime),
                    new SqlParameter("@LongTime", SqlDbType.Int,4),
                    new SqlParameter("@IvrLongTime", SqlDbType.Int,4),
                    new SqlParameter("@RingLongTime", SqlDbType.Int,4),
                    new SqlParameter("@TalkLongTime", SqlDbType.Int,4),
                    new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@UserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@UserName", SqlDbType.NVarChar,20),
                    new SqlParameter("@ExtNumber", SqlDbType.VarChar,50),
                    new SqlParameter("@FilePath", SqlDbType.VarChar,200),
                    new SqlParameter("@IsExitWorkOrder", SqlDbType.Bit,1),
                    new SqlParameter("@IsDeal", SqlDbType.Int,4),
                    new SqlParameter("@OperateType", SqlDbType.Int,4),
                    new SqlParameter("@OperateObject", SqlDbType.VarChar,20),
                    new SqlParameter("@OperateTime", SqlDbType.DateTime),
                    new SqlParameter("@Remark", SqlDbType.NVarChar,1000),
                    new SqlParameter("@MYD", SqlDbType.Int,4),
                    new SqlParameter("@BusinessType", SqlDbType.Int,4),
                    new SqlParameter("@Location", SqlDbType.NVarChar,100),
                    new SqlParameter("@WaitStartTime", SqlDbType.DateTime),
                    new SqlParameter("@WaitEndTime", SqlDbType.DateTime),
                    new SqlParameter("@WaitLongTime", SqlDbType.Int,4),
                    new SqlParameter("@NoCallState", SqlDbType.Int,4),
                    new SqlParameter("@WorkTimesDiff", SqlDbType.Int,4),
                    new SqlParameter("@F_QCState", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_QCScore", SqlDbType.Decimal,9),
                    new SqlParameter("@F_QCRemark", SqlDbType.Text),
                    new SqlParameter("@F_QCUserId", SqlDbType.Int,4),
                    new SqlParameter("@F_QCUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_QCTime", SqlDbType.DateTime),
                    new SqlParameter("@F_QCIsSelect", SqlDbType.SmallInt,2),
                    new SqlParameter("@ActionID", SqlDbType.Int,4),
                    new SqlParameter("@ActionType", SqlDbType.Int,4),
                    new SqlParameter("@WorkOrderId", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CallInType", SqlDbType.Int,4),
                    new SqlParameter("@F_CallOutType", SqlDbType.Int,4),
                    new SqlParameter("@PhoneType", SqlDbType.VarChar,50),
                    new SqlParameter("@TaskType", SqlDbType.VarChar,50)};
            parameters[0].Value = model.CallId;
            parameters[1].Value = model.CallNumber;
            parameters[2].Value = model.CusId;
            parameters[3].Value = model.CusCode;
            parameters[4].Value = model.CallType;
            parameters[5].Value = model.CallState;
            parameters[6].Value = model.DealType;
            parameters[7].Value = model.BeginTime;
            parameters[8].Value = model.IvrStartTime;
            parameters[9].Value = model.IvrEndTime;
            parameters[10].Value = model.RingStartTime;
            parameters[11].Value = model.RingEndTime;
            parameters[12].Value = model.TalkStartTime;
            parameters[13].Value = model.TalkEndTime;
            parameters[14].Value = model.EndTime;
            parameters[15].Value = model.LongTime;
            parameters[16].Value = model.IvrLongTime;
            parameters[17].Value = model.RingLongTime;
            parameters[18].Value = model.TalkLongTime;
            parameters[19].Value = model.UserId;
            parameters[20].Value = model.UserCode;
            parameters[21].Value = model.UserName;
            parameters[22].Value = model.ExtNumber;
            parameters[23].Value = model.FilePath;
            parameters[24].Value = model.IsExitWorkOrder;
            parameters[25].Value = model.IsDeal;
            parameters[26].Value = model.OperateType;
            parameters[27].Value = model.OperateObject;
            parameters[28].Value = model.OperateTime;
            parameters[29].Value = model.Remark;
            parameters[30].Value = model.MYD;
            parameters[31].Value = model.BusinessType;
            parameters[32].Value = model.Location;
            parameters[33].Value = model.WaitStartTime;
            parameters[34].Value = model.WaitEndTime;
            parameters[35].Value = model.WaitLongTime;
            parameters[36].Value = model.NoCallState;
            parameters[37].Value = model.WorkTimesDiff;
            parameters[38].Value = model.F_QCState;
            parameters[39].Value = model.F_QCScore;
            parameters[40].Value = model.F_QCRemark;
            parameters[41].Value = model.F_QCUserId;
            parameters[42].Value = model.F_QCUserName;
            parameters[43].Value = model.F_QCTime;
            parameters[44].Value = model.F_QCIsSelect;
            parameters[45].Value = model.ActionID;
            parameters[46].Value = model.ActionType;
            parameters[47].Value = model.WorkOrderId;
            parameters[48].Value = model.F_CallInType;
            parameters[49].Value = model.F_CallOutType;
            parameters[50].Value = model.PhoneType;
            parameters[51].Value = model.TaskType;

            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_Call_CallRecords model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_CallRecords set ");
            strSql.Append("CallId=@CallId,");
            strSql.Append("CallNumber=@CallNumber,");
            strSql.Append("CusId=@CusId,");
            strSql.Append("CusCode=@CusCode,");
            strSql.Append("CallType=@CallType,");
            strSql.Append("CallState=@CallState,");
            strSql.Append("DealType=@DealType,");
            strSql.Append("BeginTime=@BeginTime,");
            strSql.Append("IvrStartTime=@IvrStartTime,");
            strSql.Append("IvrEndTime=@IvrEndTime,");
            strSql.Append("RingStartTime=@RingStartTime,");
            strSql.Append("RingEndTime=@RingEndTime,");
            strSql.Append("TalkStartTime=@TalkStartTime,");
            strSql.Append("TalkEndTime=@TalkEndTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("LongTime=@LongTime,");
            strSql.Append("IvrLongTime=@IvrLongTime,");
            strSql.Append("RingLongTime=@RingLongTime,");
            strSql.Append("TalkLongTime=@TalkLongTime,");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UserCode=@UserCode,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("ExtNumber=@ExtNumber,");
            strSql.Append("FilePath=@FilePath,");
            strSql.Append("IsExitWorkOrder=@IsExitWorkOrder,");
            strSql.Append("IsDeal=@IsDeal,");
            strSql.Append("OperateType=@OperateType,");
            strSql.Append("OperateObject=@OperateObject,");
            strSql.Append("OperateTime=@OperateTime,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("MYD=@MYD,");
            strSql.Append("BusinessType=@BusinessType,");
            strSql.Append("Location=@Location,");
            strSql.Append("WaitStartTime=@WaitStartTime,");
            strSql.Append("WaitEndTime=@WaitEndTime,");
            strSql.Append("WaitLongTime=@WaitLongTime,");
            strSql.Append("NoCallState=@NoCallState,");
            strSql.Append("WorkTimesDiff=@WorkTimesDiff,");
            strSql.Append("F_QCState=@F_QCState,");
            strSql.Append("F_QCScore=@F_QCScore,");
            strSql.Append("F_QCRemark=@F_QCRemark,");
            strSql.Append("F_QCUserId=@F_QCUserId,");
            strSql.Append("F_QCUserName=@F_QCUserName,");
            strSql.Append("F_QCTime=@F_QCTime,");
            strSql.Append("F_QCIsSelect=@F_QCIsSelect,");
            strSql.Append("ActionID=@ActionID,");
            strSql.Append("ActionType=@ActionType,");
            strSql.Append("WorkOrderId=@WorkOrderId,");
            strSql.Append("F_CallInType=@F_CallInType,");
            strSql.Append("F_CallOutType=@F_CallOutType,");
            strSql.Append("PhoneType=@PhoneType,");
            strSql.Append("TaskType=@TaskType,");
            strSql.Append("IsReply=@IsReply,");
 
            strSql.Append("F_TransferToPhone=@F_TransferToPhone,");
            strSql.Append("F_TransferToDept=@F_TransferToDept,");
            strSql.Append("F_TransferCallState=@F_TransferCallState,");
            strSql.Append("F_TransferToTime=@F_TransferToTime");
     

            strSql.Append(" where CallRecordsId=@CallRecordsId");
            SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50),
                    new SqlParameter("@CallNumber", SqlDbType.VarChar,100),
                    new SqlParameter("@CusId", SqlDbType.VarChar,50),
                    new SqlParameter("@CusCode", SqlDbType.VarChar,50),
                    new SqlParameter("@CallType", SqlDbType.Int,4),
                    new SqlParameter("@CallState", SqlDbType.Int,4),
                    new SqlParameter("@DealType", SqlDbType.Int,4),
                    new SqlParameter("@BeginTime", SqlDbType.DateTime),
                    new SqlParameter("@IvrStartTime", SqlDbType.DateTime),
                    new SqlParameter("@IvrEndTime", SqlDbType.DateTime),
                    new SqlParameter("@RingStartTime", SqlDbType.DateTime),
                    new SqlParameter("@RingEndTime", SqlDbType.DateTime),
                    new SqlParameter("@TalkStartTime", SqlDbType.DateTime),
                    new SqlParameter("@TalkEndTime", SqlDbType.DateTime),
                    new SqlParameter("@EndTime", SqlDbType.DateTime),
                    new SqlParameter("@LongTime", SqlDbType.Int,4),
                    new SqlParameter("@IvrLongTime", SqlDbType.Int,4),
                    new SqlParameter("@RingLongTime", SqlDbType.Int,4),
                    new SqlParameter("@TalkLongTime", SqlDbType.Int,4),
                    new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@UserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@UserName", SqlDbType.NVarChar,20),
                    new SqlParameter("@ExtNumber", SqlDbType.VarChar,50),
                    new SqlParameter("@FilePath", SqlDbType.VarChar,200),
                    new SqlParameter("@IsExitWorkOrder", SqlDbType.Bit,1),
                    new SqlParameter("@IsDeal", SqlDbType.Int,4),
                    new SqlParameter("@OperateType", SqlDbType.Int,4),
                    new SqlParameter("@OperateObject", SqlDbType.VarChar,20),
                    new SqlParameter("@OperateTime", SqlDbType.DateTime),
                    new SqlParameter("@Remark", SqlDbType.NVarChar,1000),
                    new SqlParameter("@MYD", SqlDbType.Int,4),
                    new SqlParameter("@BusinessType", SqlDbType.Int,4),
                    new SqlParameter("@Location", SqlDbType.NVarChar,100),
                    new SqlParameter("@WaitStartTime", SqlDbType.DateTime),
                    new SqlParameter("@WaitEndTime", SqlDbType.DateTime),
                    new SqlParameter("@WaitLongTime", SqlDbType.Int,4),
                    new SqlParameter("@NoCallState", SqlDbType.Int,4),
                    new SqlParameter("@WorkTimesDiff", SqlDbType.Int,4),
                    new SqlParameter("@F_QCState", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_QCScore", SqlDbType.Decimal,9),
                    new SqlParameter("@F_QCRemark", SqlDbType.Text),
                    new SqlParameter("@F_QCUserId", SqlDbType.Int,4),
                    new SqlParameter("@F_QCUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_QCTime", SqlDbType.DateTime),
                    new SqlParameter("@F_QCIsSelect", SqlDbType.SmallInt,2),
                    new SqlParameter("@ActionID", SqlDbType.Int,4),
                    new SqlParameter("@ActionType", SqlDbType.Int,4),
                    new SqlParameter("@WorkOrderId", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CallInType", SqlDbType.Int,4),
                    new SqlParameter("@F_CallOutType", SqlDbType.Int,4),
                    new SqlParameter("@PhoneType", SqlDbType.VarChar,50),
                    new SqlParameter("@TaskType", SqlDbType.VarChar,50),
                    new SqlParameter("@IsReply", SqlDbType.Int,4),


            new SqlParameter("@F_TransferToPhone", SqlDbType.NVarChar, 50),
      new SqlParameter("@F_TransferToDept", SqlDbType.Int, 4),
               new SqlParameter("@F_TransferCallState", SqlDbType.Int, 4),
            new SqlParameter("@F_TransferToTime", SqlDbType.DateTime),

        
            new SqlParameter("@CallRecordsId", SqlDbType.Int,4)


            };
            parameters[0].Value = model.CallId;
            parameters[1].Value = model.CallNumber;
            parameters[2].Value = model.CusId;
            parameters[3].Value = model.CusCode;
            parameters[4].Value = model.CallType;
            parameters[5].Value = model.CallState;
            parameters[6].Value = model.DealType;
            parameters[7].Value = model.BeginTime;
            parameters[8].Value = model.IvrStartTime;
            parameters[9].Value = model.IvrEndTime;
            parameters[10].Value = model.RingStartTime;
            parameters[11].Value = model.RingEndTime;
            parameters[12].Value = model.TalkStartTime;
            parameters[13].Value = model.TalkEndTime;
            parameters[14].Value = model.EndTime;
            parameters[15].Value = model.LongTime;
            parameters[16].Value = model.IvrLongTime;
            parameters[17].Value = model.RingLongTime;
            parameters[18].Value = model.TalkLongTime;
            parameters[19].Value = model.UserId;
            parameters[20].Value = model.UserCode;
            parameters[21].Value = model.UserName;
            parameters[22].Value = model.ExtNumber;
            parameters[23].Value = model.FilePath;
            parameters[24].Value = model.IsExitWorkOrder;
            parameters[25].Value = model.IsDeal;
            parameters[26].Value = model.OperateType;
            parameters[27].Value = model.OperateObject;
            parameters[28].Value = model.OperateTime;
            parameters[29].Value = model.Remark;
            parameters[30].Value = model.MYD;
            parameters[31].Value = model.BusinessType;
            parameters[32].Value = model.Location;
            parameters[33].Value = model.WaitStartTime;
            parameters[34].Value = model.WaitEndTime;
            parameters[35].Value = model.WaitLongTime;
            parameters[36].Value = model.NoCallState;
            parameters[37].Value = model.WorkTimesDiff;
            parameters[38].Value = model.F_QCState;
            parameters[39].Value = model.F_QCScore;
            parameters[40].Value = model.F_QCRemark;
            parameters[41].Value = model.F_QCUserId;
            parameters[42].Value = model.F_QCUserName;
            parameters[43].Value = model.F_QCTime;
            parameters[44].Value = model.F_QCIsSelect;
            parameters[45].Value = model.ActionID;
            parameters[46].Value = model.ActionType;
            parameters[47].Value = model.WorkOrderId;
            parameters[48].Value = model.F_CallInType;
            parameters[49].Value = model.F_CallOutType;
            parameters[50].Value = model.PhoneType;
            parameters[51].Value = model.TaskType;
            parameters[52].Value = model.IsReply;
            parameters[53].Value = model.F_TransferToPhone;
            parameters[54].Value = model.F_TransferToDept;
            parameters[55].Value = model.F_TransferCallState;
            parameters[56].Value = model.F_TransferToTime;


            parameters[57].Value = model.CallRecordsId;

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

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int CallRecordsId)
        {
            lock (Dal.T_Call_CallRecords.Global_Lock)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from T_Call_CallRecords ");
                strSql.Append(" where CallRecordsId=@CallRecordsId");
                SqlParameter[] parameters = {
                    new SqlParameter("@CallRecordsId", SqlDbType.Int,4)
            };
                parameters[0].Value = CallRecordsId;

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


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

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

            Model.T_Call_CallRecords model = new Model.T_Call_CallRecords();
            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_Call_CallRecords DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Call_CallRecords model = new RMYY_CallCenter_Api.Model.T_Call_CallRecords();
            if (row != null)
            {
                if (row["CallId"] != null)
                {
                    model.CallId = row["CallId"].ToString();
                }
                if (row["CallNumber"] != null)
                {
                    model.CallNumber = row["CallNumber"].ToString();
                }
                if (row["CusId"] != null)
                {
                    model.CusId = row["CusId"].ToString();
                }
                if (row["CusCode"] != null)
                {
                    model.CusCode = row["CusCode"].ToString();
                }
                if (row["CallType"] != null && row["CallType"].ToString() != "")
                {
                    model.CallType = int.Parse(row["CallType"].ToString());
                }
                if (row["CallState"] != null && row["CallState"].ToString() != "")
                {
                    model.CallState = int.Parse(row["CallState"].ToString());
                }
                if (row["DealType"] != null && row["DealType"].ToString() != "")
                {
                    model.DealType = int.Parse(row["DealType"].ToString());
                }
                if (row["BeginTime"] != null && row["BeginTime"].ToString() != "")
                {
                    model.BeginTime = DateTime.Parse(row["BeginTime"].ToString());
                }
                if (row["IvrStartTime"] != null && row["IvrStartTime"].ToString() != "")
                {
                    model.IvrStartTime = DateTime.Parse(row["IvrStartTime"].ToString());
                }
                if (row["IvrEndTime"] != null && row["IvrEndTime"].ToString() != "")
                {
                    model.IvrEndTime = DateTime.Parse(row["IvrEndTime"].ToString());
                }
                if (row["RingStartTime"] != null && row["RingStartTime"].ToString() != "")
                {
                    model.RingStartTime = DateTime.Parse(row["RingStartTime"].ToString());
                }
                if (row["RingEndTime"] != null && row["RingEndTime"].ToString() != "")
                {
                    model.RingEndTime = DateTime.Parse(row["RingEndTime"].ToString());
                }
                if (row["TalkStartTime"] != null && row["TalkStartTime"].ToString() != "")
                {
                    model.TalkStartTime = DateTime.Parse(row["TalkStartTime"].ToString());
                }
                if (row["TalkEndTime"] != null && row["TalkEndTime"].ToString() != "")
                {
                    model.TalkEndTime = DateTime.Parse(row["TalkEndTime"].ToString());
                }
                if (row["EndTime"] != null && row["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(row["EndTime"].ToString());
                }
                if (row["LongTime"] != null && row["LongTime"].ToString() != "")
                {
                    model.LongTime = int.Parse(row["LongTime"].ToString());
                }
                if (row["IvrLongTime"] != null && row["IvrLongTime"].ToString() != "")
                {
                    model.IvrLongTime = int.Parse(row["IvrLongTime"].ToString());
                }
                if (row["RingLongTime"] != null && row["RingLongTime"].ToString() != "")
                {
                    model.RingLongTime = int.Parse(row["RingLongTime"].ToString());
                }
                if (row["TalkLongTime"] != null && row["TalkLongTime"].ToString() != "")
                {
                    model.TalkLongTime = int.Parse(row["TalkLongTime"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["UserCode"] != null)
                {
                    model.UserCode = row["UserCode"].ToString();
                }
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["ExtNumber"] != null)
                {
                    model.ExtNumber = row["ExtNumber"].ToString();
                }
                if (row["FilePath"] != null)
                {
                    model.FilePath = row["FilePath"].ToString();
                }
                if (row["IsExitWorkOrder"] != null && row["IsExitWorkOrder"].ToString() != "")
                {
                    if ((row["IsExitWorkOrder"].ToString() == "1") || (row["IsExitWorkOrder"].ToString().ToLower() == "true"))
                    {
                        model.IsExitWorkOrder = true;
                    }
                    else
                    {
                        model.IsExitWorkOrder = false;
                    }
                }
                if (row["IsDeal"] != null && row["IsDeal"].ToString() != "")
                {
                    model.IsDeal = int.Parse(row["IsDeal"].ToString());
                }
                if (row["OperateType"] != null && row["OperateType"].ToString() != "")
                {
                    model.OperateType = int.Parse(row["OperateType"].ToString());
                }
                if (row["OperateObject"] != null)
                {
                    model.OperateObject = row["OperateObject"].ToString();
                }
                if (row["OperateTime"] != null && row["OperateTime"].ToString() != "")
                {
                    model.OperateTime = DateTime.Parse(row["OperateTime"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["MYD"] != null && row["MYD"].ToString() != "")
                {
                    model.MYD = int.Parse(row["MYD"].ToString());
                }
                if (row["BusinessType"] != null && row["BusinessType"].ToString() != "")
                {
                    model.BusinessType = int.Parse(row["BusinessType"].ToString());
                }
                if (row["Location"] != null)
                {
                    model.Location = row["Location"].ToString();
                }
                if (row["CallRecordsId"] != null && row["CallRecordsId"].ToString() != "")
                {
                    model.CallRecordsId = int.Parse(row["CallRecordsId"].ToString());
                }
                if (row["WaitStartTime"] != null && row["WaitStartTime"].ToString() != "")
                {
                    model.WaitStartTime = DateTime.Parse(row["WaitStartTime"].ToString());
                }
                if (row["WaitEndTime"] != null && row["WaitEndTime"].ToString() != "")
                {
                    model.WaitEndTime = DateTime.Parse(row["WaitEndTime"].ToString());
                }
                if (row["WaitLongTime"] != null && row["WaitLongTime"].ToString() != "")
                {
                    model.WaitLongTime = int.Parse(row["WaitLongTime"].ToString());
                }
                if (row["NoCallState"] != null && row["NoCallState"].ToString() != "")
                {
                    model.NoCallState = int.Parse(row["NoCallState"].ToString());
                }
                if (row["WorkTimesDiff"] != null && row["WorkTimesDiff"].ToString() != "")
                {
                    model.WorkTimesDiff = int.Parse(row["WorkTimesDiff"].ToString());
                }
                if (row["F_QCState"] != null && row["F_QCState"].ToString() != "")
                {
                    model.F_QCState = int.Parse(row["F_QCState"].ToString());
                }
                if (row["F_QCScore"] != null && row["F_QCScore"].ToString() != "")
                {
                    model.F_QCScore = decimal.Parse(row["F_QCScore"].ToString());
                }
                if (row["F_QCRemark"] != null)
                {
                    model.F_QCRemark = row["F_QCRemark"].ToString();
                }
                if (row["F_QCUserId"] != null && row["F_QCUserId"].ToString() != "")
                {
                    model.F_QCUserId = int.Parse(row["F_QCUserId"].ToString());
                }
                if (row["F_QCUserName"] != null)
                {
                    model.F_QCUserName = row["F_QCUserName"].ToString();
                }
                if (row["F_QCTime"] != null && row["F_QCTime"].ToString() != "")
                {
                    model.F_QCTime = DateTime.Parse(row["F_QCTime"].ToString());
                }
                if (row["F_QCIsSelect"] != null && row["F_QCIsSelect"].ToString() != "")
                {
                    model.F_QCIsSelect = int.Parse(row["F_QCIsSelect"].ToString());
                }
                if (row["ActionID"] != null && row["ActionID"].ToString() != "")
                {
                    model.ActionID = int.Parse(row["ActionID"].ToString());
                }
                if (row["ActionType"] != null && row["ActionType"].ToString() != "")
                {
                    model.ActionType = int.Parse(row["ActionType"].ToString());
                }
                if (row["WorkOrderId"] != null)
                {
                    model.WorkOrderId = row["WorkOrderId"].ToString();
                }
                if (row["F_CallInType"] != null && row["F_CallInType"].ToString() != "")
                {
                    model.F_CallInType = int.Parse(row["F_CallInType"].ToString());
                }
                if (row["F_CallOutType"] != null && row["F_CallOutType"].ToString() != "")
                {
                    model.F_CallOutType = int.Parse(row["F_CallOutType"].ToString());
                }
                if (row["PhoneType"] != null)
                {
                    model.PhoneType = row["PhoneType"].ToString();
                }
                if (row["TaskType"] != null)
                {
                    model.TaskType = row["TaskType"].ToString();
                }


                if (row["F_TransferToPhone"] != null)
                {
                    model.F_TransferToPhone = row["F_TransferToPhone"].ToString();
                }

                if (row["F_TransferCallState"] != null && row["F_TransferCallState"].ToString() != "")
                {
                    model.F_TransferCallState = int.Parse(row["F_TransferCallState"].ToString());
                }
                if (row["F_TransferToDept"] != null && row["F_TransferToDept"].ToString() != "")
                {
                    model.F_TransferToDept = int.Parse(row["F_TransferToDept"].ToString());
                }
                if (row["F_TransferToTime"] != null && row["F_TransferToTime"].ToString() != "")
                {
                    model.F_TransferToTime = DateTime.Parse(row["F_TransferToTime"].ToString());
                }


            }
            return model;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Call_CallRecords  WITH(NOLOCK)");
            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_Call_CallRecords  WITH(NOLOCK)");
            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_CallRecords  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

        #region 更新来电振铃信息

        /// <summary>
        /// 更新来电振铃信息
        /// </summary>
        public bool UpdateCallInRingTelRecord(Model.T_Call_CallRecords model)
        {
            lock (Dal.T_Call_CallRecords.Global_Lock)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_Call_CallRecords set ");
                //strSql.Append("IvrLongTime=datediff(second,IvrStartTime,IvrEndTime),");
                //strSql.Append("WaitEndTime=getdate(),");
                //strSql.Append("WaitLongTime=datediff(second,WaitStartTime,getdate()),");
                //strSql.Append("RingStartTime=getdate(),");
                strSql.Append("UserId=@UserId,");
                strSql.Append("UserCode=@UserCode,");
                strSql.Append("UserName=@UserName,");
                strSql.Append("ExtNumber=@ExtNumber,");
                strSql.Append("NoCallState=3,");//未接状态为振铃
                strSql.Append("DealType=@DealType");
                strSql.Append(" where CallId=@CallId ");
                SqlParameter[] parameters = {
                    new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@UserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@UserName", SqlDbType.NVarChar,20),
                    new SqlParameter("@ExtNumber", SqlDbType.VarChar,50),
                    new SqlParameter("@CallId", SqlDbType.VarChar,50),
                    new SqlParameter("@DealType", SqlDbType.Int,4)};

                parameters[0].Value = model.UserId;
                parameters[1].Value = model.UserCode;
                parameters[2].Value = model.UserName;
                parameters[3].Value = model.ExtNumber;
                parameters[4].Value = model.CallId;
                parameters[5].Value = model.DealType;
                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        #endregion

        #region 更新来电接通信息

        /// <summary>
        /// 更新来电接通信息
        /// </summary>
        public bool UpdateCallInAnswerTelRecord(Model.T_Call_CallRecords model)
        {
            lock (Dal.T_Call_CallRecords.Global_Lock)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_Call_CallRecords set ");
                strSql.Append("CallState=@CallState,");
                //strSql.Append("RingEndTime=getdate(),");
                //strSql.Append("RingLongTime=datediff(second,RingStartTime,getdate()),");
                //strSql.Append("TalkStartTime=getdate(),");
                strSql.Append("DealType=@DealType");
                strSql.Append(" where CallId=@CallId");
                SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50),
                    new SqlParameter("@CallState", SqlDbType.Int,4),
                      new SqlParameter("@DealType", SqlDbType.Int,4)};
                parameters[0].Value = model.CallId;
                parameters[1].Value = model.CallState;
                parameters[2].Value = model.DealType;
                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        #endregion

        #region 更新来电挂断电话记录信息

        /// <summary>
        /// 更新来电挂断电话记录信息
        /// </summary>
        public bool UpdateCallInHookTelRecord(string p_CallId)
        {
            lock (Dal.T_Call_CallRecords.Global_Lock)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_Call_CallRecords SET DealType=(case CallState when 1 then 6 else DealType end)");
                strSql.Append(" where CallId=@CallId");
                SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50)};
                parameters[0].Value = p_CallId;

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

        #endregion

        #region 更新来电录音开始信息

        /// <summary>
        /// 更新来电录音开始信息
        /// </summary>
        public bool UpdateCallInPathTelRecord(Model.T_Call_CallRecords model)
        {
            lock (Dal.T_Call_CallRecords.Global_Lock)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_Call_CallRecords set ");
                strSql.Append("CallState=@CallState,");
                strSql.Append("FilePath=@FilePath,");
                strSql.Append("DealType=@DealType");
                strSql.Append(" where CallId=@CallId");
                SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50),
                    new SqlParameter("@CallState", SqlDbType.Int,4),
                    new SqlParameter("@FilePath", SqlDbType.VarChar,200),
                      new SqlParameter("@DealType", SqlDbType.Int,4)};
                parameters[0].Value = model.CallId;
                parameters[1].Value = model.CallState;
                parameters[2].Value = model.FilePath;
                parameters[3].Value = model.DealType;
                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        #endregion

        #region 根据来电号码获取电话记录
        public Model.T_Call_CallRecords GetModelByTelphone(string CallNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Call_CallRecords  WITH(NOLOCK)");
            strSql.Append(" where CallNumber=@CallNumber order by BeginTime desc");
            SqlParameter[] parameters = {
                    new SqlParameter("@CallNumber", SqlDbType.VarChar,100)
            };
            parameters[0].Value = CallNumber;

            Model.T_Call_CallRecords model = new Model.T_Call_CallRecords();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 根据CallId获取电话记录
        public Model.T_Call_CallRecords GetModelByCallId(string CallId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Call_CallRecords  WITH(NOLOCK)");
            //strSql.Append(" where CallId=@CallId order by BeginTime desc");
            strSql.Append(" where CallId=@CallId order by BeginTime asc");//三方通话
            SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50)
            };
            parameters[0].Value = CallId;

            Model.T_Call_CallRecords model = new Model.T_Call_CallRecords();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public Model.T_Call_CallRecords GetModelByRecordId(string CallId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Call_CallRecords  WITH(NOLOCK)");
            strSql.Append(" where CallRecordsId=@CallRecordsId order by BeginTime desc");
            SqlParameter[] parameters = {
                    new SqlParameter("@CallRecordsId", SqlDbType.Int)
            };
            parameters[0].Value = Convert.ToInt32(CallId);

            Model.T_Call_CallRecords model = new Model.T_Call_CallRecords();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 更新来电处理状态信息

        /// <summary>
        /// 更新来电处理状态信息
        /// </summary>
        public bool UpdateCallInRecordDealType(string callid, int type)
        {
            lock (Dal.T_Call_CallRecords.Global_Lock)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_Call_CallRecords set ");
                strSql.Append("DealType=@DealType");
                strSql.Append(" where CallId=@CallId ");
                SqlParameter[] parameters = {
                    new SqlParameter("@CallId", SqlDbType.VarChar,50),
                    new SqlParameter("@DealType", SqlDbType.Int,4)};


                parameters[0].Value = callid;
                parameters[1].Value = type;
                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        #endregion
        
    }
}
