# 计划的生成 周 月 季 半年 年

 /// <summary>
/// 某年某月第几个星期几是几号
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="weekIndex"></param>
/// <param name="week"></param>
/// <returns></returns>
private DateTime? GetDateTimeByWeekAndDay(int year, int month, int weekIndex, DayOfWeek week)
{
DateTime? result = null;
DateTime date = new DateTime(year, month, 1);
int startWeek = (int)date.DayOfWeek;  //当前月  第一天是星期几
int week2 = (int)week;
if (startWeek <= week2)
{
}
else
{
}
if (date.Month == month)
{
return date;
}
if (result == null && weekIndex == 5)
{
return GetDateTimeByWeekAndDay(year, month, weekIndex - 1, week);
}
return null;
}

/// <summary>
///某个月是季度的第几月
/// </summary>
/// <param name="month"></param>
/// <returns></returns>
private int GetMonthSeqOfQuarterBySeason(int month)
{
int k = (month - 1) / 3;
return month - 3 * k;
}

/// <summary>
/// 某个月是半年的第几月
/// </summary>
/// <param name="month"></param>
/// <returns></returns>
private int GetMonthSeqOfQuarterByHalf(int month)
{
if (month > 6)
{
return month - 6;
}
else
{
return month;
}
}


  /// <summary>
/// 提交生成日历
/// 已经生成订单的schedule和订单不变，删除其他schedule重新生成
/// </summary>
/// <param name="maintenancePlan"></param>
/// <returns></returns>
{
var maintenanceSchedules = new List<MaintenanceSchedule>();

foreach (var schedule in maintenancePlan.MaintenancePlanSchedules)
{
schedule.MaintenancePlan = maintenancePlan;
switch (schedule.Cycle)
{
case MaintenanceCycle.Weekly:
CreateWeeklyData(maintenanceSchedules, schedule);
break;
case MaintenanceCycle.Monthly:
CreatMonthlyData(maintenanceSchedules, schedule);
break;
case MaintenanceCycle.Season:
CreatSeasonData(maintenanceSchedules, schedule);
break;
case MaintenanceCycle.Half:
CreatHalfData(maintenanceSchedules, schedule);
break;
case MaintenanceCycle.Year:
CreatYearData(maintenanceSchedules, schedule);
break;
}
}

await DeleteSchedules(maintenancePlan.Id);

await _maintenanceScheduleRepository.InsertManyAsync(maintenanceSchedules);

}

/// <summary>
/// 生成年计划
/// </summary>
/// <param name="schedules"></param>
/// <param name="schedule"></param>
private void CreatYearData(List<MaintenanceSchedule> schedules, MaintenancePlanSchedule schedule)
{
var monthVal = schedule.Month;
var weekVal = schedule.Week;
var dayVal = schedule.Day;
var dt = DateTime.Now;
var year = dt.Year;
var month = dt.Month;
var dateCount = 0;
for (int i = 0; i <= MaintenanceConsts.YEARCOUNT; i++)
{
if (dateCount == MaintenanceConsts.YEARCOUNT)
{
break;
}
for (int j = 0; j < MaintenanceConsts.MONTHCOUNT; j++)
{
if (month == monthVal)
{
DateTime? result = GetDateTimeByWeekAndDay(year, month, weekVal, (DayOfWeek)dayVal);
if (result.HasValue && result.Value > dt)
{
dateCount++;
MaintenanceSchedule maintenanceSchedule = new MaintenanceSchedule();
maintenanceSchedule.OperatorDate = result.Value;
maintenanceSchedule.Cycle = MaintenanceCycle.Year;
maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
MaintenanceConsts.YEAR + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");

maintenanceSchedule.StartTime = schedule.StartTime;
maintenanceSchedule.EndTime = schedule.EndTime;
}
}
month++;

if (month == 13)
{
month = 1;
year++;
}
}

}
}

/// <summary>
/// 生成半年计划
/// </summary>
/// <param name="schedules"></param>
/// <param name="schedule"></param>
private void CreatHalfData(List<MaintenanceSchedule> schedules, MaintenancePlanSchedule schedule)
{

var monthVal = schedule.Month;
var weekVal = schedule.Week;
var dayVal = schedule.Day;
var dt = DateTime.Now;
var year = dt.Year;
var month = dt.Month;
var dateCount = 0;
for (int i = 0; i <= MaintenanceConsts.HALFCOUNT; i++)
{
if (dateCount == MaintenanceConsts.HALFCOUNT)
{
break;
}
for (int j = 0; j < MaintenanceConsts.HALFMONTHCOUNT; j++)
{
if (GetMonthSeqOfQuarterByHalf(month) == monthVal)
{
DateTime? result = GetDateTimeByWeekAndDay(year, month, weekVal, (DayOfWeek)dayVal);
if (result.HasValue && result.Value > dt)
{
dateCount++;
MaintenanceSchedule maintenanceSchedule = new MaintenanceSchedule();
maintenanceSchedule.OperatorDate = result.Value;
maintenanceSchedule.Cycle = MaintenanceCycle.Half;
maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
MaintenanceConsts.HALF + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");

maintenanceSchedule.StartTime = schedule.StartTime;
maintenanceSchedule.EndTime = schedule.EndTime;
}
}
month++;

if (month == 13)
{
month = 1;
year++;
}
}

}
}

