天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

217 lines
5.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QMTask.Core
{
/// <summary>
/// Cron表达式
/// </summary>
public class CronExpression
{
#region 构造函数
public CronExpression()
{
Seconds = "*";
Minutes = "*";
Hours = "*";
Day_of_Month = "*";
Month = "*";
Day_of_Week = "?";
Year = "";
}
#endregion
#region 创建
public void Build(PlanInfo plan)
{
switch (plan.PeriodType)
{
case "d":
if (plan.Period != 1)
{
this.Day_of_Month = "1/" + plan.Period;
}
if (string.IsNullOrEmpty(plan.OnceTime) == true)
{
//循环执行
this.SetInterval(plan.Interval, plan.IntervalType, plan.DayStartTime, plan.DayEndTime);
}
else
{
//每天执行一次
this.SetOnceTime(plan.OnceTime);
}
break;
case "w":
this.Day_of_Month = "?";
this.Day_of_Week = plan.ExecuteDay;
if (string.IsNullOrEmpty(plan.OnceTime) == true)
{
//循环执行
this.SetInterval(plan.Interval, plan.IntervalType, plan.DayStartTime, plan.DayEndTime);
}
else
{
//每天执行一次
this.SetOnceTime(plan.OnceTime);
}
break;
case "m":
if (plan.Period != 1)
{
this.Month = "1/" + plan.Period;
}
this.Day_of_Week = "?";
this.Day_of_Month = plan.ExecuteDay;
if (string.IsNullOrEmpty(plan.OnceTime) == true)
{
//循环执行
this.SetInterval(plan.Interval, plan.IntervalType, plan.DayStartTime, plan.DayEndTime);
}
else
{
//每天执行一次
this.SetOnceTime(plan.OnceTime);
}
break;
}
}
#endregion
#region 设置频率
private void SetInterval(int interval, string intervalType)
{
switch (intervalType)
{
case "s":
Seconds = "0/"+interval.ToString();
break;
case "m":
Minutes = "0/" + interval.ToString();
break;
case "h":
Hours = "0/" + interval.ToString();
break;
}
}
private void SetInterval(int interval, string intervalType, string DayStartTime, string DayEndTime)
{
if (string.IsNullOrEmpty(DayStartTime))
{
DayStartTime = "00";
}
else
{
DayStartTime = DayStartTime + "";
}
if (string.IsNullOrEmpty(DayEndTime))
{
DayEndTime = "23";
}
else
{
DayEndTime = DayEndTime + "";
}
Hours = DayStartTime + "-" + DayEndTime;
switch (intervalType)
{
case "s":
Seconds = "0/" + interval.ToString();
break;
case "m":
{
Seconds = "0";
Minutes = "0/" + interval.ToString();
break;
}
case "h":
{
Seconds = "0";
Minutes = "0";
Hours = Hours + "/" + interval.ToString();
break;
}
}
}
#endregion
#region 设置一次执行时间
private void SetOnceTime(string time)
{
string[] values = time.Split(":".ToCharArray());
Hours=values[0].TrimStart("0".ToCharArray());
if (Hours == "")
Hours = "0";
Minutes = values[1].TrimStart("0".ToCharArray());
if (Minutes == "")
Minutes = "0";
Seconds = values[2].TrimStart("0".ToCharArray());
if (Seconds == "")
Seconds = "0";
}
#endregion
#region 生成字符串
public override string ToString()
{
string str = string.Format("{0} {1} {2} {3} {4} {5} {6}"
,Seconds,Minutes,Hours,Day_of_Month,Month,Day_of_Week,Year);
return str.Trim();
}
#endregion
#region 属性
/// <summary>
/// 秒
/// </summary>
public string Seconds { get; set; }
/// <summary>
/// 分钟
/// </summary>
public string Minutes { get; set; }
/// <summary>
/// 小时
/// </summary>
public string Hours { get; set; }
/// <summary>
/// 每月的天
/// </summary>
public string Day_of_Month { get; set; }
/// <summary>
/// 月
/// </summary>
public string Month { get; set; }
/// <summary>
/// 每周的天
/// </summary>
public string Day_of_Week { get; set; }
//年
public string Year { get; set; }
#endregion
}
}