using CallCenter.Utility;
using CallCenterApi.Common;
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.tel
{
    public class CallrecordsController : BaseController
    {
        //获取通话记录列表
        public ActionResult GetList(string phone, string usercode)
        {
            //ActionResult res = NoToken("未知错误，请重新登录");
            //if (Request.IsAuthenticated)
            //{              

            //}
            //return res;
            string sql = "";
            int roleid = 0, deptid = 0;
            string currcode = string.Empty;
            string deptcode = string.Empty;
            DataTable dt = new DataTable();
            if (Request.IsAuthenticated)
            {
                roleid = CurrentUser.UserData.F_RoleId;
                currcode = CurrentUser.UserData.F_UserCode;
                deptid = CurrentUser.UserData.F_DeptId;
                deptcode = CurrentUser.UserData.F_DeptCode;
            }
            string callstate = HttpUtility.UrlDecode(RequestString.GetQueryString("callstate"));
            string calltype = HttpUtility.UrlDecode(RequestString.GetQueryString("calltype"));
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string dept = HttpUtility.UrlDecode(RequestString.GetQueryString("dept"));
            string depttype = HttpUtility.UrlDecode(RequestString.GetQueryString("depttype"));
            
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            //if (roleid != 0)
            //{
            //    if (roleid != 17)
            //    {
            //        if (deptcode.IndexOf("|533|") > 0)
            //        {
            //            sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + deptid + ")";
            //        }
            //        if (deptcode.IndexOf("|531|") > 0)
            //        {
            //            if (roleid == 32)
            //            {
            //                usercode = CurrentUser.UserData.F_UserCode;
            //                sql += " and UserCode='" + usercode + "'";
            //            }
            //            else
            //            {
            //                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptID in (select F_DeptId from T_Sys_Department where F_DeptCode like '" + deptcode + "%')) ";
            //            }
            //        }
            //    }
            //}
            ////20180515 坐席能看到同组的 
            //var usergroup = new BLL.T_Sys_UserAccount().GetModel(currcode) == null ? "" : new BLL.T_Sys_UserAccount().GetModel(currcode).F_SeartGroup;
            //var usercodes = new BLL.T_Sys_UserAccount().GetModelList("F_SeartGroup='" + usergroup + "'") == null ? null : new BLL.T_Sys_UserAccount().GetModelList("F_SeartGroup='" + usergroup + "'").Select(p => p.F_UserCode);
            //var str = "";
            //if (usercodes != null)
            //{
            //    foreach (var item in usercodes)
            //    {
            //        str += item.ToString() + ',';
            //    }
            //    str = str.TrimEnd(',');
            //    sql += " and UserCode in (" + str + ") ";
            //}
            //else
            //    sql += " and UserCode='" + usercode + "' ";
            //#region 2018-05-23 lihai 坐席能看到同组的通话记录
            //int seartGroupId = 0;
            //var modelUser = new BLL.T_Sys_UserAccount().GetModel(currcode);
            //if (modelUser != null)
            //    seartGroupId = modelUser.F_SeartGroupID ?? 0;
            //if (seartGroupId != 0)
            //{
            //    //安图特殊，0客服部，1市场部
            //    //BusinessType设置是市场部或客服部的通话记录
            //    var modelSeartGroup = new BLL.T_Sys_SeatGroup().GetModel(seartGroupId);
            //    if (modelSeartGroup != null)
            //    {
            //        if (modelSeartGroup.F_ZXZCode == "SCZ")
            //        {
            //            sql += " and ISNULL(BusinessType,0) = 1 ";
            //        }
            //        else
            //        {
            //            sql += " and ISNULL(BusinessType,0) = 0 ";
            //        }
            //    }
            //    else
            //    {
            //        //找不到对应坐席组
            //        sql += " and UserCode='" + usercode + "' ";
            //    }
            //}
            //else
            //{
            //    sql += " and UserCode='" + usercode + "' ";
            //}
            //#endregion

            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and UserCode='" + usercode + "'";
            }
            if (dept != null && dept.Trim() != "")
            {
                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dept.Trim() + ")";
            }
            if (phone != null && phone.Trim() != "")
            {
                sql += " and CallNumber like '%" + phone + "%'";
            }
            if (callstate.Trim() != "")
            {
                sql += " and CallState='" + callstate + "'";
            }
            if (depttype .Trim() != "")
            {
                sql += " and DealType='" + depttype + "'";
            }
            if (calltype.Trim() != "")
            {
                sql += " and CallType='" + calltype + "'";
            }
            if (starttime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
            }
            if (endtime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
            }

            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_CallRecords left join T_Sys_UserAccount on UserCode=F_UserCode",
                "CallRecordsId",
                "T_Call_CallRecords.*,T_Sys_UserAccount.F_SeartGroup,(SELECT F_DeptName FROM dbo.T_Sys_Department WHERE F_DeptId = T_Sys_UserAccount.F_DeptId) AS DeptName",
                sql,
                "ORDER BY CallRecordsId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

            dt.Columns.Add("CusName");
           
            var config = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='PlayPath' ").FirstOrDefault();
            var users = new BLL.T_Sys_UserAccount().GetModelList("");

            foreach (DataRow dr in dt.Rows)
            {
                string path = dr["FilePath"] != null ? dr["FilePath"].ToString() : "";
                if (path != "" && config != null && !string.IsNullOrEmpty(config.F_ParamValue))
                {
                    var ym = config.F_ParamValue;
                    if (ym.Substring(ym.Length - 1) == "/")
                    {
                        ym = ym.Substring(0, ym.Length - 1);
                    }
                    dr["FilePath"] = ym + path.Substring(path.IndexOf(':') + 1).Replace('\\', '/');
                }
                string ucode = dr["UserCode"] != null ? dr["UserCode"].ToString() : "";
                string uname = dr["UserName"] != null ? dr["UserName"].ToString() : "";
                string tephone= dr["CallNumber"] != null ? dr["CallNumber"].ToString() : "";
                if (ucode != "")
                {
                    if (uname == "")
                    {
                        var uu = users.Where(u => u.F_UserCode.Equals(ucode)).FirstOrDefault();
                        if (uu != null)
                            dr["UserName"] = uu.F_UserName;
                    }
                }
                if (!string.IsNullOrEmpty(tephone))
                {
                    //12:08 2020/2/27 过滤字段F_LegalPhone字段中的空格
                    Model.T_Cus_CustomerBaseNew cus = new BLL.T_Cus_CustomerBaseNew().GetModelList(" REPLACE(F_LegalPhone,' ','') ='" + tephone + "'  ").FirstOrDefault();
                    Model.T_Cus_CustomerBaseNew cusll = new BLL.T_Cus_CustomerBaseNew().GetModelList(" REPLACE(F_LegalPhone,' ','') ='0" + tephone + "'  ").FirstOrDefault();
                    if (cus != null)
                    {
                        dr["CusName"] = new BLL.T_Cus_CustomerBaseNew().GetModelList(" REPLACE(F_LegalPhone,' ','') ='" + tephone + "'  ").FirstOrDefault().F_Legal;
                    }
                    else if (cusll!=null )
                    {
                        dr["CusName"] = new BLL.T_Cus_CustomerBaseNew().GetModelList(" REPLACE(F_LegalPhone,' ','') ='0" + tephone + "'  ").FirstOrDefault().F_Legal;
                    }
                }

            }

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());
        }

        //通话记录导出
        public ActionResult GetListExpt(string phone, string usercode)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " 1=1 ";
                DataTable dt = new DataTable();

                int userid = CurrentUser.UserData.F_UserId;
                int roleid = CurrentUser.UserData.F_RoleId;
                int deptid = CurrentUser.UserData.F_DeptId;
                string currcode = CurrentUser.UserData.F_UserCode;
                string deptcode = CurrentUser.UserData.F_DeptCode;
                string callstate = HttpUtility.UrlDecode(RequestString.GetQueryString("callstate"));
                string calltype = HttpUtility.UrlDecode(RequestString.GetQueryString("calltype"));
                string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
                string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
                string dept = HttpUtility.UrlDecode(RequestString.GetQueryString("dept"));

                //if (roleid != 0)
                //{
                //    if (roleid != 17)
                //    {
                //        if (deptcode.IndexOf("|533|") > 0)
                //        {
                //            sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + deptid + ")";
                //        }
                //        if (deptcode.IndexOf("|531|") > 0)
                //        {
                //            if (roleid == 32)
                //            {
                //                usercode = CurrentUser.UserData.F_UserCode;
                //                sql += " and UserCode='" + usercode + "'";
                //            }
                //            else
                //            {
                //                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptID in (select F_DeptId from T_Sys_Department where F_DeptCode like '" + deptcode + "%')) ";
                //            }
                //        }
                //    }
                //}

                //if (usercode != null && usercode.Trim() != "")
                //{
                //    sql += " and UserCode='" + usercode + "'";
                //}
                //if (dept != null && dept.Trim() != "")
                //{
                //    sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dept.Trim() + ")";
                //}
                //if (phone != null && phone.Trim() != "")
                //{
                //    sql += " and CallNumber like '%" + phone + "%'";
                //}
                //if (callstate.Trim() != "")
                //{
                //    sql += " and CallState='" + callstate + "'";
                //}
                //if (calltype.Trim() != "")
                //{
                //    sql += " and CallType='" + calltype + "'";
                //}
                //if (starttime.Trim() != "")
                //{
                //    sql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
                //}
                //if (endtime.Trim() != "")
                //{
                //    sql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
                //}
                ////20180515 坐席能看到同组的 
                //var usergroup = new BLL.T_Sys_UserAccount().GetModel(currcode) == null ? "" : new BLL.T_Sys_UserAccount().GetModel(currcode).F_SeartGroup;
                //var usercodes = new BLL.T_Sys_UserAccount().GetModelList("F_SeartGroup='" + usergroup + "'") == null ? null : new BLL.T_Sys_UserAccount().GetModelList("F_SeartGroup='" + usergroup + "'").Select(p => p.F_UserCode);
                //var str = "";
                //if (usercodes != null)
                //{
                //    foreach (var item in usercodes)
                //    {
                //        str += item.ToString() + ',';
                //    }
                //    str = str.TrimEnd(',');
                //    sql += " and UserCode in (" + str + ") ";
                //}
                //else
                //    sql += " and UserCode='" + usercode + "' ";

                if (usercode != null && usercode.Trim() != "")
                {
                    sql += " and UserCode='" + usercode + "'";
                }
                if (dept != null && dept.Trim() != "")
                {
                    sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dept.Trim() + ")";
                }
                if (phone != null && phone.Trim() != "")
                {
                    sql += " and CallNumber like '%" + phone + "%'";
                }
                if (callstate.Trim() != "")
                {
                    sql += " and CallState='" + callstate + "'";
                }
                if (calltype.Trim() != "")
                {
                    sql += " and CallType='" + calltype + "'";
                }
                if (starttime.Trim() != "")
                {
                    sql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
                }
                if (endtime.Trim() != "")
                {
                    sql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
                }

                dt = new BLL.T_Call_CallRecords().GetListExpt(sql).Tables[0];
               
                NPOIHelper npoi = new NPOIHelper();
                if (npoi.ExportToExcel("通话记录数据", dt, col()) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }

            }
            return res;
        }

        /// <summary>
        /// 2018-05-23 lihai 添加 处理方式
        /// </summary>
        /// <returns></returns>
        private string[] col()
        {
            string[] ccc = {
                   "客户姓名","电话号码","录音","呼叫方向","处理方式","部门","呼叫状态","坐席工号","坐席姓名",
                  "开始时间","通话开始时间","通话结束时间","通话时长"
            };
            return ccc;
        }

        //删除通话记录
        public ActionResult DelCallRecord(string[] ids)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                if (ids != null && ids.Length > 0)
                {
                    string idd = " ";
                    foreach (string str in ids)
                    {
                        idd += str + ",";
                    }
                    if (new BLL.T_Call_CallRecords().DeleteList(idd.TrimEnd(',')))
                    {
                        res = Success("删除成功");
                    }
                    else
                        res = Error("删除失败");
                }
                else
                {
                    res = Error("请选择要删除的记录");
                }

            }
            return res;
        }

        //2017-12-28获取KPI数据分析报表数据
        DataTable dtnew = new DataTable();
        #region 获取KPI数据分析报表接口(默认当月1号到当天)
        public ActionResult GetKPI()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                string startdate = HttpUtility.UrlDecode(RequestString.GetQueryString("startdate"));
                string enddate = HttpUtility.UrlDecode(RequestString.GetQueryString("enddate"));
                if (startdate == "")
                {
                    startdate = DateTime.Now.ToString("yyyy-MM") + "-01";
                }
                if (enddate == "")
                {
                    enddate = DateTime.Now.ToString("yyyy-MM-dd");
                }
                string starttime = startdate + " 00:00:00";
                string endtime = enddate + " 23:59:59";
                //DataTable dtnew = new DataTable();
                dtnew.Columns.Add("ID", Type.GetType("System.String"));
                dtnew.Columns.Add("num", Type.GetType("System.Decimal"));
                dtnew.Columns.Add("percent", Type.GetType("System.String"));
                #region 业务电话量和回访满意度
                string sql0 = "select  isnull(SUM(case when CallType='0' then 1 else 0 end),0) as callinnum,isnull(SUM(case when CallType='1' then 1 else 0 end),0) as calloutnum,COUNT(1) as total,isnull(SUM(case when CallType='0' and Callstate='0' then 1 else 0 end),0) as unconnect,isnull(SUM(case when CallType='0' and Callstate='1' then 1 else 0 end),0) as connected,isnull(SUM(case when MYD='1' and Callstate='1' then 1 else 0 end),0) as verysatisfy,isnull(SUM(case when MYD='2' and Callstate='1' then 1 else 0 end),0) as satisfy,isnull(SUM(case when MYD='3' and Callstate='1' then 1 else 0 end),0) as unsatisfy,isnull(SUM(case when (MYD IS null OR MYD='0') and Callstate='1' then 1 else 0 end),0) as unappraise from T_Call_CallRecords where BeginTime BETWEEN '" + starttime + "' AND '" + endtime + "'";
                DataRow dr = dtnew.NewRow();
                DataSet ds0 = DbHelperSQL.Query(sql0);
                if (ds0 != null && ds0.Tables.Count > 0)
                {
                    DataTable dt0 = ds0.Tables[0];
                    if (dt0.Rows.Count > 0)
                    {
                        Int32 callinnum = Convert.ToInt32(dt0.Rows[0]["callinnum"]);
                        Int32 calloutnum = Convert.ToInt32(dt0.Rows[0]["calloutnum"]);
                        Int32 totalnum = Convert.ToInt32(dt0.Rows[0]["total"]);
                        if (totalnum == 0)
                        { totalnum = 1; }
                        Int32 unconnectnum = Convert.ToInt32(dt0.Rows[0]["unconnect"]);
                        Int32 connectednum = Convert.ToInt32(dt0.Rows[0]["connected"]);
                        Int32 ctotalnum = unconnectnum + connectednum;
                        if (ctotalnum == 0)
                        { ctotalnum = 1; }
                        Int32 verysatisfy = Convert.ToInt32(dt0.Rows[0]["verysatisfy"]);
                        Int32 satisfy = Convert.ToInt32(dt0.Rows[0]["satisfy"]);
                        Int32 unsatisfy = Convert.ToInt32(dt0.Rows[0]["unsatisfy"]);
                        Int32 unappraise = Convert.ToInt32(dt0.Rows[0]["unappraise"]);
                        Int32 stotalnum = satisfy + verysatisfy + unsatisfy + unappraise;
                        if (stotalnum == 0)
                        { stotalnum = 1; }
                        //添加呼入项目数据
                        dr["num"] = callinnum;
                        dr["percent"] = Math.Round(Convert.ToDecimal(callinnum) / totalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //添加呼入项目数据
                        dr = dtnew.NewRow();
                        dr["num"] = calloutnum;
                        dr["percent"] = Math.Round(Convert.ToDecimal(calloutnum) / totalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //呼入接通率
                        dr = dtnew.NewRow();
                        dr["num"] = connectednum;
                        dr["percent"] = Math.Round(Convert.ToDecimal(connectednum) / ctotalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //呼入未接通率
                        dr = dtnew.NewRow();
                        dr["num"] = unconnectnum;
                        dr["percent"] = Math.Round(Convert.ToDecimal(unconnectnum) / ctotalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //总计
                        dr = dtnew.NewRow();
                        dr["num"] = totalnum;
                        dr["percent"] = "100.00%";
                        dtnew.Rows.Add(dr);
                        //非常满意
                        dr = dtnew.NewRow();
                        dr["num"] = verysatisfy;
                        dr["percent"] = Math.Round(Convert.ToDecimal(verysatisfy) / stotalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //满意
                        dr = dtnew.NewRow();
                        dr["num"] = satisfy;
                        dr["percent"] = Math.Round(Convert.ToDecimal(satisfy) / stotalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //不满意
                        dr = dtnew.NewRow();
                        dr["num"] = unsatisfy;
                        dr["percent"] = Math.Round(Convert.ToDecimal(unsatisfy) / stotalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //未评价
                        dr = dtnew.NewRow();
                        dr["num"] = unappraise;
                        dr["percent"] = Math.Round(Convert.ToDecimal(unappraise) / stotalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr);
                        //合计
                        dr = dtnew.NewRow();
                        dr["num"] = stotalnum;
                        dr["percent"] = "100.00%";
                        dtnew.Rows.Add(dr);
                    }
                }
                #endregion

                #region 服务水平
                string sql1 = "select  isnull(SUM(case when isnull(RingLongTime,0)<=20 and Callstate='1' then 1 else 0 end),0) as InTime,isnull(SUM(case when RingLongTime>20 and Callstate='1' then 1 else 0 end),0) as Delay,isnull(SUM(case when Callstate='1' then 1 else 0 end),0) as total from T_Call_CallRecords where calltype=0 and BeginTime BETWEEN '" + starttime + "' AND '" + endtime + "'";
                DataRow dr1 = dtnew.NewRow();
                DataSet ds1 = DbHelperSQL.Query(sql1);
                if (ds1 != null && ds1.Tables.Count > 0)
                {
                    DataTable dt1 = ds1.Tables[0];
                    if (dt1.Rows.Count > 0)
                    {
                        Int32 intime = Convert.ToInt32(dt1.Rows[0]["InTime"]);
                        Int32 delay = Convert.ToInt32(dt1.Rows[0]["Delay"]);
                        Int32 totalnum = Convert.ToInt32(dt1.Rows[0]["total"]);
                        if (totalnum == 0)
                        { totalnum = 1; }
                        //添加呼入20s内接通数据
                        dr1["num"] = intime;
                        dr1["percent"] = Math.Round(Convert.ToDecimal(intime) / totalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr1);
                        //添加呼入非20s内接通数据
                        dr1 = dtnew.NewRow();
                        dr1["num"] = delay;
                        dr1["percent"] = Math.Round(Convert.ToDecimal(delay) / totalnum * 100, 2).ToString() + "%";
                        dtnew.Rows.Add(dr1);
                        //总计
                        dr1 = dtnew.NewRow();
                        dr1["num"] = totalnum;
                        dr1["percent"] = "100.00%";
                        dtnew.Rows.Add(dr1);

                    }
                }

                #endregion

                #region 话务平均时间
                //string sql2 = "select  isnull(SUM(WaitLongTime),0) as WaitTime,isnull(SUM(TalkLongTime),0) as TalkLongTime,isnull(SUM(RingLongTime),0) as RingLongTime,isnull(SUM(case when CallType='0' and Callstate='1' then 1 else 0 end),0)as total from T_Call_CallRecords where BeginTime BETWEEN '" + starttime + "' AND '" + endtime + "'";
                //DataRow dr2 = dtnew.NewRow();
                //DataSet ds2 = DbHelperSQL.Query(sql2);
                //if (ds2 != null && ds2.Tables.Count > 0)
                //{
                //    DataTable dt2 = ds2.Tables[0];
                //    if (dt2.Rows.Count > 0)
                //    {
                //        Int32 WaitTime = Convert.ToInt32(dt2.Rows[0]["WaitTime"]);
                //        Int32 TalkLongTime = Convert.ToInt32(dt2.Rows[0]["TalkLongTime"]);
                //        Int32 RingLongTime = Convert.ToInt32(dt2.Rows[0]["RingLongTime"]);
                //        Int32 totalnum = Convert.ToInt32(dt2.Rows[0]["total"]);
                //        if (totalnum == 0)
                //        { totalnum = 1; }
                //        //添加平均排队数据
                //        dr2["num"] = Math.Round(Convert.ToDecimal(WaitTime) / totalnum, 2);
                //        dr2["percent"] = "";
                //        dtnew.Rows.Add(dr2);
                //        //添加通话时长数据
                //        dr2 = dtnew.NewRow();
                //        dr2["num"] = Math.Round(Convert.ToDecimal(TalkLongTime) / totalnum, 2);
                //        dr2["percent"] = "";
                //        dtnew.Rows.Add(dr2);
                //        //添加平均振铃时长数据
                //        dr2 = dtnew.NewRow();
                //        dr2["num"] = Math.Round(Convert.ToDecimal(RingLongTime) / totalnum, 2);
                //        dr2["percent"] = "";
                //        dtnew.Rows.Add(dr2);
                //        //添加平均处理时长数据
                //        dr2 = dtnew.NewRow();
                //        dr2["num"] = 0;
                //        dr2["percent"] = "";
                //        dtnew.Rows.Add(dr2);
                //        //添加平均振铃时长数据
                //        dr2 = dtnew.NewRow();
                //        dr2["num"] = 0;
                //        dr2["percent"] = "0.00%";
                //        dtnew.Rows.Add(dr2);
                //    }
                //}
                res = Success("获取KPI数据成功", dtnew);
                #endregion
            }
            return res;
        }
        #endregion

        #region  数据导出--获取KPI数据
        public ActionResult GetKPIExpt()
        {

            ActionResult res = NoToken("未知错误，请重新登录");
            GetKPI();

            string startdate = "2017-11-12 00:00:00";
            string enddate = "2017-11-14 23:59:59";
            DataTable dtexpt = new DataTable();
            dtexpt.Columns.Add("fenlei2", Type.GetType("System.String"));
            dtexpt.Columns.Add("fenlei", Type.GetType("System.String"));
            dtexpt.Columns.Add("num", Type.GetType("System.String"));
            dtexpt.Columns.Add("percent", Type.GetType("System.String"));
            List<string> list2 = new List<string>();
            list2.Add("业务电话量");
            list2.Add("业务电话量");
            list2.Add("业务电话量");
            list2.Add("业务电话量");
            list2.Add("业务电话量");
            list2.Add("回访满意度");
            list2.Add("回访满意度");
            list2.Add("回访满意度");
            list2.Add("回访满意度");
            list2.Add("回访满意度");
            list2.Add("服务水平");
            list2.Add("服务水平");
            list2.Add("服务水平");
            //list2.Add("平均排队时间(秒)");
            //list2.Add("平均振铃时间(秒)");
            //list2.Add("平均通话时间(秒)");
            //list2.Add("平均处理时间(小时)");
            //list2.Add("已关闭并已解决工单数/已关闭工单数*100%");
            List<string> list = new List<string>();
            list.Add("呼入项目占有率");
            list.Add("呼出项目占有率");
            list.Add("呼入接通率");
            list.Add("呼入未接通率");
            list.Add("合计(总电话量)");
            list.Add("非常满意");
            list.Add("满意");
            list.Add("不满意");
            list.Add("未评价");
            list.Add("合计(总回访数)");
            list.Add("20秒内应答");
            list.Add("非20秒内应答");
            list.Add("合计(总服务量)");
            //list.Add("平均排队时间(秒)");
            //list.Add("平均振铃时间(秒)");
            //list.Add("平均通话时间(秒)");
            //list.Add("平均处理时间(小时)");
            //list.Add("已关闭并已解决工单数/已关闭工单数*100%");
            for (int i = 0; i < dtnew.Rows.Count; i++)
            {
                DataRow dr = dtexpt.NewRow();
                dr["fenlei2"] = list2[i].ToString();
                dr["fenlei"] = list[i].ToString();
                dr["num"] = dtnew.Rows[i]["num"].ToString();
                dr["percent"] = dtnew.Rows[i]["percent"].ToString();
                dtexpt.Rows.Add(dr);
            }

            //导出dtnew
            NPOIHelper npoi = new NPOIHelper();
            string[] col = { "分类", "分类", "数量", "占有率" };
            if (npoi.ExportToExcel("话务KPI数据分析报表", dtexpt, col) == "")
            {
                return Success("导出成功");
            }
            else
            {
                return Error("导出失败");
            }
        }
        #endregion
    }
}