using PushPlatform.Common.Util;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushInfo.Core.Quartz.Jobs
{
    public class PushJobExample
    {
        static ISchedulerFactory _sf = new StdSchedulerFactory();

        static IScheduler _sched = _sf.GetScheduler();

        static PushJobExample _instance = null;

        static object lockObj = new object();

        //private Sys_TaskLogic taskLogic { get; set; }

        public string Name
        {
            get
            {
                return GetType().Name;
            }
        }

        public string Group
        {
            get
            {
                return "startgroup";
            }
        }

        /// <summary>
        /// 单例模式
        /// </summary>
        public static PushJobExample Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObj)
                    {
                        if (_instance == null)
                        {
                            _instance = new PushJobExample();
                        }
                    }
                }
                return _instance;
            }
        }

        #region 运行
        /// <summary>
        /// 运行
        /// </summary>
        public virtual void Run()
        {
            try
            {
                IJobDetail loadTaskJob = JobBuilder.Create<LoadPushJob>()
                    .WithIdentity("loadTaskJob", Group)
                    .WithDescription("加载任务")
                    .Build();

                ISimpleTrigger loadTaskTrigger = (ISimpleTrigger)TriggerBuilder.Create()
                    .WithIdentity("loadTaskTrigger", Group)
                    .WithSimpleSchedule(x => x.WithIntervalInMinutes(3).RepeatForever())
                    .StartNow()
                    .Build();

                _sched.ScheduleJob(loadTaskJob, loadTaskTrigger);

                if (!_sched.IsStarted)
                    _sched.Start();
            }
            catch (Exception ex)
            {
                LogHelper.Error("加载任务异常" + ex.Message);
            }
        }
        #endregion
    }
}
