Beispiel #1
0
        public async Task <IActionResult> GetUserStatusTable()
        {
            var model = new UserStatusTableModel();

            model.Department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);

            model.LastUserActionlogs = await _actionLogsService.GetLastActionLogsForDepartmentAsync(DepartmentId);

            model.UserStates       = new List <UserState>();
            model.DepartmentGroups = await _departmentGroupsService.GetAllGroupsForDepartmentAsync(DepartmentId);

            model.Stations = await _departmentGroupsService.GetAllStationGroupsForDepartmentAsync(DepartmentId);

            model.UsersGroup = await _departmentGroupsService.GetGroupForUserAsync(UserId, DepartmentId);

            model.States = await _customStateService.GetActivePersonnelStateForDepartmentAsync(DepartmentId);

            model.StaffingLevels = await _customStateService.GetActiveStaffingLevelsForDepartmentAsync(DepartmentId);

            var personnelSortOrder = await _departmentSettingsService.GetDepartmentPersonnelSortOrderAsync(DepartmentId);

            var personnelStatusSortOrder = await _departmentSettingsService.GetDepartmentPersonnelListStatusSortOrderAsync(DepartmentId);

            var userStates = await _userStateService.GetLatestStatesForDepartmentAsync(DepartmentId);

            var allUsers = _usersService.GetUserGroupAndRolesByDepartmentId(DepartmentId, false, false, false);

            model.ExcludedUsers = await _departmentsService.GetAllDisabledOrHiddenUsersAsync(DepartmentId);

            List <string> groupedUserIds = new List <string>();

            foreach (var dg in model.DepartmentGroups)
            {
                UserStatusGroup group = new UserStatusGroup();
                group.Group = dg;

                var membersToProcess = from member in dg.Members
                                       where !(model.ExcludedUsers.Any(item2 => item2 == member.UserId))
                                       select member;

                foreach (var u in membersToProcess)
                {
                    if (allUsers.Any(x => x.UserId == u.UserId))
                    {
                        groupedUserIds.Add(u.UserId);
                        var userInfo = allUsers.FirstOrDefault(x => x.UserId == u.UserId);

                        UserState state = userStates.FirstOrDefault(x => x.UserId == u.UserId);

                        if (state == null)
                        {
                            state               = new UserState();
                            state.UserId        = u.UserId;
                            state.AutoGenerated = true;
                            state.Timestamp     = DateTime.UtcNow;
                            state.State         = (int)UserStateTypes.Available;
                        }

                        if (!model.DepartmentUserStates.ContainsKey(u.UserId))
                        {
                            model.DepartmentUserStates.Add(u.UserId, state);
                        }

                        var al = model.LastUserActionlogs.FirstOrDefault(x => x.UserId == u.UserId);

                        UserStatus userStatus = new UserStatus();
                        userStatus.UserInfo        = userInfo;
                        userStatus.CurrentStatus   = al;
                        userStatus.CurrentStaffing = state;

                        if (al != null)
                        {
                            if (personnelStatusSortOrder != null && personnelStatusSortOrder.Any())
                            {
                                var statusSorting = personnelStatusSortOrder.FirstOrDefault(x => x.StatusId == al.ActionTypeId);
                                if (statusSorting != null)
                                {
                                    userStatus.Weight = statusSorting.Weight;
                                }
                                else
                                {
                                    userStatus.Weight = 9000;
                                }
                            }
                            else
                            {
                                userStatus.Weight = 9000;
                            }
                        }
                        else
                        {
                            userStatus.Weight = 9000;
                        }

                        group.UserStatuses.Add(userStatus);
                    }
                }

                switch (personnelSortOrder)
                {
                case PersonnelSortOrders.Default:
                    group.UserStatuses = group.UserStatuses.OrderBy(x => x.Weight).ToList();
                    break;

                case PersonnelSortOrders.FirstName:
                    group.UserStatuses = group.UserStatuses.OrderBy(x => x.Weight).ThenBy(x => x.UserInfo.FirstName).ToList();
                    break;

                case PersonnelSortOrders.LastName:
                    group.UserStatuses = group.UserStatuses.OrderBy(x => x.Weight).ThenBy(x => x.UserInfo.LastName).ToList();
                    break;

                default:
                    group.UserStatuses = group.UserStatuses.OrderBy(x => x.Weight).ToList();
                    break;
                }

                model.UserStatusGroups.Add(group);

                var allGroupMembers = new List <DepartmentGroupMember>(dg.Members);
            }

            var ungroupedUsers = from u in allUsers
                                 where !(groupedUserIds.Contains(u.UserId)) && !(model.ExcludedUsers.Any(item2 => item2 == u.UserId))
                                 select u;

            UserStatusGroup unGroupedUsersGroup = new UserStatusGroup();

            unGroupedUsersGroup.Group = null;
            foreach (var u in ungroupedUsers)
            {
                model.UnGroupedUsers.Add(u.UserId);

                UserState state    = userStates.FirstOrDefault(x => x.UserId == u.UserId);
                var       userInfo = allUsers.FirstOrDefault(x => x.UserId == u.UserId);

                if (state == null)
                {
                    state               = new UserState();
                    state.UserId        = u.UserId;
                    state.AutoGenerated = true;
                    state.Timestamp     = DateTime.UtcNow;
                    state.State         = (int)UserStateTypes.Available;
                }

                var al = model.LastUserActionlogs.FirstOrDefault(x => x.UserId == u.UserId);

                UserStatus userStatus = new UserStatus();
                userStatus.UserInfo        = userInfo;
                userStatus.CurrentStatus   = al;
                userStatus.CurrentStaffing = state;

                if (al != null)
                {
                    if (personnelStatusSortOrder != null && personnelStatusSortOrder.Any())
                    {
                        var statusSorting = personnelStatusSortOrder.FirstOrDefault(x => x.StatusId == al.ActionTypeId);
                        if (statusSorting != null)
                        {
                            userStatus.Weight = statusSorting.Weight;
                        }
                        else
                        {
                            userStatus.Weight = 9000;
                        }
                    }
                    else
                    {
                        userStatus.Weight = 9000;
                    }
                }
                else
                {
                    userStatus.Weight = 9000;
                }

                unGroupedUsersGroup.UserStatuses.Add(userStatus);
            }

            switch (personnelSortOrder)
            {
            case PersonnelSortOrders.Default:
                unGroupedUsersGroup.UserStatuses = unGroupedUsersGroup.UserStatuses.OrderBy(x => x.Weight).ToList();
                break;

            case PersonnelSortOrders.FirstName:
                unGroupedUsersGroup.UserStatuses = unGroupedUsersGroup.UserStatuses.OrderBy(x => x.Weight).ThenBy(x => x.UserInfo.FirstName).ToList();
                break;

            case PersonnelSortOrders.LastName:
                unGroupedUsersGroup.UserStatuses = unGroupedUsersGroup.UserStatuses.OrderBy(x => x.Weight).ThenBy(x => x.UserInfo.LastName).ToList();
                break;

            default:
                unGroupedUsersGroup.UserStatuses = unGroupedUsersGroup.UserStatuses.OrderBy(x => x.Weight).ToList();
                break;
            }
            model.UserStatusGroups.Add(unGroupedUsersGroup);

            return(PartialView("_UserStatusTablePartial", model));
        }