using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.report
{
    public class ZuoXiWorkTotalController : BaseController
    {
        private readonly BLL.T_Sys_UserAccount userAccountBLL = new BLL.T_Sys_UserAccount();
        private readonly BLL.T_Sys_Department deptBLL = new BLL.T_Sys_Department();
        private List<string> str = new List<string>() { "公司","分类","渠道名称", "坐席工号", "坐席姓名", "角色", "固话号码",
                             "呼入电话数", "接通量", "接通率", "呼入通话分钟数",
                             "呼出电话数", "呼通量", "呼通率", "呼出通话分钟数",
                             "坐席登录次数", "登录时长分钟数", "工作时长分钟数", "平均每天工作小时数", "休息时长分钟数", "质检平均成绩"
            };

        //坐席工作统计
        //获取表头
        public ActionResult GetColumnList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");


            res = Success("获取坐席工作统计表头成功", str);

            return res;
        }

        //获取数据
        public ActionResult GetDataList(string stime, string endtime, string dpt, string dayss, string usercode = "", string extensionnumber = "")
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            DataTable dtNew = new DataTable();
            List<int> indexs = new List<int>();
            dtNew = getData(stime, endtime, dpt, dayss, usercode, extensionnumber, indexs);
            res = Success("获取坐席工作统计数据成功", dtNew);

            return res;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="stime">开始时间</param>
        /// <param name="endtime">结束时间</param>
        /// <param name="dpt">部门</param>
        /// <param name="dayss">天数</param>
        /// <param name="indexs">未选中导出的字段索引数组</param>
        /// <returns></returns>
        public ActionResult ExptList(string stime, string endtime, string dpt, string dayss, string indexstr = "")
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                List<int> indexs = new List<int>();
                if (!string.IsNullOrWhiteSpace(indexstr))
                    indexs.AddRange(indexstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(x => Convert.ToInt32(x)));
                DataTable dt = getData(stime, endtime, dpt, dayss, "", "", indexs);
                if (npoi.ExportToExcel("坐席工作数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private DataTable getData(string stime, string endtime, string dpt, string dayss, string _usercode = "", string extensionnumber = "", List<int> indexs = null)
        {
            DataTable dtNew = new DataTable();
            var columns = dtNew.Columns;
            #region


            for (int i = 0; i < str.Count(); i++)
            {
                if (!indexs.Contains(i))
                    dtNew.Columns.Add(new DataColumn(str[i]));
            }

            #endregion
            int days = 30;
            string sqltimeCallRecords = "";
            string strsqlcall = "";
            string strCallOutSql = "";
            if (stime != null && stime.Trim() != "")
            {
                sqltimeCallRecords += $"  and TimeAlerting>='{stime.Trim()} 00:00:01' ";
                strsqlcall += $"  and  BeginTime>='{stime.Trim()} 00:00:01' ";
                strCallOutSql += $"  and TimeRingBack>='{stime.Trim()} 00:00:01'  ";
                //sqltimeCallRecords += " and CONVERT(varchar , TimeAlerting, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
                //strsqlcall += " and CONVERT(varchar , BeginTime, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                sqltimeCallRecords += $"  and  TimeAlerting<='{endtime.Trim()} 23:59:59'  ";
                strsqlcall += $"  and  BeginTime<= '{endtime.Trim()} 23:59:59'  ";
                strCallOutSql += $"  and  TimeRingBack<='{endtime.Trim()} 23:59:59'   ";
                //sqltimeCallRecords += " and CONVERT(varchar , TimeAlerting, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
                //strsqlcall += " and CONVERT(varchar , BeginTime, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            if (dayss != null && dayss.Trim() != "")
            {
                days = int.Parse(dayss.Trim());
            }
            #region
            int ijtcs = 0;//接听通话次数 
            int ithms = 0;//通话秒数
            int izlms = 0;//振铃秒数
            int ipjjtms = 0;//平均接听秒数
            int ipjzlms = 0;//平均振铃秒数
            double ihrzyl = 0.00;//呼入占有率
            int izxdlcs = 0;//坐席登录次数
            int idlsc = 0;//登录时长
            int iWorktime = 0;//工作时长
            int izmcs = 0;//置忙次数
            int ixxsc = 0;//休息时长
            int ipjczsc = 0;//平均操作时长
            int ipjzmxxsc = 0;//平均置忙休息时长


            int itrunkct = 0;//总数
            int itrunkjj = 0;//总接听
            int itrunkhs = 0;//呼损量
            int itrunkpjsc = 0;//平均排队时间
            double itrunkjtl = 0.00;//接通率
            double itrunkhsl = 0.00;//互损率
            double itrunkpjpd = 0.00;//平均排队

            var callOutCount = 0; //呼出总次数
            var callOutJTCount = 0;//呼出接通总次数
            var callOutJTRate = 0.00;//呼出接通率
            var callOutJTSeconds = 0;//呼出接通秒数
            var callOutJTMinutes = 0;//呼出接通分钟数


            #endregion

            //17=系统管理员,36=客服,37=组长,38=组员,39=业主,40=话务员,41=客服主管,43=分公司市场部主任,45=业务稽核,46=集团客户部,
            //47 =网络部,48=市场部经理,50=业务支撑中心,51=市场部,52=客户服务中心,53=总经理,54=副总经理,55=业务负责人,56=投诉处理员,57=基础业务主管,


            var deptId = CurrentUser.UserData.F_DeptId;
            var roleId = CurrentUser.UserData.F_RoleId;
            var deptCode = CurrentUser.UserData.F_DeptCode;
            var usercode = CurrentUser.UserData.F_UserCode;
            var deptSQL = "";
            var usercodeSQL = "";
            int[] deptArr = new int[] { 1, 4, 19, 20, 21 };
            int[] roleArr = new int[] { 50, 53, 54, 59 };
            if (roleId != 0)
            {
                if (roleId != 17)
                {
                    if (roleId == 36 || roleId == 38 || roleId == 40)
                    {
                        usercodeSQL += $" and F_UserCode='{usercode}'";
                    }
                    else if (!deptArr.Contains(deptId))
                    {
                        deptSQL += $" and t1.F_DeptId={deptId} ";
                    }
                    else
                    {
                        if (!roleArr.Contains(roleId))
                        {
                            deptSQL += $" and t1.F_DeptId={deptId} ";
                        }

                    }

                }
            }
            if (!string.IsNullOrWhiteSpace(_usercode))
            {
                usercodeSQL += $" and F_UserCode='{_usercode}' ";
            }
            if (!string.IsNullOrWhiteSpace(extensionnumber))
            {
                usercodeSQL += $" and F_ExtensionNumber like '%{extensionnumber}%'";
            }
            DataTable dtdpt = null;
            var deptModel = deptBLL.GetModel(!string.IsNullOrWhiteSpace(dpt) ? Convert.ToInt32(dpt) : 0);
            if (deptModel != null)
            {
                dtdpt = DbHelperSQL.Query($"select t1.F_DeptId,t1.F_DeptName,t1.F_Remark,t2.F_DeptName as ParentName from  T_Sys_Department t1 left join T_Sys_Department t2 ON t1.F_ParentId=t2.F_DeptId where t1.F_ParentId!=0 and t1.F_DeptCode like '{deptModel.F_DeptCode}%'  {deptSQL}").Tables[0];
            }
            else
            {
                dtdpt = DbHelperSQL.Query("select t1.F_DeptId,t1.F_DeptName,t1.F_Remark,t2.F_DeptName as ParentName from  T_Sys_Department t1 left join T_Sys_Department t2 ON t1.F_ParentId=t2.F_DeptId where t1.F_ParentId!=0" + deptSQL).Tables[0];
            }

            string sql = $"SELECT F_UserCode,F_UserName,F_WorkNumber,F_ExtensionNumber,F_UserId,F_DeptId,T_Sys_RoleInfo.F_RoleName FROM T_Sys_UserAccount left join T_Sys_RoleInfo on T_Sys_RoleInfo.F_RoleId=T_Sys_UserAccount.F_RoleId where f_seatFlag=1 and  F_WorkNumber!='' {usercodeSQL} order by  F_WorkNumber asc";
            var userAccountList = DbHelperSQL.Query(sql).Tables[0];



            string strsql = "";
            if (stime != null && stime.Trim() != "")
            {
                strsql += " and CONVERT(varchar , TimeCallIn, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                strsql += " and CONVERT(varchar , TimeCallIn, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            var callInDt1 = DbHelperSQL.Query($"select AgentID, COUNT(*) as ct, SUM(DATEDIFF(Second, timewantagent, timeconnected)) as dd from rep_trunk_call_in where 1=1 {strsql}  group by AgentID").Tables[0];
            var callInDt2 = DbHelperSQL.Query($"select userid,UserCode,CallState,CallType from T_Call_CallRecords where  1=1 {strsqlcall} ").Tables[0];
            var callInDt3 = DbHelperSQL.Query($"select CalleeAgentID,COUNT(*) as ct,SUM(PeriodTalking) as sc,SUM(PeriodAlerting) as pa from rep_ext_call_in  where  1=1  {sqltimeCallRecords} group by CalleeAgentID").Tables[0];
            var callOutDt1 = DbHelperSQL.Query($"select CallerAgentID,count(CallID) as ct,SUM(PeriodTalking) as st from rep_ext_call_out  where 1=1 {strCallOutSql} group by  CallerAgentID ").Tables[0];
            var callOutDt2 = DbHelperSQL.Query($"select CallerAgentID,count(CallID) as ct,SUM(PeriodTalking) as st from rep_ext_call_out  where 1=1  and IsCallOutSucceed=1  {strCallOutSql} group by  CallerAgentID ").Tables[0];
            var loginDt = DbHelperSQL.Query($"select AgentId,COUNT(*) as dl,SUM(LoginTimes) as dlsc,SUM(reposenum) as zm,SUM(ReposeTimes) as xx from rep_agent_detail  where  1=1 {sqltimeCallRecords.Replace("TimeAlerting", "TimeLogin")} group by AgentId").Tables[0];



            var depts = deptBLL.DataTableToListForLevel(DbHelperSQL.Query($"select F_DeptId,F_DeptName,F_ParentId,F_DeptCode,F_Layer,0 lvl from T_Sys_Department").Tables[0]);



            foreach (DataRow dr in dtdpt.Rows)
            {
                //string sql = $"SELECT F_UserCode,F_UserName,F_WorkNumber,F_ExtensionNumber,F_UserId,T_Sys_RoleInfo.F_RoleName FROM T_Sys_UserAccount left join T_Sys_RoleInfo on T_Sys_RoleInfo.F_RoleId=T_Sys_UserAccount.F_RoleId where f_seatFlag=1 and F_DeptId={dr["F_DeptId"]} and F_WorkNumber!='' {usercodeSQL} order by  F_WorkNumber asc";
                //dt = DbHelperSQL.Query(sql).Tables[0];
                var deptList = deptBLL.DataTableToListForLevel(DbHelperSQL.Query($"with cte(F_DeptId,F_DeptName,F_ParentId,F_DeptCode,F_Layer,lvl) as( select F_DeptId,F_DeptName,F_ParentId,F_DeptCode,F_Layer,0 lvl from T_Sys_Department where F_DeptId={dr["F_DeptId"]} union all select dept.F_DeptId,dept.F_DeptName,dept.F_ParentId,dept.F_DeptCode,dept.F_Layer,lvl+1 from T_Sys_Department dept inner join cte on dept.F_DeptId = cte.F_ParentId) select F_DeptId,F_ParentId,F_DeptName,F_DeptCode,F_Layer,lvl from  cte ").Tables[0]);
                if (Convert.ToInt32(dr["F_DeptId"]) == 8)
                {

                }
                var deptCompany = deptList.Where(x => x.Layer == 1).OrderBy(x => x.Level).FirstOrDefault();
                var deptChannel = deptList.Where(x => x.DeptId == Convert.ToInt32(dr["F_DeptId"]) && x.Layer == 3).OrderBy(x => x.Level).FirstOrDefault();
                var deptCategory = deptList.Where(x => x.Layer == 2 && deptChannel?.ParentId == x.DeptId).OrderBy(x => x.Level).FirstOrDefault();

                var arr = userAccountList.Select($"F_DeptId={dr["F_DeptId"]}");

                for (int i = 0; i < arr.Length; i++)
                {
                    if (!string.IsNullOrEmpty(arr[i]["F_WorkNumber"].ToString()))
                    {
                        DataRow drNew = dtNew.NewRow();
                        if (columns.Contains("公司")) { drNew["公司"] = deptCompany?.DeptName ?? ""; }
                        if (columns.Contains("分类")) { drNew["分类"] = deptCategory?.DeptName ?? ""; }
                        if (columns.Contains("渠道名称")) { drNew["渠道名称"] = deptChannel?.DeptName ?? ""; }
                        if (columns.Contains("坐席工号")) { drNew["坐席工号"] = $"{arr[i]["F_WorkNumber"].ToString()}"; }
                        if (columns.Contains("坐席姓名")) { drNew["坐席姓名"] = $"{arr[i]["F_UserName"].ToString()}"; }
                        if (columns.Contains("角色")) { drNew["角色"] = arr[i]["F_RoleName"].ToString(); }
                        if (columns.Contains("固话号码")) { drNew["固话号码"] = arr[i]["F_ExtensionNumber"].ToString(); }

                        #region 呼入

                        //string strsql = "";
                        //if (stime != null && stime.Trim() != "")
                        //{
                        //    strsql += " and CONVERT(varchar , TimeCallIn, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
                        //}
                        //if (endtime != null && endtime.Trim() != "")
                        //{
                        //    strsql += " and CONVERT(varchar , TimeCallIn, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
                        //}

                        //ct=总呼入/呼出次数？   dd=总等待时长
                        //DataTable dtTrunk = DbHelperSQL.Query("select COUNT(*) as ct,SUM(DATEDIFF(Second,timewantagent,timeconnected)) as dd from rep_trunk_call_in where AgentID='" + arr[i]["F_UserCode"].ToString() + "' " + strsql).Tables[0];
                        //ct=总呼入次数？   jt=总呼入接通数
                        //DataTable dtcall = DbHelperSQL.Query("select COUNT(*) as ct,(select COUNT(*) from T_Call_CallRecords where CallState=1 and CallType=0 and  UserCode='" + arr[i]["F_UserCode"].ToString() + "' " + strsqlcall + ") as jt from T_Call_CallRecords where CallType=0 and UserCode='" + arr[i]["F_UserCode"].ToString() + "' " + strsqlcall).Tables[0];

                        var dtTrunk = callInDt1.Select($"AgentID={arr[i]["F_UserCode"] ?? ""}");
                        var dtcallJt = callInDt2.Select($" CallState=1  and calltype=0  and UserCode='{arr[i]["F_UserCode"] ?? ""}' ");
                        var dtcallCt = callInDt2.Select($" calltype=0  and UserCode='{arr[i]["F_UserCode"] ?? ""}' ");
                        //if (dtcall != null && dtcall.Rows.Count > 0)
                        //{
                        //if (dtcall.Rows[0]["ct"].ToString() != "")
                        //{
                        //    //ct=次数
                        //    itrunkct = int.Parse(dtcall.Rows[0]["ct"].ToString());
                        itrunkct = dtcallCt.Length;
                        //}
                        //else
                        //{
                        //    //ct=次数
                        //    itrunkct = 0;
                        //}
                        //if (dtcall.Rows[0]["jt"].ToString() != "")
                        //{
                        //    //jt = 总呼入接通数
                        //    itrunkjj = int.Parse(dtcall.Rows[0]["jt"].ToString());
                        itrunkjj = dtcallJt.Length;
                        //}
                        //else
                        //{
                        //    //jt = 总呼入接通数
                        //    itrunkjj = 0;
                        //}
                        //if (dtTrunk.Rows.Count > 0 && dtTrunk.Rows[0]["dd"].ToString() != "")
                        //{
                        //    //dd=总等待时长
                        //    itrunkpjsc = int.Parse(dtTrunk.Rows[0]["dd"].ToString());
                        itrunkpjsc = dtTrunk.Length > 0 && dtTrunk[0]["dd"] != DBNull.Value ? Convert.ToInt32(dtTrunk[0]["dd"]) : 0;
                        //}
                        //else
                        //{
                        //    //dd=总等待时长
                        //    itrunkpjsc = 0;
                        //}
                        //}
                        // 总呼入次数-总呼入接通数=0
                        itrunkhs = itrunkct - itrunkjj;
                        if (itrunkct != 0)
                        {
                            //呼入接通率 = 总呼入接通数/总呼入次数
                            itrunkjtl = itrunkjj / double.Parse(itrunkct.ToString());
                            //呼入呼损率 = (总呼入次数-总呼入接通数)/总呼叫次数
                            itrunkhsl = itrunkhs / double.Parse(itrunkct.ToString());
                            //平均排队时长 = 总等待时长/总呼入次数
                            itrunkpjpd = itrunkpjsc / double.Parse(itrunkct.ToString());
                        }
                        else
                        {
                            itrunkjtl = 0.00;
                            itrunkhsl = 0.00;
                            itrunkpjpd = 0.00;
                        }
                        if (columns.Contains("呼入电话数")) { drNew["呼入电话数"] = itrunkct; }
                        if (columns.Contains("接通量")) { drNew["接通量"] = itrunkjj; }
                        if (columns.Contains("接通率")) { drNew["接通率"] = (itrunkjtl * 100).ToString("0.00") + "%"; }


                        //-- ct=总呼入次数？  sc=总通话时长   pa=总xxx
                        //DataTable dtext = DbHelperSQL.Query("select COUNT(*) as ct,SUM(PeriodTalking) as sc,SUM(PeriodAlerting) as pa from rep_ext_call_in where CalleeAgentID=" + arr[i]["F_WorkNumber"] + " " + sqltimeCallRecords).Tables[0];
                        //-- ct=总呼入接通次数？  sc=总通话时长
                        //DataTable dtCallEvey = DbHelperSQL.Query("select COUNT(*) as ct,SUM(TalkLongTime) as sc from T_Call_CallRecords where CallState=1 and  userid=" + arr[i]["F_UserId"] + " " + strsqlcall).Tables[0];

                        var dtCallEvey = callInDt3.Select($"CalleeAgentID={arr[i]["F_UserCode"] ?? ""}");
                        //if (dtCallEvey != null && dtCallEvey.Rows.Count > 0)
                        //{
                        //    if (dtCallEvey.Rows[0]["ct"].ToString() != "")
                        //    {
                        //        //-- ct=总呼入接通次数
                        //        ijtcs = int.Parse(dtCallEvey.Rows[0]["ct"].ToString());//接听次数
                        ijtcs = itrunkjj;//接听次数
                        //    }
                        //    else
                        //    {
                        //        //-- ct=总呼入接通次数
                        //        ijtcs = 0;
                        //    }
                        //    if (dtCallEvey.Rows[0]["sc"].ToString() != "")
                        //    {
                        //        //sc = 总通话时长
                        //        ithms = int.Parse(dtCallEvey.Rows[0]["sc"].ToString());//通话秒数
                        ithms = dtCallEvey.Length > 0 && dtCallEvey[0]["sc"] != DBNull.Value ? Convert.ToInt32(dtCallEvey[0]["sc"]) : 0;
                        //    }
                        //    else
                        //    {
                        //        //sc = 总通话时长
                        //        ithms = 0;
                        //    }
                        //    if (dtext.Rows[0]["pa"].ToString() != "")
                        //    {
                        //        //pa=总振铃秒数
                        //        izlms = int.Parse(dtext.Rows[0]["pa"].ToString());//振铃秒数
                        izlms = dtCallEvey.Length > 0 && dtCallEvey[0]["pa"] != DBNull.Value ? Convert.ToInt32(dtCallEvey[0]["pa"]) : 0;
                        //    }
                        //    else
                        //    {
                        //        //pa=总振铃秒数
                        //        izlms = 0;
                        //    }
                        //}
                        if (columns.Contains("呼入通话分钟数")) { drNew["呼入通话分钟数"] = (ithms / 60).ToString(); }

                        #endregion

                        #region 呼出

                        //--ct=呼出总次数   st=呼出总通话时长
                        //DataTable dtCallOut = DbHelperSQL.Query($"select count(CallID) as ct,SUM(PeriodTalking) as st from rep_ext_call_out where IsCallOutSucceed = 1 and CallerAgentID ={arr[i]["F_WorkNumber"].ToString()} {strCallOutSql}").Tables[0];
                        //-- ct=呼出接通次数
                        //DataTable dtCallOut2 = DbHelperSQL.Query($"select count(CallID) as ct from rep_ext_call_out where IsCallOutSucceed=1 and CallerAgentID={arr[i]["F_WorkNumber"].ToString()} {strCallOutSql}  and IsCallOutSucceed=1 ").Tables[0];
                        var dtCallOut = callOutDt1.Select($"CallerAgentID={arr[i]["F_WorkNumber"].ToString()}");
                        var dtCallOut2 = callOutDt2.Select($"CallerAgentID={arr[i]["F_WorkNumber"].ToString()}");

                        //if (dtCallOut.Rows.Count > 0)
                        //{
                        //    callOutCount = dtCallOut.Rows[0]["ct"] != DBNull.Value ? Convert.ToInt32(dtCallOut.Rows[0]["ct"]) : 0;
                        //    callOutJTSeconds = dtCallOut.Rows[0]["st"] != DBNull.Value ? Convert.ToInt32(dtCallOut.Rows[0]["st"]) : 0;
                        callOutCount = dtCallOut.Length > 0 && dtCallOut[0]["ct"] != DBNull.Value ? Convert.ToInt32(dtCallOut[0]["ct"]) : 0;
                        callOutJTSeconds = dtCallOut.Length > 0 && dtCallOut[0]["st"] != DBNull.Value ? Convert.ToInt32(dtCallOut[0]["st"]) : 0;
                        //}
                        //if (dtCallOut2.Rows.Count > 0)
                        //{
                        //    callOutJTCount = dtCallOut2.Rows[0]["ct"] != DBNull.Value ? Convert.ToInt32(dtCallOut2.Rows[0]["ct"]) : 0;
                        callOutJTCount = dtCallOut2.Length > 0 && dtCallOut2[0]["ct"] != DBNull.Value ? Convert.ToInt32(dtCallOut2[0]["ct"]) : 0;
                        //}

                        if (callOutCount != 0)
                        {
                            if (columns.Contains("呼出电话数")) { drNew["呼出电话数"] = callOutCount; }
                            if (columns.Contains("呼通量")) { drNew["呼通量"] = callOutJTCount; }
                            if (columns.Contains("呼通率")) { drNew["呼通率"] = ((double)callOutJTCount / callOutCount * 100).ToString("0.00") + "%"; }
                            if (columns.Contains("呼出通话分钟数")) { drNew["呼出通话分钟数"] = (callOutJTSeconds / 60).ToString(); }
                        }
                        else
                        {
                            if (columns.Contains("呼出电话数")) { drNew["呼出电话数"] = 0; }
                            if (columns.Contains("呼通量")) { drNew["呼通量"] = 0; }
                            if (columns.Contains("呼通率")) { drNew["呼通率"] = "0%"; }
                            if (columns.Contains("呼出通话分钟数")) { drNew["呼出通话分钟数"] = 0; }
                        }

                        #endregion

                        #region 登录

                        #endregion
                        //坐席登录次数
                        //DataTable dtAgent = DbHelperSQL.Query("select COUNT(*) as dl,SUM(LoginTimes) as dlsc,SUM(reposenum) as zm,SUM(ReposeTimes) as xx from rep_agent_detail where AgentId=" + arr[i]["F_WorkNumber"] + " " + sqltimeCallRecords.Replace("TimeAlerting", "TimeLogin")).Tables[0];
                        var dtAgent = loginDt.Select($"AgentId={arr[i]["F_WorkNumber"]}");

                        //if (dtAgent != null && dtAgent.Rows.Count > 0)
                        //{
                        //    if (dtAgent.Rows[0]["dl"] != null && dtAgent.Rows[0]["dl"].ToString() != "")
                        //    {
                        //        izxdlcs = int.Parse(dtAgent.Rows[0]["dl"].ToString());//登录次数
                        izxdlcs = dtAgent.Length > 0 && dtAgent[0]["dl"] != DBNull.Value ? Convert.ToInt32(dtAgent[0]["dl"]) : 0;
                        //    }
                        //    else
                        //    {
                        //        izxdlcs = 0;
                        //    }
                        //    if (dtAgent.Rows[0]["dlsc"] != null && dtAgent.Rows[0]["dlsc"].ToString() != "")
                        //    {
                        //        idlsc = int.Parse(dtAgent.Rows[0]["dlsc"].ToString());//登录时长
                        idlsc = dtAgent.Length > 0 && dtAgent[0]["dlsc"] != DBNull.Value ? Convert.ToInt32(dtAgent[0]["dlsc"]) : 0;
                        //    }
                        //    else
                        //    {
                        //        idlsc = 0;
                        //    }
                        //    if (dtAgent.Rows[0]["zm"] != null && dtAgent.Rows[0]["zm"].ToString() != "")
                        //    {
                        //        izmcs = int.Parse(dtAgent.Rows[0]["zm"].ToString());//置忙次数
                        izmcs = dtAgent.Length > 0 && dtAgent[0]["zm"] != DBNull.Value ? Convert.ToInt32(dtAgent[0]["zm"]) : 0;
                        //    }
                        //    else
                        //    {
                        //        izmcs = 0;
                        //    }
                        //    if (dtAgent.Rows[0]["xx"] != null && dtAgent.Rows[0]["xx"].ToString() != "")
                        //    {
                        //        ixxsc = int.Parse(dtAgent.Rows[0]["xx"].ToString());//休息时长
                        ixxsc = dtAgent.Length > 0 && dtAgent[0]["xx"] != DBNull.Value ? Convert.ToInt32(dtAgent[0]["xx"]) : 0;
                        //    }
                        //    else
                        //    {
                        //        ixxsc = 0;
                        //    }

                        //}
                        iWorktime = idlsc - ixxsc;//工作时长
                        if (izxdlcs != 0)
                        {
                            ipjczsc = iWorktime / izxdlcs;//平均操作秒数
                        }
                        else
                        {
                            ipjczsc = 0;
                        }
                        if (izmcs != 0)
                        {
                            ipjzmxxsc = ixxsc / izmcs;//置忙平均操作时长
                        }
                        else
                        {
                            ipjzmxxsc = 0;
                        }

                        //呼入占有率
                        if (iWorktime != 0)
                        {
                            ihrzyl = ithms / Convert.ToDouble(iWorktime);
                        }
                        else
                        {
                            ihrzyl = 0.00;
                        }
                        //drNew["呼入占有率"] = (ihrzyl * 100).ToString("0.00") + "%";
                        //用户评价

                        //object obj = DbHelperSQL.GetSingle("select count(*) from T_Call_CallRecords where MYD is not null and CallState=1 and userid=" + arr[i]["F_UserId"] + strsqlcall);
                        //string myd = "";
                        //if (obj != null)
                        //{
                        //    myd = obj.ToString();
                        //}
                        if (columns.Contains("坐席登录次数")) { drNew["坐席登录次数"] = izxdlcs.ToString(); }
                        if (columns.Contains("登录时长分钟数")) { drNew["登录时长分钟数"] = (idlsc / 60).ToString(); }
                        if (columns.Contains("工作时长分钟数")) { drNew["工作时长分钟数"] = (iWorktime / 60).ToString(); }
                        if (columns.Contains("平均每天工作小时数")) { drNew["平均每天工作小时数"] = Math.Round((double.Parse(iWorktime.ToString()) / 60 / 60 / days), 2).ToString(); }
                        if (columns.Contains("休息时长分钟数")) { drNew["休息时长分钟数"] = (ixxsc / 60).ToString(); }
                        //DataSet dszj = DbHelperSQL.Query("select convert(int,SUM(f_qcscore)) as fs,COUNT(*) as fsct from T_Call_CallRecords where F_QCState=1 and UserId=" + arr[i]["F_UserId"] + strsqlcall);
                        int zfs = 0;
                        //if (dszj != null && dszj.Tables.Count > 0)
                        //{
                        //    DataTable dtzj = dszj.Tables[0];
                        //    if (dtzj.Rows.Count > 0 && dtzj.Rows[0]["fs"].ToString() != "" && dtzj.Rows[0]["fsct"].ToString() != "")
                        //    {
                        //        zfs = Convert.ToInt32(dtzj.Rows[0]["fs"].ToString()) / int.Parse(dtzj.Rows[0]["fsct"].ToString());
                        //    }
                        //}
                        if (columns.Contains("质检平均成绩"))
                        {
                            drNew["质检平均成绩"] = zfs.ToString();
                        }
                        dtNew.Rows.Add(drNew);
                    }
                }
            }
            //}
            return dtNew;
        }
    }
}