xiazhengtongxue
2026-01-04 46908c0f79e7aab8a3fa41bfdcd8390bbc3659f2
ÏîÄ¿´úÂë/WCSServices/WIDESEAWCS_SystemServices/Sys_LogService.cs
@@ -1,6 +1,8 @@
using System;
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;
@@ -12,8 +14,49 @@
{
    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}");
                    // å‘生错误时不更新最后执行日期,以便重试
                }
            }
        }
    }
}
}