YZ_BSF/HJGL/FineUIPro.Web/Global.asax.cs

214 lines
8.2 KiB
C#

namespace FineUIPro.Web
{
using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using BLL;
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 自动启用插件标志文件路径
/// </summary>
private static string applicationActiveFlagFilePhysicalPath = String.Empty;
protected void Application_Start(object sender, EventArgs e)
{
Application["OnlineUserCount"] = 0;
try
{
Funs.RootPath = Server.MapPath("~/");
Funs.ClientId = ConfigurationManager.AppSettings["clientId"];
Funs.ClientSecret = ConfigurationManager.AppSettings["clientSecret"];
Funs.Redirect_url = ConfigurationManager.AppSettings["redirect_url"];
// 日志文件所在目录
ErrLogInfo.DefaultErrLogFullPath = Server.MapPath("~/ErrLog.txt");
//Funs.ConnString = ConfigurationManager.AppSettings["ConnectionString"];
Funs.SystemName = ConfigurationManager.AppSettings["SystemName"];
Funs.ConnString = ConfigurationManager.ConnectionStrings["HJGLDBConnectionString"].ConnectionString;
}
catch (Exception ex)
{
ErrLogInfo.WriteLog(ex);
AppDomain.Unload(AppDomain.CurrentDomain);
}
//开启线程 点口周期
System.Threading.Thread LoadServiceData = new System.Threading.Thread(new System.Threading.ThreadStart(CheckCycleDay));
LoadServiceData.Start();
// 清理日志
if (DateTime.Now.Hour == 7)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
//12小时执行一次
aTimer.Interval = 12 * 60 * 60 * 1000;
aTimer.Enabled = true;
aTimer.Start();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(DeleteLog);
}
}
private void CheckCycleDay()
{
//定义一个定时器,并开启和配置相关属性
System.Timers.Timer ExpirePoint = new System.Timers.Timer();
//执行任务的周期 ,60分钟
ExpirePoint.Interval = 1000 * 60 * 60;
ExpirePoint.Enabled = true;
ExpirePoint.Start();
ExpirePoint.Elapsed += new System.Timers.ElapsedEventHandler(CheckCycleDay_Elapsed);
}
void CheckCycleDay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var project = from x in Funs.DB.Base_Project where x.IsClosed == false select x;
foreach (var pro in project)
{
DateTime newDate = DateTime.Now.Date;
int cycleDay = 15;
var set = BLL.Sys_SetService.GetSetById("4", pro.ProjectId);
if (set != null && !string.IsNullOrEmpty(set.SetValue))
{
cycleDay = Convert.ToInt32(set.SetValue);
}
var point = from x in Funs.DB.Batch_PointBatch where x.ProjectId == pro.ProjectId && x.StartDate <= newDate.AddDays(-cycleDay) && x.EndDate == null select x;
foreach (var p in point)
{
var pointBatchItem = (from x in Funs.DB.Batch_PointBatchItem
where x.PointBatchId == p.PointBatchId
orderby x.CreatDate
select x).ToList();
var rate = BLL.Base_DetectionRateService.GetDetectionRateByDetectionRateId(p.DetectionRateId);
if (pointBatchItem.Where(x => x.PointState == "1").Count() == 0) // 表示批里没有点口
{
if (pointBatchItem.Count() > 0)
{
BLL.Batch_PointBatchItemService.UpdatePointBatchItem(pointBatchItem[0].PointBatchItemId, "1", System.DateTime.Now, null);
Batch_PointBatchService.UpdatePointBatch(p.PointBatchId, System.DateTime.Now);
}
}
if (pointBatchItem.Count() > 0 && pointBatchItem.Count() >= (100 / rate.DetectionRateValue))
{
//结束批前如点口未关闭,则先关闭点口
Batch_PointBatchService.UpdatePointBatch(p.PointBatchId, System.DateTime.Now);
Batch_PointBatchService.UpdatePointBatchClearDate(p.PointBatchId, System.DateTime.Now); //结束批
}
}
}
}
private void DeleteLog(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime delDate = DateTime.Now.AddMonths(-1);
var logs = from x in Funs.DB.Sys_Log where x.OperationTime <= delDate select x;
Funs.DB.Sys_Log.DeleteAllOnSubmit(logs);
Funs.DB.SubmitChanges();
}
protected void Session_Start(object sender, EventArgs e)
{
Session.Timeout = 36000;
// 这种统计在线人数的做法会有一定的误差
Application.Lock();
Application["OnlineUserCount"] = (int)Application["OnlineUserCount"] + 1;
Application.UnLock();
// https访问secure设置为true
if (Request.IsSecureConnection)
{
Response.Cookies["ASP.NET_SessionId"].Secure = true;
}
}
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;
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));
if (Request.UserHostAddress != null)
{
errLog.Append(String.Format(CultureInfo.InvariantCulture, "IP地址:{0}\r\n", Request.UserHostAddress));
}
if (Session != null && Session["CurrUser"] != null)
{
errLog.Append(String.Format(CultureInfo.InvariantCulture, "操作人员:{0}\r\n", BLL.Sys_UserService.GetUsersByUserId(((Model.Sys_User)Session["CurrUser"]).UserId).UserName));
}
else
{
PageBase.ZXRefresh(Request.ApplicationPath + "/LogOff.aspx");
}
if (ex is HttpRequestValidationException)
{
PageBase.ZXRefresh(Request.ApplicationPath + "/Wrong.aspx?Message=0");
}
else if (ex is FriendlyException)
{
PageBase.ZXRefresh(Request.ApplicationPath + "/Wrong.aspx?MessageText=" + ex.Message);
}
}
catch
{
try
{
PageBase.ZXRefresh(Request.ApplicationPath + "/OperationError.aspx");
}
catch
{
}
}
finally
{
ErrLogInfo.WriteLog(ex, errLog == null ? null : errLog.ToString());
Server.ClearError();
PageBase.ZXRefresh(Request.ApplicationPath + "/OperationError.aspx");
}
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}