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

namespace RMYY_CallCenter_Api.Controllers.report
{
    public class YearContrastController : BaseController
    {
        //呼叫数据对比
        public ActionResult GetDataList(string beginyear, string endyear)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            ArrayList arrlist = new ArrayList();

            if (beginyear != null && beginyear.Trim() != "")
            {
                var data = new
                {
                    key = beginyear + "客户呼叫数",
                    value = GetKhCall(beginyear).TrimEnd(',').Split(','),
                    type = 0
                };
                arrlist.Add(data);

                var data1 = new
                {
                    key = beginyear + "坐席接听数",
                    value = GetZxJt(beginyear).TrimEnd(',').Split(','),
                    type = 0
                };
                arrlist.Add(data1);

                var data2 = new
                {
                    key = beginyear + "接通率",
                    value = GetJTL(beginyear).TrimEnd(',').Split(','),
                    type = 0
                };
                arrlist.Add(data2);
            }
            if (endyear != null && endyear.Trim() != "")
            {
                var data = new
                {
                    key = endyear + "客户呼叫数",
                    value = GetKhCall(endyear).TrimEnd(',').Split(','),
                    type = 1
                };
                arrlist.Add(data);

                var data1 = new
                {
                    key = endyear + "坐席接听数",
                    value = GetZxJt(endyear).TrimEnd(',').Split(','),
                    type = 1
                };
                arrlist.Add(data1);

                var data2 = new
                {
                    key = endyear + "接通率",
                    value = GetJTL(endyear).TrimEnd(',').Split(','),
                    type = 1
                };
                arrlist.Add(data2);
            }

            res = Success("获取呼叫数据分析数据成功", arrlist);

            return res;
        }

