public async Task GenerateReport(IReportServiceContext reportServiceContext, ZipArchive archive, bool isFis, CancellationToken cancellationToken)
        {
            Task <IMessage>   ilrFileTask = _ilrProviderService.GetIlrFile(reportServiceContext, cancellationToken);
            Task <FM81Global> fm81Task    =
                _fm81TrailBlazerProviderService.GetFM81Data(reportServiceContext, cancellationToken);
            Task <List <string> > validLearnersTask =
                _validLearnersService.GetLearnersAsync(reportServiceContext, cancellationToken);

            await Task.WhenAll(ilrFileTask, fm81Task, validLearnersTask);

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            List <ILearner> learners =
                ilrFileTask.Result?.Learners?.Where(x => validLearnersTask.Result.Contains(x.LearnRefNumber)).ToList();

            if (learners == null)
            {
                _logger.LogWarning("Failed to get learners for Trailblazer Apprenticeships Occupancy Report");
                return;
            }

            string[] learnAimRefs = learners.SelectMany(x => x.LearningDeliveries).Select(x => x.LearnAimRef).Distinct().ToArray();
            Dictionary <string, LarsLearningDelivery> larsLearningDeliveries = await _larsProviderService.GetLearningDeliveriesAsync(learnAimRefs, cancellationToken);

            var fm81Data = fm81Task.Result;
            var trailblazerAppsOccupancyModels = new List <TrailblazerAppsOccupancyModel>();

            foreach (var learner in learners)
            {
                FM81Learner fm81Learner = fm81Data?.Learners?.SingleOrDefault(x => string.Equals(x.LearnRefNumber, learner.LearnRefNumber, StringComparison.OrdinalIgnoreCase));
                foreach (ILearningDelivery learningDelivery in learner.LearningDeliveries)
                {
                    if (!CheckIsApplicableLearner(learningDelivery))
                    {
                        continue;
                    }

                    LarsLearningDelivery larsDelivery             = larsLearningDeliveries.SingleOrDefault(x => string.Equals(x.Key, learningDelivery.LearnAimRef, StringComparison.OrdinalIgnoreCase)).Value;
                    LearningDelivery     ruleBaseLearningDelivery = fm81Learner?.LearningDeliveries
                                                                    ?.SingleOrDefault(x => x.AimSeqNumber == learningDelivery.AimSeqNumber);
                    trailblazerAppsOccupancyModels.Add(_trailblazerAppsOccupancyModelBuilder
                                                       .BuildTrailblazerAppsOccupancyModel(
                                                           learner,
                                                           learningDelivery,
                                                           larsDelivery,
                                                           ruleBaseLearningDelivery));
                }
            }

            trailblazerAppsOccupancyModels.Sort(new TrailblazerAppsOccupancyModelComparer());

            var csv = GetReportCsv(trailblazerAppsOccupancyModels);

            var jobId            = reportServiceContext.JobId;
            var ukPrn            = reportServiceContext.Ukprn.ToString();
            var externalFileName = GetExternalFilename(ukPrn, jobId, reportServiceContext.SubmissionDateTimeUtc);
            var fileName         = GetFilename(ukPrn, jobId, reportServiceContext.SubmissionDateTimeUtc);

            await _streamableKeyValuePersistenceService.SaveAsync($"{externalFileName}.csv", csv, cancellationToken);

            await WriteZipEntry(archive, $"{fileName}.csv", csv);
        }
 public TBL_LearningDelivery BuildLearningDelivery(LearningDelivery ld, int ukprn, string learnRefNumber)
 {
     return(new TBL_LearningDelivery
     {
         UKPRN = ukprn,
         LearnRefNumber = learnRefNumber,
         AimSeqNumber = ld.AimSeqNumber.Value,
         AchApplicDate = ld.LearningDeliveryValues.AchApplicDate,
         AchEligible = ld.LearningDeliveryValues.AchEligible,
         Achieved = ld.LearningDeliveryValues.Achieved,
         AchievementApplicVal = ld.LearningDeliveryValues.AchievementApplicVal,
         AchPayment = ld.LearningDeliveryValues.AchPayment,
         ActualDaysIL = ld.LearningDeliveryValues.ActualDaysIL,
         ActualNumInstalm = ld.LearningDeliveryValues.ActualNumInstalm,
         AdjProgStartDate = ld.LearningDeliveryValues.AdjProgStartDate,
         AdjStartDate = ld.LearningDeliveryValues.AdjStartDate,
         AgeStandardStart = ld.LearningDeliveryValues.AgeStandardStart,
         ApplicFundValDate = ld.LearningDeliveryValues.ApplicFundValDate,
         CombinedAdjProp = ld.LearningDeliveryValues.CombinedAdjProp,
         CoreGovContCapApplicVal = ld.LearningDeliveryValues.CoreGovContCapApplicVal,
         CoreGovContPayment = ld.LearningDeliveryValues.CoreGovContPayment,
         CoreGovContUncapped = ld.LearningDeliveryValues.CoreGovContUncapped,
         EmpIdAchDate = ld.LearningDeliveryValues.EmpIdAchDate,
         EmpIdFirstDayStandard = ld.LearningDeliveryValues.EmpIdFirstDayStandard,
         EmpIdFirstYoungAppDate = ld.LearningDeliveryValues.EmpIdFirstYoungAppDate,
         EmpIdSecondYoungAppDate = ld.LearningDeliveryValues.EmpIdSecondYoungAppDate,
         EmpIdSmallBusDate = ld.LearningDeliveryValues.EmpIdSmallBusDate,
         FundLine = ld.LearningDeliveryValues.FundLine,
         InstPerPeriod = ld.LearningDeliveryValues.InstPerPeriod,
         LearnDelDaysIL = ld.LearningDeliveryValues.LearnDelDaysIL,
         LearnDelStandardAccDaysIL = ld.LearningDeliveryValues.LearnDelStandardAccDaysIL,
         LearnDelStandardPrevAccDaysIL = ld.LearningDeliveryValues.LearnDelStandardPrevAccDaysIL,
         LearnDelStandardTotalDaysIL = ld.LearningDeliveryValues.LearnDelStandardTotalDaysIL,
         LearnSuppFund = ld.LearningDeliveryValues.LearnSuppFund,
         LearnSuppFundCash = ld.LearningDeliveryValues.LearnSuppFundCash,
         MathEngAimValue = ld.LearningDeliveryValues.MathEngAimValue,
         MathEngBalPayment = ld.LearningDeliveryValues.MathEngBalPayment,
         MathEngBalPct = ld.LearningDeliveryValues.MathEngBalPct,
         MathEngLSFFundStart = ld.LearningDeliveryValues.MathEngLSFFundStart,
         MathEngLSFThresholdDays = ld.LearningDeliveryValues.MathEngLSFThresholdDays,
         MathEngOnProgPayment = ld.LearningDeliveryValues.MathEngOnProgPayment,
         MathEngOnProgPct = ld.LearningDeliveryValues.MathEngOnProgPct,
         OutstandNumOnProgInstalm = ld.LearningDeliveryValues.OutstandNumOnProgInstalm,
         PlannedNumOnProgInstalm = ld.LearningDeliveryValues.PlannedNumOnProgInstalm,
         PlannedTotalDaysIL = ld.LearningDeliveryValues.PlannedTotalDaysIL,
         ProgStandardStartDate = ld.LearningDeliveryValues.ProgStandardStartDate,
         SmallBusApplicVal = ld.LearningDeliveryValues.SmallBusApplicVal,
         SmallBusEligible = ld.LearningDeliveryValues.SmallBusEligible,
         SmallBusPayment = ld.LearningDeliveryValues.SmallBusPayment,
         SmallBusStatusFirstDayStandard = ld.LearningDeliveryValues.SmallBusStatusFirstDayStandard,
         SmallBusStatusThreshold = ld.LearningDeliveryValues.SmallBusStatusThreshold,
         SmallBusThresholdDate = ld.LearningDeliveryValues.SmallBusThresholdDate,
         YoungAppApplicVal = ld.LearningDeliveryValues.YoungAppApplicVal,
         YoungAppEligible = ld.LearningDeliveryValues.YoungAppEligible,
         YoungAppFirstPayment = ld.LearningDeliveryValues.YoungAppFirstPayment,
         YoungAppFirstThresholdDate = ld.LearningDeliveryValues.YoungAppFirstThresholdDate,
         YoungAppPayment = ld.LearningDeliveryValues.YoungAppPayment,
         YoungAppSecondPayment = ld.LearningDeliveryValues.YoungAppSecondPayment,
         YoungAppSecondThresholdDate = ld.LearningDeliveryValues.YoungAppSecondThresholdDate
     });
 }