/// <summary>
/// 生成季度计划
/// </summary>
/// <param name="schedules"></param>
/// <param name="schedule"></param>
private void CreatSeasonData(List<MaintenanceSchedule> schedules, MaintenancePlanSchedule schedule)
{
var monthVal = schedule.Month;
var weekVal = schedule.Week;
var dayVal = schedule.Day;
var dt = DateTime.Now;
var year = dt.Year;
var month = dt.Month;
var dateCount = 0;
//循环季度
for (int i = 0; i <= MaintenanceConsts.SEASONCOUNT; i++)
{
if (dateCount == MaintenanceConsts.SEASONCOUNT)
{
break;
}
//循环季度里的三个月
for (int j = 0; j < MaintenanceConsts.SEASONMONTHCOUNT; j++)
{
if (GetMonthSeqOfQuarterBySeason(month) == monthVal)
{
DateTime? result = GetDateTimeByWeekAndDay(year, month, weekVal, (DayOfWeek)dayVal);
if (result.HasValue && result.Value > dt)
{
dateCount++;
MaintenanceSchedule maintenanceSchedule = new MaintenanceSchedule();
maintenanceSchedule.Cycle = MaintenanceCycle.Season;
maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
MaintenanceConsts.SEASON + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");

maintenanceSchedule.StartTime = schedule.StartTime;
maintenanceSchedule.EndTime = schedule.EndTime;
}
}
month++;

if (month == 13)
{
month = 1;
year++;
}
}

}

}

/// <summary>
/// 生成月计划
/// chedule.Week第几周？
/// schedule.Day周几？
/// </summary>
/// <param name="schedules"></param>
/// <param name="schedule"></param>
private void CreatMonthlyData(List<MaintenanceSchedule> schedules, MaintenancePlanSchedule schedule)
{
var weekVal = schedule.Week;
var dayVal = schedule.Day;
var dt = DateTime.Now;
var year = dt.Year;
var month = dt.Month;

var dateCount = 0;

for (int i = 0; i <= MaintenanceConsts.MONTHCOUNT; i++)
{
if (dateCount == MaintenanceConsts.MONTHCOUNT)
{
break;
}
DateTime? result = GetDateTimeByWeekAndDay(year, month, weekVal, (DayOfWeek)dayVal);
if (result.HasValue && result.Value > dt)
{
dateCount++;
MaintenanceSchedule maintenanceSchedule = new MaintenanceSchedule();
maintenanceSchedule.Cycle = MaintenanceCycle.Monthly;
maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
MaintenanceConsts.MONTHLY + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");

maintenanceSchedule.StartTime = schedule.StartTime;
maintenanceSchedule.EndTime = schedule.EndTime;
}

month++;

if (month == 13)
{
month = 1;
year++;
}
}

}

/// <summary>
/// 生成周计划
/// </summary>
/// <param name="schedules"></param>
/// <param name="schedule"></param>
/// <returns></returns>
private void CreateWeeklyData(List<MaintenanceSchedule> schedules,
MaintenancePlanSchedule schedule)
{
//一年的周数
var dayVal = schedule.Day;
//如果7就是周日周日c# DayOfWeek是0
if (dayVal == 7)
{
dayVal = 0;
}
var firstDate = DateTime.Now;

for (int i = 0; i < MaintenanceConsts.WEEKDAYS; i++)
{
{
break;
}
}

for (int i = 0; i < MaintenanceConsts.WEEKCOUNT; i++)
{
MaintenanceSchedule maintenanceSchedule = new MaintenanceSchedule();
maintenanceSchedule.Cycle = MaintenanceCycle.Weekly;
maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
MaintenanceConsts.WEEKLY + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");

maintenanceSchedule.StartTime = schedule.StartTime;
maintenanceSchedule.EndTime = schedule.EndTime;
}

}


        /// <summary>
/// 生成多久的计划
/// 1.提交计划
/// 2.定时任务
/// </summary>
public const int WEEKCOUNT = 52;
/// <summary>
/// 一周有几天
/// </summary>
public const int WEEKDAYS = 7;

/// <summary>
/// 一年12月
/// </summary>
public const int MONTHCOUNT = 12;
/// <summary>
/// 一年四个季度
/// </summary>
public const int SEASONCOUNT = 4;
/// <summary>
/// 一年包含2个半年
/// </summary>
public const int HALFCOUNT = 2;
/// <summary>
/// 一年包含一个一年
/// </summary>
public const int YEARCOUNT = 1;

/// <summary>
/// 半年包含6个月
/// </summary>
public const int HALFMONTHCOUNT = 6;
/// <summary>
/// 一季度包含3个月
/// </summary>
public const int SEASONMONTHCOUNT = 3;

public const string YEAR = "Year";
public const string HALF = "Half";
public const string SEASON = "Season";
public const string MONTHLY = "Monthly";
public const string WEEKLY = "Weekly";


posted @ 2023-08-31 14:12  一个土豆一棵青菜  阅读(8)  评论(0编辑  收藏  举报