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>
    /// TowUserGZL 的摘要说明
    /// </summary>
    public class TowUserGZL : 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 sqltimeTask = "";
                if (txtStartTime != null && txtStartTime.Trim() != "")
                {
                    sqltimeTask += " and CONVERT(varchar , F_CREATEDATE, 120)>=CONVERT(varchar , '" + txtStartTime.Trim() + " 00:00:01', 120)  ";
                }
                if (txtEndTime != null && txtEndTime.Trim() != "")
                {
                    sqltimeTask += "  and CONVERT(varchar , F_CREATEDATE, 120)<=CONVERT(varchar , '" + txtEndTime.Trim() + " 23:59:59', 120) ";
                }
                double zong1 = 0;
                double zong2 = 0;
                double zong3 = 0;
                double zong4 = 0;

                double ESTIMATED = 0;
                double DECLARATION = 0;
                double EXAMION = 0;

                DataTable dt = new DataTable();
                string sql = "SELECT  *,(select COUNT(*) from T_Wo_WorkOrderTask where F_WORKORDERSTATEID=6 and F_OWNERID=T_Sys_UserAccount.F_UserId " + sqltimeTask + ") as counts from T_Sys_UserAccount where F_RoleId=31 order by counts desc ";
                dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ESTIMATED = 0;
                    DECLARATION = 0;
                    EXAMION = 0;
                    sql = " select (select COUNT(*) from T_Wo_WorkOrderTask where F_WORKORDERSTATEID=6 and F_OWNERID=" + dt.Rows[i]["F_UserId"].ToString() + "" + sqltimeTask + ") as counts,";
                    sql += "(select sum(convert(float ,F_ESTIMATED)) from T_Wo_WorkOrderHistoryInfo where F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where F_WORKORDERSTATEID=6 and F_OWNERID=" + dt.Rows[i]["F_UserId"].ToString() + "" + sqltimeTask + ") and F_ESTIMATED is not null) as ESTIMATED,";
                    sql += " (select sum(convert(float ,F_DECLARATION)) from T_Wo_WorkOrderHistoryInfo where F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where F_WORKORDERSTATEID=6 and F_OWNERID=" + dt.Rows[i]["F_UserId"].ToString() + "" + sqltimeTask + ") and F_DECLARATION is not null) as DECLARATION,";
                    sql += " (select sum(convert(float ,F_EXAMION)) from T_Wo_WorkOrderHistoryInfo where F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where F_WORKORDERSTATEID=6 and F_OWNERID=" + dt.Rows[i]["F_UserId"].ToString() + "" + sqltimeTask + ") and F_EXAMION is not null) as EXAMION ";
                    DataTable dt1 = new DataTable();
                    dt1 = DBUtility.DbHelperSQL.Query(sql).Tables[0];

                    if (dt1.Rows[0]["counts"] == null || dt1.Rows[0]["counts"].ToString() == "" || dt1.Rows[0]["counts"].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>", dt1.Rows[0]["counts"].ToString());
                    zong1 += Convert.ToDouble(dt1.Rows[0]["counts"]);
                    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>", dt1.Rows[0]["ESTIMATED"].ToString());
                    if (dt1.Rows[0]["ESTIMATED"] != null && dt1.Rows[0]["ESTIMATED"].ToString() != "")
                    {
                        zong2 += Convert.ToDouble(dt1.Rows[0]["ESTIMATED"]);
                        ESTIMATED = Convert.ToDouble(dt1.Rows[0]["ESTIMATED"]);
                    }
                    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>", dt1.Rows[0]["DECLARATION"].ToString() + "&nbsp");
                    if (dt1.Rows[0]["DECLARATION"] != null && dt1.Rows[0]["DECLARATION"].ToString() != "")
                    {
                        zong3 += Convert.ToDouble(dt1.Rows[0]["DECLARATION"]);
                        DECLARATION = Convert.ToDouble(dt1.Rows[0]["DECLARATION"]);
                    }
                    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>", dt1.Rows[0]["EXAMION"].ToString());
                    if (dt1.Rows[0]["EXAMION"] != null && dt1.Rows[0]["EXAMION"].ToString() != "")
                    {
                        zong4 += Convert.ToDouble(dt1.Rows[0]["EXAMION"]);
                        EXAMION = Convert.ToDouble(dt1.Rows[0]["EXAMION"]);
                    }
                    if (DECLARATION != 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>", (ESTIMATED / DECLARATION * 100).ToString("f2") + "%");
                    }
                    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>", "0.00" + "%");
                    }
                    if (EXAMION != 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>", (ESTIMATED / EXAMION * 100).ToString("f2") + "%");
                    }
                    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>", "0.00" + "%");
                    }
                    if (DECLARATION != 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>", (EXAMION / DECLARATION * 100).ToString("f2") + "%");
                    }
                    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>", "0.00" + "%");
                    }
                 
                    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);
                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);
                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);
                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>", zong4);
                if (zong3 == 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 / zong3) * 100).ToString("f2") + "%");
                }
                if ( zong4 == 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 / zong4) * 100).ToString("f2") + "%");
                }
                if ( zong4 == 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(zong4 / zong3) * 100).ToString("f2") + "%");
                }
                htmlReport.Append("</tr>");
                htmlReport.Append("</table>");
            }
            catch { }

            return htmlReport.ToString();
        }

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