using Edu.Common.Plugin;
using Edu.Module.Course;
using Edu.Module.Duty;
using System;
using System.IO;
using System.Threading;

namespace Edu.WebApi.Timers
{
    /// <summary>
    /// 定时任务
    /// </summary>
    public class TimerJobj
    {
        /// <summary>
        /// 值班处理对象
        /// </summary>
        private static readonly DutyPlanModule dutyPlanModule = AOP.AOPHelper.CreateAOPObject<DutyPlanModule>();

        /// <summary>
        /// 订单处理类对象
        /// </summary>
        private static readonly OrderModule orderModule = AOP.AOPHelper.CreateAOPObject<OrderModule>();

        /// <summary>
        /// 订单处理类对象
        /// </summary>
        private static readonly Module.EduTask.StopStudentClassModule stopStudentClassModule = AOP.AOPHelper.CreateAOPObject<Module.EduTask.StopStudentClassModule>();

        static System.Timers.Timer timer1;//计时器

        static System.Timers.Timer timer2;//定期处理未值班数据

        static System.Timers.Timer timer3;//定时处理订单生效日期

        static System.Timers.Timer timer4;//定时处理停课申请生效日期以及学员的上课状态

        public static void RunTimer()
        {
            timer1 = new System.Timers.Timer
            {
                Interval = 1000 * (60 * 60) * 3 //60分钟
            };
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(ClearFile);
            timer1.Enabled = true;

            timer2 = new System.Timers.Timer
            {
                Interval = (1000 * 60) * (1 * 60) //1小时执行一次
            };
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(DealDuty);
            timer2.Enabled = true;

            timer3 = new System.Timers.Timer()
            {
                Interval = (1000 * 60) * (1 * 60 * 6) //6小时执行一次
            };
            timer3.Elapsed += new System.Timers.ElapsedEventHandler(DealOrderEffectStatus);
            timer3.Enabled = true;


            timer4 = new System.Timers.Timer()
            {
                Interval = (1000 * 60) * (1 * 10 ) //1小时执行一次
            };
            timer4.Elapsed += new System.Timers.ElapsedEventHandler(UpdateAuditThroughStudent);
            timer4.Enabled = true;

        }

        public static void RunStop()
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
        }

        /// <summary>
        /// 防止重复提交
        /// </summary>
        private static int inTimer = 0;
        private static int inTimer2 = 0;
        private static int inTimer3 = 0;
        private static int inTimer4 = 0;

        /// <summary>
        /// 清理文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void ClearFile(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Interlocked.Exchange(ref inTimer, 1) == 0)
            {
                string rootBook = AppDomain.CurrentDomain.BaseDirectory;
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(rootBook + "/upfile/temporary");
                    if (dir != null && dir.Exists)
                    {
                        DirectoryInfo[] dirArr = dir.GetDirectories();
                        foreach (DirectoryInfo item in dirArr)
                        {
                            if (item.CreationTime < DateTime.Now.AddDays(-1))
                                item.Delete(true);
                        }
                        foreach (FileInfo fi in dir.GetFiles())
                        {
                            if (fi.CreationTime < DateTime.Now.AddDays(-1))
                                fi.Delete();
                        }
                    }

                    #region 导出文件的
                    DirectoryInfo QRdir = new DirectoryInfo(rootBook + "/upfile/ExportFile");
                    if (QRdir != null && QRdir.Exists)
                    {
                        foreach (FileInfo fi in QRdir.GetFiles())
                        {
                            if (fi.CreationTime < DateTime.Now.AddDays(-1))
                                fi.Delete();
                        }
                    }
                    #endregion
                }
                catch
                {
                    LogHelper.Write("清理临时文件失败_ClearFile");
                }
                Interlocked.Exchange(ref inTimer, 0);
            }
        }

        /// <summary>
        /// 处理值班数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void DealDuty(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Interlocked.Exchange(ref inTimer2, 1) == 0)
            {
                dutyPlanModule.UpdateDutyModule();
                Interlocked.Exchange(ref inTimer2, 0);
            }
        }

        /// <summary>
        /// 定时处理订单生效日期
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void DealOrderEffectStatus(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Interlocked.Exchange(ref inTimer3, 1) == 0)
            {
                orderModule.DealOrderEffectStatusModule();
                Interlocked.Exchange(ref inTimer3, 0);
            }
        }


        // <summary>
        /// 定时处理停课申请生效日期以及学员的上课状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void UpdateAuditThroughStudent(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Interlocked.Exchange(ref inTimer4, 1) == 0)
            {
                stopStudentClassModule.UpdateAuditThroughStudent();
                Interlocked.Exchange(ref inTimer4, 0);
            }
        }
    }
}