namespace FineUIPro.Web
{
    using BLL;
    using Model;
    using System;
    using System.Configuration;
    using System.Globalization;
    using System.Text;
    using System.Timers;
    using System.Linq;
    public class Global : System.Web.HttpApplication
    {
        /// 
        /// 自动启用插件标志文件路径
        /// 
        //private static string applicationActiveFlagFilePhysicalPath = String.Empty;
        protected void Application_Start(object sender, EventArgs e)
        {
            Application["OnlineUserCount"] = 0;
            try
            {
                Funs.RootPath = Server.MapPath("~/");
                // 日志文件所在目录
                ErrLogInfo.DefaultErrLogFullPath = Server.MapPath("~/ErrLog.txt");
                Funs.ConnString = ConfigurationManager.AppSettings["ConnectionString"];
                Funs.SystemName = ConfigurationManager.AppSettings["SystemName"];
                Funs.SGGLUrl = ConfigurationManager.AppSettings["SGGLUrl"];
                Funs.RealNameApiUrl = ConfigurationManager.AppSettings["RealNameApiUrl"];
                Funs.ControlApiUrl = ConfigurationManager.AppSettings["ControlApiUrl"];
                Funs.SystemVersion = ConfigurationManager.AppSettings["SystemVersion"];
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog("数据库连接:" + Funs.ConnString);
                ErrLogInfo.WriteLog(string.Empty, ex);
                //AppDomain.Unload(AppDomain.CurrentDomain);
            }
            ////焊接软件定时
            try
            {
                BLL.MonitorService.StartGetPipelineWeldingQuantities();
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog("焊接软件定时器启动失败!", ex);
            }
            ////材料软件定时
            try
            {
                BLL.MCSWebService.StartMonitor();
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog("材料软件定时器启动失败!", ex);
            }
            if (ConfigurationManager.AppSettings["EnableRealName"] == "True")
            {
                ////实名制同步定时器
                try
                {
                    BLL.RealNameMonitorService.StartMonitor();
                    //  BLL.RealNameMonitorService.StartMonitorProject();
                    SynchroSetService.InsertRealNamePushLog(null, null, "实名制定时器开始启动", "sucess", "200", null, "成功", null);
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("实名制同步定时器启动失败!", ex);
                }
                //////根据考勤记录修改人员状态定时器  此任务放入推送实名制定时任务中,同时操作人员表,导致死锁
                //try
                //{
                //    //BLL.RealNameMonitorService.StartUpdateSitePerson(); 
                //    //  BLL.RealNameMonitorService.StartMonitorProject();
                //    //SynchroSetService.InsertRealNamePushLog(null, null, "根据考勤记录修改人员状态定时器开始启动", "sucess", "200", null, "成功", null);
                //}
                //catch (Exception ex)
                //{
                //    ErrLogInfo.WriteLog("根据考勤记录修改人员状态定时器启动失败!", ex);
                //}
                ///实名制出入记录去重
                try
                {
                    BLL.RealNameMonitorService.StartInOutMonitor();
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("实名制出入记录去重定时器启动失败!", ex);
                }
                ///实名制日志清理定时器
                try
                {
                    BLL.RealNameMonitorService.StartMonitorDeletePushLog();
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("实名制日志清理定时器启动失败!", ex);
                }
                ///考勤记录清理定时器
                try
                {
                    BLL.RealNameMonitorService.StartMonitorCleanAttendance();
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("考勤记录清理定时器定时器启动失败!", ex);
                }
            }
            else
            {
                ////通用定时器
                try
                {
                    QuartzServices.Init();
                    BLL.MonitorService.StartMonitor();
                    BLL.MonitorService.StartMonitorEve();
                    BLL.MonitorService.StartInOutSync();
                    // BLL.MonitorService.StartPersonQuarterCheck();               
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("通用定时器启动失败!", ex);
                }
                ////自动校正人工时
                try
                {
                    BLL.MonitorService.StartMonitorPersonHour();
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("自动校正人工时定时器启动失败!", ex);
                }
                ////从博晟获取培训考试数据定时器
                try
                {
                    BLL.BOSHENGMonitorService.StartMonitor();
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("从博晟获取培训考试数据定时器启动失败!", ex);
                }
                ////从集团获取数据定时器
                try
                {
                    BLL.CNCECHSSEMonitorService.StartMonitor();
                }
                catch (Exception ex)
                {
                    ErrLogInfo.WriteLog("从集团获取数据定时器启动失败!", ex);
                }
            }
            //Aspose21.8.0破解注入
            new Aspose.Words.License().SetLicense(new System.IO.MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4yMDA2MDIwMTI2MzM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+OTM2ZTVmZDEtODY2Mi00YWJmLTk1YmQtYzhkYzBmNTNhZmE2PC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjEwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT5wSkpjQndRdnYxV1NxZ1kyOHFJYUFKSysvTFFVWWRrQ2x5THE2RUNLU0xDQ3dMNkEwMkJFTnh5L3JzQ1V3UExXbjV2bTl0TDRQRXE1aFAzY2s0WnhEejFiK1JIWTBuQkh1SEhBY01TL1BSeEJES0NGbWg1QVFZRTlrT0FxSzM5NVBSWmJRSGowOUNGTElVUzBMdnRmVkp5cUhjblJvU3dPQnVqT1oyeDc4WFE9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 60;
            // 这种统计在线人数的做法会有一定的误差
            Application.Lock();
            Application["OnlineUserCount"] = (int)Application["OnlineUserCount"] + 1;
            Application.UnLock();
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            StringBuilder errLog = null;
            Exception ex = null;
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                Sys_ErrLogInfo newErr = new Sys_ErrLogInfo
                {
                    ErrLogId = SQLHelper.GetNewID()
                };
                try
                {
                    // 获取错误类
                    ex = Server.GetLastError().InnerException;
                    if (ex == null)
                    {
                        ex = Server.GetLastError().GetBaseException();
                    }
                    errLog = new StringBuilder();
                    errLog.Append(String.Format(CultureInfo.InvariantCulture, "出错文件:{0}\r\n", Request.Url.AbsoluteUri));
                    newErr.ErrUrl = Request.Url.AbsoluteUri;
                    if (Request.UserHostAddress != null)
                    {
                        errLog.Append(String.Format(CultureInfo.InvariantCulture, "IP地址:{0}\r\n", Request.UserHostAddress));
                        newErr.ErrIP = Request.UserHostAddress;
                    }
                    if (Session != null && Session["CurrUser"] != null)
                    {
                        errLog.Append(String.Format(CultureInfo.InvariantCulture, "操作人员:{0}\r\n", ((Model.Sys_User)Session["CurrUser"]).UserName));
                        newErr.UserName = ((Model.Sys_User)Session["CurrUser"]).UserId;
                    }
                    else
                    {
                        PageBase.PageRefresh(Request.ApplicationPath + "/LogOff.aspx");
                    }
                }
                catch
                {
                    try
                    {
                        PageBase.PageRefresh(Request.ApplicationPath + "/LogOff.aspx");
                    }
                    catch
                    {
                    }
                }
                finally
                {
                    if (errLog != null)
                    {
                        db.Sys_ErrLogInfo.InsertOnSubmit(newErr);
                        db.SubmitChanges();
                    }
                    ErrLogInfo.WriteLog(newErr.ErrLogId, ex, errLog == null ? null : errLog.ToString());
                    Server.ClearError();
                    PageBase.PageRefresh(Request.ApplicationPath + "/LogOff.aspx");
                }
            }
        }
        /// 
        /// 缓存结束
        /// 
        /// 
        /// 
        protected void Session_End(object sender, EventArgs e)
        {
        }
        /// 
        /// 活动结束
        /// 
        /// 
        /// 
        protected void Application_End(object sender, EventArgs e)
        {
        }
    }
}