public static void CalucateEmployeAttendence(string filePath, DateTime targetDate) { LogHelper.Debug("start to calucate attendence log."); try { Dictionary <string, List <Log> > employeeLogs = importData(filePath); var attendLogs = new List <AttendLog>(); //var targetDate = employeeLogs.Values.ElementAt(0).First().Clock; foreach (var employee in employees) { if (employeeLogs.ContainsKey(employee.EmployeeID)) { var logs = employeeLogs[employee.EmployeeID]; //logs.Sort((x, y) => x.Clock.CompareTo(y.Clock)); //var firstAttendLog = logs.First(); //var lastAttendLog = logs.Last(); AttendLog attendLog = checkAttendStatus(logs, employee, targetDate); attendLogs.Add(attendLog); } else { //absent AttendLog attendLog = new AttendLog() { Year = targetDate.Year, Month = targetDate.Month, Day = targetDate.Day, EmployeeID = employee.EmployeeID, AttendStatus = AttendStatus.Absent }; attendLogs.Add(attendLog); } } if (Directory.Exists(exportPath) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(exportPath); } var fileName = exportPath + "\\attendlog" + targetDate.ToString("MM-dd-yyyy") + ".xlsx"; SaveDataToExcelFile(attendLogs, fileName); } catch (Exception ex) { LogHelper.Error("calcucate attend: ", ex); throw ex; } }
private static AttendLog checkAttendStatus(List <Log> logs, Employee employee, DateTime targetDate) { logs.Sort((x, y) => x.Clock.CompareTo(y.Clock)); var dateFolder = exportPath + "\\logs\\" + targetDate.ToString("MM-dd-yyyy"); if (Directory.Exists(dateFolder) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(dateFolder); } var logFile = dateFolder + "\\" + employee.EmployeeID + ".xlsx"; SaveDataToExcelFile(logs, logFile); //var firstAttendIn = logs.Where(l => l.EntryType == EntryType.In).FirstOrDefault(); //var lastAttendOut = logs.Where(l => l.EntryType == EntryType.Out).LastOrDefault(); //var firstAttendLog = logs.Where(l => l.Clock.Year == targetDate.Year //&& l.Clock.Month == targetDate.Month //&& l.Clock.Day == targetDate.Day); var targetYear = targetDate.Year; var targetMonth = targetDate.Month; var targetDay = targetDate.Day; var firstAttendLog = logs.FirstOrDefault(l => l.Clock.Year == targetYear && l.Clock.Month == targetMonth && l.Clock.Day == targetDay); var lastAttendLog = logs.LastOrDefault(l => l.Clock.Year == targetYear && l.Clock.Month == targetMonth && l.Clock.Day == targetDay); AttendLog attendLog = new AttendLog() { Year = targetYear, Month = targetMonth, Day = targetDay, EmployeeID = employee.EmployeeID }; if (firstAttendLog == null || firstAttendLog.EntryType != EntryType.In) { attendLog.AttendStatus = AttendStatus.MissIn; return(attendLog); } attendLog.EarlyIn = firstAttendLog.Clock; if (lastAttendLog == null || lastAttendLog.EntryType != EntryType.Out) { attendLog.AttendStatus = AttendStatus.MissOut; return(attendLog); } attendLog.LastOut = lastAttendLog.Clock; var targetTimeIn = new DateTime(targetYear, targetMonth, targetDay, 9, 3, 0); var targetTimeOut = new DateTime(targetYear, targetMonth, targetDay, 17, 27, 0); if (employee.AttendType.IsShift) { targetTimeIn = new DateTime(targetYear, targetMonth, targetDay, employee.AttendType.TargetHourIn, employee.AttendType.TargetHourIn, 0); targetTimeOut = targetTimeIn.AddHours(employee.AttendType.ShiftTime); } if (targetTimeIn.CompareTo(firstAttendLog.Clock) >= 0) { TimeSpan ts = lastAttendLog.Clock - targetTimeOut; if (targetTimeOut.CompareTo(lastAttendLog.Clock) <= 0) { //normal off work if (ts.Hours >= 1) { attendLog.AttendStatus = AttendStatus.OverTime; attendLog.Overtime = ts.TotalMinutes; } } else { //early leave attendLog.AttendStatus = AttendStatus.EarlyLeave; attendLog.EarlyLeaveMinute = ts.TotalMinutes; } } else { //late TimeSpan ts = firstAttendLog.Clock - targetTimeIn; if (ts.TotalHours <= 1) { attendLog.AttendStatus = AttendStatus.Late; attendLog.LateMinute = ts.TotalMinutes; } else { attendLog.AttendStatus = AttendStatus.Absent; attendLog.LateMinute = ts.TotalMinutes; } } return(attendLog); }