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

namespace HySoft.BaseCallCenter.Web.reportmanage
{
    public partial class callerdealreport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //  GetStatisticalTime("","");
            }
            else
            {
                ViewState["StatisticalData"] = null;
            }
        }

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

            DateTime beginTime;
            DateTime endTime;
            string sql = "";
            Dictionary<string, CallRecordWay> myDic = new Dictionary<string, CallRecordWay>();
            CallRecordWay model = null;

            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 CallType=0" + sql).Tables[0];



            for (DateTime t = beginTime; t <= endTime; t = t.AddDays(1))
            {
                string key = t.ToString("{yyyyMMdd}");

                if (!myDic.ContainsKey(key))
                {
                    model = new CallRecordWay();

                    model.IVR = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=0").Length;
                    model.Harassment = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=1").Length;
                    model.SelfService = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=2").Length;
                    model.TurnDutyPhone = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=3").Length;
                    model.LeaveVoice = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=4").Length;
                    model.CallLoss = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=5").Length;
                    model.ManualDeal = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=6").Length;
                    myDic.Add(key, model);
                }
            }
            ViewState["StatisticalData"] = myDic;
            return myDic;
        }

        /// <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统计中IVR处理的数量
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalIVRDeal(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).IVR + ",";
            }

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

        /// <summary>
        /// 获取highcharts统计中骚扰电话处理的数量
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalHarassment(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).Harassment + ",";
            }

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

        /// <summary>
        /// 获取highcharts统计中自助服务的数量
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalSelfService(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).SelfService + ",";
            }

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

        /// <summary>
        /// 获取highcharts统计中转值班的数量
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalTurnDutyPhone(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).TurnDutyPhone + ",";
            }

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

        /// <summary>
        /// 获取highcharts统计中留言的数量
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalLeaveVoice(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).LeaveVoice + ",";
            }

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

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

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).CallLoss + ",";
            }

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

        /// <summary>
        /// 获取highcharts统计中转人工的数量
        /// </summary>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public string GetStatisticalManualDeal(string BeginTime, string EndTime)
        {
            string result = "[";

            foreach (var item in GetStatistical(BeginTime, EndTime))
            {
                result += (item.Value).ManualDeal + ",";
            }

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

        /// <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>
        /// 获取表格统计数据
        /// </summary>
        /// <param name="SB"></param>
        /// <param name="BeginTime"></param>
        /// <param name="EndTime"></param>
        public void GetTabelData(StringBuilder SB, string BeginTime, string EndTime)
        {
            string[] DataTimeArr = GetStatisticalTime(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataIVRDeal = GetStatisticalIVRDeal(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataHarassment = GetStatisticalHarassment(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataSelfService = GetStatisticalSelfService(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataTurnDutyPhone = GetStatisticalTurnDutyPhone(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataLeaveVoice = GetStatisticalLeaveVoice(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataCallLoss = GetStatisticalCallLoss(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            string[] DataManualDeal = GetStatisticalManualDeal(BeginTime, EndTime).TrimStart('[').TrimEnd(']').Split(',');
            for (int i = 0; i < DataTimeArr.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;\">" + DataTimeArr[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;\">" + DataIVRDeal[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;\">" + DataHarassment[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;\">" + DataSelfService[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;\">" + DataTurnDutyPhone[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;\">" + DataLeaveVoice[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;\">" + DataCallLoss[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;\">" + DataManualDeal[i] + "</td>");
                SB.Append("</tr>");
            }

        }

        /// <summary>
        /// 电话处理方式辅助类
        /// </summary>
        [Serializable]
        public class CallRecordWay
        {
            /// <summary>
            /// IVR处理方式 0
            /// </summary>
            public int IVR
            {
                get;
                set;
            }

            /// <summary>
            /// 骚扰电话 1
            /// </summary>
            public int Harassment
            {
                set;
                get;
            }

            /// <summary>
            /// 自助服务 2
            /// </summary>
            public int SelfService
            {
                get;
                set;
            }

            /// <summary>
            /// 转值班电话 3
            /// </summary>
            public int TurnDutyPhone
            {
                get;
                set;
            }

            /// <summary>
            /// 留言 4
            /// </summary>
            public int LeaveVoice
            {
                get;
                set;
            }


            /// <summary>
            /// 呼损 5
            /// </summary>
            public int CallLoss
            {
                set;
                get;
            }

            /// <summary>
            /// 人工处理 6
            /// </summary>
            public int ManualDeal
            {
                get;
                set;
            }

        }
    }
}
