2016-08-01 3 views
0

날짜가 두 개인 선택기가 있습니다. 데이터베이스에 이미 데이터가 저장되어 있다고 가정하십시오.여러 체크인/체크 아웃/점심 시간을 갖고 총 시간을 받으십시오.

//Status  //Date  //Time  //name 
---------------------------------------------- 
Check In  1/8/2016 12:30:36pm  ali 
    Lunch  1/8/2016  2:40:36pm  ali 
Check In  1/8/2016  3:40:36pm  ali 
Check Out 1/8/2016  6:40:36pm  ali 

나는 점심 시간을 계산하고 싶지 않습니다. 내가 그 일을 위해 무엇을 내 직원의 일을 계산하기 원하는

6:40:36 PM - 12:30:36pm = 6 //total hours worked include lunch 

그래서 나는 점심 빼기해야 - 1시간

을하는 Checkk을
6 - (3:40:36 PM - 2:40:36 PM) = 5 hours //total hours that worked 

내가이 일을해야 어떤 종류의 논리 ? 내 데이터베이스에서 선택할 모든 유형의 SQL 절을 이미 알고 있습니다. 하지만 코드의 거대한 라인을 구현하지 않고도 이것을 쉽게 계산할 수있는 방법이 필요합니다.

답변

0

이것은 특히 강력하지는 않지만 일부 리팩터링과 관련이있을 수 있지만 자신의 논리에 맞게 확장 할 수있는 출발점을 제공 할 수 있습니다. 열거로

귀하의 상태 :

public enum Status 
    { 
     CheckIn, 
     CheckOut, 
     Lunch 
    } 
다음의 목록에 데이터를 돌려

:

public class EmployeeStatusChange 
    { 
     public int EmployeeId { get; set; } 

     public DateTime DateTime { get; set; } 

     public Status Status { get; set; } 
    } 

그리고이 확장 방법을 사용하십시오

public static double CalculateHours(this List<EmployeeStatusChange> changes) 
    { 
     changes = changes.OrderBy(x => x.DateTime).ToList(); 

     double hours = 0; 
     var start = DateTime.MinValue; 
     var lunch = DateTime.MinValue; 
     var checkedOut = false; 

     foreach (var change in changes) 
     { 
      // exclude anything before the first check in, or if we have already checked out 
      if ((start == DateTime.MinValue && change.Status != Status.CheckIn) || checkedOut) 
      { 
       continue; 
      } 

      // Set the start time 
      if (start == DateTime.MinValue && change.Status == Status.CheckIn) 
      { 
       start = change.DateTime; 
       continue; 
      } 

      switch (change.Status) 
      { 
       case Status.CheckIn: 
        if (lunch == DateTime.MinValue) 
        { 
         continue; 
        } 

        start = change.DateTime; 
        continue; 

       case Status.Lunch: 
        lunch = change.DateTime; 
        hours += (change.DateTime - start).TotalHours; 
        break; 

       case Status.CheckOut: 
        checkedOut = true; 
        hours += (change.DateTime - start).TotalHours; 
        break; 
      } 
     } 

     return hours; 
    } 

이 돌아갑니다 6.5 :

var items = new List<EmployeeStatusChange>(); 
items.Add(new EmployeeStatusChange { EmployeeId = 1, Status = Status.CheckIn, DateTime = new DateTime(2015, 1, 1, 9, 0, 0) }); 
items.Add(new EmployeeStatusChange { EmployeeId = 1, Status = Status.Lunch, DateTime = new DateTime(2015, 1, 1, 10, 30, 0) }); 
items.Add(new EmployeeStatusChange { EmployeeId = 1, Status = Status.CheckIn, DateTime = new DateTime(2015, 1, 1, 11, 0, 0) }); 
items.Add(new EmployeeStatusChange { EmployeeId = 1, Status = Status.Lunch, DateTime = new DateTime(2015, 1, 1, 12, 0, 0) }); 
items.Add(new EmployeeStatusChange { EmployeeId = 1, Status = Status.CheckIn, DateTime = new DateTime(2015, 1, 1, 13, 0, 0) }); 
items.Add(new EmployeeStatusChange { EmployeeId = 1, Status = Status.CheckOut, DateTime = new DateTime(2015, 1, 1, 17, 0, 0) }); 
items.CalculateHours(); 
관련 문제