using RMYY_CallCenter_Api.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers
{
    [AllowAnonymous]
    public class InfoController : BaseController 
    {
        /// <summary>
        /// 获取当日顶部数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTopCountByNow()
        {
            var date = DateTime.Now;

         
            string sqlhw = "   select (select count(1) from T_Call_CallRecords " +
                " where datediff(day , BeginTime  , getdate())= 0)as hwcount" +
                " , COALESCE(SUM( case a.F_Type when 1000 then 1 else 0 end), 0)  zxcount" +
                "  , COALESCE(sum(case a.F_Type when 2000 then 1 else 0 end), 0) gzcount" +
                "  ,  COALESCE(sum(case a.F_Type when 3000 then 1 else 0 end), 0) zhcount" +
                "  , COALESCE(sum(case a.F_Type when 4000 then 1 else 0 end), 0) clcount " +
                "from T_Wo_CommonBase a   where datediff(day , F_CreateTIme  , getdate())= 0 and F_IsDelete = 0";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
          

            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }
        /// <summary>
        /// 获取当日话务量数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetConversationByNow()
        {
            var date = DateTime.Now;


            string sqlhw = " select COALESCE(SUM( case a.CallType when 0 then 1 else 0 end),0) hrcount," +
                "   COALESCE(SUM( case a.CallType when 1 then 1 else 0 end), 0) hccount," +
                "    (case count(1) when 0 then '100.00%' else (CONVERT(varchar(10), CONVERT(decimal(18, 2)," +
                "    COALESCE(SUM( case a.CallState when 1 then 1 else 0 end), 0) / COUNT(1) * 100)) + '%') end ) onrate" +
                "           from T_Call_CallRecords a WITH(NOLOCK)   where datediff(day , BeginTime  , getdate())= 0   ";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }


        /// <summary>
        /// 获取当日24小时呼入呼出
        /// </summary>
        /// <returns></returns>
        public ActionResult GetConversation24ByNow()
        {
            var date = DateTime.Now;
            var hr = date.Hour + 1;
            int[] hours = Enumerable.Range(0, hr).ToArray<int>();
            var obj = new
            {
                hours,
                hrcount = new List<int[]>(),
                hccount = new List<int[]>()
            };
            string sql = $" and   BeginTime>='{date.ToString("yyyy-MM-dd")} 00:00:00'  ";
            string sqlhw = " select datepart(hh, BeginTime ) hour,SUM(case CallType when 0 then 1 else 0 end ) hrcount ,SUM(case CallType when 1 then 1 else 0 end ) hccount  from T_Call_CallRecords where  1=1" + sql + " group by datepart(hh, BeginTime) order by datepart(hh, BeginTime)";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];

            int[] hrl = new int[hours.Length]; int[] hcl = new int[hours.Length];
            for (int i = 0; i < hours.Length; i++)
            {
                var drs = dthw.Select("hour='" + hours[i] + "'");
                int hrr = 0; int hc = 0;
                if (drs.Length > 0)
                {
                    hrr = (from DataRow dr in drs select dr.Field<int>("hrcount")).FirstOrDefault();
                    hc = (from DataRow dr in drs select dr.Field<int>("hccount")).FirstOrDefault();
                }
                hrl[i] = hrr;
                hcl[i] = hc;
            }
            obj.hrcount.Add(hrl);
            obj.hccount.Add(hcl);

            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取当日24小时工单
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWorkorder24ByNow()
        {
            var date = DateTime.Now;
            var hr = date.Hour + 1;
            int[] hours = Enumerable.Range(0, hr).ToArray<int>();
            var obj = new
            {
                hours,
                count=new List<int[]>()
            };
            string sql = $" and   F_CreateTIme>='{date.ToString("yyyy-MM-dd")} 00:00:00'  ";
            string sqlhw = " select datepart(hh, F_CreateTIme ) hour,count(1) count  from T_Wo_CommonBase where" +
                "   1 = 1  " + sql+
                "  group by datepart(hh, F_CreateTIme) order by datepart(hh, F_CreateTIme) ";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];

            int[] hrl = new int[hours.Length]; int[] hcl = new int[hours.Length];
            for (int i = 0; i < hours.Length; i++)
            {
                var drs = dthw.Select("hour='" + hours[i] + "'");
                int hrr = 0; 
                if (drs.Length > 0)
                {
                    hrr = (from DataRow dr in drs select dr.Field<int>("count")).FirstOrDefault();
                }
                hrl[i] = hrr;
            }
            obj.count.Add(hrl);
            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取当日IVR满意度统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GetIVRSatisfactionByNow()
        {
            var date = DateTime.Now;
          
            string sqlhw = "    select COALESCE(SUM( case a.MYD when 0 then 1 else 0 end),0) satisfied, " +
                "       COALESCE(SUM( case a.MYD when 1 then 1 else 0 end), 0) basatisfied," +
                "   COALESCE(SUM( case a.MYD when 2 then 1 else 0 end), 0) nosatisfied" +
                "     from T_Call_CallRecords a WITH(NOLOCK)" +
                "   where datediff(day , BeginTime  , getdate())= 0 ";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取当日工单满意度统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOrderSatisfactionByNow()
        {
            var date = DateTime.Now;

            string sqlhw = "    select COALESCE(SUM( case a.MYD when 0 then 1 else 0 end),0) satisfied, " +
                "       COALESCE(SUM( case a.MYD when 1 then 1 else 0 end), 0) basatisfied," +
                "   COALESCE(SUM( case a.MYD when 2 then 1 else 0 end), 0) nosatisfied" +
                "     from T_Wo_CommonBase a WITH(NOLOCK)" +
                "   where datediff(day , F_CreateTime  , getdate())= 0 ";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取当日车辆调度工单统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCarTypeByNow()
        {
            var date = DateTime.Now;


            string sqlhw = "       select a. F_GDID F_Type , a.F_Name,(SELECT COUNT(1) FROM T_Car_WorkOrder WHERE   F_IsDel=0 AND F_Type in( select F_GDId  from [dbo].[GetValueId](a.F_GDID))  " +
                "    AND datediff(day , F_CreateTime  , getdate())= 0) COUNT    from T_Sys_Gongdan a" +
                "     where F_ParentID = 4000 AND F_IsDelete = 0   ";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取当日故障报修工单统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GetFaultRepairTypeByNow()
        {
            var date = DateTime.Now;

            string sqlhw = " select a. F_GDID F_Type , a.F_Name,(SELECT COUNT(1) FROM T_FaultRepair_Base WHERE   F_IsDelete=0 AND F_WorkOrderCategory in( select F_GDId  from [dbo].[GetValueId](a.F_GDID)) " +
                "    AND datediff(day , F_CreateTime  , getdate())= 0) COUNT    from T_Sys_Gongdan  a" +
                "     where F_ParentID = 3000 AND F_IsDelete = 0  ";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }
        /// <summary>
        /// 获取当日综合调度工单统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSchedulingTypeByNow()
        {
            var date = DateTime.Now;

            string sqlhw = "    select a. F_GDID F_Type, a.F_Name,(SELECT COUNT(1) FROM T_IntegratedScheduling_Base WHERE F_IsDelete = 0" +
                " AND F_WorkOrderCategory in( select F_GDId  from [dbo].[GetValueId](a.F_GDID)) " +
                "    AND datediff(day , F_CreateTime  , getdate())= 0) COUNT    from T_Sys_Gongdan a" +
                "     where F_ParentID = 2000 AND F_IsDelete = 0";
            DataTable dthw = DbHelperSQL.Query(sqlhw).Tables[0];
            var obj = new
            {
                data = dthw
            };
            return Success("加载成功", obj);
        }
    }
}