using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace HySoft.BaseCallCenter.Web.reportmanage
{
    public partial class switchedlosscallreport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] == "show")
            {
                string str = GetStatisticalTime("","") + "|" + GetStatisticalCallConnectData("", "") + "|" + GetStatisticalCallLossData("", "") + "|" + GetStatisticalCallLossDataRate("", "") + "|" + GetStatisticalCallConnectDataRate("", "");
                Response.Write(str);
                Response.End();
            }

            if (!this.IsPostBack)
            {
            }
            else
            {
                ViewState["StatisticalData"] = null;
            }
        }


        /// <summary>
        /// 生成表格的标题和统计字段
        /// </summary>
        /// <param name="TableTitle">表格的标题</param>
        /// <param name="TableHead">表格统计字段</param>
        /// <returns></returns>
        public StringBuilder CreateTableTitleAndHead(string TableTitle, string BeginTime, string EndTime, params object[] TableHead)
        {
            StringBuilder html = new StringBuilder();
            html.Append("<h1 style='font-size: 18px;font-weight: bold;color: #333333;text-align:center;width:100%;'><b>" + TableTitle + "</b></h1>");
            html.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" align='center'>");
            html.Append("<tr style=\"text-align: center;\">");
            for (int i = 0; i < TableHead.Length; i++)
            {
                //标题第一行
                html.Append("<td class=\"formtabletitle1\"style=\"height: 25px;background-color: #D5EDFE;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + TableHead[i] + "</td>");
            }
            html.Append("</tr>");
            GetTabelData(html, BeginTime, EndTime);
            html.Append("</table>");
            return html;
        }
        /// <summary>
        /// 获取highcharts统计中的时间轴坐标
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalTime(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += item.Key.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 (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (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 (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (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 (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (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 (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).CallLossRate.ToString("F2") + ",";
            }

            result = result.TrimEnd(',');
            return result + "]";
        }
        /// <summary>
        /// 获取需要统计的数据
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public Dictionary<string, JTHS> GetStatistical(string BeginTime, string EndTime)
        {
            if (ViewState["StatisticalData"] != null)
            {
                return (Dictionary<string, JTHS>)ViewState["StatisticalData"];
            }

            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 != "")
                beginTime = DateTime.Parse(BeginTime);
            else
                beginTime = DateTime.Parse(DateTime.Now.AddDays(-days).ToShortDateString());

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

            sql += " and DATEDIFF(d,BeginTime,'" + beginTime + "')<=0";
            sql += " and DATEDIFF(d,BeginTime,'" + endTime + "')>=0";

            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 and BusinessType=0 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").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;

                    myDic.Add(key, model);
                }
            }
            ViewState["StatisticalData"] = myDic;
            return myDic;
        }
        /// <summary>
        /// 获取表格统计数据
        /// </summary>
        /// <param name="SB"></param>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        public void GetTabelData(StringBuilder SB, string BeginTime, string EndTime)
        {
            string[] DataArr = GetStatisticalTime(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallConnectData = GetStatisticalCallConnectData(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallLossData = GetStatisticalCallLossData(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallConnectDataRate = GetStatisticalCallConnectDataRate(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] CallLossDataRate = GetStatisticalCallLossDataRate(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');

            for (int i = 0; i < DataArr.Length; i++)
            {
                SB.Append("<tr style=\"text-align: center;\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\" onmouseout=\"this.style.backgroundColor='#ffffff'\">");
                SB.Append("<td class=\"formtabletitle1\" style=\"height: 25px;background-color: #fff;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + DataArr[i] + "</td>");
                SB.Append("<td class=\"formtabletitle1\"style=\"height: 25px;background-color: #fff;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + CallConnectData[i] + "</td>");
                SB.Append("<td class=\"formtabletitle1\"style=\"height: 25px;background-color: #fff;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + CallLossData[i] + "</td>");
                SB.Append("<td class=\"formtabletitle1\"style=\"height: 25px;background-color: #fff;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + CallConnectDataRate[i] + "%</td>");
                SB.Append("<td class=\"formtabletitle1\"style=\"height: 25px;background-color: #fff;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + CallLossDataRate[i] + "%</td>");
                SB.Append("</tr>");
            }

        }
        /// <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;
            }
        }
    }
}