using NPOI.HSSF.Record;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEAWCS_Core.BaseServices;
|
using WIDESEAWCS_ISystemRepository;
|
using WIDESEAWCS_ISystemServices;
|
using WIDESEAWCS_Model.Models;
|
|
namespace WIDESEAWCS_SystemServices
|
{
|
public class Sys_LogService : ServiceBase<Sys_Log, ISys_LogRepository>, ISys_LogService
|
{
|
private static DateTime? _lastExecutionDate = null;
|
private readonly object _lock = new object();
|
|
public Sys_LogService(ISys_LogRepository BaseDal) : base(BaseDal)
|
{
|
|
}
|
|
public void DeleteOldLogs()
|
{
|
// 使用锁确保线程安全
|
lock (_lock)
|
{
|
var today = DateTime.Today;
|
|
// 检查今天是否已经执行过
|
if (_lastExecutionDate.HasValue && _lastExecutionDate.Value.Date == today)
|
{
|
return;
|
}
|
try
|
{
|
// 计算3个月前的日期
|
var threeMonthsAgo = DateTime.Now.AddMonths(-3);
|
// 计算3天前的日期
|
var sevenDaysAgo = DateTime.Now.AddDays(-3);
|
// 特定URL
|
var specificUrl = "http://11.2.30.141:10870/interfaces/api/amr/robotQuery";
|
|
// 构建删除条件:3个月前的日志 或 (7天前且URL为特定地址的日志)
|
var result = BaseDal.Db.Deleteable<Sys_Log>()
|
.Where(log => log.EndDate < threeMonthsAgo ||
|
(log.EndDate < sevenDaysAgo && log.Url == specificUrl))
|
.ExecuteCommand();
|
// 更新最后执行日期为今天
|
_lastExecutionDate = today;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"执行日志清理时发生错误:{ex.Message}");
|
// 发生错误时不更新最后执行日期,以便重试
|
}
|
}
|
}
|
}
|
}
|