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

namespace HySoft.BaseCallCenter.Web.reportmanage.dataanalysis
{
    public partial class CallInDept : System.Web.UI.Page
    {
        protected string TableHtml = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            OutputTable();
        }
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            OutputTable();
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string fileName = "客服部来电分布情况报表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
            Response.ContentType = "application/ms-excel";
            Response.Write(TableHtml);
            Response.End();
        }
        private void OutputTable()
        {
            //查询条件
            string starttime = txtStartTime.Text.Trim();
            if (string.IsNullOrWhiteSpace(starttime))
            { starttime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd"); txtStartTime.Text = starttime; }
            string endtime = txtEndTime.Text.Trim();
            if (string.IsNullOrWhiteSpace(endtime)) { endtime = DateTime.Today.ToString("yyyy-MM-dd"); txtEndTime.Text = endtime; }

            string[] head = { "客服部", "来电请求量", "占比" };
            TableHtml = createTable("客服部来电分布", head, starttime, endtime);
        }
        private string createTable(string title, string[] head, string starttime, string endtime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(createTableTitle(title));
            sb.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" align='center'>");
            sb.Append(createTableHead(head));
            sb.Append(createTableBody(starttime, endtime));
            sb.Append("</table>");
            return sb.ToString();
        }
        /// <summary>
        /// 表名称
        /// </summary>
        /// <param name="title">表名</param>
        /// <returns></returns>
        protected string createTableTitle(string title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<h1 style='font-size: 18px; font-weight: bold; color: #333333; text-align: center;width: 100%;'><b>" + title + "</b></h1>");
            return sb.ToString();
        }
        private string createTableHead(string[] head)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr style=\"text-align: center;\">");
            for (int i = 0; i < head.Length; i++)
            {
                sb.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;\">" + head[i] + "</td>");
            }
            sb.Append("</tr>");
            return sb.ToString();
        }
        private string createTableBody(string starttime, string endtime)
        {
            string sql = "";
            if (starttime != null && starttime.Trim() != "")
            {
                sql += " and CONVERT(varchar , F_CREATEDATE, 120)>=CONVERT(varchar , '" + starttime.Trim() + " 00:00:01', 120)  ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                sql += "  and CONVERT(varchar , F_CREATEDATE, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            //存储数据
            Dictionary<string, List<string>> body = new Dictionary<string, List<string>>();
            //总数量
            int allcount = 0;
            var deptlist = new BLL.T_Sys_Department().GetModelList("");
            if (deptlist.Count > 0)
            {
                foreach (var deptname in deptlist)
                {
                    string sqlcount = "SELECT COUNT(*) FROM [T_Wo_WorkOrderBase] WHERE [F_STANDARDADDRESS]='一线技术员' AND F_WORKORDERSTATEID=0 AND F_WORKORDERTYPEID=16 AND F_CUSTOMERNAME IN (SELECT [F_CustomerName] FROM [T_Cus_CustomerBase] WHERE F_ServiceDept='" + deptname.F_DeptName + "')" + sql + "";
                    DataTable dt = DBUtility.DbHelperSQL.Query(sqlcount).Tables[0];
                    if (dt != null)
                    {
                        int count = 0;
                        int.TryParse(dt.Rows[0][0].ToString(), out count);
                        if (count > 0)
                        {
                            allcount = allcount + count;
                            body.Add(deptname.F_DeptName, new List<string>() { count.ToString(), "" });
                            dt.Dispose();
                        }
                    }
                }
            }
            //打印表格
            StringBuilder sb = new StringBuilder();
            var dicSort = from objDic in body orderby int.Parse(objDic.Value[0]) descending select objDic;
            foreach (var kv in dicSort)
            {
                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;\">" + kv.Key + "</td>");
                List<string> bodyitem = kv.Value;
                bodyitem[1] = "0.00%";
                if (allcount > 0) bodyitem[1] = (int.Parse(bodyitem[0]) * 100.0 / allcount).ToString("0.00") + "%";
                foreach (var v in bodyitem)
                {
                    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;\">" + v + "</td>");
                }
                sb.Append("</tr>");
            }
            //打印合计数据
            List<string> sum = new List<string>() { "合计：", allcount.ToString(), "100%" };
            sb.Append("<tr style=\"text-align: center;\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\" onmouseout=\"this.style.backgroundColor='#ffffff'\">");
            for (int j = 0; j < sum.Count; j++)
            {
                sb.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;\">" + sum[j] + "</td>");
            }
            sb.Append("</tr>");
            return sb.ToString();
        }
    }
}