        //导出数据
        public ActionResult ExptList(string beginyear, string endyear)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            //if (Request.IsAuthenticated)
            if (1 == 1)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = GetData(beginyear, endyear);
                if (npoi.ExportToExcel("呼叫数据分析数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private DataTable GetData(string beginyear, string endyear)
        {
            DataTable dtNew = new DataTable();
            if ((beginyear != null && beginyear.Trim() != "") && (endyear != null && endyear.Trim() != ""))
            {
                #region 编辑表头
                DataColumn dc1 = new DataColumn("月份", Type.GetType("System.String"));
                DataColumn dc2 = new DataColumn(beginyear + "年客户呼叫数", Type.GetType("System.String"));
                DataColumn dc3 = new DataColumn(beginyear + "年坐席接听数", Type.GetType("System.String"));
                DataColumn dc4 = new DataColumn(beginyear + "年坐席接通率", Type.GetType("System.String"));
                DataColumn dc5 = new DataColumn(endyear + "年客户呼叫数", Type.GetType("System.String"));
                DataColumn dc6 = new DataColumn(endyear + "年坐席接听数", Type.GetType("System.String"));
                DataColumn dc7 = new DataColumn(endyear + "年坐席接通率", 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);
                #endregion
                string[] bkhcall = GetKhCall(beginyear).Split(',');
                string[] bzxjt = GetZxJt(beginyear).Split(',');
                string[] bjtl = GetJTL(beginyear).Split(',');

                string[] ekhcall = GetKhCall(endyear).Split(',');
                string[] ezxjt = GetZxJt(endyear).Split(',');
                string[] ejtl = GetJTL(endyear).Split(',');

                for (int i = 0; i < 12; i++)
                {
                    DataRow drNew = dtNew.NewRow();
                    drNew["月份"] = (i + 1).ToString() + "月";
                    drNew[beginyear + "年客户呼叫数"] = bkhcall[i];
                    drNew[beginyear + "年坐席接听数"] = bzxjt[i];
                    drNew[beginyear + "年坐席接通率"] = bjtl[i];
                    drNew[endyear + "年客户呼叫数"] = ekhcall[i];
                    drNew[endyear + "年坐席接听数"] = ezxjt[i];
                    drNew[endyear + "年坐席接通率"] = ejtl[i];
                    dtNew.Rows.Add(drNew);
                }
            }

            return dtNew;
        }

        //客户呼叫数
        private string GetKhCall(string strtime)
        {
            DataTable dt = new DataTable();
            string str = "";
            for (int i = 1; i <= 12; i++)
            {
                string sqltimeCallRecords = "";
                string strmm = i.ToString();
                if (i <= 9)
                {
                    strmm = "0" + i;
                }
                if (strtime != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)>=CONVERT(varchar , '" + strtime + "-" + strmm + "-01 00:00:01', 120) ";
                    sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)<=CONVERT(varchar , '" + strtime + "-" + strmm + "-31 23:59:59', 120) ";
                }

                dt = DbHelperSQL.Query("select COUNT(*) as ct from rep_trunk_call_in  where 1=1 " + sqltimeCallRecords).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["ct"].ToString() != "")
                    {
                        str += dt.Rows[0]["ct"].ToString() + ",";
                    }
                    else
                    {
                        str += "0,";
                    }
                }
                else
                {
                    str += "0,";
                }
            }
            if (str != "")
            {
                str.Remove(str.Length - 1);
            }
            return str;
        }

        //坐席接听数
        private string GetZxJt(string strtime)
        {
            DataTable dt = new DataTable();
            string str = "";
            for (int i = 1; i <= 12; i++)
            {
                string sqltimeCallRecords = "";
                string strmm = i.ToString();
                if (i <= 9)
                {
                    strmm = "0" + i;
                }
                if (strtime != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)>=CONVERT(varchar , '" + strtime + "-" + strmm + "-01 00:00:01', 120) ";
                    sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)<=CONVERT(varchar , '" + strtime + "-" + strmm + "-31 23:59:59', 120) ";
                }

                dt = DbHelperSQL.Query("select COUNT(*) as ct from rep_trunk_call_in where PeriodTalking>0 " + sqltimeCallRecords).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["ct"].ToString() != "")
                    {
                        str += dt.Rows[0]["ct"].ToString() + ",";
                    }
                    else
                    {
                        str += "0,";
                    }
                }
                else
                {
                    str += "0,";
                }
            }
            if (str != "")
            {
                str.Remove(str.Length - 1);
            }
            return str;
        }

        //坐席接听率
        private string GetJTL(string strtime)
        {
            DataTable dt = new DataTable();
            string str = "";
            for (int i = 1; i <= 12; i++)
            {
                string sqltimeCallRecords = "";
                string strmm = i.ToString();
                if (i <= 9)
                {
                    strmm = "0" + i;
                }
                if (strtime != "")
                {
                    sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)>=CONVERT(varchar , '" + strtime + "-" + strmm + "-01 00:00:01', 120) ";
                    sqltimeCallRecords += " and CONVERT(varchar , timecallin, 120)<=CONVERT(varchar , '" + strtime + "-" + strmm + "-31 23:59:59', 120) ";
                }

                dt = DbHelperSQL.Query("select COUNT(*) as ct,(select COUNT(*) from rep_trunk_call_in where PeriodTalking>0 " + sqltimeCallRecords + ") as jt from rep_trunk_call_in  where 1=1  " + sqltimeCallRecords).Tables[0];

                int ibgnjt = 0;
                double ibgnjtl = 0.00;

                if (dt != null && dt.Rows.Count > 0)
                {

                    if (dt.Rows[0]["jt"].ToString() != "")
                    {
                        ibgnjt = int.Parse(dt.Rows[0]["jt"].ToString());
                    }

                    if (dt.Rows[0]["ct"].ToString() != "" && dt.Rows[0]["ct"].ToString() != "0")
                    {
                        ibgnjtl = ibgnjt / Convert.ToDouble(dt.Rows[0]["ct"].ToString());
                        str += (ibgnjtl * 100).ToString("0.00") + ",";
                    }
                    else
                    {
                        str += "0.00,";
                    }
                }
                else
                {
                    str += "0.00,";
                }
            }
            if (str != "")
            {
                str.Remove(str.Length - 1);
            }
            return str;
        }
    }
}