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

namespace CallCenterApi.Interface.Controllers.report
{
   // 
    public class SwitchedlossCallController : BaseController
    {
        //接通呼损统计
        //获取表头
        public ActionResult GetColumnList()
        {
                String[] str = { "日期", "接通次数", "呼损次数", "接通率", "呼损率" };
                return Success("获取接通呼损统计表头成功", str);
        }

        //获取数据
        public ActionResult GetDataList(string stime, string endtime)
        {
           
                DataTable dtNew = new DataTable();
                dtNew = GetData(stime, endtime);
                #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"));

                //dtNew.Columns.Add(dc1);
                //dtNew.Columns.Add(dc2);
                //dtNew.Columns.Add(dc3);
                //dtNew.Columns.Add(dc4);
                //dtNew.Columns.Add(dc5);
                //#endregion
                //string[] DataArr = GetStatisticalTime(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
                //string[] CallConnectData = GetStatisticalCallConnectData(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
                //string[] CallLossData = GetStatisticalCallLossData(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
                //string[] CallConnectDataRate = GetStatisticalCallConnectDataRate(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
                //string[] CallLossDataRate = GetStatisticalCallLossDataRate(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');

                //for (int i = 0; i < DataArr.Length; i++)
                //{
                //    DataRow drNew = dtNew.NewRow();

                //    drNew["日期"] = DataArr[i];
                //    drNew["接通次数"] = CallConnectData[i];
                //    drNew["呼损次数"] = CallLossData[i];
                //    drNew["接通率"] = CallConnectDataRate[i];
                //    drNew["呼损率"] = CallLossDataRate[i];

                //    dtNew.Rows.Add(drNew);
                //}
                #endregion
                return Success("获取接通呼损统计数据成功", dtNew);
       
        }

        //导出数据
        public ActionResult ExptList(string stime, string endtime)
        {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = GetData(stime, endtime);
                if (npoi.ExportToExcel("接通呼损数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
        }

        //获取数据源
        private DataTable GetData(string stime, string endtime)
        {
            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"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            #endregion
            string[] DataArr = GetStatisticalTime(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallConnectData = GetStatisticalCallConnectData(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallLossData = GetStatisticalCallLossData(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallConnectDataRate = GetStatisticalCallConnectDataRate(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallLossDataRate = GetStatisticalCallLossDataRate(stime, endtime).TrimStart('[').TrimEnd(']').Split(',');

            for (int i = 0; i < DataArr.Length; i++)
            {
                DataRow drNew = dtNew.NewRow();

                drNew["日期"] = DataArr[i];
                drNew["接通次数"] = CallConnectData[i];
                drNew["呼损次数"] = CallLossData[i];
                drNew["接通率"] = CallConnectDataRate[i];
                drNew["呼损率"] = CallLossDataRate[i];

                dtNew.Rows.Add(drNew);
            }
            dtNew.DefaultView.Sort = "日期 ASC";

            dtNew = dtNew.DefaultView.ToTable();
            return dtNew;
        }

        #region
        /// <summary>
        /// 获取highcharts统计中的时间轴坐标
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalTime(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (DictionaryEntry item in GetStatistical(BeginTime, EndTime))
            {
                //result = (JTHS)item.Value.ToString();
                result += item.Key.ToString().TrimStart('{').TrimEnd('}') + ",";
            }

            result = result.TrimEnd(',');
            return result + "]";
        }
        /// <summary>
        /// 获取highcharts统计中的接通次数的数据
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalCallConnectData(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (DictionaryEntry item in GetStatistical(BeginTime, EndTime))
            {
                result += ((JTHS)item.Value).CallConnectCount + ",";
            }

            result = result.TrimEnd(',');
            return result + "]";
        }
        /// <summary>
        /// 获取highcharts统计中呼损次数的数据
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalCallLossData(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (DictionaryEntry item in GetStatistical(BeginTime, EndTime))
            {
                result += ((JTHS)item.Value).CallLossCount + ",";
            }

            result = result.TrimEnd(',');
            return result + "]";
        }
        /// <summary>
        /// 获取highcharts统计中接通率数据
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalCallConnectDataRate(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (DictionaryEntry item in GetStatistical(BeginTime, EndTime))
            {
                result += ((JTHS)item.Value).CallConnectRate.ToString("F2") + ",";
            }

            result = result.TrimEnd(',');
            return result + "]";
        }

        /// <summary>
        /// 获取highcharts统计中呼损率数据
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalCallLossDataRate(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (DictionaryEntry item in GetStatistical(BeginTime, EndTime))
            {
                result += ((JTHS)item.Value).CallLossRate.ToString("F2") + ",";
            }

            result = result.TrimEnd(',');
            return result + "]";
        }
        /// <summary>
        /// 获取需要统计的数据
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public Hashtable GetStatistical(string BeginTime, string EndTime)
        {
            Hashtable tab = new Hashtable();


            DateTime beginTime;
            DateTime endTime;
            string sql = "";
            Dictionary<string, JTHS> myDic = new Dictionary<string, JTHS>();
            JTHS model = null;
            double callConnectRate = 0, callLossRate = 0;

            int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);


            if (BeginTime != null && BeginTime != "")
                beginTime = DateTime.Parse(BeginTime);
            else
                beginTime = DateTime.Parse(DateTime.Now.AddDays(-days).ToShortDateString());

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

            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 and calltype=0" + sql).Tables[0]; //BusinessType=0代表ivr中选择了转人工,calltype=1代表呼入



            for (DateTime t = beginTime; t <= endTime; t = t.AddDays(1))
            {
                string key = t.ToString("{yyyyMMdd}");
                int callConnectCount = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallState=1 and dealtype<>1").Length;
                int callLossCount = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallState=0 and dealtype=5").Length;

                if ((callConnectCount + callLossCount) != 0)
                {
                    callConnectRate = (callConnectCount + callLossCount) == 0 ? 0 : double.Parse((callConnectCount * 100).ToString()) / (callConnectCount + callLossCount);
                    callLossRate = 100 - callConnectRate;
                }
                else
                {
                    callConnectRate = 0;
                    callLossRate = 0;
                }

                if (!myDic.ContainsKey(key))
                {
                    model = new JTHS();
                    model.CallConnectCount = callConnectCount;
                    model.CallLossCount = callLossCount;
                    model.CallConnectRate = callConnectRate;
                    model.CallLossRate = callLossRate;

                    tab.Add(key, model);
                }
            }
            return tab;
        }
        #endregion
    }
    /// <summary>
    /// 接通呼损率统计辅助类
    /// </summary>
    [Serializable]
    public class JTHS
    {
        public int CallConnectCount
        {
            get;
            set;
        }
        public int CallLossCount
        {
            get;
            set;
        }
        public double CallConnectRate
        {
            get;
            set;
        }
        public double CallLossRate
        {
            get;
            set;
        }
    }
}