using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections;
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 TotalCallController : BaseController
    {
        //总呼叫量统计

        //获取表头
        public ActionResult GetColumnList()
        {
            String[] str = { "月份", "日期", "总数" };
            return Success("获取总呼叫量统计表头成功", str);
        }

        //获取数据
        public ActionResult GetDataList(string years)
        {
            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("Count", Type.GetType("System.String"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            #endregion
            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 and CallType=0 ").Tables[0];
            DateTime dtNow;
            int Count = 0;
            int DayCount = 0;

            if (string.IsNullOrEmpty(years))
            {
                dtNow = DateTime.Now;
            }
            else
            {
                dtNow = DateTime.Parse(years );
            }
                DataRow drNew = dtNew.NewRow();

                DateTime beginTime = Convert.ToDateTime(dtNow.ToString("yyyy-MM-01 00:00:00"));
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);
                DataRow[] dtRow = dt.Select("begintime >='" + beginTime.ToString() + "' and  beginTime<='" + beginTime.AddDays(days).AddSeconds(-1) + "'");
                if (dtRow.Length > 0)
                {
                    Count += dtRow.Length;
                    for (int j = 1; j <= days; j++)
                    {
                        DataRow dr1 = dtNew.NewRow();
                        if (dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<='" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length > 0)
                        {
                            DayCount += 1;

                            dr1["月份"] = beginTime.Month + "月";
                            dr1["日期"] = beginTime.AddDays(j - 1).ToShortDateString();
                            dr1["总数"] = dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<='" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length;
                            dr1["Count"] = "1";
                            dtNew.Rows.Add(dr1);
                        }
                    }

                    if (DayCount != 0)
                    {
                        drNew["月份"] = beginTime.Month + "月";
                        drNew["日期"] = "总计";
                        drNew["总数"] = dtRow.Length;
                        drNew["Count"] = DayCount.ToString();
                        dtNew.Rows.Add(drNew);
                    }
                    DayCount = 0;
                }

            return Success("获取呼叫量数据成功", dtNew);
        }

        private DataTable GetData(string years)
        {
            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"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            #endregion
            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 and CallType=0 ").Tables[0];
            DateTime dtNow;
            int Count = 0;
            int DayCount = 0;

            if (string.IsNullOrEmpty(years))
            {
                dtNow = DateTime.Now;
            }
            else
            {
                dtNow = DateTime.Parse(years);
            }
                DataRow drNew = dtNew.NewRow();

                DateTime beginTime = Convert.ToDateTime(dtNow.ToString("yyyy-MM-01 00:00:00"));
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);
                DataRow[] dtRow = dt.Select("begintime >='" + beginTime.ToString() + "' and  beginTime<='" + beginTime.AddDays(days).AddSeconds(-1) + "'");
                if (dtRow.Length > 0)
                {
                    Count += dtRow.Length;
                    for (int j = 1; j <= days; j++)
                    {
                        DataRow dr1 = dtNew.NewRow();
                        if (dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<='" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length > 0)
                        {
                            DayCount += 1;

                            dr1["月份"] = beginTime.Month + "月";
                            dr1["日期"] = beginTime.AddDays(j - 1).ToShortDateString();
                            dr1["总数"] = dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<='" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length;

                            dtNew.Rows.Add(dr1);
                        }
                    }

                    if (DayCount != 0)
                    {
                        drNew["月份"] = beginTime.Month + "月";
                        drNew["日期"] = "总计";
                        drNew["总数"] = dtRow.Length;
                        dtNew.Rows.Add(drNew);
                    }
                    DayCount = 0;
                }
            
            return dtNew;
        }

        public ActionResult ExptList(string years)
        {
            NPOIHelper npoi = new NPOIHelper();
            DataTable dt = GetData(years);
            if (npoi.ExportToExcel("呼叫量数据", dt) == "")
            {
                return Success("导出成功");
            }
            else
            {
                return Error("导出失败");
            }
        }

        //获取图形数据
        public ActionResult GetChartData(string years)
        {
            string[] arr = new string[12];

            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1 and CallType=0 ").Tables[0];
            DateTime dtNow;

            if (string.IsNullOrEmpty(years))
            {
                dtNow = DateTime.Parse(DateTime.Now.ToString("yyyy-01-01 00:00:00"));
            }
            else
            {
                dtNow = DateTime.Parse(DateTime.Parse(years).ToString("yyyy-01-01 00:00:00"));
            }
            for (int i = 1; i <= 12; i++)
            {
                DateTime beginTime = Convert.ToDateTime(dtNow.ToString("yyyy-MM-01 00:00:00"));
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);
                DataRow[] dtRow = dt.Select("begintime >='" + beginTime.AddMonths(i-1).ToString() + "' and  beginTime<='" + beginTime.AddMonths(i - 1).AddDays(days).AddSeconds(-1) + "'");
                if (dtRow.Length > 0)
                {
                    arr[i - 1] = dtRow.Length.ToString();
                }
                else
                {
                    arr[i - 1] = "0";
                }
            }

            return Success("获取呼叫量图形数据成功", arr);
        }
    }
}