using CallCenter.Utility;
using CallCenter.Utility.Time;
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
{
    [Authority]
    public class TelWOReportController : BaseController
    {
        //获取表头
        public ActionResult GetColumnList()
        {
            String[] str = { "日期", "接听来电", "有效来电", "受理", "在线办结（直办）", "转办工单", "转办工单办结" };
            return Success("获取话务报表表头成功", str);
        }

        //话务报表
        public ActionResult GetTrafficReport(string usercode, DateTime? btime, DateTime? etime, int? type)
        {
            if (type == null)
            {
                type = 0;
            }
            DataTable dt = getDaydate(usercode, btime, etime);

            if (type == 1)
            {
                dt = getWeekdate(usercode, btime, etime);
            }
            if (type == 2)
            {
                dt = getMonthdate(usercode, btime, etime);
            }

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

        //导出数据
        public ActionResult ExptList(string usercode, DateTime? btime, DateTime? etime, int? type)
        {
            NPOIHelper npoi = new NPOIHelper();
            if (type == null)
            {
                type = 0;
            }
            DataTable dt = getDaydate(usercode, btime, etime);

            if (type == 1)
            {
                dt = getWeekdate(usercode, btime, etime);
            }
            if (type == 2)
            {
                dt = getMonthdate(usercode, btime, etime);
            }
            if (npoi.ExportToExcel("话务量数据", dt) == "")
            {
                return Success("导出成功");
            }
            else
            {
                return Error("导出失败");
            }
        }

        //日报表
        private DataTable getDaydate(string usercode, DateTime? btime, DateTime? etime)
        {
            DataTable dtNew = new DataTable();
            #region
            DataColumn dc1 = new DataColumn("日期", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("接听来电", Type.GetType("System.String"));
            DataColumn dc7 = 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(dc7);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            dtNew.Columns.Add(dc6);
            #endregion
            string sql = "", sqlwo = "";
            DateTime stime = DateTime.Now;
            DateTime endtime = DateTime.Now;

            if (btime != null)
            {
                stime = btime.Value;
            }
            if (etime != null)
            {
                endtime = etime.Value;
            }
            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and usercode='" + usercode.Trim() + "' ";
                sqlwo += " and F_CreateUser'" + usercode.Trim() + "' ";
            }
            sql += " and datediff(day,BeginTime,'" + stime.ToString("yyyy-MM-dd") + "')<=0 ";
            sql += " and datediff(day,BeginTime,'" + endtime.ToString("yyyy-MM-dd") + "')>=0 ";
            sqlwo += " and datediff(day,F_CreateTime,'" + stime.ToString("yyyy-MM-dd") + "')<=0 ";
            sqlwo += " and datediff(day,F_CreateTime,'" + endtime.ToString("yyyy-MM-dd") + "')>=0 ";
            TimeSpan ts = endtime - stime;
            //DataTable dt = DbHelperSQL.Query("select * from v_call_wo where CallType=0 and CallState=1 "+ sql).Tables[0];
            DataTable dt = DbHelperSQL.Query("select * from T_Call_CallRecords where CallType=0 and UserCode is not null " + sql).Tables[0];
            //DataTable dtwo = DbHelperSQL.Query("select * from T_Bus_WorkOrder where F_CallRecordId is not null and F_IsDelete=0 " + sqlwo).Tables[0];
            DataTable dtwo = DbHelperSQL.Query("select * from T_Bus_WorkOrder where F_InfoSource=1 and F_IsDelete=0 " + sqlwo).Tables[0];

            //日期 接听来电 受理（有效来电） 在线办结（直办） 转派工单 办结
            int ldcount = 0, yxldcount = 0, slcount = 0, zbcount = 0, zpcount = 0, bjcount = 0;
            int ldcountz = 0, yxldcountz = 0, slcountz = 0, zbcountz = 0, zpcountz = 0, bjcountz = 0;

            for (int i = 0; i <= ts.Days; i++)
            {
                #region 获取
                DataRow drNew = dtNew.NewRow();
                string key = stime.AddDays(i).ToString("yyyy-MM-dd");
                string sqlw = " BeginTime>='" + stime.AddDays(i).ToString("yyyy-MM-dd") + " 00:00:01' and BeginTime<='" + stime.AddDays(i).ToString("yyyy-MM-dd") + " 23:59:59' ";
                string sqlwo1 = " F_CreateTime>='" + stime.AddDays(i).ToString("yyyy-MM-dd") + " 00:00:01' and F_CreateTime<='" + stime.AddDays(i).ToString("yyyy-MM-dd") + " 23:59:59' ";
                ldcount = dt.Select(sqlw).Length;
                yxldcount = dt.Select(sqlw + " and CallState=1 ").Length;
                slcount = dtwo.Select(sqlwo1 + " ").Length;
                zbcount = dtwo.Select(sqlwo1 + " and F_IsResult=1 ").Length;
                zpcount = dtwo.Select(sqlwo1 + " and F_IsResult=0 ").Length;
                bjcount = dtwo.Select(sqlwo1 + " and F_IsResult=0 and F_WorkState=9 ").Length;

                drNew["日期"] = key;
                drNew["接听来电"] = ldcount;
                drNew["有效来电"] = yxldcount;
                drNew["受理"] = slcount;
                drNew["在线办结"] = zbcount;
                drNew["转派工单"] = zpcount;
                drNew["办结"] = bjcount;
                dtNew.Rows.Add(drNew);

                ldcountz += ldcount;
                yxldcountz += yxldcount;
                slcountz += slcount;
                zbcountz += zbcount;
                zpcountz += zpcount;
                bjcountz += bjcount;
                #endregion
            }

            #region 总计
            DataRow drNewz = dtNew.NewRow();
            drNewz["日期"] = "总计";
            drNewz["接听来电"] = ldcountz;
            drNewz["有效来电"] = yxldcountz;
            drNewz["受理"] = slcountz;
            drNewz["在线办结"] = zbcountz;
            drNewz["转派工单"] = zpcountz;
            drNewz["办结"] = bjcountz;

            dtNew.Rows.Add(drNewz);
            #endregion

            return dtNew;
        }

        //周报表
        private DataTable getWeekdate(string usercode, DateTime? btime, DateTime? etime)
        {
            DataTable dtNew = new DataTable();
            #region
            DataColumn dc1 = new DataColumn("日期", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("接听来电", Type.GetType("System.String"));
            DataColumn dc7 = 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(dc7);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            dtNew.Columns.Add(dc6);
            #endregion
            string sql = "", sqlwo = "";
            DateTime stime = DateTime.Now;
            DateTime endtime = DateTime.Now;

            if (btime != null)
            {
                stime = btime.Value;
            }
            if (etime != null)
            {
                endtime = etime.Value;
            }
            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and usercode='" + usercode.Trim() + "' ";
                sqlwo += " and F_CreateUser'" + usercode.Trim() + "' ";
            }
            sql += " and datediff(day,BeginTime,'" + stime.ToString("yyyy-MM-dd") + "')<=0 ";
            sql += " and datediff(day,BeginTime,'" + endtime.ToString("yyyy-MM-dd") + "')>=0 ";
            sqlwo += " and datediff(day,F_CreateTime,'" + stime.ToString("yyyy-MM-dd") + "')<=0 ";
            sqlwo += " and datediff(day,F_CreateTime,'" + endtime.ToString("yyyy-MM-dd") + "')>=0 ";
            TimeSpan ts = endtime - stime;
            //DataTable dt = DbHelperSQL.Query("select * from v_call_wo where CallType=0 and CallState=1 "+ sql).Tables[0];
            DataTable dt = DbHelperSQL.Query("select * from T_Call_CallRecords where CallType=0 and UserCode is not null " + sql).Tables[0];
            //DataTable dtwo = DbHelperSQL.Query("select * from T_Bus_WorkOrder where F_CallRecordId is not null " + sqlwo).Tables[0];
            DataTable dtwo = DbHelperSQL.Query("select * from T_Bus_WorkOrder where F_InfoSource=1 and F_IsDelete=0 " + sqlwo).Tables[0];

            //日期 接听来电 受理（有效来电） 在线办结（直办） 转派工单 办结
            int ldcount = 0, yxldcount = 0, slcount = 0, zbcount = 0, zpcount = 0, bjcount = 0;
            int ldcountz = 0, yxldcountz = 0, slcountz = 0, zbcountz = 0, zpcountz = 0, bjcountz = 0;
            int weeks = ts.Days / 7;
            if (ts.Days % 7 > 0)
                weeks = ts.Days / 7 + 1;

            for (int i = 0; i <= weeks; i++)
            {
                #region 获取
                DateTime st = DateTools.GetWeekFirstDayMon(stime.AddDays(i * 7));
                //DateTime st = stime.AddDays(i);
                DateTime et = DateTools.GetWeekLastDaySun(stime.AddDays(i * 7));
                string key = st.ToString("yyyy-MM-dd") + "-" + et.ToString("yyyy-MM-dd");
                #region
                if (i == 0)
                {
                    key = stime.ToString("yyyy-MM-dd") + "-" + et.ToString("yyyy-MM-dd");
                    if (i == weeks)
                        key = stime.ToString("yyyy-MM-dd") + "-" + endtime.ToString("yyyy-MM-dd");
                }
                else if (i > weeks)
                {
                    key = st.ToString("yyyy-MM-dd") + "-" + endtime.ToString("yyyy-MM-dd");
                }
                else if (i == weeks)
                {
                    if (st > endtime)
                        break;
                    else
                        key = st.ToString("yyyy-MM-dd") + "-" + endtime.ToString("yyyy-MM-dd");
                }

                if (et > endtime)
                    key = st.ToString("yyyy-MM-dd") + "-" + endtime.ToString("yyyy-MM-dd");
                #endregion
                string sqlw = " BeginTime>='" + st.ToString("yyyy-MM-dd") + " 00:00:01' and  BeginTime<='" + et.ToString("yyyy-MM-dd") + " 23:59:59'";
                string sqlwo1 = " F_CreateTime>='" + st.ToString("yyyy-MM-dd") + " 00:00:01' and F_CreateTime<='" + et.ToString("yyyy-MM-dd") + " 23:59:59' ";
                ldcount = dt.Select(sqlw).Length;
                yxldcount = dt.Select(sqlw + " and CallState=1 ").Length;
                slcount = dtwo.Select(sqlwo1 + " ").Length;
                zbcount = dtwo.Select(sqlwo1 + " and F_IsResult=1 ").Length;
                zpcount = dtwo.Select(sqlwo1 + " and F_IsResult=0 ").Length;
                //bjcount = dtwo.Select(sqlwo1 + " and F_WorkState=9 ").Length;
                bjcount = dtwo.Select(sqlwo1 + " and F_IsResult=0 and F_WorkState=9 ").Length;

                DataRow drNew = dtNew.NewRow();
                drNew["日期"] = key;
                drNew["接听来电"] = ldcount;
                drNew["有效来电"] = yxldcount;
                drNew["受理"] = slcount;
                drNew["在线办结"] = zbcount;
                drNew["转派工单"] = zpcount;
                drNew["办结"] = bjcount;
                dtNew.Rows.Add(drNew);

                ldcountz += ldcount;
                yxldcountz += yxldcount;
                slcountz += slcount;
                zbcountz += zbcount;
                zpcountz += zpcount;
                bjcountz += bjcount;
                #endregion
            }

            #region 总计
            DataRow drNewz = dtNew.NewRow();
            drNewz["日期"] = "总计";
            drNewz["接听来电"] = ldcountz;
            drNewz["有效来电"] = yxldcountz;
            drNewz["受理"] = slcountz;
            drNewz["在线办结"] = zbcountz;
            drNewz["转派工单"] = zpcountz;
            drNewz["办结"] = bjcountz;

            dtNew.Rows.Add(drNewz);
            #endregion

            return dtNew;
        }

        //月报表
        private DataTable getMonthdate(string usercode, DateTime? btime, DateTime? etime)
        {
            DataTable dtNew = new DataTable();
            #region
            DataColumn dc1 = new DataColumn("日期", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("接听来电", Type.GetType("System.String"));
            DataColumn dc7 = 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(dc7);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            dtNew.Columns.Add(dc6);
            #endregion
            string sql = "", sqlwo = "";
            DateTime stime = DateTime.Now;
            DateTime endtime = DateTime.Now;

            if (btime != null)
            {
                stime = btime.Value;
            }
            if (etime != null)
            {
                endtime = etime.Value;
            }
            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and usercode='" + usercode.Trim() + "' ";
                sqlwo += " and F_CreateUser'" + usercode.Trim() + "' ";
            }
            sql += " and datediff(day,BeginTime,'" + stime.ToString("yyyy-MM-dd") + "')<=0 ";
            sql += " and datediff(day,BeginTime,'" + endtime.ToString("yyyy-MM-dd") + "')>=0 ";
            sqlwo += " and datediff(day,F_CreateTime,'" + stime.ToString("yyyy-MM-dd") + "')<=0 ";
            sqlwo += " and datediff(day,F_CreateTime,'" + endtime.ToString("yyyy-MM-dd") + "')>=0 ";
            TimeSpan ts = endtime - stime;
            //DataTable dt = DbHelperSQL.Query("select * from v_call_wo where CallType=0 and CallState=1 "+ sql).Tables[0];
            DataTable dt = DbHelperSQL.Query("select * from T_Call_CallRecords where CallType=0 and UserCode is not null  " + sql).Tables[0];
            //DataTable dtwo = DbHelperSQL.Query("select * from T_Bus_WorkOrder where F_CallRecordId is not null " + sqlwo).Tables[0];
            DataTable dtwo = DbHelperSQL.Query("select * from T_Bus_WorkOrder where F_InfoSource=1 and F_IsDelete=0 " + sqlwo).Tables[0];

            int month = (endtime.Year - stime.Year) * 12 + (endtime.Month - stime.Month);

            //日期 接听来电 受理（有效来电） 在线办结（直办） 转派工单 办结
            int ldcount = 0, yxldcount = 0, slcount = 0, zbcount = 0, zpcount = 0, bjcount = 0;
            int ldcountz = 0, yxldcountz = 0, slcountz = 0, zbcountz = 0, zpcountz = 0, bjcountz = 0;

            for (int i = 0; i <= month; i++)
            {
                #region 获取
                DataRow drNew = dtNew.NewRow();

                DateTime st = DateTools.FirstDayOfMonth(stime.AddMonths(i));
                //DateTime st = stime.AddDays(i);
                DateTime et = DateTools.LastDayOfMonth(stime.AddMonths(i));

                string key = stime.AddMonths(i).ToString("yyyy-MM");

                string sqlw = " BeginTime>='" + st.ToString("yyyy-MM-dd") + " 00:00:01' and BeginTime<='" + et.ToString("yyyy-MM-dd") + " 23:59:59' ";
                string sqlwo1 = " F_CreateTime>='" + st.ToString("yyyy-MM-dd") + " 00:00:01' and F_CreateTime<='" + et.ToString("yyyy-MM-dd") + " 23:59:59' ";
                ldcount = dt.Select(sqlw).Length;
                yxldcount = dt.Select(sqlw + " and CallState=1 ").Length;
                slcount = dtwo.Select(sqlwo1 + " ").Length;
                zbcount = dtwo.Select(sqlwo1 + " and F_IsResult=1 ").Length;
                zpcount = dtwo.Select(sqlwo1 + " and F_IsResult=0 ").Length;
                //bjcount = dtwo.Select(sqlwo1 + " and F_WorkState=9 ").Length;
                bjcount = dtwo.Select(sqlwo1 + " and F_IsResult=0 and F_WorkState=9 ").Length;

                drNew["日期"] = key;
                drNew["接听来电"] = ldcount;
                drNew["有效来电"] = yxldcount;
                drNew["受理"] = slcount;
                drNew["在线办结"] = zbcount;
                drNew["转派工单"] = zpcount;
                drNew["办结"] = bjcount;
                dtNew.Rows.Add(drNew);

                ldcountz += ldcount;
                yxldcountz += yxldcount;
                slcountz += slcount;
                zbcountz += zbcount;
                zpcountz += zpcount;
                bjcountz += bjcount;
                #endregion
            }

            #region 总计
            DataRow drNewz = dtNew.NewRow();
            drNewz["日期"] = "总计";
            drNewz["接听来电"] = ldcountz;
            drNewz["有效来电"] = yxldcountz;
            drNewz["受理"] = slcountz;
            drNewz["在线办结"] = zbcountz;
            drNewz["转派工单"] = zpcountz;
            drNewz["办结"] = bjcountz;

            dtNew.Rows.Add(drNewz);
            #endregion

            return dtNew;
        }
    }
}