using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace CallCenterApi.DAL
{
    public class CallRecords
    {
        public CallRecords()
        { }
        #region  BasicMethod
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CallResult DataRowToModel(DataRow row)
        {
            Model.CallResult model = new Model.CallResult();
            if (row != null)
            {
                model.Id = row["Id"] == DBNull.Value ? 0 : Convert.ToInt32(row["Id"]);
                model.TaskId = row["itemid"] == DBNull.Value ? 0 : Convert.ToInt32(row["itemid"]);
                model.TaskName = row["TaskName"] == DBNull.Value ? "" : row["TaskName"].ToString();
                model.CallNumber = row["Callee"] == DBNull.Value ? "" : row["Callee"].ToString();
                model.Usercode = row["AgentId"] == DBNull.Value ? "" : row["AgentId"].ToString();
                model.StartDate = row["StartDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["StartDate"].ToString());
                model.GetInAgentDate = row["GetInAgentDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["GetInAgentDate"].ToString());
                model.EndDate = row["EndDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["EndDate"].ToString());
                model.RecordPath = row["RecordPath"] == DBNull.Value ? "" : row["RecordPath"].ToString();
                model.Username = row["Username"] == DBNull.Value ? "" : row["Username"].ToString();
                model.CallType = row["CallType"] == DBNull.Value ? 1 : Convert.ToInt32(row["CallType"].ToString());
                model.TalkTime = row["TalkTime"] == DBNull.Value ? 0 : Convert.ToInt32(row["TalkTime"]);
            }
            return model;
        }
        public Model.CallResult DataRowToModelQC(DataRow row)
        {
            Model.CallResult model = new Model.CallResult();
            if (row != null)
            {
                model.Id = row["Id"] == DBNull.Value ? 0 : Convert.ToInt32(row["Id"]);
                model.TaskName = row["TaskName"] == DBNull.Value ? "" : row["TaskName"].ToString();
                model.TaskId = row["itemid"] == DBNull.Value ? 0 : Convert.ToInt32(row["itemid"]);
                model.CallNumber = row["Callee"] == DBNull.Value ? "" : row["Callee"].ToString();
                model.Usercode = row["AgentId"] == DBNull.Value ? "" : row["AgentId"].ToString();
                model.StartDate = row["StartDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["StartDate"].ToString());
                model.GetInAgentDate = row["GetInAgentDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["GetInAgentDate"].ToString());
                model.EndDate = row["EndDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["EndDate"].ToString());
                model.RecordPath = row["RecordPath"] == DBNull.Value ? "" : row["RecordPath"].ToString();
                model.Username = row["Username"] == DBNull.Value ? "" : row["Username"].ToString();
                model.CallType = row["CallType"] == DBNull.Value ? 1 : Convert.ToInt32(row["CallType"].ToString());
                model.TalkTime = row["TalkTime"] == DBNull.Value ? 0 : Convert.ToInt32(row["TalkTime"]);
                model.QCState = row["QCState"] == DBNull.Value ? 0 : Convert.ToInt32(row["QCState"].ToString());
                model.QCScore = row["QCScore"] == DBNull.Value ? 0 : Convert.ToInt32(row["QCScore"].ToString());
                model.QCRemark = row["QCRemark"] == DBNull.Value ? "" : row["QCRemark"].ToString();
                model.QCUserId = row["QCUserId"] == DBNull.Value ? 0 : Convert.ToInt32(row["QCUserId"].ToString());
                model.QCUserCode = row["QCUserCode"] == DBNull.Value ? "" : row["QCUserCode"].ToString();
                model.QCUserName = row["QCUserName"] == DBNull.Value ? "" : row["QCUserName"].ToString();
                model.QCTime = row["QCTime"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["QCTime"].ToString());


            }
            return model;
        }
        public Model.CallResult GetModel(string callId)
        {
            string sql = "select itemid,RecordPath from CallResult where id=@callid";
            var ds = DbHelperSQL.Query(sql, new SqlParameter[] {
                new SqlParameter("@callid",callId)
            });
            var model = new Model.CallResult();
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    model.TaskId = ds.Tables[0].Rows[0]["itemid"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["itemid"]);
                    model.RecordPath = ds.Tables[0].Rows[0]["RecordPath"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["RecordPath"].ToString();
                }
            }
            return model;
        }
        public bool UpdateQC(Model.CallResult model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CallResult set ");
            strSql.Append("QCState=@QCState,");
            strSql.Append("QCScore=@QCScore,");
            strSql.Append("QCRemark=@QCRemark,");
            strSql.Append("QCUserId=@QCUserId,");
            strSql.Append("QCUserCode=@QCUserCode,");
            strSql.Append("QCUserName=@QCUserName,");
            strSql.Append("QCTime=@QCTime ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@QCState", SqlDbType.SmallInt,2),
                    new SqlParameter("@QCScore", SqlDbType.Decimal,9),
                    new SqlParameter("@QCRemark", SqlDbType.Text),
                    new SqlParameter("@QCUserId", SqlDbType.Int),
                    new SqlParameter("@QCUserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@QCTime", SqlDbType.DateTime),
                    new SqlParameter("@Id", SqlDbType.Int)};
            parameters[0].Value = model.QCState;
            parameters[1].Value = model.QCScore;
            parameters[2].Value = model.QCRemark;
            parameters[3].Value = model.QCUserId;
            parameters[4].Value = model.QCUserName;
            parameters[5].Value = model.QCTime;
            parameters[9].Value = model.Id;

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

        #endregion  ExtensionMethod
    }
}
