using RMYY_CallCenter_Api.DB;
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers.Quality
{
    public class QCResultController : BaseController
    {
        //质检报表

        List<string> arlist = new List<string>();
        DataTable dtNew = new DataTable();
        //获取表头
        public ActionResult GetColumnList(string isext)
        {

            List<Model.QCModel> qclist = new List<Model.QCModel>();

            #region 加载固定项
            DataColumn dc1 = new DataColumn("QCName", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("UserName", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("UserCode", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("ZJCount", Type.GetType("System.String"));
            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            Model.QCModel qcm1 = new Model.QCModel();
            qcm1.Qcid = "cpr1";
            qcm1.Qcname = "测评人";
            qcm1.Rowspan = "4";
            Model.QCModel qcm2 = new Model.QCModel();
            qcm2.Qcid = "xm2";
            qcm2.Qcname = "姓名";
            qcm2.Rowspan = "4";
            Model.QCModel qcm3 = new Model.QCModel();
            qcm3.Qcid = "gh3";
            qcm3.Qcname = "工号";
            qcm3.Rowspan = "4";
            Model.QCModel qcm4 = new Model.QCModel();
            qcm4.Qcid = "zjts4";
            qcm4.Qcname = "质检条数";
            qcm4.Rowspan = "4";
            qclist.Add(qcm1);
            qclist.Add(qcm2);
            qclist.Add(qcm3);
            qclist.Add(qcm4);
            #endregion
            Model.QCModel qcmzf = new Model.QCModel();
            qcmzf.Qcid = "zf999";
            qcmzf.Qcname = "总分";
            qcmzf.Rowspan = "3";

            object obj = null;

            DataSet ds = DbHelperSQL.Query("select F_CategoryId,F_CategoryName,F_ParentId from T_QC_IndexCategory where F_DeleteFlag=0 order by F_CategoryId asc");
            DataTable dsConten = 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 = DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId =" + drlist[i]["F_CategoryId"] + "");
                        Model.QCModel qcml = new Model.QCModel();
                        qcml.Qcid = drlist[i]["F_CategoryId"].ToString();
                        qcml.Qcname = drlist[i]["F_CategoryName"].ToString();
                        qcml.Colspan = obj.ToString();
                        DataRow[] drlistnew = dt.Select("F_CategoryId=" + drlist[i]["F_CategoryId"]);
                        if (drlistnew != null && drlist.Length > 0)
                        {
                            List<Model.QCModel> qcclist = new List<Model.QCModel>();
                            int sc = 0;
                            string strCatid = "";
                            for (int n = 0; n < drlistnew.Length; n++)
                            {
                                Model.QCModel qccml = new Model.QCModel();
                                obj = DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
                                qccml.Qcid = drlist[i]["F_CategoryId"].ToString() + "-" + drlistnew[n]["F_CategoryId"].ToString();
                                qccml.Qcname = drlistnew[n]["F_CategoryName"].ToString();
                                qccml.Colspan = obj.ToString();

                                DataRow[] drContent = dsConten.Select("F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
                                if (drContent != null && drContent.Length > 0)
                                {
                                    List<Model.QCModel> qccllist = new List<Model.QCModel>();
                                    for (int m = 0; m < drContent.Length; m++)
                                    {
                                        Model.QCModel qccmll = new Model.QCModel();
                                        qccmll.Qcid = drlist[i]["F_CategoryId"].ToString() + "-" + drlistnew[n]["F_CategoryId"].ToString() + "-" + drContent[m]["F_IndexId"].ToString();
                                        qccmll.Qcname = drContent[m]["F_Title"].ToString();
                                        qccmll.Qcscore = drContent[m]["F_Score"].ToString();
                                        if (!string.IsNullOrEmpty(drContent[m]["F_Score"].ToString()))
                                        {
                                            sc += int.Parse(drContent[m]["F_Score"].ToString());
                                        }

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

                                        DataColumn dc = new DataColumn("Q" + drContent[m]["F_IndexId"].ToString(), Type.GetType("System.String"));
                                        dtNew.Columns.Add(dc);

                                        qccllist.Add(qccmll);
                                    }

                                    qccml.Qclist = qccllist;
                                }
                                qcclist.Add(qccml);
                            }

                            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;


                            qcml.Qclist = qcclist;
                            qclist.Add(qcml);

                            Model.QCModel qcml1 = new Model.QCModel();
                            qcml1.Qcid = "fx" + drlist[i]["F_CategoryId"].ToString();
                            qcml1.Qcname = "分项得分";
                            qcml1.Rowspan = "3";
                            qcml1.Qcscore = sc.ToString();
                            qclist.Add(qcml1);
                            DataColumn dcdf = new DataColumn("df" + i.ToString(), Type.GetType("System.String"));
                            dtNew.Columns.Add(dcdf);
                        }

                    }

                    qcmzf.Qcscore = ctsc.ToString();
                }

            }

            qclist.Add(qcmzf);
            DataColumn dczf = new DataColumn("AllScore", Type.GetType("System.String"));
            dtNew.Columns.Add(dczf);
            if (isext != null && isext.Trim() != "")
            {
                //导出增加显示问题和建议
                Model.QCModel qcmlzj1 = new Model.QCModel();
                Model.QCModel qcmlzj2 = new Model.QCModel();
                qcmlzj1.Qcid = "wt99";
                qcmlzj1.Qcname = "问题";
                qcmlzj1.Rowspan = "4";
                qclist.Add(qcmlzj1);
                qcmlzj2.Qcid = "jy99";
                qcmlzj2.Qcname = "建议";
                qcmlzj2.Rowspan = "4";
                qclist.Add(qcmlzj2);
                DataColumn dcwt = new DataColumn("Question", Type.GetType("System.String"));
                dtNew.Columns.Add(dcwt);
                DataColumn dcjy = new DataColumn("Advise", Type.GetType("System.String"));
                dtNew.Columns.Add(dcjy);
            }

            return Success("获取质检表头成功", qclist);


        }

        //获取数据
        public ActionResult GetDataList(string stime, string endtime)
        {

            Model.PageData<object> pagedata = new Model.PageData<object>();

            string dpt = RequestString.GetQueryString("dpt");
            string isext = RequestString.GetQueryString("isext");

            GetColumnList(isext);

            string sqltimeCallRecords = "";
            string strsql = "";
            if (stime != null && stime.Trim() != "")
            {
                sqltimeCallRecords += " and CONVERT(varchar , BeginTime, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                sqltimeCallRecords += " and CONVERT(varchar , BeginTime, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            if (dpt != null && dpt.ToString() != "")
            {
                strsql = " and UserCode in(select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dpt.ToString() + ") ";
            }

            DataSet ds = DbHelperSQL.Query("select *,(select F_UserName from  T_Sys_UserAccount as b where b.F_UserCode=a.UserCode) as name,(select F_UserCode from  T_Sys_UserAccount as b where b.F_UserCode=a.UserCode) as wknmb from (select COUNT(*) as ct,UserCode from T_Call_CallRecords where F_QCState=1 " + sqltimeCallRecords + strsql + " group by  UserCode) as a order by wknmb asc");
            DataTable dtTitle = 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)
                {
                    DataRow drNew = dtNew.NewRow();

                    object obj = DbHelperSQL.GetSingle("select F_QCUserName from T_Call_CallRecords where UserCode='" + dr["wknmb"] + "' and F_QCState=1 " + sqltimeCallRecords);
                    if (obj != null)
                    {
                        strname = obj.ToString();
                    }
                    else
                    {
                        strname = "";
                    }
                    drNew["QCName"] = strname;
                    drNew["UserName"] = dr["name"].ToString();
                    drNew["UserCode"] = dr["wknmb"].ToString();

                    int ct = 0;
                    if (dr["ct"] != null && dr["ct"].ToString() != "")
                    {
                        ct = int.Parse(dr["ct"].ToString());//评价数量
                    }
                    drNew["ZJCount"] = dr["ct"].ToString();

                    double itemfs = 0;//每一大项分数和
                    double ctfs = 0;//总分

                    for (int i = 0; i < arlist.Count; i++)
                    {
                        string strList = arlist[i];
                        string[] strTitleList = strList.Split(',');
                        foreach (string str in strTitleList)
                        {
                            if (str != "")
                            {
                                double fs = 0;//每一项分数
                                obj = DbHelperSQL.GetSingle("select sum(F_QCScore) from T_QC_IndexRes where CallRecordsId in (select CallRecordsId from T_Call_CallRecords where F_QCState=1 and UserCode='" + dr["wknmb"] + "'" + " " + 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();
                                }
                                drNew["Q" + str] = 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();
                        }
                        drNew["df" + i.ToString()] = imm;
                        ctfs += itemfs;
                        itemfs = 0;
                    }
                    drNew["AllScore"] = ((int)ctfs).ToString();
                    if (isext != null && isext.Trim() != "")
                    {
                        string question = "";
                        string advise = "";
                        DataSet dss = DbHelperSQL.Query("select * from T_Call_CallRecords where UserCode='" + dr["wknmb"] + "'" + " and F_QCState=1 " + sqltimeCallRecords);
                        if (dss.Tables[0].Rows.Count > 0)
                        {
                            for (int ii = 0; ii < dss.Tables[0].Rows.Count; ii++)
                            {
                                question += dss.Tables[0].Rows[ii]["F_QCQuestion"].ToString() + ";";
                                advise += dss.Tables[0].Rows[ii]["F_QCAdvise"].ToString() + ";";
                            }
                        }
                        drNew["Question"] = question;
                        drNew["Advise"] = advise;
                    }
                    ctfs = 0;

                    dtNew.Rows.Add(drNew);
                }
            }

            return Success("获取质检数据成功", dtNew);


        }
    }
}