-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkItemChange.cs
More file actions
170 lines (140 loc) · 6.49 KB
/
WorkItemChange.cs
File metadata and controls
170 lines (140 loc) · 6.49 KB
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
using AzureDevopsTracker.Extensions;
using System;
namespace AzureDevopsTracker.Entities
{
public class WorkItemChange : Entity
{
public string WorkItemId { get; private set; }
public DateTime NewDate { get; private set; }
public DateTime? OldDate { get; private set; }
public string NewState { get; private set; }
public string OldState { get; private set; }
public string ChangedBy { get; private set; }
public string IterationPath { get; private set; }
public double TotalWorkedTime { get; private set; }
public WorkItem WorkItem { get; private set; }
private WorkItemChange() { }
public WorkItemChange(string workItemId,
string changedBy,
string iterationPath,
DateTime newDate,
string newState,
string oldState,
DateTime? oldDate)
{
WorkItemId = workItemId;
NewDate = newDate;
OldDate = oldDate;
NewState = newState;
OldState = oldState;
ChangedBy = changedBy;
IterationPath = iterationPath;
TotalWorkedTime = CalculateTotalWorkedTime();
Validate();
}
public void Validate()
{
if (WorkItemId.IsNullOrEmpty() || Convert.ToInt64(WorkItemId) <= 0)
throw new Exception("WorkItemId is required");
}
public TimeSpan CalculateTotalTime() => OldDate is null ? TimeSpan.Zero : NewDate.ToDateTimeFromTimeZoneInfo() - OldDate.Value.ToDateTimeFromTimeZoneInfo();
public double CalculateTotalWorkedTime()
{
if (OldDate.GetValueOrDefault() == DateTime.MinValue)
return 0;
var oldDate = OldDate.Value.ToDateTimeFromTimeZoneInfo();
var newDate = NewDate.ToDateTimeFromTimeZoneInfo();
var secondsWorked = 0D;
var majorDateBeforeLunch = DateTime.MinValue;
var minorDateBeforeLunch = DateTime.MinValue;
var majorDateAfterLunch = DateTime.MinValue;
var minorDateAfterLunch = DateTime.MinValue;
DateTime dataAux = DateTime.MinValue;
int diasCompletos = 0;
TimeSpan afterLunch;
TimeSpan beforeLunch;
for (var dateAux = oldDate; dateAux <= newDate; dateAux = dateAux.AddSeconds(1))
{
if (dateAux.DayOfWeek == DayOfWeek.Saturday || dateAux.DayOfWeek == DayOfWeek.Sunday)
continue;
if (dateAux.TimeOfDay.Hours >= 12 && dateAux.TimeOfDay.Hours < 14)
continue;
if (dateAux.TimeOfDay.Hours > 18 && dateAux.TimeOfDay.Hours < 23 ||
dateAux.TimeOfDay.Hours >= 0 && dateAux.TimeOfDay.Hours < 8)
continue;
if (dataAux == DateTime.MinValue)
dataAux = dateAux;
if (dataAux != DateTime.MinValue && dataAux.Date != dateAux.Date)
{
beforeLunch = SubtractDates(majorDateBeforeLunch, minorDateBeforeLunch);
afterLunch = SubtractDates(majorDateAfterLunch, minorDateAfterLunch);
if ((beforeLunch + afterLunch).Hours == 8)
diasCompletos++;
else
secondsWorked += (beforeLunch + afterLunch).TotalSeconds;
majorDateBeforeLunch = DateTime.MinValue;
minorDateBeforeLunch = DateTime.MinValue;
majorDateAfterLunch = DateTime.MinValue;
minorDateAfterLunch = DateTime.MinValue;
dataAux = dateAux;
}
if (dateAux.TimeOfDay.Hours >= 8 && dateAux.TimeOfDay.Hours < 12)
{
if (minorDateBeforeLunch == DateTime.MinValue || dateAux < minorDateBeforeLunch)
minorDateBeforeLunch = dateAux;
if (majorDateBeforeLunch == DateTime.MinValue || dateAux > majorDateBeforeLunch)
majorDateBeforeLunch = dateAux;
continue;
}
if (dateAux.TimeOfDay.Hours >= 14 && dateAux.TimeOfDay.Hours < 18)
{
if (minorDateAfterLunch == DateTime.MinValue || dateAux < minorDateAfterLunch)
minorDateAfterLunch = dateAux;
if (majorDateAfterLunch == DateTime.MinValue || dateAux > majorDateAfterLunch)
majorDateAfterLunch = dateAux;
continue;
}
}
if (dataAux != DateTime.MinValue &&
oldDate.Date == newDate.Date ||
(majorDateBeforeLunch != DateTime.MinValue || minorDateBeforeLunch != DateTime.MinValue) ||
(majorDateAfterLunch != DateTime.MinValue || minorDateAfterLunch != DateTime.MinValue))
{
beforeLunch = SubtractDates(majorDateBeforeLunch, minorDateBeforeLunch);
afterLunch = SubtractDates(majorDateAfterLunch, minorDateAfterLunch);
secondsWorked += (beforeLunch + afterLunch).TotalSeconds;
}
if (diasCompletos > 0)
secondsWorked += diasCompletos * 28800;
return secondsWorked;
}
#region Private Methods
private TimeSpan SubtractDates(DateTime biger, DateTime minor)
{
if (biger.Hour == minor.Hour)
{
return biger.Subtract(minor);
}
else if (biger.Hour > 8 && biger.Hour == 11 && biger.Minute == 59)
{
var newMajorDate = new DateTime(biger.Year, biger.Month, biger.Day, 12, 0, 0);
return newMajorDate.Subtract(minor);
}
else if (biger.Hour > 14 && biger.Hour < minor.Hour)
{
var newMinorDate = new DateTime(minor.Year, minor.Month, minor.Day, 14, 0, 0);
return biger.Subtract(newMinorDate);
}
else if (biger.Hour > 14 && biger.Hour == 17 && biger.Minute == 59)
{
var newMajorDate = new DateTime(biger.Year, biger.Month, biger.Day, 18, 0, 0);
return newMajorDate.Subtract(minor);
}
else
{
return biger.Subtract(minor);
}
}
#endregion
}
}