AttendanceSummaryMonthMetricsHelper.cs
12.9 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.Linq;
using NCC.Extend.Entitys.Dto.LqAttendanceRecord;
using NCC.Extend.Entitys.Enum;
using NCC.Extend.Entitys.lq_attendance_record;
using Newtonsoft.Json.Linq;
namespace NCC.Extend
{
/// <summary>
/// 考勤汇总列表用:按自然月、每日一条有效记录口径统计应休/到岗/请假/迟到早退/旷工及可自快照计算的固定扣款。
/// </summary>
public static class AttendanceSummaryMonthMetricsHelper
{
private const string PersonalLeaveType = "事假";
private const string SickLeaveType = "病假";
/// <summary>
/// 单月扩展指标
/// </summary>
public sealed class MonthMetrics
{
/// <summary>应休天数(考勤组月应休额度,优先取当月打卡规则快照中的分组配置)</summary>
public decimal EntitledRestDays { get; set; }
/// <summary>到岗天数(正常+迟到)</summary>
public decimal OnDutyDays { get; set; }
/// <summary>工作天数(应休+到岗)</summary>
public decimal CompositeWorkDays { get; set; }
/// <summary>带薪请假天数(请假状态中假种非事假、非病假;不含病假状态)</summary>
public decimal PaidLeaveDays { get; set; }
/// <summary>请假天数(请假+病假状态合计)</summary>
public decimal LeaveDaysTotal { get; set; }
/// <summary>迟到或早退分钟大于 0 的天数</summary>
public decimal LateEarlyDays { get; set; }
/// <summary>旷工天数</summary>
public decimal AbsenteeismDays { get; set; }
/// <summary>迟到/早退规则为「固定金额」时,自 F_RuleSnapshotJson 汇总</summary>
public decimal LateDeductionAmount { get; set; }
/// <summary>事假/病假扣款依赖日薪×倍率;未传日薪时为 0</summary>
public decimal LeaveDeductionAmount { get; set; }
/// <summary>旷工规则为「固定金额」时,自 F_AllRuleSnapshotJson 的旷工规则表取首条匹配单日区间</summary>
public decimal AbsenteeismDeductionAmount { get; set; }
}
/// <summary>
/// 按用户当月有效打卡记录计算扩展指标(须已按日折叠为每日一条,与汇总同步口径一致)。
/// </summary>
/// <param name="perDayRecords">该用户该月「每日一条」记录</param>
/// <param name="fallbackMonthlyRestDays">无快照时的月应休兜底(主档分组)</param>
/// <param name="dailySalaryForLeaveDeduct">日薪;为空则请假扣款金额为 0</param>
public static MonthMetrics Compute(
IReadOnlyList<LqAttendanceRecordEntity> perDayRecords,
int fallbackMonthlyRestDays,
decimal? dailySalaryForLeaveDeduct = null)
{
var metrics = new MonthMetrics();
if (perDayRecords == null || perDayRecords.Count == 0)
{
metrics.EntitledRestDays = fallbackMonthlyRestDays;
metrics.CompositeWorkDays = metrics.EntitledRestDays + metrics.OnDutyDays;
return metrics;
}
var entitled = TryResolveMonthlyRestDays(perDayRecords, fallbackMonthlyRestDays);
metrics.EntitledRestDays = entitled;
decimal? leaveRate = null;
decimal? sickRate = null;
foreach (var r in perDayRecords)
{
TryReadBaseLeaveRates(r.AllRuleSnapshotJson, ref leaveRate, ref sickRate);
}
foreach (var r in perDayRecords)
{
var st = r.Status;
if (st == (int)AttendanceRecordStatusEnum.正常 || st == (int)AttendanceRecordStatusEnum.迟到)
{
metrics.OnDutyDays += 1;
}
if (st == (int)AttendanceRecordStatusEnum.请假)
{
metrics.LeaveDaysTotal += 1;
var lt = ResolveLeaveType(r);
if (IsPaidLeaveCategory(lt))
{
metrics.PaidLeaveDays += 1;
}
if (dailySalaryForLeaveDeduct.HasValue && leaveRate.HasValue
&& string.Equals(lt, PersonalLeaveType, StringComparison.Ordinal))
{
metrics.LeaveDeductionAmount += dailySalaryForLeaveDeduct.Value * leaveRate.Value;
}
}
else if (st == (int)AttendanceRecordStatusEnum.病假)
{
metrics.LeaveDaysTotal += 1;
if (dailySalaryForLeaveDeduct.HasValue && sickRate.HasValue)
{
metrics.LeaveDeductionAmount += dailySalaryForLeaveDeduct.Value * sickRate.Value;
}
}
if (r.LateMinutes > 0 || r.EarlyLeaveMinutes > 0)
{
metrics.LateEarlyDays += 1;
}
if (st == (int)AttendanceRecordStatusEnum.旷工)
{
metrics.AbsenteeismDays += 1;
}
metrics.LateDeductionAmount += TrySumFixedLateEarlyDeduction(r);
if (st == (int)AttendanceRecordStatusEnum.旷工)
{
metrics.AbsenteeismDeductionAmount += TryPickFixedAbsenteeismDeduction(r.AllRuleSnapshotJson);
}
}
metrics.CompositeWorkDays = metrics.EntitledRestDays + metrics.OnDutyDays;
return metrics;
}
/// <summary>
/// 将原始当月记录按日取最新一条(UpdateTime 优先)。
/// </summary>
public static List<LqAttendanceRecordEntity> CollapseToLatestPerDay(
IEnumerable<LqAttendanceRecordEntity> monthRecords)
{
var list = monthRecords?.Where(x => x != null).ToList() ?? new List<LqAttendanceRecordEntity>();
return list
.GroupBy(x => x.AttendanceDate.Date)
.Select(g => g.OrderByDescending(x => x.UpdateTime ?? x.CreateTime ?? DateTime.MinValue).First())
.OrderBy(x => x.AttendanceDate)
.ToList();
}
private static int TryResolveMonthlyRestDays(
IReadOnlyList<LqAttendanceRecordEntity> perDayRecords,
int fallback)
{
foreach (var r in perDayRecords.OrderByDescending(x => x.AttendanceDate))
{
var v = TryGetMonthlyRestFromRuleSnapshot(r.RuleSnapshotJson);
if (v.HasValue)
{
return Math.Max(0, v.Value);
}
}
return Math.Max(0, fallback);
}
private static int? TryGetMonthlyRestFromRuleSnapshot(string json)
{
if (string.IsNullOrWhiteSpace(json))
{
return null;
}
try
{
var o = JObject.Parse(json);
var ag = o["attendanceGroup"];
return ag?["monthlyRestDays"]?.Value<int?>();
}
catch
{
return null;
}
}
private static void TryReadBaseLeaveRates(
string allJson,
ref decimal? leaveRate,
ref decimal? sickRate)
{
if (string.IsNullOrWhiteSpace(allJson) || (leaveRate.HasValue && sickRate.HasValue))
{
return;
}
try
{
var o = JObject.Parse(allJson);
var bs = o["baseSetting"];
if (bs == null)
{
return;
}
if (!leaveRate.HasValue)
{
leaveRate = bs["leaveDeductDailySalaryRate"]?.Value<decimal?>();
}
if (!sickRate.HasValue)
{
sickRate = bs["sickLeaveDeductDailySalaryRate"]?.Value<decimal?>();
}
}
catch
{
// ignore
}
}
private static decimal TrySumFixedLateEarlyDeduction(LqAttendanceRecordEntity r)
{
if (string.IsNullOrWhiteSpace(r?.RuleSnapshotJson))
{
return 0m;
}
try
{
var o = JObject.Parse(r.RuleSnapshotJson);
decimal sum = 0;
if (r.LateMinutes > 0)
{
sum += ReadFixedDeductAmount(o["lateRule"]);
}
if (r.EarlyLeaveMinutes > 0)
{
sum += ReadFixedDeductAmount(o["earlyLeaveRule"]);
}
return sum;
}
catch
{
return 0m;
}
}
private static decimal ReadFixedDeductAmount(JToken rule)
{
if (rule == null || rule.Type == JTokenType.Null)
{
return 0m;
}
var mode = rule["deductMode"]?.Value<int?>();
if (mode != (int)AttendanceDeductModeEnum.FixedAmount)
{
return 0m;
}
return rule["deductValue"]?.Value<decimal?>() ?? 0m;
}
private static decimal TryPickFixedAbsenteeismDeduction(string allJson)
{
if (string.IsNullOrWhiteSpace(allJson))
{
return 0m;
}
try
{
var o = JObject.Parse(allJson);
var arr = o["allRules"]?["absenteeismRules"] as JArray;
if (arr == null)
{
return 0m;
}
foreach (var token in arr)
{
var minDays = token["minDays"]?.Value<decimal?>() ?? 1m;
var maxDays = token["maxDays"]?.Value<decimal?>();
if (minDays > 1m)
{
continue;
}
if (maxDays.HasValue && maxDays.Value <= 1m)
{
continue;
}
var mode = token["deductMode"]?.Value<int?>();
if (mode != (int)AttendanceDeductModeEnum.FixedAmount)
{
continue;
}
return token["deductValue"]?.Value<decimal?>() ?? 0m;
}
}
catch
{
// ignore
}
return 0m;
}
private static string ResolveLeaveType(LqAttendanceRecordEntity r)
{
var workflows = ParseRelatedWorkflows(r.RelatedWorkflowsJson);
var fromWf = workflows
.FirstOrDefault(x => x != null && x.type == "请假" && !string.IsNullOrWhiteSpace(x.leaveType));
if (fromWf != null)
{
return fromWf.leaveType.Trim();
}
return TryGetLeaveTypeLabelFromSyncRemark(r.Remark);
}
private static List<RelatedWorkflowItem> ParseRelatedWorkflows(string json)
{
if (string.IsNullOrWhiteSpace(json))
{
return new List<RelatedWorkflowItem>();
}
try
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<RelatedWorkflowItem>>(json)
?? new List<RelatedWorkflowItem>();
}
catch
{
return new List<RelatedWorkflowItem>();
}
}
private static string TryGetLeaveTypeLabelFromSyncRemark(string remark)
{
if (string.IsNullOrWhiteSpace(remark))
{
return null;
}
const string prefix = "请假审批通过:";
var idx = remark.IndexOf(prefix, StringComparison.Ordinal);
if (idx < 0)
{
return null;
}
var tail = remark.Substring(idx + prefix.Length);
var paren = tail.IndexOf('(');
if (paren <= 0)
{
return null;
}
var label = tail.Substring(0, paren).Trim();
return string.IsNullOrEmpty(label) ? null : label;
}
private static bool IsPaidLeaveCategory(string leaveType)
{
if (string.IsNullOrWhiteSpace(leaveType))
{
return false;
}
if (string.Equals(leaveType, PersonalLeaveType, StringComparison.Ordinal)
|| string.Equals(leaveType, SickLeaveType, StringComparison.Ordinal))
{
return false;
}
return true;
}
}
}