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()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                String[] str = { "月份", "日期", "总数" };
                res = Success("获取总呼叫量统计表头成功", str);
            }
            return res;
        }

        //获取数据
        public ActionResult GetDataList(string years)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                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


                //17=系统管理员,36=客服,37=组长,38=组员,39=业主,40=话务员,41=客服主管,43=分公司市场部主任,45=业务稽核,46=集团客户部,
                //47 =网络部,48=市场部经理,50=业务支撑中心,51=市场部,52=客户服务中心,53=总经理,54=副总经理,55=业务负责人,56=投诉处理员,57=基础业务主管,

                var sql = " 1=1 ";
                var roleId = CurrentUser.UserData.F_RoleId;
                var deptId = CurrentUser.UserData.F_DeptId;
                var deptCode = CurrentUser.UserData.F_DeptCode;
                var usercode = CurrentUser.UserData.F_UserCode;
                if (roleId != 0)
                {
                    if (roleId != 17)
                    {
                        if (deptCode.Replace("|0|1|", "").Length > 0)
                        {
                            if (roleId == 36 || roleId == 38 || roleId == 40)
                            {
                                sql += $" and  UserCode='{usercode}'";
                            }
                            else
                            {
                                sql += $" and  UserCode  in (SELECT F_UserCode FROM T_Sys_UserAccount WHERE F_DeptId={deptId})";
                            }

                        }
                    }
                }
                DataTable dt = new BLL.T_Call_CallRecords().GetList(sql).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;
                                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;
                        monthcount += 1;
                    }
                }
                if (monthcount != 0)
                {
                    DataRow drzj = dtNew.NewRow();
                    drzj["月份"] = dtNow.Year + "年";
                    drzj["日期"] = "总计";
                    drzj["总数"] = Count;
                    drzj["Count"] = monthcount.ToString();
                    dtNew.Rows.Add(drzj);
                }

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

        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)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = GetData(years);
                if (npoi.ExportToExcel("总呼叫量数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        //获取图形数据
        public ActionResult GetChartData(string years)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string[] arr = new string[12];
                var sql = " 1=1 ";
                var roleId = CurrentUser.UserData.F_RoleId;
                var deptId = CurrentUser.UserData.F_DeptId;
                var deptCode = CurrentUser.UserData.F_DeptCode;
                var usercode = CurrentUser.UserData.F_UserCode;
                if (roleId != 0)
                {
                    if (roleId != 17)
                    {
                        if (deptCode.Replace("|0|1|", "").Length > 0)
                        {
                            if (roleId == 36 || roleId == 38 || roleId == 40)
                            {
                                sql += $" and  UserCode='{usercode}'";
                            }
                            else
                            {
                                sql += $" and  UserCode  in (SELECT F_UserCode FROM T_Sys_UserAccount WHERE F_DeptId={deptId})";
                            }

                        }
                    }
                }
                DataTable dt = new BLL.T_Call_CallRecords().GetList(sql).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);
                    DataRow[] dtRow = dt.Select("begintime >='" + beginTime.ToString() + "' and  beginTime<'" + beginTime.AddDays(days).AddSeconds(-1) + "'");
                    if (dtRow.Length > 0)
                    {
                        arr[i - 1] = dtRow.Length.ToString();
                    }
                    else
                    {
                        arr[i - 1] = "0";
                    }
                }

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