mh_frame_sps/APT.Infrastructure.Core/Thread/LDXThreadTimeRule.cs
2026-04-07 13:47:52 +08:00

156 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace APT.Infrastructure.Core
{
public class APTThreadTimeRuleParam
{
private ThreadTimeRuleLoopType _type;
public APTThreadTimeRuleParam(ThreadTimeRuleLoopType type)
{
_type = type;
}
public ThreadTimeRuleLoopType Type { get { return _type; } }
public int InternalTime { get; set; }
public int FiexedDay { get; set; }
public int FiexedHour { get; set; }
public int FiexedMinute { get; set; }
public int FiexedSecond { get; set; }
public int FiexedMonth { get; set; }
public int FiexedYear { get; set; }
public DateTime FiexedTime { get; set; }
}
public class APTThreadTimeRule
{
private ThreadTimeRuleType _rule = ThreadTimeRuleType.ExecuteOne;
private int DefaultInternal = 1;
private APTThreadTimeRuleParam _param;
public APTThreadTimeRuleParam Param
{
get { return _param; }
}
public APTThreadTimeRule(ThreadTimeRuleType rule, APTThreadTimeRuleParam param)
{
_rule = rule;
_param = param;
}
public bool NextRuleTime(ref DateTime time)
{
bool result = false;
DateTime t = time;
switch (_rule)
{
case ThreadTimeRuleType.EveryDayLoopByTime:
if (this.Param.Type == ThreadTimeRuleLoopType.Hour)
t = t.AddHours(this.Param.InternalTime <= 0 ? DefaultInternal : this.Param.InternalTime);
else if (this.Param.Type == ThreadTimeRuleLoopType.Second)
t = t.AddSeconds(this.Param.InternalTime <= 0 ? DefaultInternal : this.Param.InternalTime);
else if (this.Param.Type == ThreadTimeRuleLoopType.Minute)
t = t.AddMinutes( this.Param.InternalTime <= 0 ? DefaultInternal : this.Param.InternalTime);
else if (this.Param.Type == ThreadTimeRuleLoopType.Day)
t = t.AddDays(this.Param.InternalTime <= 0 ? DefaultInternal : this.Param.InternalTime);
else if (this.Param.Type == ThreadTimeRuleLoopType.Month)
t = t.AddMonths( this.Param.InternalTime <= 0 ? DefaultInternal : this.Param.InternalTime);
else if (this.Param.Type == ThreadTimeRuleLoopType.Year)
t = t.AddYears(this.Param.InternalTime <= 0 ? DefaultInternal : this.Param.InternalTime);
time = t;
result = true;
break;
case ThreadTimeRuleType.EveryDayFiexedTime:
if ( this.Param.Type == ThreadTimeRuleLoopType.Day)
{
t = t.Date.AddDays( this.Param.FiexedDay < 0 ? 1 : this.Param.FiexedDay);
t = t.AddHours(this.Param.FiexedHour < 1 || this.Param.FiexedHour >= 24 ? 12 : this.Param.FiexedHour);
t = t.AddMinutes(this.Param.FiexedMinute < 0 || this.Param.FiexedMinute >= 60 ? 0 : this.Param.FiexedMinute);
t = t.AddSeconds( this.Param.FiexedSecond < 0 || this.Param.FiexedSecond >= 60 ? 0 : this.Param.FiexedSecond);
}
else if (this.Param.Type == ThreadTimeRuleLoopType.Hour)
{
t = t.Date.AddHours(t.Hour + (this.Param.FiexedHour < 0 ? 1 : this.Param.FiexedHour));
t = t.AddMinutes( this.Param.FiexedMinute < 0 || this.Param.FiexedMinute >= 60 ? 0 : this.Param.FiexedMinute);
t = t.AddSeconds( this.Param.FiexedSecond < 0 || this.Param.FiexedSecond >= 60 ? 0 : this.Param.FiexedSecond);
}
else if (this.Param.Type == ThreadTimeRuleLoopType.Month)
{
int day = t.Day;
t = t.Date.AddDays(-day).AddMonths(this.Param.FiexedMonth < 0 ? 1 : this.Param.FiexedMonth);
t = t.AddDays( this.Param.FiexedDay < 1 || this.Param.FiexedDay > MaxDay(t.Year, t.Month) ? 1 : this.Param.FiexedDay);
t = t.AddHours(this.Param.FiexedHour < 1 || this.Param.FiexedHour >= 24 ? 12 : this.Param.FiexedHour);
t = t.AddMinutes(this.Param.FiexedMinute < 0 || this.Param.FiexedMinute >= 60 ? 0 : this.Param.FiexedMinute);
t = t.AddSeconds( this.Param.FiexedSecond < 0 || this.Param.FiexedSecond >= 60 ? 0 : this.Param.FiexedSecond);
}
else if (this.Param.Type == ThreadTimeRuleLoopType.Year)
{
int day = t.Day;
int month = t.Month;
t = t.Date.AddMonths(-month).AddDays(-day).AddYears( this.Param.FiexedYear < 0 ? 1 : this.Param.FiexedYear);
t = t.AddMonths(this.Param.FiexedMonth < 1 || this.Param.FiexedMonth > 12 ? 1 : this.Param.FiexedMonth);
t = t.AddDays(this.Param.FiexedDay < 1 || this.Param.FiexedDay > MaxDay(t.Year, t.Month) ? 1 : this.Param.FiexedDay);
t = t.AddHours( this.Param.FiexedHour < 1 || this.Param.FiexedHour >= 24 ? 12 : this.Param.FiexedHour);
t = t.AddMinutes( this.Param.FiexedMinute < 0 || this.Param.FiexedMinute >= 60 ? 0 : this.Param.FiexedMinute);
t = t.AddSeconds(this.Param.FiexedSecond < 0 || this.Param.FiexedSecond >= 60 ? 0 : this.Param.FiexedSecond);
}
if (t > time)
{
time = t;
result = true;
}
break;
case ThreadTimeRuleType.FiexedTime:
DateTime fiexdTime = this.Param.FiexedTime == null ? DateTime.MinValue : this.Param.FiexedTime;
if (fiexdTime > t)
{
time = t;
result = true;
}
break;
case ThreadTimeRuleType.Always:
time = t;
result = true;
break;
}
return result;
}
private int MaxDay(int year, int month)
{
int maxDay = 0;//用来标识一月几天
if (month == 2)
{
if (year % 4 == 0)//闰年
{
maxDay = 29;
}
else
maxDay = 28;
}
else
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: maxDay = 31; break;
case 4:
case 6:
case 9:
case 11: maxDay = 30; break;
}
}
return maxDay;
}
}
}