-1

所以我有一个与门口的设备连接的数据库,员工进出时检查它现在我可以获取状态历史记录,但我想知道如何计算每周一天的工作时间和每个员工一个月。我为员工上了这门课。

public class Employee : BaseEntity
{        
    public Employee()
    {
        this.HistoryOfStatuses = new List<Checkinout>();
        this.TodayCheckedStatus = new List<Checkinout>();
    }

    public string Name { get; set; }
    public string Department { get; set; }
    public string CardNumber { get; set; }
    public string Status { get; set; }
    public byte[] Picture { get; set; }
    public Checkinout ActualCheckinStatuse { get; set; }
    public List<Checkinout> HistoryOfStatuses { get; set; }
    public List<Checkinout> TodayCheckedStatus { get; set; }
    public int UserId { get; internal set; }

这是检查状态类

public class Checkinout : BaseEntity
{
    public Checkinout()
    {
    }
    public int EmployeeId { get; set; }
    public string CheckStatus { get; set; }
    public DateTime CheckTime { get; set; }
    public Employee EmployeeObject { get; set; }

}

我的控制器看起来像这样

public IActionResult Index()
    {
        using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
        {
            var baseViewModel = base.GetLayoutViewModel();
            var viewModel = new HomeViewModel()
            {
                User = baseViewModel.User,
                RoleCollection = baseViewModel.RoleCollection,
                TableCollection = baseViewModel.TableCollection,
                //Olap = baseViewModel.Olap,
                //Localization = baseViewModel.Localization,
                EmployeeCollection = (from userinfo in context.Userinfo
                                      join department in context.Dept on userinfo.Deptid equals department.Deptid
                                      select new Employee()
                                      {
                                          Id = userinfo.Userid,
                                          Name = userinfo.Name,
                                          Picture = userinfo.Picture,
                                          Department = department.DeptName,
                                          CardNumber = userinfo.CardNum,
                                          Status = userinfo.UserFlag.ToString(),
                                          ActualCheckinStatuse = (from checkinout in context.Checkinout
                                                                  join status in context.Status on checkinout.CheckType equals status.Statusid
                                                                  where checkinout.Userid == userinfo.Userid
                                                                  orderby checkinout.CheckTime descending
                                                                  select new Checkinout
                                                                  {
                                                                      CheckStatus = status.StatusText,
                                                                      CheckTime = checkinout.CheckTime
                                                                  }).FirstOrDefault()
                                      }).ToList()
            };
            return View(viewModel);
        }
    }

    public IActionResult WorkingHours()
    {
        var inTime = "10:00";
        var outTime = DateTime.Now.TimeOfDay;
        var totalHours = Convert.ToDateTime(inTime).TimeOfDay.Subtract(outTime);
    }

我希望有人帮助我弄清楚如何做到这一点以及如何连接控制器中的最后一个代码。

4

1 回答 1

1

您的模型是否在某处具有确定条目是进出检查的属性?你会需要这个。

您现在可以做的是创建一个方法,该方法采用两个日期形成一个范围。您可以返回此范围内的总工作时间。然后,您可以轻松地为一周、一个月、一年创建一个方法,在其中指定此方法的日期范围。

这里有一些伪代码可以帮助您入门。

decimal GetRangeWorkHours(Employee employee, DateTime startOfRange, DateTime endOfRange){
    //1. Ask the database for all the entries from this employee within the range of dates.
    //2. Sort them by date, figuring out what entry the start/end is. You might get some edgecases where employees clock out midday and then clock back in later on the day. You can work this out using TimeSpans.
    //3. Calculate the difference between all the now paired in/out entries.
    //4. Sum the results of the previous step, return the result.
}

周/月/年消费变得容易

Employee someEmployee = new Employee(/*wetherever you use to identify employees*/);
//Replace these 2 values with whatever you need to provide your user with.
DateTime startOfRange = new DateTime(2019, 1, 1);
DateTime endOfRange = startOfRange.AddDays(7);
decimal workHours = GetRangeWorkHours(someEmployee, startOfRange, endOfRange);
于 2019-01-15T14:38:22.443 回答