计划的生成 周 月 季 半年 年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// <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)
           {
               date = date.AddDays((weekIndex - 1) * 7).AddDays(week2 - startWeek);
           }
           else
           {
               date = date.AddDays(weekIndex * 7).AddDays(week2 - startWeek);
           }
           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;
           }
       }

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/// <summary>
      /// 提交生成日历
      /// 已经生成订单的schedule和订单不变,删除其他schedule重新生成
      /// </summary>
      /// <param name="maintenancePlan"></param>
      /// <returns></returns>
      private async Task CreateScheduleAsync(MaintenancePlan maintenancePlan)
      {
          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;
                          schedules.Add(maintenanceSchedule);
                      }
                  }
                  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;
                          schedules.Add(maintenanceSchedule);
                      }
                  }
                  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.OperatorDate = result.Value;//firstDate.AddDays(i * 7);
                          maintenanceSchedule.Cycle = MaintenanceCycle.Season;
                          maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
                              MaintenanceConsts.SEASON + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");
 
 
                          maintenanceSchedule.StartTime = schedule.StartTime;
                          maintenanceSchedule.EndTime = schedule.EndTime;
                          schedules.Add(maintenanceSchedule);
                      }
                  }
                  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.OperatorDate = result.Value;//firstDate.AddDays(i * 7);
                  maintenanceSchedule.Cycle = MaintenanceCycle.Monthly;
                  maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
                      MaintenanceConsts.MONTHLY + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");
 
 
                  maintenanceSchedule.StartTime = schedule.StartTime;
                  maintenanceSchedule.EndTime = schedule.EndTime;
                  schedules.Add(maintenanceSchedule);
              }
 
 
              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++)
          {
              if ((int)DateTime.Now.AddDays(i).DayOfWeek == dayVal)
              {
                  firstDate = DateTime.Now.AddDays(i);
                  break;
              }
          }
 
 
          for (int i = 0; i < MaintenanceConsts.WEEKCOUNT; i++)
          {
              MaintenanceSchedule maintenanceSchedule = new MaintenanceSchedule();
              maintenanceSchedule.OperatorDate = firstDate.AddDays(i * 7);
              maintenanceSchedule.Cycle = MaintenanceCycle.Weekly;
              maintenanceSchedule.Name = schedule.MaintenancePlan.Name + " " +
                  MaintenanceConsts.WEEKLY + " " + maintenanceSchedule.OperatorDate.ToString("yyyyMMdd");
 
 
              maintenanceSchedule.StartTime = schedule.StartTime;
              maintenanceSchedule.EndTime = schedule.EndTime;
              schedules.Add(maintenanceSchedule);
          }
 
 
      }

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <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 @   一个土豆一棵青菜  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2011-08-31 datatable是引用类型
2011-08-31 查询一个int类型的变量(数字)是否在一个字符串里边(逗号分隔的)以及 sqlif 与case
点击右上角即可分享
微信分享提示