配对并计算checkTypes的小时数

时间:2018-04-17 07:21:28

标签: c# sql-server asp.net-mvc

Continuing from here,我有一个名为Attendancelogs的表格,其中包含所有已排序的日志CHeckTypes InOut。 (感谢@StephenMuecke,之前的问题已得到很好的解决)但是,我接下来想要实现的是在特定时期({{1})创建所有InOut的配对列表}和StartDate)然后计算员工的总小时数。

Different Cases, already been taken care of in the previous question

我有两个方法,一个名为EndDate,用于创建getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate),第二个方法pairList用于计算每天的总小时数。

Pair.cs

getTimeSpentEachDay(List<Pair> pairList)

^我用不同的方法改变了上述方法,因为上面的方法会弄乱上面链接中显示的 public class Pair { public int id { get; set; } public int RegisterationId { get; set; } public int EmpID { get; set; } public DateTime InnDateTime { get; set; } public DateTime OutDateTime { get; set; } } public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) { DateTime today = DateTime.Now; List<Pair> pairList = new List<Pair>(); var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate && x.Date <= EndDate && x.isIgnore != true && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList(); int loopEnd = 0; bool oddLogs = false; if (logs.Count % 2 == 0) { loopEnd = logs.Count; } else { loopEnd = logs.Count - 1; oddLogs = true; } bool inn = true; if (loopEnd > 1) { Pair pair = new Pair(); for (int v = 0; v < loopEnd; v++) { if (inn) { pair.InnDateTime = logs[v].DateTime; inn = false; } else { pair.OutDateTime = logs[v].DateTime; inn = true; pairList.Add(pair); pair = new Pair(); } } } Bogus bogus = new Bogus(); DateTime bogusDate = new DateTime(); if (oddLogs) { bogus.MachineNum = logs[logs.Count - 1].DeviceID; bogus.RegisterationId = logs[logs.Count - 1].RegisterationId; bogus.DateTime = logs[logs.Count - 1].DateTime; bogusDate = logs[logs.Count - 1].DateTime; } return pairList; }

Case 1

^这种方法在案例1中再次失败,因为它在pairList中依次添加 public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) { DateTime today = DateTime.Now; List<Pair> pairList = new List<Pair>(); var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate && x.Date <= EndDate && x.isIgnore != true && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList(); bool isCheck = false; Pair pair = new Pair(); DateTime previous = logs.FirstOrDefault().DateTime; foreach (var log in logs) { if (!isCheck) { pair.InnDateTime = log.DateTime; isCheck = true; } else { pair.OutDateTime = log.DateTime; isCheck = false; } pairList.Add(pair); pair = new Pair(); } return pairList; } In

Out

我试图获得每对的总小时数以及将奇数public List<DateAndTime> getTimeSpentEachDay(List<Pair> pairList) { List<DateAndTime> list = new List<DateAndTime>(); if (pairList.Count > 0) { for (int i = 0; i < pairList.Count; i++) { TimeSpan span = TimeSpan.Zero; // bool flag = false; int result = -1; do { span = span + (pairList[i].OutDateTime - pairList[i].InnDateTime); result = -1; if (i < pairList.Count - 1) { DateTime p = (DateTime)pairList[i].InnDateTime; DateTime q = (DateTime)pairList[i + 1].InnDateTime; result = DateTime.Compare(p.Date, q.Date); } if (result == 0) { i++; // flag = true; } } while (result == 0); //if (i == pairList.Count - 1) //{ // span = span + (pairList[i].OutDateTime - pairList[i].InnDateTime) ?? TimeSpan.Zero; //} DateAndTime dnt = new DateAndTime(); dnt.date = ((DateTime)pairList[i].InnDateTime).ToString("yyyy-MM-dd"); dnt.Time = span; list.Add(dnt); } } return list.ToList(); } 放在一起的方法,我可以在日历上显示。

以下是员工的日历视图图片,该图片仅显示配对小时数,我甚至想要显示In,表明员工忘记退房或输入丢失的条目

*假设他在4月9日晚上08:00左右办理了登记手续,当天没有退房,也在NightersLimit(上午12点至早上7点),那么日历上应该会显示一行这种情况只有在我从以前的方法中带出一天的虚假记录时才会发生。

enter image description here

1 个答案:

答案 0 :(得分:0)

getTimeSpentEachDay();只是getSingleDevicePairs();方法稍有变化,没有任何问题,你的第一种方法失败了,因为它消除了奇数的最后一个日志条目和你的第二种方法失败,因为它没有考虑日志存储在表中的顺序。

public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate, int? missingEntry)
{
    var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && 
               x.Date >= StartDate && x.Date <= EndDate && x.isIgnore != true 
               && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList();

            if (logs.Count > 0)
            {
                bool isCheck = false;
                Pair pair = new Pair();
                DateTime previous = logs.FirstOrDefault().DateTime;

                foreach (var log in logs)
                {
                    if (!isCheck)
                    {
                        if (log.CheckType == "In")
                        {
                            pair.InnDateTime = log.DateTime;
                            isCheck = true;
                        }
                    }
                    else
                    {
                        if (log.CheckType == "Out")
                        {
                            pair.OutDateTime = log.DateTime;
                            isCheck = false;
                            pairList.Add(pair);
                            pair = new Pair();

                        }
                        if (pair.OutDateTime == DateTime.MinValue)
                        {
                            pair.InnDateTime = log.DateTime;
                        }

                    }
                }
            }
            return pairList;
        }

仅当OutIn时才会完成对。