zs
2024-11-21 ffa436a9b3ee7ed01c749350f5aed44c7d6e43f6
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
using DataCapture_MA.Entity;
using DataCapture_MA.log4Net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace DataCapture_MA.DataHnadle
{
    /// <summary>
    /// 自动删除数据
    /// </summary>
    public class AutoDeleteHistoryDate
    {
        public static void Handler()
        {
            while (true)
            {
                try
                {
                    using(DbModel dbModel = new DbModel())
                    {
                        var beforeMonth = DateTimeOffset.Now.AddDays(-30).ToUnixTimeSeconds();
                        var oldDate = dbModel.RobotInfo.Where(x => x.CreateStamp < beforeMonth).ToList();
                        if (oldDate.Count > 0)
                        {
                            dbModel.RemoveRange(oldDate);
                            dbModel.SaveChanges();
                        }
                    }
                }
                catch (Exception e)
                {
                    Log4NetHelper.WriteInfoLog(LogType.PushMes, "删除数据异常:" + JsonConvert.SerializeObject(e));
                }
 
                Thread.Sleep(1000*60*60);
            }
        }
    }
}