xiazhengtongxue
2026-01-04 46908c0f79e7aab8a3fa41bfdcd8390bbc3659f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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}");
                    // 发生错误时不更新最后执行日期,以便重试
                }
            }
        }
    }
}