using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.report
{
    public class DaysTalkTimeController : BaseController
    {
        //获取日通话时长

        //获取表头
        public ActionResult GetColumnList()
        {
            
                String[] str = { "日期", "呼入次数", "呼入时长", "呼出次数", "呼出时长", "呼出未接通次数", "振铃时长", "通话总时长", "平均通话时长" };
                return Success("获取日通话时长统计表头成功", str);
        }

        //获取数据
        public ActionResult GetDataList(string stime, string etime,string usercode)
        {
           
                DataTable dtNew = new DataTable();
                dtNew = getData(stime, etime, usercode);
                #region
                //#region 编辑表头
                //DataColumn dc1 = new DataColumn("日期", Type.GetType("System.String"));
                //DataColumn dc2 = new DataColumn("呼入次数", Type.GetType("System.String"));
                //DataColumn dc3 = new DataColumn("呼入时长", Type.GetType("System.String"));
                //DataColumn dc4 = new DataColumn("呼出次数", Type.GetType("System.String"));
                //DataColumn dc5 = new DataColumn("呼出时长", Type.GetType("System.String"));
                //DataColumn dc6 = new DataColumn("呼出未接通次数", Type.GetType("System.String"));
                //DataColumn dc7 = new DataColumn("振铃时长", Type.GetType("System.String"));
                //DataColumn dc8 = new DataColumn("通话总时长", Type.GetType("System.String"));
                //DataColumn dc9 = new DataColumn("平均通话总时长", Type.GetType("System.String"));

                //dtNew.Columns.Add(dc1);
                //dtNew.Columns.Add(dc2);
                //dtNew.Columns.Add(dc3);
                //dtNew.Columns.Add(dc4);
                //dtNew.Columns.Add(dc5);
                //dtNew.Columns.Add(dc6);
                //dtNew.Columns.Add(dc7);
                //dtNew.Columns.Add(dc8);
                //dtNew.Columns.Add(dc9);
                //#endregion
                
                //DateTime beginTime;
                //DateTime endTime;
                //string sql = "";

                //int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
                //if (stime != null && stime != "")
                //    beginTime = DateTime.Parse(stime);
                //else
                //    beginTime = DateTime.Parse(DateTime.Now.AddDays(-days).ToShortDateString());

                //if (etime != null && etime != "")
                //    endTime = DateTime.Parse(etime + " 23:59:59");
                //else
                //    endTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59");

                //if (usercode != null && usercode != "")
                //{
                //    sql += " and UserCode='" + usercode + "'";
                //}

                //sql += " and DATEDIFF(d,BeginTime,'" + beginTime.ToString("yyyy-MM-dd HH:mm:ss") + "')<=0";
                //sql += " and DATEDIFF(d,BeginTime,'" + endTime.ToString("yyyy-MM-dd HH:mm:ss") + "')>=0";

                //DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 " + sql).Tables[0];
                
                //for (DateTime t = beginTime; t <= endTime; t = t.AddDays(1))
                //{
                //    string key = t.ToString("yyyyMMdd");
                //    DataRow[] drsin = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=0");
                //    DataRow[] drsout = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=1");
                //    DataRow[] drswout = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=1 and CallState=0 ");
                //    int callincount = drsin.Length;
                //    int calloutcount = drsout.Length;
                //    int callwoutcount = drswout.Length;
                //    string thsc = gettimes(dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' "), 0);
                //    string hrsc = gettimes(drsin, 0);
                //    string hcsc = gettimes(drsout, 0);
                //    string zlsc = gettimes(drswout, 1);
                //    DataRow drNew = dtNew.NewRow();

                //    drNew["日期"] = key;
                //    drNew["呼入次数"] = callincount;
                //    drNew["呼入时长"] = hrsc + "S";
                //    drNew["呼出次数"] = calloutcount;
                //    drNew["呼出时长"] = hcsc + "S";
                //    drNew["呼出未接通次数"] = callwoutcount;
                //    drNew["振铃时长"] = zlsc + "S";
                //    drNew["通话总时长"] = thsc + "S";
                //    if ((callincount + calloutcount) > 0)
                //        drNew["平均通话总时长"] = (double.Parse(thsc) / (callincount + calloutcount)).ToString("F2") + "S";
                //    else
                //        drNew["平均通话总时长"] = "0S";

                //    dtNew.Rows.Add(drNew);
                //}
                #endregion
                return Success("获取日通话时长数据成功", dtNew);
     
        }

        //导出数据
        public ActionResult ExptList(string stime, string etime, string usercode)
        {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = getData(stime, etime, usercode);
                if (npoi.ExportToExcel("日通话时长数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
        }

        private DataTable getData(string stime, string etime, string usercode)
        {
            DataTable dtNew = new DataTable();
            #region 编辑表头
            DataColumn dc1 = new DataColumn("日期", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("呼入次数", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("呼入时长", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("呼出次数", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("呼出时长", Type.GetType("System.String"));
            DataColumn dc6 = new DataColumn("呼出未接通次数", Type.GetType("System.String"));
            DataColumn dc7 = new DataColumn("振铃时长", Type.GetType("System.String"));
            DataColumn dc8 = new DataColumn("通话总时长", Type.GetType("System.String"));
            DataColumn dc9 = new DataColumn("平均通话总时长", Type.GetType("System.String"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            dtNew.Columns.Add(dc6);
            dtNew.Columns.Add(dc7);
            dtNew.Columns.Add(dc8);
            dtNew.Columns.Add(dc9);
            #endregion

            DateTime beginTime;
            DateTime endTime;
            string sql = "";

            int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            if (stime != null && stime != "")
                beginTime = DateTime.Parse(stime);
            else
                beginTime = DateTime.Parse(DateTime.Now.AddDays(-days).ToShortDateString());

            if (etime != null && etime != "")
                endTime = DateTime.Parse(etime + " 23:59:59");
            else
                endTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59");

            if (usercode != null && usercode != "")
            {
                sql += " and UserCode='" + usercode + "'";
            }

            sql += " and DATEDIFF(d,BeginTime,'" + beginTime.ToString("yyyy-MM-dd HH:mm:ss") + "')<=0";
            sql += " and DATEDIFF(d,BeginTime,'" + endTime.ToString("yyyy-MM-dd HH:mm:ss") + "')>=0";

            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 " + sql).Tables[0];

            for (DateTime t = beginTime; t <= endTime; t = t.AddDays(1))
            {
                string key = t.ToString("yyyyMMdd");
                DataRow[] drsin = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=0");
                DataRow[] drsout = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=1");
                DataRow[] drswout = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=1 and CallState=0 ");
                int callincount = drsin.Length;
                int calloutcount = drsout.Length;
                int callwoutcount = drswout.Length;
                string thsc = gettimes(dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' "), 0);
                string hrsc = gettimes(drsin, 0);
                string hcsc = gettimes(drsout, 0);
                string zlsc = gettimes(drswout, 1);
                DataRow drNew = dtNew.NewRow();

                drNew["日期"] = key;
                drNew["呼入次数"] = callincount;
                drNew["呼入时长"] = hrsc + "S";
                drNew["呼出次数"] = calloutcount;
                drNew["呼出时长"] = hcsc + "S";
                drNew["呼出未接通次数"] = callwoutcount;
                drNew["振铃时长"] = zlsc + "S";
                drNew["通话总时长"] = thsc + "S";
                if ((callincount + calloutcount) > 0)
                    drNew["平均通话总时长"] = (double.Parse(thsc) / (callincount + calloutcount)).ToString("F2") + "S";
                else
                    drNew["平均通话总时长"] = "0S";

                dtNew.Rows.Add(drNew);
            }
            return dtNew;
        }

        private string gettimes(DataRow[] drs,int type)
        {
            int zlsc = 0;int thsc = 0;
            for (int i = 0; i < drs.Length; i++)
            {
                if (drs[i]["TalkLongTime"].ToString() != "")
                    thsc += int.Parse(drs[i]["TalkLongTime"].ToString());
                if (type == 1)
                {
                    if (drs[i]["RingStartTime"].ToString() != "" && drs[i]["EndTime"].ToString() != "")
                    {
                        DateTime rstime = DateTime.Parse(drs[i]["RingStartTime"].ToString());
                        DateTime entime = DateTime.Parse(drs[i]["EndTime"].ToString());
                        TimeSpan ts = entime - rstime;
                        zlsc += ts.Seconds;
                    }
                }
            }
            if (type == 1)
                return zlsc.ToString();
            else
                return thsc.ToString();
        }
        
    }
}