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 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;
        }
        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
