using CallCenter.Utility;
using CallCenterApi.DB;
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
{
    //
    public class TotalCallController : BaseController
    {
        BLL.T_Call_CallRecords bll = new BLL.T_Call_CallRecords();
        //总呼叫量统计

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

        }

        //获取数据
        public ActionResult GetDataList(string years)
        {
            string sql = "";
            if (!string.IsNullOrEmpty(years))
            {
                sql += $" and CONVERT(char(4),BeginTime,20) = '{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").Tables[0];
            DataTable dtConnect = DbHelperSQL.Query($"select CONVERT(varchar(10),BeginTime, 23) AS yearmonthdays,CONVERT(char(7),BeginTime,20) AS yearmonths,COUNT(1) ccount FROM T_Call_CallRecords WITH(NOLOCK) WHERE 1=1 {sql} group by CONVERT(varchar(10),BeginTime, 23),CONVERT(char(7),BeginTime,20) order by CONVERT(varchar(10),BeginTime, 23)").Tables[0];
            DateTime dtNow;
            int Count = 0;
            int DayCount = 0;

            if (string.IsNullOrEmpty(years))
            {
                dtNow = DateTime.Now;
            }
            else
            {
                dtNow = DateTime.Parse(years + "年");
            }
            int monthcount = 0;
            for (int i = 1; i <= 12; i++)
            {
                DataRow drNew = dtNew.NewRow();

                DateTime beginTime = Convert.ToDateTime(dtNow.Year + "-" + i + "-01 00:00:00");
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);

                //var drConnect = dtConnect.Select("yearmonthdays='" + beginTime.AddDays(days).AddSeconds(-1).ToString("yyyy-MM-dd") + "' ");
                //int count = (from DataRow dr in drConnect select dr.Field<int>("ccount")).FirstOrDefault();

                var dtlist = dtConnect.Select("yearmonths='" + beginTime.ToString("yyyy-MM") + "' ");
                var dtlist_sum = (from DataRow dr in dtlist select dr.Field<int>("ccount")).Sum();

                //DataRow[] dtRow = dt.Select("begintime >='" + beginTime.ToString() + "' and  beginTime<'" + beginTime.AddDays(days).AddSeconds(-1) + "'");
                //if (dtRow.Length > 0)
                if (dtlist_sum > 0)
                {
                    //Count += dtRow.Length;
                    Count += dtlist_sum;
                    for (int j = 1; j <= days; j++)
                    {
                        string j_day = j.ToString().PadLeft(2, '0');
                        DataRow dr1 = dtNew.NewRow();
                        var dayinlist = dtConnect.Select("yearmonthdays = '" + beginTime.ToString("yyyy-MM") + "-" + j_day + "' ");
                        //if (dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<'" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length > 0)
                        int day_count = (from DataRow dr in dayinlist select dr.Field<int>("ccount")).FirstOrDefault();
                        if (day_count > 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["总数"] = day_count;
                            dr1["Count"] = "1";
                            dtNew.Rows.Add(dr1);
                        }
                    }

                    if (DayCount != 0)
                    {
                        drNew["月份"] = beginTime.Month + "月";
                        drNew["日期"] = "总计";
                        //drNew["总数"] = dtRow.Length;
                        drNew["总数"] = dtlist_sum;
                        drNew["Count"] = DayCount.ToString();
                        dtNew.Rows.Add(drNew);
                    }
                    DayCount = 0;
                    monthcount += 1;
                }
            }
            if (monthcount != 0)
            {
                DataRow drzj = dtNew.NewRow();
                drzj["月份"] = dtNow.Year + "年";
                drzj["日期"] = "总计";
                drzj["总数"] = Count;
                drzj["Count"] = monthcount.ToString();
                dtNew.Rows.Add(drzj);
            }

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

        }

        private DataTable GetData(string years)
        {
            DataTable dtNew = new DataTable();
            try
            {
                #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").Tables[0];
                DateTime dtNow;
                int Count = 0;
                int DayCount = 0;

                if (string.IsNullOrEmpty(years))
                {
                    dtNow = DateTime.Now;
                }
                else
                {
                    dtNow = DateTime.Parse(years + "年");
                }
                int monthcount = 0;
                for (int i = 1; i <= 12; i++)
                {
                    DataRow drNew = dtNew.NewRow();

                    DateTime beginTime = Convert.ToDateTime(dtNow.Year + "-" + i + "-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;
                        monthcount += 1;
                    }
                }
                if (monthcount != 0)
                {
                    DataRow drzj = dtNew.NewRow();
                    drzj["月份"] = dtNow.Year + "年";
                    drzj["日期"] = "总计";
                    drzj["总数"] = Count;
                    dtNew.Rows.Add(drzj);
                }
            }
            catch (Exception ex)
            { }
            return dtNew;
        }

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

        //获取图形数据
        public ActionResult GetChartData(string years)
        {
            string sql = "";
            string[] arr = new string[12];
            if (!string.IsNullOrEmpty(years))
            {
                sql += $" and CONVERT(char(4),BeginTime,20) = '{years}' ";
            }
            //DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1").Tables[0];
            //var list = bll.DataTableToList(bll.GetList($" 1=1 ").Tables[0]);
            DataTable dtConnect = DbHelperSQL.Query($"select CONVERT(varchar(10),BeginTime, 23) AS yearmonthdays,CONVERT(char(7),BeginTime,20) AS yearmonths,COUNT(1) ccount FROM T_Call_CallRecords WITH(NOLOCK) WHERE 1=1 {sql} group by CONVERT(varchar(10),BeginTime, 23),CONVERT(char(7),BeginTime,20) order by CONVERT(varchar(10),BeginTime, 23)").Tables[0];
            DateTime dtNow;

            if (string.IsNullOrEmpty(years))
            {
                dtNow = DateTime.Now;
            }
            else
            {
                dtNow = DateTime.Parse(years + "年");
            }
            for (int i = 1; i <= 12; i++)
            {
                DateTime beginTime = Convert.ToDateTime(dtNow.Year + "-" + i + "-01 00:00:00");
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);

                var drConnect = dtConnect.Select("yearmonths='" + beginTime.ToString("yyyy-MM") + "' ");
                var count = (from DataRow dr in drConnect select dr.Field<int>("ccount")).Sum();
                if (count > 0)
                {
                    arr[i - 1] = count.ToString();
                }
                else
                {
                    arr[i - 1] = "0";
                }


                //DateTime beginTime = Convert.ToDateTime(dtNow.Year + "-" + i + "-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) + "'");
                //var listByTime = list.Where(x => x.BeginTime >= Convert.ToDateTime(beginTime.ToString()) && x.BeginTime < beginTime.AddDays(days).AddSeconds(-1));
                ////if (dtRow.Length > 0)
                //if (listByTime.Count() > 0)
                //{
                //    //arr[i-1] = dtRow.Length.ToString();
                //    arr[i - 1] = listByTime.Count().ToString();
                //}
                //else
                //{
                //    arr[i-1] = "0";
                //}
            }

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

        }
    }
}