using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.QuartzService
{
    public class SMSTask : QuartzJob
    {
        public SMSTask()
        {
            DoWork += SMSTask_DoWork;
        }

        private void SMSTask_DoWork(object sender, EventArgs e)
        {
            Quartz.IJobExecutionContext it = sender as Quartz.IJobExecutionContext;
            var _scheduler = it.Scheduler;
            var jobDetail = it.JobDetail;

            string name = it.JobDetail.Key.Name;
            int id = Int32.Parse(name.Split('_')[1]);

            Log log = LogFactory.GetLogger("执行");
            QuartzWorker qw = new QuartzWorker();

            try
            {
                log.Warn("主任务执行");

                Common com = new Common();

                lock (this)
                {
                    string url = ConfigurationManager.AppSettings["smsurl"];
                    if (!string.IsNullOrEmpty(url))
                    {
                        string strzxUrl = url + "/SMSZXZB/ExecSendByJob";
                        string zxresult = com.HttpGet(strzxUrl, "count=" + ConfigurationManager.AppSettings["t_count"]);
                        JObject zxrt = JsonConvert.DeserializeObject<JObject>(zxresult);
                        if (zxrt != null && zxrt["state"].ToString() == "success")
                        {
                            JObject jsrt = JsonConvert.DeserializeObject<JObject>(zxrt["data"].ToString());
                            if (jsrt["suc"].ToString() != "" && jsrt["err"].ToString() != "")
                            {
                                log.Error("发送成功手机号：" + jsrt["suc"].ToString() + "\r\n" + "发送失败手机号：" + jsrt["err"].ToString());
                            }
                        }
                        else
                        {
                            log.Error("主任务执行失败，失败原因：" + zxrt["message"].ToString());
                        }
                    }
                    else
                    {
                        log.Warn("接口地址不存在");
                    }
                }
            }
            catch (Exception ex)
            {

                log.Warn("主任务执行失败，失败原因：" + ex.ToString());
            }
        }
    }
}
