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
{
    public class InfoController : BaseController
    {
        private BLL.T_Wo_WorkOrder workOrderBLL = new BLL.T_Wo_WorkOrder();
        private BLL.T_RegionCategory regionCategoryBLL = new BLL.T_RegionCategory();
        private BLL.T_Sys_DictionaryValue dicValueBLL = new BLL.T_Sys_DictionaryValue();
        private BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department();

        #region 
        //来源渠道情况
        public ActionResult GetByQD()
        {
            var dt1 = Getslqd24();
            var dt2 = GetArea();
            var dt3 = GetArea24();


            return Success("加载成功", new
            {
                a = dt1,
                b = dt2,
                c = dt3
            });
        }

        //接单部门情况
        public ActionResult GetByDept()
        {
            var dt1 = GetJD();
            var dt2 = Getdpt();
            var dt3 = GetGDCL();


            return Success("加载成功", new
            {
                a = dt1,
                b = dt2,
                c = dt3
            });
        }

        //投诉举报情况
        #endregion

        #region 1.来源渠道情况
        //受理渠道24小时实时统计
        private object Getslqd24()
        {
            DataTable dt=DbHelperSQL.Query("select F_Name,(select COUNT(1) from T_Wo_WorkOrder where  IsDel=0 and datediff(day,createtime,getdate())=0 and Source=F_Name) con from T_Sys_DictionaryValue where F_DictionaryFlag='GDLY'  and F_State=1").Tables[0];
            
            return dt;
        }
        //受理区域统计
        private object GetArea()
        {
            //186 郑州市
            DataTable dt = DbHelperSQL.Query("select F_RegionName,(select COUNT(1) from T_Wo_WorkOrder where  IsDel=0 and datediff(day,createtime,getdate())=0 and County=F_RegionName) con from T_RegionCategory where  F_DeleteFlag=0 and  F_ParentId=186").Tables[0];

            return dt;
        }
        //受理区域统计(按小时统计)
        private object GetArea24()
        {
            DataTable dt = DbHelperSQL.Query("select datepart(hh,CreateTime) hor,County, count(1) con from T_Wo_WorkOrder where IsDel = 0 and datediff(day, createtime, getdate()) = 0 group by datepart(hh, CreateTime), County").Tables[0];
            var regionList = regionCategoryBLL.DataTableToList(regionCategoryBLL.GetList(" F_DeleteFlag=0 and  F_ParentId=186 ").Tables[0]);

            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x => new
            {
                Time = x,
                Arealist = regionList.Select(z =>
                {
                    var con = dt.Select(" hor=" + x.ToString() + " and County='" + z.F_RegionName + "'")[0]["con"].ToString();
                    if (string.IsNullOrEmpty(con))
                    {
                        con = "0";
                    }
                    return new
                    {
                        Area = z.F_RegionName,
                        Count = con
                    };
                }).OrderBy(z => z.Area)
            });
            
            return query3;
        }

        #endregion

        #region 2.接单部门情况
        //接单情况汇总(月份)
        private object GetJD()
        {
            DataTable dt = DbHelperSQL.Query("select datepart(month,CreateTime) mon,TypeClass, count(1) con from T_Wo_WorkOrder where IsDel = 0 and datediff(month,createtime,getdate())<=12 group by datepart(month, CreateTime), TypeClass) desc").Tables[0];
            var tsList = dicValueBLL.DataTableToList(dicValueBLL.GetList(" F_DictionaryFlag='TSLX'  and F_State=1  ").Tables[0]);

            int[] months = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            var query3 = months.Select(x => new
            {
                Month = DateTime.Now.AddMonths(x - 1).ToString("yyyyMM"),
                TSList = tsList.Select(z =>
                {
                    var con = dt.Select(" mon=" + DateTime.Now.AddMonths(x - 1).Month + " and TypeClass='" + z.F_DictionaryValueId + "'")[0]["con"].ToString();
                    if (string.IsNullOrEmpty(con))
                    {
                        con = "0";
                    }
                    return new
                    {
                        tsname = z.F_Name,
                        Count = con
                    };
                }).OrderBy(z => z.tsname)
            });

            return query3;
        }

        //各部门受理工单情况
        private object Getdpt()
        {
            DataTable dt = DbHelperSQL.Query("select F_DeptName,(select COUNT(1) from T_Wo_WorkOrder where  IsDel=0 and datediff(day,createtime,getdate())=0 and ResponDept=F_DeptId) con from T_Sys_Department where F_State=1").Tables[0];

            return dt;
        }

        //工单处理时间分布（小时）
        private object GetGDCL()
        {
            DataTable dt = DbHelperSQL.Query("select datepart(hh,CreateTime) hor,TypeClass, count(1) con from T_Wo_WorkOrder where IsDel = 0 and datediff(day,createtime,getdate())=0 group by datepart(hh, CreateTime), TypeClass").Tables[0];
            var tsList = dicValueBLL.DataTableToList(dicValueBLL.GetList(" F_DictionaryFlag='TSLX'  and F_State=1  ").Tables[0]);

            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x => new
            {
                Time = x,
                TSList = tsList.Select(z =>
                {
                    var con = dt.Select(" hor=" + x.ToString() + " and TypeClass='" + z.F_DictionaryValueId + "'")[0]["con"].ToString();
                    if (string.IsNullOrEmpty(con))
                    {
                        con = "0";
                    }
                    return new
                    {
                        tsname = z.F_Name,
                        Count = con
                    };
                }).OrderBy(z => z.tsname)
            });

            return query3;
        }
        #endregion

        #region 3.投诉举报情况
        //投诉/举报汇总
        //举报单位/个人统计
        //投诉关键字统计
        //投诉单位统计
        //举报关键字统计
        #endregion

        #region 4.话务数量情况
        //话务量实时数据统计
        private object GetHWLSS()
        {
            //接通量
            DataTable dt = DbHelperSQL.Query(" select datepart(hh,BeginTime) hor, count(1) con from T_Call_CallRecords where CallState=1 and datediff(day, BeginTime, getdate()) = 0 group by datepart(hh,BeginTime) ").Tables[0];
            //来电量
            DataTable dt1 = DbHelperSQL.Query(" select datepart(hh,BeginTime) hor, count(1) con from T_Call_CallRecords where CallType=0 and datediff(day, BeginTime, getdate()) = 0 group by datepart(hh,BeginTime) ").Tables[0];

            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x => {
                var hwcon = dt.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();//接通量
                var lhcon = dt1.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();//来电量

                if (string.IsNullOrEmpty(hwcon))
                {
                    hwcon = "0";
                }
                if (string.IsNullOrEmpty(lhcon))
                {
                    lhcon = "0";
                }
                return new
                {

                    Time = x,
                    hwcount = hwcon,
                    lhcount = lhcon,
                };
            });

            return query3;
        }
        //管辖区域来话量统计
        //坐席闲忙比例
        private object GetZXXM()
        {
            DataTable dt = DbHelperSQL.Query(" select datepart(hh,OccurTime) hor,State, count(1) from rep_agent_state where datediff(day, OccurTime, getdate()) = 0 group by datepart(hh,OccurTime),State ").Tables[0];

            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x => {
                var con= dt.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();//总量
                var kxcon = dt.Select(" hor=" + x.ToString() + " State=2 ")[0]["con"].ToString();//空闲
                var thcon = dt.Select(" hor=" + x.ToString() + " State=3 ")[0]["con"].ToString();//通话中
                var hhclcon = dt.Select(" hor=" + x.ToString() + " State=4 ")[0]["con"].ToString();//话后处理中
                var xxcon = dt.Select(" hor=" + x.ToString() + " State=5 ")[0]["con"].ToString();//小休
                var zlcon = dt.Select(" hor=" + x.ToString() + " State=6 ")[0]["con"].ToString();//被请求

                var kxbl = 0.00; var thbl = 0.00; var zmbl = 0.00;

                if (!string.IsNullOrEmpty(con))
                {
                    if (!string.IsNullOrEmpty(kxcon))
                    {
                        kxbl = double.Parse(kxcon) / double.Parse(con);
                    }
                    if (!string.IsNullOrEmpty(thcon))
                    {
                        thbl = double.Parse(thcon) / double.Parse(con);
                    }
                    if (!string.IsNullOrEmpty(xxcon))
                    {
                        zmbl = double.Parse(xxcon) / double.Parse(con);
                    }
                }
                return new
                {
                    Time = x,
                    kxbl = Math.Round(kxbl, 2),
                    thbl = Math.Round(thbl, 2),
                    zmbl = Math.Round(zmbl, 2),
                };
            });

            return query3;
        }
        //话务量总体统计
        private object GetHWZL()
        {
            //话务量
            DataTable dt = DbHelperSQL.Query(" select count(1) con from T_Call_CallRecords where datediff(day, BeginTime, getdate()) = 0 ").Tables[0];

            var hwcon = dt.Rows[0]["con"].ToString();//话务量
            if (string.IsNullOrEmpty(hwcon))
            {
                hwcon = "0";
            }
            //来话量
            DataTable dt1 = DbHelperSQL.Query(" select count(1) con from T_Call_CallRecords where CallType=0 and datediff(day, BeginTime, getdate()) = 0 ").Tables[0];
            var lhcon = dt1.Rows[0]["con"].ToString();//话务量
            if (string.IsNullOrEmpty(lhcon))
            {
                lhcon = "0";
            }

            //接通量,通话时长
            DataTable dt2 = DbHelperSQL.Query(" select count(1) con,sum(TalkLongTime) tltimes from T_Call_CallRecords where CallState=1 and datediff(day, BeginTime, getdate()) = 0 ").Tables[0];
            var jtcon = dt2.Rows[0]["con"].ToString();//接通量
            var thtimes = dt2.Rows[0]["tltimes"].ToString();//通话时长
            if (string.IsNullOrEmpty(jtcon))
            {
                jtcon = "0";
            }
            if (string.IsNullOrEmpty(thtimes))
            {
                thtimes = "0";
            }
            var jtl = 0.00;//接通率
            if (hwcon != "0")
            {
                jtl = double.Parse(jtcon) / double.Parse(hwcon) ;
            }
            var pjthtimes = 0;//平均通话时长
            if (jtcon != "0")
            {
                pjthtimes = int.Parse(thtimes) / int.Parse(jtcon);
            }

            var query3 = new {
                hwcon = hwcon,
                lhcon = lhcon,
                jtcon = jtcon,
                pjthtimes = pjthtimes,
                jtll= Math.Round(jtl, 2)
            };
            return query3;
        }
        #endregion

        #region 5.中心大数据
        //工单类型各数量
        //接通率统计源
        //投诉举报关键话题排名
        //通话数量统计
        private object GetHWL()
        {
            //话务量
            DataTable dt = DbHelperSQL.Query(" select datepart(hh,BeginTime) hor,count(1) con from T_Call_CallRecords where datediff(day, BeginTime, getdate()) = 0 group by datepart(hh,BeginTime) ").Tables[0];
            
            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x =>
            {
                var hwcon = dt.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();//话务量
                var wjt = 0;
                if (hwcon == "0")
                {
                    hwcon = "0";
                }
                return new
                {
                    Time = x,
                    hwcon = hwcon
                };
            });
            return query3;
        }
        //接通率统计
        private object GetJTL()
        {
            //话务量
            DataTable dt = DbHelperSQL.Query(" select datepart(hh,BeginTime) hor,count(1) con from T_Call_CallRecords where datediff(day, BeginTime, getdate()) = 0 group by datepart(hh,BeginTime) ").Tables[0];
            
            //接通量,通话时长
            DataTable dt2 = DbHelperSQL.Query(" select datepart(hh,BeginTime) hor,count(1) con from T_Call_CallRecords where CallState=1 and datediff(day, BeginTime, getdate()) = 0 group by datepart(hh,BeginTime) ").Tables[0];
            
            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x =>
            {
                var hwcon = dt.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();//话务量
                var jtcon = dt2.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();

                var wjt = 0;
                if (hwcon != "0")
                {
                    wjt = int.Parse(hwcon) - int.Parse(jtcon);
                }
                return new
                {
                    Time = x,
                    jtcon = jtcon,
                    wjtcon = wjt,
                };
            });
            return query3;
        }
        //平均通话时长统计
        private object GetPjthsc()
        {
            //接通量，通话时长
            DataTable dt = DbHelperSQL.Query(" select datepart(hh,BeginTime) hor,count(1) con,sum(TalkLongTime) tltimes from T_Call_CallRecords where CallState=1 and datediff(day, BeginTime, getdate()) = 0 group by datepart(hh,BeginTime) ").Tables[0];

            int[] times = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            var query3 = times.Select(x => {
                var jtcon = dt.Select(" hor=" + x.ToString() + " ")[0]["con"].ToString();
                var thtimes = dt.Select(" hor=" + x.ToString() + " ")[0]["tltimes"].ToString();

                var pjthsc = 0;
                if (!string.IsNullOrEmpty(jtcon))
                {
                    pjthsc = int.Parse(thtimes) / int.Parse(jtcon);
                }
                return new
                {
                    Time = x,
                    pjthsc= pjthsc,
                };
            });

            return query3;
        }
        #endregion

        #region 6.服务知识
        //工单处理时间统计和分布
        //群众咨询知识库情况统计
        //工单处理时间统计和分布
        #endregion
    }
}