예제 #1
0
        public List <TimeEntriesGroupByEmployeeView> GetTimeEntriesByProjectGroupByEmployee(string projectGid)
        {
            // 获取项目相关的员工
            // var projectUsers = _projectUserService.GetUserByProject(projectGid, null);
            var allUsers   = _employeeService.AllEmployeesWithRole();
            var roleTitles = _roleTitleService.AllInternal();


            // 看这个projectGid,如果属于taskgid,就从taskgid过滤
            //var task = _projectTaskService.GetProjectTaskById(projectGid);

            // 再根据每个员工统计timeentry
            //var timeEntry = Repository.Entities.Where(a => a.IsDeleted != true);
            //if (task != null && task.Count > 0)
            //{
            //    timeEntry = timeEntry.Where(a => a.TaskId == projectGid);
            //}
            //else
            //{
            //    timeEntry = timeEntry.Where(a => a.ProjectId == projectGid);
            //}

            var sql = @"
                select * from TimeEntry TE where 
                exists(select 1 from Project P where P.Gid = TE.projectId and Gid = '{0}')
                or exists (select 1 from TaskToProjectMapping TPM where TPM.TaskGid = TE.TaskId and TPM.ProjectGid='{0}')
                order by [date]";

            var timeEntry        = Repository.SqlQuery(string.Format(sql, projectGid));
            var timeEntryByUsers = timeEntry.GroupBy(a => a.UserId);
            var timeEntriesGroupByEmployeesView = new List <TimeEntriesGroupByEmployeeView>();

            foreach (var a in timeEntryByUsers)
            {
                var employee = allUsers.FirstOrDefault(b => b.Gid == a.Key);

                var tv = new TimeEntriesGroupByEmployeeView()
                {
                    UserId         = a.Key,
                    TotalHours     = a.Sum(b => b.TotalHours),
                    EmployeeRate   = employee.RoleRate,
                    EmployeeRole   = employee.RoleName,
                    TotalHoursRate = a.Sum(b => b.TotalHours) * employee.RoleRate,
                    EmployeeName   = employee.Name
                };

                tv.TotalEffortByWeek = new List <TotalEffortByWeek>();

                // 计算周数
                foreach (var v in a)
                {
                    // 计算这个日期属于第几周
                    var week = v.Date.WeekOfYear();
                    var year = v.Date.YearOfWeekOfYear();

                    var totalEffortByWeek = tv.TotalEffortByWeek.FirstOrDefault(b => b.WeekNumber == week);
                    if (totalEffortByWeek == null)
                    {
                        tv.TotalEffortByWeek.Add(new TotalEffortByWeek()
                        {
                            Year           = year,
                            WeekNumber     = week,
                            TotalHours     = v.TotalHours,
                            TotalHoursRate = v.TotalHours * employee.RoleRate
                        });
                    }
                    else
                    {
                        totalEffortByWeek.TotalHours     += v.TotalHours;
                        totalEffortByWeek.TotalHoursRate += v.TotalHours * employee.RoleRate;
                    }
                }

                timeEntriesGroupByEmployeesView.Add(tv);
            }
            return(timeEntriesGroupByEmployeesView);
        }
예제 #2
0
        public List <TimeEntriesGroupByEmployeeView> GetTimeEntriesByProjectGroupByEmployee(string projectGid)
        {
            // 获取项目相关的员工
            var allUsers   = _employeeService.AllEmployeesWithRole();
            var roleTitles = _roleTitleService.AllInternal();


            // 看这个projectGid,如果属于taskgid,就从taskgid过滤
            //var task = _projectTaskService.GetProjectTaskById(projectGid);

            // 再根据每个员工统计timeentry
            //var timeEntry = Repository.Entities.Where(a => a.IsDeleted != true);
            //if (task != null && task.Count > 0)
            //{
            //    timeEntry = timeEntry.Where(a => a.TaskId == projectGid);
            //}
            //else
            //{
            //    timeEntry = timeEntry.Where(a => a.ProjectId == projectGid);
            //}


            var resourcePlans    = GetResourcePlanByProject(projectGid);
            var timeEntryByUsers = resourcePlans.GroupBy(a => a.EmployeeGid);

            var resorcePlansByEmployeesView = new List <TimeEntriesGroupByEmployeeView>();

            foreach (var a in timeEntryByUsers)
            {
                var employee = allUsers.FirstOrDefault(b => b.Gid == a.Key);

                var tv = new TimeEntriesGroupByEmployeeView()
                {
                    UserId         = a.Key,
                    TotalHours     = a.Sum(b => b.Amount),
                    EmployeeRate   = employee.RoleRate,
                    EmployeeRole   = employee.RoleName,
                    TotalHoursRate = a.Sum(b => b.Amount) * employee.RoleRate,
                    EmployeeName   = employee.Name
                };

                tv.TotalEffortByWeek = new List <TotalEffortByWeek>();

                // 计算周数
                foreach (var v in a)
                {
                    // 计算这个日期属于第几周
                    var week = v.Week;
                    var year = v.Year;

                    var totalEffortByWeek = tv.TotalEffortByWeek.FirstOrDefault(b => b.WeekNumber == week);
                    tv.TotalEffortByWeek.Add(new TotalEffortByWeek()
                    {
                        Year           = year,
                        WeekNumber     = week,
                        TotalHours     = v.Amount,
                        TotalHoursRate = v.Amount * employee.RoleRate
                    });
                }

                resorcePlansByEmployeesView.Add(tv);
            }
            return(resorcePlansByEmployeesView);
        }