using CallCenter.Utility;
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
{
    [Authority]
    public class TotalTelController : BaseController
    {
        //话务量统计

        //获取表头
        public ActionResult GetColumnList()
        {
            String[] str = { "时间", "电话呼入次数", "电话呼出次数", "正常接听次数", "未接通次数", "主动放弃次数", "黑名单拒接", "平均呼入时间" };
            //String[] str = { "时间", "电话呼入次数", "电话呼出次数", "正常接听次数","未接通次数", "语音留言次数", "主动放弃次数", "黑名单拒接", "平均呼入时间（秒）" };
            //String[] str = { "时间", "电话呼入次数", "电话呼出次数", "正常接听次数", "语音留言次数", "主动放弃次数", "平均呼入时间（秒）" };
            return Success("获取话务量统计表头成功", str);
        }

        //获取数据
        public ActionResult GetDataList(string stime, string etime)
        {
            DataTable dtNew = new DataTable();
            dtNew = getData(stime, etime);

            var obj = new
            {
                state = "success",
                message = "获取话务量数据成功",
                rows = dtNew,
                total = dtNew.Rows.Count
            };

            return Content(obj.ToJson());
        }

        //导出数据
        public ActionResult ExptList(string stime, string etime)
        {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = getData(stime, etime);
                if (npoi.ExportToExcel("话务量数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
        }

        private DataTable getData(string stime, string etime)
        {
            DataTable dtNew = new DataTable();
            #region 编辑表头
            DataColumn dc1 = new DataColumn("时间", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("电话呼入次数", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("电话呼出次数", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("正常接听次数", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("未接通次数", Type.GetType("System.String"));
            //DataColumn dc6 = new DataColumn("语音留言次数", Type.GetType("System.String"));
            DataColumn dc7 = new DataColumn("主动放弃次数", Type.GetType("System.String"));
            DataColumn dc8 = new DataColumn("电话骚扰次数", Type.GetType("System.String"));
            DataColumn dc9 = new DataColumn("平均呼入时间", Type.GetType("System.String"));
            
            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            //dtNew.Columns.Add(dc6);
            dtNew.Columns.Add(dc7);
            dtNew.Columns.Add(dc8);
            dtNew.Columns.Add(dc9);
            #endregion

            DateTime beginTime;
            DateTime endTime;
            string sql = "", sqll = "";

            int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            if (stime != null && stime != "")
                beginTime = DateTime.Parse(stime);
            else
                beginTime = DateTime.Parse(DateTime.Now.AddDays(-days).ToShortDateString());

            if (etime != null && etime != "")
                endTime = DateTime.Parse(etime + " 23:59:59");
            else
                endTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59");
            

            sql += " and DATEDIFF(d,BeginTime,'" + beginTime.ToString("yyyy-MM-dd HH:mm:ss") + "')<=0";
            sql += " and DATEDIFF(d,BeginTime,'" + endTime.ToString("yyyy-MM-dd HH:mm:ss") + "')>=0";
            sqll += " and DATEDIFF(d,F_LeaveTime,'" + beginTime.ToString("yyyy-MM-dd HH:mm:ss") + "')<=0";
            sqll += " and DATEDIFF(d,F_LeaveTime,'" + endTime.ToString("yyyy-MM-dd HH:mm:ss") + "')>=0";            

            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 " + sql).Tables[0];
            DataTable dtly = new BLL.T_Call_LeaveRecord().GetList("1=1"+ sqll).Tables[0];
            int tcallincount = 0;
            int tcalloutcount = 0;
            int tcallinjtcount = 0;
            int tlycount = 0;
            int tsrcount = 0;
            int tfqcount = 0;
            int twjtcount = 0;
            double tthsc = 0;
            for (DateTime t = beginTime; t <= endTime; t = t.AddDays(1))
            {
                string key = t.ToString("yyyy.MM.dd");
                int callincount = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=0").Length;
                int calloutcount = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=1").Length;
                int callinjtcount = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=0 and CallState=1 ").Length;
                //int lycount = dtly.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=4 ").Length; ;
                int lycount = dtly.Select("F_LeaveTime >='" + t.ToString() + "' and F_LeaveTime<'" + t.AddDays(1).ToString() + "' ").Length; ;
                int srcount = dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and DealType=1 ").Length;
                int wjtcount=dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallState=0 and CallType=0 and isnull(usercode,'')<>'' ").Length;
                //int fqcount = callincount - callinjtcount - lycount - srcount;//- wjtcount;
                int fqcount= dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallState=0 and isnull(usercode,'')='' ").Length;
                string thsc = gettimes(dt.Select("begintime >='" + t.ToString() + "' and begintime<'" + t.AddDays(1).ToString() + "' and CallType=0 and CallState=1 "));

                tcallincount = tcallincount + callincount;
                tcalloutcount = tcalloutcount + calloutcount;
                tcallinjtcount = tcallinjtcount + callinjtcount;
                twjtcount = twjtcount + wjtcount;
                tlycount = tlycount + lycount;
                tsrcount = tsrcount + srcount;
                tfqcount = tfqcount + fqcount;
                tthsc = tthsc + double.Parse(thsc);

                DataRow drNew = dtNew.NewRow();
                
                drNew["时间"] = key;
                drNew["电话呼入次数"] = callincount;
                drNew["电话呼出次数"] = calloutcount;
                drNew["正常接听次数"] = callinjtcount;
                drNew["未接通次数"] = wjtcount;
                //drNew["语音留言次数"] = lycount;
                drNew["主动放弃次数"] = fqcount;
                drNew["电话骚扰次数"] = srcount;
                if ((callincount + calloutcount) > 0 && double.Parse(thsc)>0)
                    drNew["平均呼入时间"] = DateTimeConvert.parseTimeSeconds((int)(double.Parse(thsc) / callinjtcount),0);
                else
                    drNew["平均呼入时间"] = "0";

                dtNew.Rows.Add(drNew);
            }

            DataRow drtNew = dtNew.NewRow();

            drtNew["时间"] = "合计";
            drtNew["电话呼入次数"] = tcallincount;
            drtNew["电话呼出次数"] = tcalloutcount;
            drtNew["正常接听次数"] = tcallinjtcount;
            drtNew["未接通次数"] = twjtcount;
            //drtNew["语音留言次数"] = tlycount;
            drtNew["主动放弃次数"] = tfqcount;
            drtNew["电话骚扰次数"] = tsrcount;
            if ((tcallincount + tcalloutcount) > 0 && tthsc > 0)
                drtNew["平均呼入时间"] = DateTimeConvert.parseTimeSeconds((int)(tthsc / tcallinjtcount), 0);
            else
                drtNew["平均呼入时间"] = "0";

            dtNew.Rows.Add(drtNew);

            return dtNew;
        }

        private string gettimes(DataRow[] drs)
        {
             int thsc = 0;
            for (int i = 0; i < drs.Length; i++)
            {
                if (drs[i]["TalkLongTime"].ToString() != "")
                    thsc += int.Parse(drs[i]["TalkLongTime"].ToString());
            }
            return thsc.ToString();
        }
    }
}