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

using System.Web.SessionState;
using System.Text;
using System.Data;

namespace HySoft.BaseCallCenter.Web.reportmanage.operationdata.controls
{
    /// <summary>
    /// ZuoXiTongHua 的摘要说明
    /// </summary>
    public class ZuoXiTongHua : 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
            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;'>通话总时长（小时）</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
            {
                string txtStartTime = context.Request.QueryString["txtStartTime"];
                string txtEndTime = context.Request.QueryString["txtEndTime"];
                string sqltimeCallRecords = "";
                if (txtStartTime != null && txtStartTime.Trim() != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , TalkStartTime, 120)>=CONVERT(varchar , '" + txtStartTime.Trim() + " 00:00:01', 120) ";
                }
                if (txtEndTime != null && txtEndTime.Trim() != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , TalkStartTime, 120)<=CONVERT(varchar , '" + txtEndTime.Trim() + " 23:59:59', 120) ";
                }
                double zong1 = 0;
                double zong2 = 0;
                double zong3 = 0;
                double zong4 = 0;
                double zong5 = 0;
                double zong6 = 0;
                double day = 0;
                 Double sumshuchu = 0;
                Double sumshuru = 0;
                if (txtStartTime != null && txtEndTime != null && txtEndTime.Trim() != "" && txtStartTime.Trim() != "")
                {
                    TimeSpan  time = Convert.ToDateTime(txtEndTime) - Convert.ToDateTime(txtStartTime);
                    day = time.Days+1;
                }

                DataTable dt = new DataTable();
                string sql = "SELECT  *,(SELECT sum(TalkLongTime)  FROM T_Call_CallRecords where  UserId=T_Sys_UserAccount.F_Userid " + sqltimeCallRecords + ") as counts from T_Sys_UserAccount where F_RoleId in(1,28,29) order by counts desc ";
                dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sql = "SELECT (SELECT sum(TalkLongTime)  FROM T_Call_CallRecords where  UserId=" + dt.Rows[i]["F_UserId"].ToString() + "" + sqltimeCallRecords + ") as sums,";
                    sql += "(SELECT sum(TalkLongTime) FROM T_Call_CallRecords where  UserId=" + dt.Rows[i]["F_UserId"].ToString() + " and CallType=0" + sqltimeCallRecords + ") as sumshuru,";
                    sql += "(SELECT sum(TalkLongTime)  FROM T_Call_CallRecords where  UserId=" + dt.Rows[i]["F_UserId"].ToString() + " and CallType=1" + sqltimeCallRecords + ") as sumshuchu";
                    DataTable dt1 = new DataTable();
                    dt1 = DBUtility.DbHelperSQL.Query(sql).Tables[0];

                    if (dt1.Rows[0]["sums"] == null || dt1.Rows[0]["sums"].ToString() == "" || dt1.Rows[0]["sums"].ToString() == "0")
                    {
                        continue;
                    }

                    htmlReport.AppendFormat("<tr><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>", dt.Rows[i]["F_UserName"].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>", (Convert.ToDouble(dt1.Rows[0]["sums"]) / 3600).ToString("f2"));
                    Double sums = 0;
                    if (dt1.Rows[0]["sums"] != null && dt1.Rows[0]["sums"].ToString() != "")
                    {
                        zong1 += Convert.ToInt32(dt1.Rows[0]["sums"]);
                        sums = Convert.ToInt32(dt1.Rows[0]["sums"]);
                    }
                    if (dt1.Rows[0]["sumshuchu"] == null || dt1.Rows[0]["sumshuchu"].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>");
                    }
                    else
                    {
                        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>", (Convert.ToDouble(dt1.Rows[0]["sumshuchu"]) / 3600).ToString("f2"));
                    }
                    sumshuchu = 0;
                    if (dt1.Rows[0]["sumshuchu"] != null && dt1.Rows[0]["sumshuchu"].ToString() != "")
                    {
                        zong2 += Convert.ToInt32(dt1.Rows[0]["sumshuchu"]);
                        sumshuchu = Convert.ToInt32(dt1.Rows[0]["sumshuchu"]);
                    }
                    if (sums == 0)
                    {
                        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>", "0.00" + "%");
                    }
                    else
                    {
                        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>", (sumshuchu / sums * 100).ToString("f2") + "%");
                    }
                    if (dt1.Rows[0]["sumshuru"] == null || dt1.Rows[0]["sumshuru"].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>");
                    }
                    else
                    {
                        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>", (Convert.ToDouble(dt1.Rows[0]["sumshuru"]) / 3600).ToString("f2"));
                    }
                    sumshuru = 0;

                    if (dt1.Rows[0]["sumshuru"] != null && dt1.Rows[0]["sumshuru"].ToString() != "")
                    {
                        zong3 += Convert.ToInt32(dt1.Rows[0]["sumshuru"]);
                        sumshuru = Convert.ToInt32(dt1.Rows[0]["sumshuru"]);
                    }
                    if (sums == 0)
                    {
                        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>", "0.00" + "%");
                    }
                    else
                    {
                        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>", (sumshuru / sums * 100).ToString("f2") + "%");
                    }
                    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>", (sumshuru / day/3600).ToString("f2"));
                    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>", (sumshuchu / day/3600).ToString("f2"));
                    htmlReport.Append("</tr>");
                }
                htmlReport.Append("<tr height='35'>");
                htmlReport.AppendFormat("<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;'>{0}</td>", "总计");
                htmlReport.AppendFormat("<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;'>{0}</td>", (zong1 / 3600).ToString("f2"));
                htmlReport.AppendFormat("<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;'>{0}</td>", (zong2 / 3600).ToString("f2"));
                if (zong1 == 0 || zong2 == 0)
                {
                    htmlReport.AppendFormat("<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;'>0.00%</td>");
                }
                else
                {
                    htmlReport.AppendFormat("<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;'>{0}</td>", (Convert.ToDouble(zong2 / zong1) * 100).ToString("f2") + "%");
                }
                htmlReport.AppendFormat("<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;'>{0}</td>", (zong3 / 3600).ToString("f2"));
                if (zong1 == 0 || zong3 == 0)
                {
                    htmlReport.AppendFormat("<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;'>0.00%</td>");
                }
                else
                {
                    htmlReport.AppendFormat("<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;'>{0}</td>", (Convert.ToDouble(zong3 / zong1) * 100).ToString("f2") + "%");
                }
                htmlReport.AppendFormat("<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;'>{0}</td>", (zong3 / day/3600).ToString("f2"));
                htmlReport.AppendFormat("<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;'>{0}</td>", (zong2/day/3600).ToString("f2"));
                htmlReport.Append("</tr>");
                htmlReport.Append("</table>");
            }
            catch { }

            return htmlReport.ToString();
        }

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