public ScoredResultDataDTO[] GetScoredResults(IEnumerable <KeyValuePair <long, long> > scoringSessionIds)
        {
            var scoredResultData = new ScoredResultDataDTO[0];
            var sessionIds       = scoringSessionIds.Select(x => x.Value);
            var scoringIds       = scoringSessionIds.Select(x => x.Key);

            DbContext.Configuration.LazyLoadingEnabled = false;

            /// Load results and check if recalculation needed
            var results = DbContext.Set <ResultEntity>().Where(x => sessionIds.Contains(x.ResultId))
                          .Include(x => x.Session);

            foreach (var result in results)
            {
                if (result.RequiresRecalculation)
                {
                    ILeagueActionProvider leagueActionProvider = new LeagueActionProvider(DbContext);
                    leagueActionProvider.CalculateScoredResult(result.ResultId);
                }
            }

            IEnumerable <ScoredResultEntity> scoredResultEntities = DbContext.Set <ScoredResultEntity>()
                                                                    .Include(x => x.Scoring)
                                                                    .Where(x => sessionIds.Contains(x.ResultId))
                                                                    .ToArray(); // Filter data before fetching from database

            scoredResultEntities = scoringSessionIds
                                   .Select(x => scoredResultEntities
                                           .SingleOrDefault(y => x.Key == y.ScoringId && x.Value == y.ResultId)).ToArray(); // Filter data after fetching from database to the exact needed scoredResults

            DbContext.Set <ScoredResultRowEntity>()
            .Where(x => sessionIds.Contains(x.ScoredResultId) && scoringIds.Contains(x.ScoringId))
            .Include(x => x.ResultRow)
            .Include(x => x.AddPenalty)
            .Include(x => x.ReviewPenalties)
            .Load();

            DbContext.Set <ScoredTeamResultRowEntity>()
            .Where(x => sessionIds.Contains(x.ScoredResultId) && scoringIds.Contains(x.ScoringId))
            .Include(x => x.ScoredResultRows)
            .Load();

            DbContext.Set <IncidentReviewEntity>()
            .Where(x => sessionIds.Contains(x.SessionId))
            .Include(x => x.AcceptedReviewVotes)
            .Load();

            DbContext.ChangeTracker.DetectChanges();

            var memberIds = new List <long>();

            foreach (var scoredResult in scoredResultEntities)
            {
                if (scoredResult.FinalResults != null)
                {
                    foreach (var row in scoredResult.FinalResults)
                    {
                        memberIds.Add(row.MemberId);
                    }
                }
                if (scoredResult is ScoredTeamResultEntity scoredTeamResultEntity && scoredTeamResultEntity.TeamResults != null)
                {
                    var scoredTeamResultRows = scoredTeamResultEntity.TeamResults
                                               .SelectMany(x => x.ScoredResultRows)
                                               .Where(x => x != null);
                    foreach (var row in scoredTeamResultRows)
                    {
                        memberIds.Add(row.MemberId);
                    }
                }
            }
            memberIds = memberIds.Distinct().ToList();

            DbContext.Set <LeagueMemberEntity>()
            .Where(x => memberIds.Contains(x.MemberId))
            .Load();

            //foreach (var scoredResultEntity in scoredResultEntities)
            //{
            //    if (scoredResultEntity is ScoredTeamResultEntity scoredTeamResultEntity)
            //    {
            //        DbContext.Entry(scoredTeamResultEntity).Collection(x => x.TeamResults).Query()
            //            .Include(x => x.ScoredResultRows).Load();
            //    }
            //}

            DbContext.ChangeTracker.DetectChanges();

            var mapper = new DTOMapper(DbContext);

            scoredResultData = scoredResultEntities.Select(x => mapper.MapTo <ScoredResultDataDTO>(x)).ToArray();
            DbContext.Configuration.LazyLoadingEnabled = true;

            return(scoredResultData);
        }
示例#2
0
        public ScoredResultDataDTO GetScoredResult(long sessionId, long scoringId)
        {
            var scoredResultData = new ScoredResultDataDTO();

            DbContext.Configuration.LazyLoadingEnabled = false;

            /// Load result and check if recalculation needed
            var result = DbContext.Set <ResultEntity>().Where(x => x.ResultId == sessionId)
                         .Include(x => x.Session)
                         .FirstOrDefault();

            if (result == null)
            {
                return(new ScoredResultDataDTO()
                {
                    ResultId = sessionId,
                    ScoringId = scoringId
                });
            }
            else if (result.RequiresRecalculation)
            {
                ILeagueActionProvider leagueActionProvider = new LeagueActionProvider(DbContext);
                leagueActionProvider.CalculateScoredResult(sessionId);
            }

            var scoredResultEntity = DbContext.Set <ScoredResultEntity>()
                                     //.AsNoTracking()
                                     //.Include(x => x.Result.Session)
                                     .Include(x => x.Scoring)
                                     .Include(x => x.HardChargers)
                                     .Include(x => x.CleanestDrivers)
                                     //.Include(x => x.Result.RawResults.Select(y => y.Member))
                                     //.Include(x => x.Result.RawResults.Select(y => y.ScoredResultRows))
                                     //.Include(x => x.FinalResults.Select(y => y.ResultRow.Member))
                                     .FirstOrDefault(x => x.ResultId == sessionId && x.ScoringId == scoringId);

            if (scoredResultEntity == null)
            {
                return new ScoredResultDataDTO()
                       {
                           ResultId  = sessionId,
                           ScoringId = scoringId
                       }
            }
            ;
            //DbContext.Set<ResultEntity>().Where(x => x.ResultId == sessionId)
            //         .Include(x => x.Session).Load();
            DbContext.Set <ScoredResultRowEntity>().Where(x => x.ScoredResultId == sessionId && x.ScoringId == scoringId)
            .Include(x => x.AddPenalty)
            .Include(x => x.ResultRow.Member.Team)
            .Include(x => x.ReviewPenalties).Load();
            //DbContext.Entry(scoredResultEntity).Reference(x => x.Scoring).Load();
            //DbContext.Entry(scoredResultEntity).Reference(x => x.Result).Query().Include(x => x.Session).Load();
            //DbContext.Entry(scoredResultEntity).Collection(x => x.FinalResults).Query()
            //    .Include(x => x.AddPenalty)
            //    .Include(x => x.ResultRow.Member.Team).Load();

            DbContext.Set <IncidentReviewEntity>()
            .Where(x => x.SessionId == sessionId)
            .Include(x => x.AcceptedReviewVotes)
            .Load();

            //if (scoredResultRowIds != null)
            //{
            //    DbContext.Set<ReviewPenaltyEntity>().Where(x => scoredResultRowIds.Contains(x.ResultRowId))
            //    .Include(x => x.ReviewVote.MemberAtFault)
            //    .Include(x => x.ReviewVote.CustomVoteCat)
            //    .Load();
            //}

            if (scoredResultEntity is ScoredTeamResultEntity scoredTeamResultEntity)
            {
                DbContext.Entry(scoredTeamResultEntity).Collection(x => x.TeamResults).Query()
                .Include(x => x.ScoredResultRows).Load();
            }

            DbContext.ChangeTracker.DetectChanges();

            var mapper = new DTOMapper(DbContext);

            scoredResultData = mapper.MapTo <ScoredResultDataDTO>(scoredResultEntity);
            DbContext.Configuration.LazyLoadingEnabled = true;

            return(scoredResultData);
        }