using System; using Module.DB.Model; using Newtonsoft.Json; using RailSee.RealTimeDBO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Module.Common; namespace Module.DB.SQLServerDAL { public class DataClear { private AlarmInfo alarmRecordService = new AlarmInfo(); private AssignmentInfo assignmentInfoService = new AssignmentInfo(); RedisHelper redis = new RedisHelper(2); //指定连接的库,默认是0 public void SqlDataClear() { string strJson = redis.GetStrValue("CurrentTimeInfo"); if (strJson != null) { MJsonTime jsonResult = JsonConvert.DeserializeObject(strJson); if (jsonResult != null) { double dResult = GetTime(Convert.ToDateTime(jsonResult.f_curr_time)); if (dResult >= 180) { alarmRecordService.DeleteAllAlarmInfo(jsonResult.f_curr_time, DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss")); assignmentInfoService.DeleteAllAssignmentInfo(jsonResult.f_curr_time, DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss")); MJsonTime mJsonTime = new MJsonTime(); mJsonTime.f_curr_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Task jsonResultToRedis = redis.SetStrValueAsync("CurrentTimeInfo", mJsonTime); } } } else { MJsonTime mJsonTime = new MJsonTime(); mJsonTime.f_curr_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Task jsonResultToRedis = redis.SetStrValueAsync("CurrentTimeInfo", mJsonTime); } } public static double GetTime(DateTime timeA) { //timeA 表示需要计算 DateTime timeB = DateTime.Now; //获取当前时间 TimeSpan ts = timeB - timeA; //计算时间差 double time = ts.TotalDays; //将时间差转换为天 return time; } } }