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

namespace HySoft.BaseCallCenter.Web.qualitymanage.ajax
{
    /// <summary>
    /// QcResultList 的摘要说明
    /// </summary>
    public class QcResultList : IHttpHandler
    {
        List<string> arlist = new List<string>();
        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 rowspan='4' 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 rowspan='4' 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 rowspan='4' 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 rowspan='4' 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(GetColoms());
            

            try
            {
                string txtStartTime = context.Request.QueryString["txtStartTime"];
                string txtEndTime = context.Request.QueryString["txtEndTime"];
                string sqltimeCallRecords = "";
                string strsql = "";
                if (txtStartTime != null && txtStartTime.Trim() != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , BeginTime, 120)>=CONVERT(varchar , '" + txtStartTime.Trim() + " 00:00:01', 120) ";
                }
                if (txtEndTime != null && txtEndTime.Trim() != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , BeginTime, 120)<=CONVERT(varchar , '" + txtEndTime.Trim() + " 23:59:59', 120) ";
                }
                if (context.Request.QueryString["dpt"] != null && context.Request.QueryString["dpt"].ToString() != "")
                {
                    strsql = " and UserId in(select F_UserId from T_Sys_UserAccount where F_DeptId=" + context.Request.QueryString["dpt"].ToString() + ") ";
                }

                DataSet ds = DBUtility.DbHelperSQL.Query("select *,(select F_UserName from  T_Sys_UserAccount as b where b.F_UserId=a.UserId) as name,(select F_WorkNumber from  T_Sys_UserAccount as b where b.F_UserId=a.UserId) as wknmb from (select COUNT(*) as ct,UserId from T_Call_CallRecords where F_QCState=1 " + sqltimeCallRecords + strsql + " group by  UserId) as a order by wknmb asc");
                DataTable dtTitle=DBUtility.DbHelperSQL.Query("select F_IndexId from T_QC_IndexBase where F_DeleteFlag=0").Tables[0];

