private async Task <string> GetCsv(IReportServiceContext reportServiceContext, CancellationToken cancellationToken) { Task <IMessage> ilrFileTask = _ilrProviderService.GetIlrFile(reportServiceContext, cancellationToken); Task <List <string> > validLearnersTask = _validLearnersService.GetLearnersAsync(reportServiceContext, cancellationToken); Task <FM25Global> fm25Task = _fm25ProviderService.GetFM25Data(reportServiceContext, cancellationToken); await Task.WhenAll(ilrFileTask, validLearnersTask, fm25Task); if (cancellationToken.IsCancellationRequested) { return(null); } List <string> ilrError = new List <string>(); List <MathsAndEnglishModel> mathsAndEnglishModels = new List <MathsAndEnglishModel>(); if (fm25Task.Result?.Learners != null) { foreach (string validLearnerRefNum in validLearnersTask.Result) { ILearner learner = ilrFileTask.Result?.Learners?.SingleOrDefault(x => x.LearnRefNumber == validLearnerRefNum); FM25Learner fm25Learner = fm25Task.Result.Learners.SingleOrDefault(x => x.LearnRefNumber == validLearnerRefNum); if (learner == null || fm25Learner == null) { ilrError.Add(validLearnerRefNum); continue; } if (!_mathsAndEnglishFm25Rules.IsApplicableLearner(fm25Learner)) { continue; } mathsAndEnglishModels.Add(_mathsAndEnglishModelBuilder.BuildModel(learner, fm25Learner)); } } if (ilrError.Any()) { _logger.LogWarning($"Failed to get one or more ILR learners while generating {nameof(MathsAndEnglishReport)}: {_stringUtilitiesService.JoinWithMaxLength(ilrError)}"); } mathsAndEnglishModels.Sort(MathsAndEnglishModelComparer); using (MemoryStream ms = new MemoryStream()) { UTF8Encoding utF8Encoding = new UTF8Encoding(false, true); using (TextWriter textWriter = new StreamWriter(ms, utF8Encoding)) { using (CsvWriter csvWriter = new CsvWriter(textWriter)) { WriteCsvRecords <MathsAndEnglishMapper, MathsAndEnglishModel>(csvWriter, mathsAndEnglishModels); csvWriter.Flush(); textWriter.Flush(); return(Encoding.UTF8.GetString(ms.ToArray())); } } } }
private void LogWarnings(List <string> larsErrors) { if (larsErrors.Any()) { _logger.LogWarning($"Failed to get LARS data while generating Main Occupancy Report: {_stringUtilitiesService.JoinWithMaxLength(larsErrors)}"); } }
public async Task <List <FundingSummaryModel> > BuildAsync(IReportServiceContext reportServiceContext, CancellationToken cancellationToken) { FundingSummaryModel fundingSummaryModelAlbFunding = new FundingSummaryModel() { Title = "ILR Advanced Loans Bursary Funding (£)" }; FundingSummaryModel fundingSummaryModelAlbAreaCosts = new FundingSummaryModel() { Title = "ILR Advanced Loans Bursary Area Costs (£)" }; List <FundingSummaryModel> fundingSummaryModels = new List <FundingSummaryModel>() { fundingSummaryModelAlbFunding, fundingSummaryModelAlbAreaCosts }; Task <IMessage> ilrFile = _ilrProviderService.GetIlrFile(reportServiceContext, cancellationToken); Task <List <string> > validLearners = _validLearnersService.GetLearnersAsync(reportServiceContext, cancellationToken); Task <ALBGlobal> albData = _allbProviderService.GetAllbData(reportServiceContext, cancellationToken); await Task.WhenAll(ilrFile, validLearners, albData); List <string> ilrError = new List <string>(); List <string> albLearnerError = new List <string>(); try { ILearner[] learners = ilrFile.Result?.Learners?.Where(x => validLearners.Result.Contains(x.LearnRefNumber)).ToArray(); foreach (ILearner learner in learners ?? Enumerable.Empty <ILearner>()) { ALBLearner albLearner = albData.Result?.Learners?.SingleOrDefault(x => string.Equals(x.LearnRefNumber, learner.LearnRefNumber, StringComparison.OrdinalIgnoreCase)); if (albLearner == null) { albLearnerError.Add(learner.LearnRefNumber); continue; } foreach (ILearningDelivery learningDelivery in learner.LearningDeliveries ?? Enumerable.Empty <ILearningDelivery>()) { LearningDelivery albLearningDeliveryAreaCosts = albLearner.LearningDeliveries?.Where(x => x.LearningDeliveryValue.FundLine == "Advanced Learner Loans Bursary").SingleOrDefault(x => x.AimSeqNumber == learningDelivery.AimSeqNumber); if (learningDelivery.LearningDeliveryFAMs != null && learningDelivery.LearningDeliveryFAMs.Any(x => !(string.Equals(x.LearnDelFAMType, Constants.LearningDeliveryFAMCodeLDM, StringComparison.OrdinalIgnoreCase) && string.Equals(x.LearnDelFAMCode, "359", StringComparison.OrdinalIgnoreCase)))) { TotalAlbFunding(albLearningDeliveryAreaCosts?.LearningDeliveryPeriodisedValues, reportServiceContext.ReturnPeriod, AlbSupportPayment, fundingSummaryModelAlbFunding); } TotalAlbAreaCosts(albLearningDeliveryAreaCosts?.LearningDeliveryPeriodisedValues, reportServiceContext.ReturnPeriod, fundingSummaryModelAlbAreaCosts); } } if (ilrError.Any()) { _logger.LogWarning($"Failed to get one or more ILR learners while {nameof(AllbBuilder)}.{nameof(BuildAsync)}: {_stringUtilitiesService.JoinWithMaxLength(ilrError)}"); } if (albLearnerError.Any()) { _logger.LogWarning($"Failed to get one or more ALB learners while {nameof(AllbBuilder)}.{nameof(BuildAsync)}: {_stringUtilitiesService.JoinWithMaxLength(albLearnerError)}"); } } catch (Exception ex) { _logger.LogError("AlbBuilder BuildAsync failed with Exception: ", ex); } return(fundingSummaryModels); }
private void CheckWarnings(IReadOnlyCollection <string> ilrError, IReadOnlyCollection <string> larsError, List <string> albLearnerError) { if (ilrError.Any()) { _logger.LogWarning($"Failed to get one or more ILR learners while generating {nameof(MathsAndEnglishReport)}: {_stringUtilitiesService.JoinWithMaxLength(ilrError)}"); } if (larsError.Any()) { _logger.LogWarning($"Failed to get one or more LARS learners while generating {nameof(MathsAndEnglishReport)}: {_stringUtilitiesService.JoinWithMaxLength(larsError)}"); } if (albLearnerError.Any()) { _logger.LogWarning($"Failed to get one or more ALB learners while generating {nameof(MathsAndEnglishReport)}: {_stringUtilitiesService.JoinWithMaxLength(albLearnerError)}"); } }
private async Task <string> GetCsv(IReportServiceContext reportServiceContext, CancellationToken cancellationToken) { Task <IMessage> ilrFileTask = _ilrProviderService.GetIlrFile(reportServiceContext, cancellationToken); Task <List <string> > validLearnersTask = _validLearnersService.GetLearnersAsync(reportServiceContext, cancellationToken); Task <FM25Global> fm25Task = _fm25ProviderService.GetFM25Data(reportServiceContext, cancellationToken); await Task.WhenAll(ilrFileTask, validLearnersTask, fm25Task); if (cancellationToken.IsCancellationRequested) { return(null); } List <string> ilrError = new List <string>(); List <SummaryOfFunding1619Model> summaryOfFunding1619Models = new List <SummaryOfFunding1619Model>(validLearnersTask.Result.Count); foreach (string validLearnerRefNum in validLearnersTask.Result) { var learner = ilrFileTask.Result?.Learners?.SingleOrDefault(x => x.LearnRefNumber == validLearnerRefNum); var fm25Learner = fm25Task.Result?.Learners?.SingleOrDefault(x => x.LearnRefNumber == validLearnerRefNum); if (learner == null || fm25Learner == null) { ilrError.Add(validLearnerRefNum); continue; } summaryOfFunding1619Models.Add(new SummaryOfFunding1619Model { FundLine = fm25Learner.FundLine, LearnRefNumber = learner.LearnRefNumber, FamilyName = learner.FamilyName, GivenNames = learner.GivenNames, DateOfBirth = learner.DateOfBirthNullable?.ToString("dd/MM/yyyy"), CampId = learner.CampId, PlanLearnHours = learner.PlanLearnHoursNullable, PlanEepHours = learner.PlanEEPHoursNullable, TotalPlannedHours = (learner.PlanLearnHoursNullable ?? 0) + (learner.PlanEEPHoursNullable ?? 0), RateBand = fm25Learner.RateBand, StartFund = fm25Learner.StartFund ?? false, OnProgPayment = fm25Learner.OnProgPayment }); } summaryOfFunding1619Models.Sort(SummaryOfFunding1619ModelComparer); if (ilrError.Any()) { _logger.LogWarning($"Failed to get one or more ILR learners while generating S{nameof(MathsAndEnglishReport)}: {_stringUtilitiesService.JoinWithMaxLength(ilrError)}"); } using (MemoryStream ms = new MemoryStream()) { UTF8Encoding utF8Encoding = new UTF8Encoding(false, true); using (TextWriter textWriter = new StreamWriter(ms, utF8Encoding)) { using (CsvWriter csvWriter = new CsvWriter(textWriter)) { WriteCsvRecords <SummaryOfFunding1619Mapper, SummaryOfFunding1619Model>(csvWriter, summaryOfFunding1619Models); csvWriter.Flush(); textWriter.Flush(); return(Encoding.UTF8.GetString(ms.ToArray())); } } } }