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
{
    /// <summary>
    /// 每日呼叫数据统计分析
    /// </summary>
    public class HourCallTotalController : BaseController
    {
        BLL.T_Call_CallRecords bllCall = new BLL.T_Call_CallRecords();
        //获取表头
        public ActionResult GetColumnList()
        {
            #region 添加表格标题
            var columns = new List<string>();
            columns.Add("时间");
            columns.Add("呼入量");
            columns.Add("呼出量");
            columns.Add("接通量");
            columns.Add("未接通量");
            columns.Add("接通率");
            #endregion
            return Success("获取工单结案率标题成功", columns);
        }

        //获取数据
        public ActionResult GetDataList(string stime)
        {            
            ActionResult res = NoToken("未知错误，请重新登录");
            DataTable dtNew = new DataTable();
            dtNew = GetData(stime);
            res = Success("获取工单结案率数据成功", dtNew);

            return res;
        }        

        //获取数据源
        private DataTable GetData(string stime)
        {
            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"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            dtNew.Columns.Add(dc6);
            #endregion

            string sql = " ";
            var list = bllCall.DataTableToList(bllCall.GetList($" 1=1 {sql} and UserCode is not null ").Tables[0]);
            int callInCount = 0, callOutCount = 0, calledCount = 0, nocalledCount = 0;
            //一天24小时
            for (int i = 0; i < 24; i++)
            {
                DateTime stimes = Convert.ToDateTime(stime + " 00:00:00").AddHours(i);
                DateTime etimes = Convert.ToDateTime(stime + " 00:59:59").AddHours(i);
                DataRow drNew = dtNew.NewRow();
                //SELECT BeginTime, * FROM T_Call_CallRecords WHERE DATEPART(hh,BeginTime) = DATEPART(hh, DATEADD(hh,-2,GETDATE())) and DATEPART(dd,BeginTime) = DATEPART(dd, GETDATE()) and DATEPART(mm, BeginTime) = DATEPART(mm, GETDATE()) and DATEPART(yy, BeginTime) = DATEPART(yy, GETDATE())
                //CallType
                //CallState呼叫状态,0:未接通电话1已接通电话
                var callInTimes = list.Where(x => x.CallType == 0 && x.BeginTime >= stimes && x.BeginTime <= etimes).Count();   //呼入量
                var callOutTimes = list.Where(x => x.CallType == 1 && x.BeginTime >= stimes && x.BeginTime <= etimes).Count();  //呼出量
                var calledTimes = list.Where(x => x.CallState == 1 && x.BeginTime >= stimes && x.BeginTime <= etimes).Count();  //接通量
                var nocalledTimes = list.Where(x => x.CallState == 0 && x.BeginTime >= stimes && x.BeginTime <= etimes).Count();//未接通量
                
                //var calledRate = list.Where(x => x.CallType == 1 && x.BeginTime >= stimes && x.BeginTime <= etimes).Count();
                drNew["时间"] = (i + 1).ToString();
                drNew["呼入量"] = callInTimes.ToString();
                drNew["呼出量"] = callOutTimes.ToString();
                drNew["接通量"] = calledTimes.ToString();
                drNew["未接通量"] = nocalledTimes.ToString();
                //计算未结案率
                var calledTotal = calledTimes + nocalledTimes;
                Double notRate = 0.00;
                if (calledTotal > 0)
                    notRate = (double)calledTimes / (double)calledTotal;
                drNew["接通率"] = (notRate * 100).ToString("0.00") + "%";
                ////统计结案，未结案数
                callInCount += callInTimes;
                callOutCount += callOutTimes;
                calledCount += calledTimes;
                nocalledCount += nocalledTimes;
                dtNew.Rows.Add(drNew);
            }
            #region 获取一条统计信息
            DataRow drzj = dtNew.NewRow();
            drzj["时间"] = "总计";
            drzj["呼入量"] = callInCount.ToString();
            drzj["呼出量"] = callOutCount.ToString();
            drzj["接通量"] = calledCount.ToString();
            drzj["未接通量"] = nocalledCount.ToString();
            //计算未结案率
            int totalAllCount = calledCount + nocalledCount;
            Double calledAllRate = 0.00;
            if (totalAllCount > 0)
                calledAllRate = (double)calledCount / (double)totalAllCount;
            drzj["接通率"] = (calledAllRate * 100).ToString("0.00") + "%";
            dtNew.Rows.Add(drzj);
            #endregion
            return dtNew;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        public ActionResult ExportExcel(string stime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = GetData(stime);
                if (npoi.ExportToExcel("通话时长数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }
    }
}