                if (ds != null && ds.Tables.Count > 0) 
                {
                    DataTable dt = ds.Tables[0];
                    string strname = "";
                    foreach (DataRow dr in dt.Rows) 
                    {
                        htmlReport.Append("<tr>");
                        object obj = DBUtility.DbHelperSQL.GetSingle("select F_QCUserName from T_Call_CallRecords where UserId=" + dr["UserId"] + " and F_QCState=1 "+sqltimeCallRecords);
                        if (obj != null)
                        {
                            strname = obj.ToString();
                        }
                        else 
                        {
                            strname = "";
                        }
                        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>",strname);
                        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>", dr["name"]);
                        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>", dr["wknmb"]);
                        int ct=0;
                        if(dr["ct"]!=null && dr["ct"].ToString()!="")
                        {
                          ct=int.Parse(dr["ct"].ToString());//评价数量
                        }
                        
                        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>", dr["ct"]);

                        double itemfs = 0;//每一大项分数和
                        double ctfs = 0;//总分
                        foreach (string strList in arlist) 
                        {
                            string[] strTitleList = strList.Split(',');
                            foreach (string str in strTitleList)
                            {
                                if (str != "")
                                {
                                    double fs = 0;//每一项分数
                                    obj = DBUtility.DbHelperSQL.GetSingle("select sum(F_QCScore) from T_QC_IndexRes where CallRecordsId in (select CallRecordsId from T_Call_CallRecords where F_QCState=1 and UserId=" + dr["UserId"] + " " + sqltimeCallRecords + ") and F_IndexId=" + str);
                                    if (obj != null && obj.ToString() != "")
                                    {
                                        if (ct != 0)
                                        {//评价数量不为0
                                            if (int.Parse(obj.ToString()) % ct == 0)
                                            {
                                                fs = int.Parse(obj.ToString()) / ct; //每一项分数
                                            }
                                            else 
                                            {
                                                fs = double.Parse(obj.ToString()) / double.Parse(ct.ToString());
                                            }
                                        }
                                    }
                                    string mm="";
                                    if(fs.ToString().IndexOf('.')>-1 && fs.ToString().Substring(fs.ToString().IndexOf('.')+1).Length>2)
                                    {
                                      mm=fs.ToString("0.00");
                                    }
                                    else
                                    {
                                      mm=fs.ToString();
                                    }
                                    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>",mm);
                                    itemfs += fs;
                                }
                            }
                            string imm = "";
                            if (itemfs.ToString().IndexOf('.') > -1 && itemfs.ToString().Substring(itemfs.ToString().IndexOf('.') + 1).Length > 2)
                            {
                                imm = itemfs.ToString("0.00");
                            }
                            else
                            {
                                imm = itemfs.ToString();
                            }


                            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>", imm.ToString());
                            ctfs += itemfs;
                            itemfs = 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}</td>",((int)ctfs).ToString());
                        ctfs = 0;

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

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

            return htmlReport.ToString();
        }


        private string GetColoms()
        {
            object obj = null;
            string strColom = "";
            string strColom1 = "";
            string strColom2 = "";
            string strColom3 = "";
            string strColom4 = "";
            DataSet ds = DBUtility.DbHelperSQL.Query("select F_CategoryId,F_CategoryName,F_ParentId from T_QC_IndexCategory where F_DeleteFlag=0 order by F_CategoryId asc");
            DataTable dsConten = DBUtility.DbHelperSQL.Query("select F_IndexId,F_Content,F_Title,F_Score,F_CategoryId from T_QC_IndexBase order by F_IndexId asc").Tables[0];
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                DataRow[] drlist = dt.Select("F_ParentId=0");
                if (drlist != null)
                {
                    int ctsc = 0;//总分
                    for (int i = 0; i < drlist.Length; i++)
                    {
                        obj = DBUtility.DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId in(select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + drlist[i]["F_CategoryId"] + ")");
                        strColom1 += "<td colspan='" + obj.ToString() + "' 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;'>" + drlist[i]["F_CategoryName"] + "</td>";
                        strColom1 += "<td rowspan='3' 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>";

                        DataRow[] drlistnew = dt.Select("F_ParentId=" + drlist[i]["F_CategoryId"]);
                        if (drlistnew != null && drlist.Length > 0)
                        {
                            int sc = 0;
                            string strCatid = "";
                            for (int n = 0; n < drlistnew.Length; n++)
                            {
                                obj = DBUtility.DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
                                strColom2 += "<td colspan='" + obj + "' 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;'>" + drlistnew[n]["F_CategoryName"] + "</td>";

                                DataRow[] drContent = dsConten.Select("F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
                                if (drContent != null && drContent.Length > 0)
                                {

                                    for (int m = 0; m < drContent.Length; m++)
                                    {
                                        strColom3 += "<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;'>" + drContent[m]["F_Title"] + "</td>";
                                        strColom4 += "<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;'>" + drContent[m]["F_Score"] + "</td>";
                                        if (!string.IsNullOrEmpty(drContent[m]["F_Score"].ToString()))
                                        {
                                            sc += int.Parse(drContent[m]["F_Score"].ToString());
                                        }

                                        strCatid += drContent[m]["F_IndexId"] + ",";

                                    }
                                    
                                }

                            }
                           
                            if (strCatid != "")
                            {
                                strCatid.Substring(0, strCatid.Length - 1);
                            }
                            arlist.Add(strCatid); //每一组的题id记下来，用在分数总和上。
                            strColom4 += "<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;'>" + sc.ToString() + "</td>";
                            ctsc += sc;
                        }


                    }
                    strColom4 += "<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;'>" + ctsc.ToString() + "</td>";

                }

            }

            strColom = strColom1 + "<td rowspan='3' 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></tr>";
            strColom += "<tr>" + strColom2 + "</tr>";
            strColom += "<tr>" + strColom3 + "</tr>";
            strColom += "<tr>" + strColom4 + "</tr>";

            return strColom;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }




    }
}