//获取按小时执行的时间 public DateTime GetHourTime(BatchExecVM item, DateTime nowDate) { DateTime?nextDate = null; var hourList = item.Exec_Moment.Split(',').ToList(); var minHour = Convert.ToInt32(hourList.First()); var maxHour = Convert.ToInt32(hourList.Last()); if (hourList.Count() == 1) { //var intervalHour = Convert.ToInt32(item.Exec_Moment); nextDate = item.Next_Execution_Date.AddHours(1); } else //分割的值有多个 { if (nowDate.Minute == maxHour) //如果值等于最后的一个时间段 { nextDate = item.Next_Execution_Date.AddMinutes(60 - maxHour + minHour); } else { //找到列表的索引序号 int currentSeq = 0; for (int i = 0; i < hourList.Count(); i++) { if (Convert.ToInt16(hourList[i]) == nowDate.Minute) { currentSeq = i; break; } } nextDate = item.Next_Execution_Date.AddMinutes(Convert.ToInt16(hourList[currentSeq + 1]) - Convert.ToInt32(hourList[currentSeq])); } } return(nextDate.Value); }
//获取按月执行的时间 public DateTime GetMonthDay(BatchExecVM item, DateTime nowDate) { DateTime?nextDate = null; var dayList = item.Exec_Moment.Split(',').ToList(); var minDay = Convert.ToInt32(dayList.First()); var maxDay = Convert.ToInt32(dayList.Last()); var execTimeList = item.Exec_Time.Split(',').ToList(); //如果只有一个执行时间点 if (execTimeList.Count() == 1) { if (dayList.Count() == 1) { nextDate = item.Next_Execution_Date.AddMonths(1); } else { //找到列表的索引序号 for (int i = 0; i < dayList.Count(); i++) { if (Convert.ToInt16(dayList[i]) == nowDate.Day) { //获取这个月有多少天 var days = DateTime.DaysInMonth(item.Next_Execution_Date.Year, item.Next_Execution_Date.Month); if (i != dayList.Count() - 1) //不是最后一天即最后一个元素 { var intervalDay = Convert.ToInt32(dayList[i + 1]) - Convert.ToInt32(dayList[i]); nextDate = item.Next_Execution_Date.AddDays(intervalDay); break; } else //是最后一天即最后一个元素 { var intervalDay = days - Convert.ToInt32(dayList[i]) + Convert.ToInt32(dayList[0]); nextDate = item.Next_Execution_Date.AddDays(intervalDay); break; } } } } } else //如果有多个执行时间点 { var maxTime = execTimeList.Last(); var nowHour = nowDate.Hour; var nowMinute = nowDate.Minute; for (int i = 0; i < execTimeList.Count(); i++) { var timeList = execTimeList[i].Split(':').ToList(); var execTimeHour = Convert.ToInt32(timeList[0]); var execTimeMinute = Convert.ToInt32(timeList[1]); //如果当前的时间点不是最后执行的时间点 if (nowHour == execTimeHour && nowMinute == execTimeMinute && (i != execTimeList.Count() - 1)) { var nextTimeList = execTimeList[i + 1].Split(':').ToList(); var nextExecTimeHour = Convert.ToInt32(nextTimeList[0]); var nextExecTimeMinute = Convert.ToInt32(nextTimeList[1]); nextDate = item.Next_Execution_Date.Date.AddHours(nextExecTimeHour).AddMinutes(nextExecTimeMinute); break; } //如果当前的时间点是最后执行的时间点 if (nowHour == execTimeHour && nowMinute == execTimeMinute && (i == execTimeList.Count() - 1)) { var firstTimeList = execTimeList[0].Split(':').ToList(); var firstExecTimeHour = Convert.ToInt32(firstTimeList[0]); var firstExecTimeMinute = Convert.ToInt32(firstTimeList[1]); if (dayList.Count() == 1) { nextDate = item.Next_Execution_Date.Date.AddMonths(1).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); } else { //找到列表的索引序号 for (int j = 0; j < dayList.Count(); j++) { if (Convert.ToInt16(dayList[j]) == nowDate.Day) { //获取这个月有多少天 var days = DateTime.DaysInMonth(item.Next_Execution_Date.Year, item.Next_Execution_Date.Month); if (j != dayList.Count() - 1) //不是最后一天即不是最后一个元素 { var intervalDay = Convert.ToInt32(dayList[j + 1]) - Convert.ToInt32(dayList[j]); nextDate = item.Next_Execution_Date.Date.AddDays(intervalDay).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); break; } else //是最后一天即最后一个元素 { var intervalDay = days - Convert.ToInt32(dayList[j]) + Convert.ToInt32(dayList[0]); nextDate = item.Next_Execution_Date.Date.AddDays(intervalDay).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); break; } } } } } } } return(nextDate.Value); }
//获取按周执行的时间 public DateTime GetWeekDay(BatchExecVM item, DateTime nowDate) { DateTime?nextDate = null; var dayList = item.Exec_Moment.Split(',').ToList(); var execTimeList = item.Exec_Time.Split(',').ToList(); //如果只有一个执行时间点 if (execTimeList.Count() == 1) { //如果分割的列表是7个则说明每天执行 if (dayList.Count() == 7) { nextDate = item.Next_Execution_Date.AddDays(1); } else if (dayList.Count() == 1) //如果分割的列表是1个则说明每周执行一次 { nextDate = item.Next_Execution_Date.AddDays(7); } else //不是每天执行则根据数字判断 { //系统里面把星期天当成0 var dayOfInt = (int)nowDate.DayOfWeek; if (dayOfInt == 0) { dayOfInt = 7; } //找到列表的索引序号 for (int i = 0; i < dayList.Count(); i++) { //是最后一个元素 if (i == dayList.Count() - 1) { var interValDay = 7 - Convert.ToInt32(dayList[i]) + Convert.ToInt32(dayList[0]); nextDate = item.Next_Execution_Date.AddDays(interValDay); break; } if (Convert.ToInt16(dayList[i]) == dayOfInt) { nextDate = item.Next_Execution_Date.AddDays(Convert.ToInt16(dayList[i + 1]) - Convert.ToInt32(dayList[i])); break; } } } } else //如果有多个执行时间点 { var maxTime = execTimeList.Last(); var nowHour = nowDate.Hour; var nowMinute = nowDate.Minute; for (int i = 0; i < execTimeList.Count(); i++) { var timeList = execTimeList[i].Split(':').ToList(); var execTimeHour = Convert.ToInt32(timeList[0]); var execTimeMinute = Convert.ToInt32(timeList[1]); //如果当前的时间点不是最后执行的时间点 if (nowHour == execTimeHour && nowMinute == execTimeMinute && (i != execTimeList.Count() - 1)) { var nextTimeList = execTimeList[i + 1].Split(':').ToList(); var nextExecTimeHour = Convert.ToInt32(nextTimeList[0]); var nextExecTimeMinute = Convert.ToInt32(nextTimeList[1]); nextDate = item.Next_Execution_Date.Date.AddHours(nextExecTimeHour).AddMinutes(nextExecTimeMinute); break; } //如果当前的时间点是最后执行的时间点 if (nowHour == execTimeHour && nowMinute == execTimeMinute && (i == execTimeList.Count() - 1)) { var firstTimeList = execTimeList[0].Split(':').ToList(); var firstExecTimeHour = Convert.ToInt32(firstTimeList[0]); var firstExecTimeMinute = Convert.ToInt32(firstTimeList[1]); //判断Exec_Moment执行的日期是不是最后一天 if (dayList.Count() == 1) //如果设置的是每周几执行 { nextDate = item.Next_Execution_Date.Date.AddDays(7).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); } else if (dayList.Count() == 7)//每天执行1,2,3,4,5,6,7 { nextDate = item.Next_Execution_Date.Date.AddDays(1).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); } else //间隔执行1,3,5 { //系统里面把星期天当成0 var dayOfInt = (int)nowDate.DayOfWeek; if (dayOfInt == 0) { dayOfInt = 7; } //找到列表的索引序号 for (int j = 0; j < dayList.Count(); j++) { //是最后一个元素 if (j == dayList.Count() - 1) { var interValBBDay = 7 - Convert.ToInt32(dayList[j]) + Convert.ToInt32(dayList[0]); nextDate = item.Next_Execution_Date.Date.AddDays(interValBBDay).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); break; } if (Convert.ToInt16(dayList[j]) == dayOfInt) { nextDate = item.Next_Execution_Date.Date.AddDays(Convert.ToInt16(dayList[j + 1]) - Convert.ToInt32(dayList[j])).AddHours(firstExecTimeHour).AddMinutes(firstExecTimeMinute); break; } } } } } } return(nextDate.Value); }