RepotTool.cs
6.03 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
using HHECS.BllModel;
using HHECS.WorkHourStatistics.Dtos;
using HHECS.WorkHourStatistics.Models;
namespace HHECS.WorkHourStatistics.Utils
{
public static class RepotTool
{
public static BllResult<List<ReportModel>> GetReport(IEnumerable<WorkRecord> workRecords)
{
try
{
var data = workRecords.Where(x => x.WorkCard != null && x.OffDutyCard != null).DistinctBy(x => new { x.Number, x.WorkCard, x.OffDutyCard }).ToList();
var reportData = data.GroupBy(x => x.Number).Select(x => new ReportModel
{
WorkNo = x.Key!,
Name = x.First(t => t.Number == x.Key)?.PersonName!,
SysOrgCode = x.First(t => t.Number == x.Key)?.DeptName!,
Values = x.ToLookup(y => $"{y.Number}#{y.WorkCard!.Value:yyyy-MM-dd}({GetWeek((DateTime)y.WorkCard)})", z => GetWT((DateTime)z.WorkCard!, (DateTime)z.OffDutyCard!)).ToDictionary(x => x.Key, y => y.Max())
}).OrderBy(x => x.SysOrgCode).ToList();
return BllResultFactory.Success(reportData);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<ReportModel>>(ex.Message);
}
}
/// <summary>
/// 计算WT值
/// <para>公式:WT = WH * 0.1 + WNH * 0.1</para>
/// </summary>
/// <param name="workingHours">上班时间</param>
/// <param name="offDutyHours"><下班时间/param>
/// <returns></returns>
private static double GetWT(DateTime workingHours, DateTime offDutyHours)
{
double wtValue = 0;
//上班时间大于下班时间,属于异常数据
if (workingHours >= offDutyHours)
{
return 0;
}
var week = GetWeek(workingHours);
var wnh = new List<string>() { "六", "日" };
//总时长
var totalTime = TimeSpan.Zero;
//周末*0.1
if (wnh.Contains(week))
{
//上班打卡时间:当天8:30
var startAMTime = workingHours.Date.AddHours(8).AddMinutes(30);
//上午结束时间:当天12:00
var endAMTime = workingHours.Date.AddHours(12);
//下午开始时间:当天13:30
var startPMTime = offDutyHours.Date.AddHours(13).AddMinutes(30);
//下班打卡时间:当天17:30
var endPMTime = offDutyHours.Date.AddHours(17).AddMinutes(30);
//上午工作时长
var amTime = TimeSpan.Zero;
//下午工作时长
var pmTime = TimeSpan.Zero;
//8:30之前打上班卡
if (workingHours <= startAMTime && offDutyHours > startAMTime)
{
amTime = endAMTime - startAMTime;
}
//上午迟到
else if (workingHours > startAMTime && workingHours <= endAMTime)
{
amTime = endAMTime - workingHours;
}
//上午早退12:00之前打下班卡
if (offDutyHours <= endAMTime)
{
if (workingHours <= startAMTime)
{
amTime = offDutyHours - startAMTime;
}
else
{
amTime = offDutyHours - workingHours;
}
}
//上午早退12:00 - 13:30之间打下班卡
else if (offDutyHours <= startPMTime && offDutyHours >= endAMTime)
{
if (workingHours <= startAMTime)
{
amTime = endAMTime - startAMTime;
}
else
{
amTime = endAMTime - workingHours;
}
}
//17:30之后打下班卡
if (offDutyHours >= endPMTime)
{
pmTime = endPMTime - startPMTime;
}
//下午早退
else if (offDutyHours >= startPMTime && offDutyHours <= endPMTime)
{
if (workingHours < startPMTime)
{
pmTime = offDutyHours - startPMTime;
}
else
{
//下午打上班卡和下班卡
pmTime = offDutyHours - workingHours;
}
}
totalTime = amTime + pmTime;
wtValue = totalTime.TotalHours * 0.1;
}
//周一至周五*0.1
else if (offDutyHours > offDutyHours.Date.AddHours(18))
{
//只统计18:30之后的时长
var endTime = offDutyHours.Date.AddHours(18).AddMinutes(30);
if (workingHours > endTime)
{
totalTime = offDutyHours - workingHours;
}
else if (offDutyHours >= endTime)
{
totalTime = offDutyHours - endTime;
}
wtValue = totalTime.TotalHours * 0.1;
}
return Math.Round(wtValue, 2, MidpointRounding.AwayFromZero);
}
private static string GetWeek(DateTime dateTime)
{
string[] weekdays = { "日", "一", "二", "三", "四", "五", "六" };
return weekdays[Convert.ToInt32(dateTime.DayOfWeek)];
}
public static List<string> GetHeadDays(DateTime startTime, DateTime endTime)
{
var li = new List<string>();
while (startTime <= endTime)
{
li.Add($"{startTime:yyyy-MM-dd}({GetWeek(startTime)})");
startTime = startTime.AddDays(1);
}
return li.ToList();
}
}
}