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

namespace HySoft.BaseCallCenter.Web.reportmanage.operationdata.controls
{
    /// <summary>
    /// YearContrast 的摘要说明
    /// </summary>
    public class YearContrast : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request.QueryString["action"];

            switch (action)
            {
                case "getlist":
                    context.Response.Write(GetDataListHtml(context));//读取数据
                    break;
            }
        }
        public string GetDataListHtml(HttpContext context)
        {
            StringBuilder htmlReport = new StringBuilder();//存储生成的HTML
            string txtStartTime = context.Request.QueryString["txtStartTime"];
            string txtEndTime = context.Request.QueryString["txtEndTime"];
            htmlReport.Append("<table cellspacing='0' cellpadding='0' border='1' width='100%'><tr height='35'>");
            htmlReport.Append("<td style='height: 18px; 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;'></td>");
            htmlReport.Append("<td style='height: 18px; 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;'>"+txtStartTime+"年度</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>"+txtStartTime+"年度</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>"+txtEndTime+"年度</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>" + txtEndTime + "年度</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>"+txtStartTime+"年度</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>"+txtEndTime+"年度</td>");
            htmlReport.Append("</tr>");
            htmlReport.Append("<tr height='35'>");
            htmlReport.Append("<td style='height: 18px; 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;'></td>");
            htmlReport.Append("<td style='height: 18px; 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;'>客户呼叫数</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>座席接听数</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>客户呼叫数</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>座席接听数</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>接通率</td>");
            htmlReport.Append("<td style='height: 18px; 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;'>接通率</td>");
            htmlReport.Append("</tr>");

            try
            {
               
                DataTable dt = new DataTable();
                for (int i = 1; i <= 12; i++)
                {
                    string sqltimeCallRecords = "";
                    int ibgnCt = 0;//总呼叫数
                    int ibgnjt = 0;//总接听数
                    double ibgnjtl = 0.00;//接通率
                    int iendct = 0;//对比呼叫数
                    int iendjt = 0;//对比接听数
                    double iendjtl = 0.00;//结束接通率
                    string strmm = i.ToString();
                    if (i <= 9)
                    {
                        strmm = "0" + i;
                    }
                    if (txtStartTime != null && txtStartTime.Trim() != "")
                    {
                        sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)>=CONVERT(varchar , '" + txtStartTime.Trim() + "-" + strmm + "-01 00:00:01', 120) ";
                        sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)<=CONVERT(varchar , '" + txtStartTime.Trim() + "-" + strmm + "-31 23:59:59', 120) ";
                    }

                    dt = DBUtility.DbHelperSQL.Query("select COUNT(*) as ct,(select COUNT(*) from rep_trunk_call_in where PeriodTalking>0 " + sqltimeCallRecords + ") as jt from rep_trunk_call_in  where 1=1 " + sqltimeCallRecords).Tables[0];
                    htmlReport.AppendFormat("<tr>");
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}月份</td>", i.ToString());
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["ct"].ToString() != "")
                        {
                            ibgnCt = int.Parse(dt.Rows[0]["ct"].ToString());
                        }
                        if (dt.Rows[0]["jt"].ToString() != "")
                        {
                            ibgnjt = int.Parse(dt.Rows[0]["jt"].ToString());
                        }
                    }
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}</td>", ibgnCt.ToString());
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}</td>", ibgnjt.ToString());

                    if (txtEndTime != null && txtEndTime.Trim() != "")
                    {
                        sqltimeCallRecords = " and CONVERT(varchar , timecallin, 120)>=CONVERT(varchar , '" + txtEndTime.Trim() + "-" + strmm + "-01 00:00:01', 120) ";
                        sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)<=CONVERT(varchar , '" + txtEndTime.Trim() + "-" + strmm + "-31 23:59:59', 120) ";
                    }
                    dt = DBUtility.DbHelperSQL.Query("select COUNT(*) as ct,(select COUNT(*) from rep_trunk_call_in where PeriodTalking>0 " + sqltimeCallRecords + ") as jt from rep_trunk_call_in  where 1=1 " + sqltimeCallRecords).Tables[0];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["ct"].ToString() != "")
                        {
                            iendct = int.Parse(dt.Rows[0]["ct"].ToString());
                        }
                        if (dt.Rows[0]["jt"].ToString() != "")
                        {
                            iendjt = int.Parse(dt.Rows[0]["jt"].ToString());
                        }
                    }
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}</td>", iendct);
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}</td>", iendjt);

                    if (ibgnCt != 0)
                    {
                        ibgnjtl = ibgnjt / Convert.ToDouble(ibgnCt);
                    }
                    if (iendct != 0)
                    {
                        iendjtl = iendjt / Convert.ToDouble(iendct);
                    }
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}</td>", (ibgnjtl*100).ToString("0.00") + "%");
                    htmlReport.AppendFormat("<td  align='center'style='border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 2px; height: 18px; padding-top: 3px; padding-bottom: 3px;'>{0}</td>", (iendjtl*100).ToString("0.00") + "%");


                    htmlReport.Append("</tr>");
                }
                htmlReport.Append("</table>");
            }
            catch { }

            return htmlReport.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}