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.Net;
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; }
        }
        string FrontUrl = Configs.GetValue("FrontUrl");
        string ISfiring = Configs.GetValue("ISfiring");
        /// <summary>
        /// 实时服务信息上报接口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        public  void submit_dept_crnt_status(object sender, EventArgs e)

        {
            var Dataurl = HttpMethods.HttpPost("http://10.0.22.201:8002/Affairs/submit_dept_crnt_status");
            var Dataurl2 = HttpMethods.HttpPost("http://10.0.22.201:8002/Converge/RePush");
            var Dataurl1 = HttpMethods.HttpPost("http://10.0.22.201:8002/Converge/ReOtherPush");
        }
   
        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; }
        }
        public void Backhaul()
        {
            string sql = "and  F_IsDelete = 0 and  F_Administrative in(1,2)";
            DataTable dt = new DataTable();
            int recordCount = 0;
            dt = CallCenterApi.BLL.PagerBLL.GetListPager(
              "T_Bus_WorkOrder b WITH(NOLOCK)",
              "F_WorkOrderId",
              "*",
              sql,
              "ORDER BY F_CreateTime DESC",
              10000,
              1,
              true,
              out recordCount);
            var now = DateTime.Now;

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["F_Administrative"].ToString() == "1")
                    {
                        WebClient web = new WebClient();
                        web.Encoding = Encoding.UTF8;
                        string Dataurl = web.DownloadString("http://10.0.22.201:8002/Affairs/FinishWorkOrder" + $"?workorderid={dr["F_WorkOrderId"].ToString()}");

                    }
                    else if (dr["F_Administrative"].ToString() == "2")
                    {
                        WebClient web = new WebClient();
                        web.Encoding = Encoding.UTF8;
                        string Dataurl = web.DownloadString("http://10.0.22.201:8002/Affairs/case_sign" + $"?workorderid={dr["F_WorkOrderId"].ToString()}");
                    }
                }
            }
        }

    }
}
