using CallCenter.Utility;
using CallCenterApi.DB;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.QuartzService
{
  public   class ZhengWuDuiJie : QuartzJob
    {

        public ZhengWuDuiJie()
        {
            CronExpression = "0 0/5 * * * ?";
           DoWork += submit_dept_crnt_status;
        }
       public class Push
        {
            public string code { set; get; }
            public string message { set; get; }

            public body body { set; get; }
        }
        public class body
        {
            public errorObjs[] errorObjs { set; get; }
        }
        public class errorObjs
        {
            public string resturl { set; get; }
            public string errorid { set; get; }
            public string errormsg { set; get; }
        }
        public class paras
        {
            public string AREA_CODE { set; get; }
            public string CRNT_SEATNUM { set; get; }
            public string CRNT_TELNUM { set; get; }
            public string TDY_NUM { set; get; }
            public string TDY_TELNUM { set; get; }
            public string TDY_ZWNUM { set; get; }
            public string TDY_WZNUM { set; get; }
            public string TDY_WXNUM { set; get; }
            public string TDY_APNUM { set; get; }
            public string TDY_OTNUM { set; get; }
            public string TDY_TELRATE { set; get; }
        }
        /// <summary>
        /// 实时服务信息上报接口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void submit_dept_crnt_status(object sender, EventArgs e)

        {

            string token = GetToken();
           
            paras paras = new paras();
            paras.AREA_CODE = "410500";
            string ZX = " select top 1  Checkin from rep_service WITH(NOLOCK)  ";
            paras. CRNT_SEATNUM = DbHelperSQL.GetSingle(ZX).ToString ();

            string TH = "select top 1  Conversation from rep_service WITH(NOLOCK)  ";
            paras.CRNT_TELNUM = DbHelperSQL.GetSingle(TH).ToString();

            string GDCount = "select COUNT(1) from  T_Bus_WorkOrder WITH(NOLOCK)  where datediff(day , F_CreateTime   , getdate())= 0  ";
            paras.TDY_NUM = DbHelperSQL.GetSingle(GDCount).ToString();

            string ldCount = "SELECT COUNT(1) FROM T_Call_CallRecords  WITH(NOLOCK)  WHERE datediff(day , BeginTime    , getdate())= 0   ";
            paras.TDY_TELNUM = DbHelperSQL.GetSingle(ldCount).ToString();

            string DHCount = "SELECT  COUNT(1)  FROM T_Bus_WorkOrder WITH(NOLOCK) WHERE F_InfoSource = 1 AND datediff(day , F_CreateTime   , getdate())= 0    ";
            paras.TDY_ZWNUM = DbHelperSQL.GetSingle(DHCount).ToString();

            //暂时不知
            string WZCount = "SELECT  COUNT(1)  FROM T_Bus_WorkOrder WITH(NOLOCK) WHERE F_InfoSource in(4,7,2459) AND datediff(day , F_CreateTime   , getdate())= 0    ";
            paras.TDY_WZNUM = DbHelperSQL.GetSingle(WZCount).ToString();

            string WXCount = "SELECT  COUNT(1)  FROM T_Bus_WorkOrder WITH(NOLOCK) WHERE F_InfoSource =5 AND datediff(day , F_CreateTime   , getdate())= 0    ";
            paras.TDY_WXNUM = DbHelperSQL.GetSingle(WXCount).ToString();

            string APPCount = "SELECT  COUNT(1)  FROM T_Bus_WorkOrder WITH(NOLOCK) WHERE F_InfoSource in (2527,2458 )AND datediff(day , F_CreateTime   , getdate())= 0    ";
            paras.TDY_APNUM = DbHelperSQL.GetSingle(APPCount).ToString();

            string WBCount = "SELECT  COUNT(1)  FROM T_Bus_WorkOrder WITH(NOLOCK) WHERE F_InfoSource =3 AND datediff(day , F_CreateTime   , getdate())= 0    ";
            paras.TDY_OTNUM = DbHelperSQL.GetSingle(WBCount).ToString();


            string jt = "SELECT COUNT(1) FROM T_Call_CallRecords  WITH(NOLOCK)  WHERE datediff(day , BeginTime    , getdate())= 0 AND CallState =1  ";
            var jtl = DbHelperSQL.GetSingle(jt);

            string   TDY_TELRATE = "0";
            try
            {
                if (int .Parse (ldCount)>0)
                {
                    TDY_TELRATE = string.Format("{0:f2}",float  .Parse (jt)/ float.Parse(ldCount));
                }
            }
            catch
            {
            }

            paras.TDY_TELRATE = TDY_TELRATE;

            //Dictionary<string, object> values = new Dictionary<string, object>();
            //values.Add("access_token", token);
            //values.Add("paras", paras);
            //var jsonParam = JsonConvert.SerializeObject(values);
            //序列化参数
            string jsonParam = "access_token=" + token + "&paras=" + paras.ToJson();
           var responseString = HttpMethods.HttpPost("http://59.227.104.19:8000/share/submit_dept_crnt_status", jsonParam);

            //Push jo =JsonConvert.DeserializeObject<Push>(responseString);
            // if (jo .code =="0")
            //{
            //    string strSql = string.Format(@"INSERT INTO T_Sys_PushMsg ( [WorkOrderId], [Conent], [CreatTime], [Code], [Message], [Title], [ErrorObjs], [Remarks]) 
            //       values   ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}') ;select @@IDENTITY ", "", jsonParam, DateTime.Now, jo.code, jo.message , "实时服务信息上报接口", "", "");
            //    object objres = DbHelperSQL.GetSingle(strSql);
            //}
            // else
            //{
            //    string error = "";
            //    if (jo .body .errorObjs  !=null && jo.body.errorObjs.Length >0)
            //    {
            //        foreach (var it in jo.body.errorObjs)
            //        {
            //            if (!string .IsNullOrEmpty (it.resturl ))
            //            {
            //                error += " 推送接口名称:" + it.resturl;
            //            }
            //            if (!string.IsNullOrEmpty(it.resturl))
            //            {
            //                error += " 推送标识:" + it.errorid;
            //            }
            //            if (!string.IsNullOrEmpty(it.resturl))
            //            {
            //                error += " 推送失败原因:" + it.errormsg;
            //            }
            //        }
            //    }
            //        string strSql = string.Format(@"INSERT INTO T_Sys_PushMsg ( [WorkOrderId], [Conent], [CreatTime], [Code], [Message], [Title], [ErrorObjs], [Remarks]) 
            //       values   ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}') ;select @@IDENTITY ", "", jsonParam, DateTime.Now, jo.code, jo.message, "实时服务信息上报接口", error, "");
            //    object objres = DbHelperSQL.GetSingle(strSql);

                //}

        }
   
        private string GetToken()
        {

            //Dictionary<string, object> values = new Dictionary<string, object>();
            //values.Add("client_id", "d620caf2-d00f-41a9-ae99-c6dfd435875e");
            //values.Add("client_secret", "4f75914e-d517-4325-ac64-c321377b12f0");
            //values.Add("grant_type", "client_credentials");
            //var jsonParam = JsonConvert.SerializeObject(values);

            string jsonParam = "client_id=d620caf2-d00f-41a9-ae99-c6dfd435875e&client_secret=4f75914e-d517-4325-ac64-c321377b12f0&grant_type=client_credentials";
            var responseString = HttpMethods.HttpPost("http://59.227.104.17/epoint-sso-web/rest/oauth2/token", jsonParam);
            Token jo = JsonConvert.DeserializeObject<Token>(responseString);
            if (jo.status.code == "1")
            {
                if (!string.IsNullOrEmpty(jo.custom.access_token))
                    return jo.custom.access_token;
                else
                    return "";
            }
            else
                return "";

        }
        public class Token
        {
            public List<Controls> controls { set; get; }
            public Custom custom { set; get; }
            public Status status { set; get; }
        }
        public class Controls
        {

        }
        public class Custom
        {
            public string access_token { set; get; }
            public string refresh_token { set; get; }
            public string jsessionid { set; get; }
            public string expires_in { set; get; }
        }
        public class Status
        {
            public string code { set; get; }
        }

    }
}
