55 using System . Data ;
66 using System . IO ;
77 using System . Linq ;
8+ using System . Threading . Tasks ;
89 using ClosedXML . Excel ;
910 using DigitalLearningSolutions . Data . DataServices ;
1011 using DigitalLearningSolutions . Data . DataServices . UserDataService ;
1112 using DigitalLearningSolutions . Data . Helpers ;
1213 using DigitalLearningSolutions . Data . Models . CustomPrompts ;
1314 using DigitalLearningSolutions . Data . Models . User ;
15+ using Microsoft . Extensions . Configuration ;
1416
1517 public interface IDelegateDownloadFileService
1618 {
@@ -41,6 +43,7 @@ public class DelegateDownloadFileService : IDelegateDownloadFileService
4143 private const string Active = "Active" ;
4244 private const string Approved = "Approved" ;
4345 private const string IsAdmin = "Is admin" ;
46+ private readonly IConfiguration configuration ;
4447 private static readonly XLTableTheme TableTheme = XLTableTheme . TableStyleLight9 ;
4548 private readonly ICentreRegistrationPromptsService centreRegistrationPromptsService ;
4649 private readonly IJobGroupsDataService jobGroupsDataService ;
@@ -49,12 +52,13 @@ public class DelegateDownloadFileService : IDelegateDownloadFileService
4952 public DelegateDownloadFileService (
5053 ICentreRegistrationPromptsService centreRegistrationPromptsService ,
5154 IJobGroupsDataService jobGroupsDataService ,
52- IUserDataService userDataService
55+ IUserDataService userDataService , IConfiguration configuration
5356 )
5457 {
5558 this . centreRegistrationPromptsService = centreRegistrationPromptsService ;
5659 this . jobGroupsDataService = jobGroupsDataService ;
5760 this . userDataService = userDataService ;
61+ this . configuration = configuration ;
5862 }
5963
6064 public byte [ ] GetDelegatesAndJobGroupDownloadFileForCentre ( int centreId )
@@ -139,13 +143,18 @@ private void PopulateAllDelegatesSheet(
139143 )
140144 {
141145 var registrationPrompts = centreRegistrationPromptsService . GetCentreRegistrationPromptsByCentreId ( centreId ) ;
142- var delegatesToExport = GetDelegatesToExport ( centreId , searchString , sortBy , sortDirection , filterString )
143- . ToList ( ) ;
144-
146+ var delegatesToExport = Task . Run ( ( ) => GetDelegatesToExport ( centreId ) ) . Result ;
147+ var searchedUsers = GenericSearchHelper . SearchItems ( delegatesToExport , searchString ) . AsQueryable ( ) ;
148+ var filteredItems = FilteringHelper . FilterItems ( searchedUsers , filterString ) . AsQueryable ( ) ;
149+ var sortedItems = GenericSortingHelper . SortAllItems (
150+ filteredItems ,
151+ sortBy ?? nameof ( DelegateUserCard . SearchableName ) ,
152+ sortDirection
153+ ) . ToList ( ) ;
145154 var dataTable = new DataTable ( ) ;
146155 SetUpDataTableColumnsForAllDelegates ( registrationPrompts , dataTable ) ;
147156
148- foreach ( var delegateRecord in delegatesToExport )
157+ foreach ( var delegateRecord in sortedItems )
149158 {
150159 SetDelegateRowValues ( dataTable , delegateRecord , registrationPrompts ) ;
151160 }
@@ -166,24 +175,24 @@ private void PopulateAllDelegatesSheet(
166175 FormatAllDelegateWorksheetColumns ( workbook , dataTable ) ;
167176 }
168177
169- private IEnumerable < DelegateUserCard > GetDelegatesToExport (
170- int centreId ,
171- string ? searchString ,
172- string ? sortBy ,
173- string sortDirection ,
174- string ? filterString
178+ private async Task < IEnumerable < DelegateUserCard > > GetDelegatesToExport (
179+ int centreId
175180 )
176181 {
177- var delegateUsers = userDataService . GetDelegateUserCardsByCentreId ( centreId ) . Where ( c => ! Guid . TryParse ( c . EmailAddress , out _ ) ) ;
178- var searchedUsers = GenericSearchHelper . SearchItems ( delegateUsers , searchString ) . AsQueryable ( ) ;
179- var filteredItems = FilteringHelper . FilterItems ( searchedUsers , filterString ) . AsQueryable ( ) ;
180- var sortedItems = GenericSortingHelper . SortAllItems (
181- filteredItems ,
182- sortBy ?? nameof ( DelegateUserCard . SearchableName ) ,
183- sortDirection
184- ) ;
182+ var exportQueryRowLimit = Data . Extensions . ConfigurationExtensions . GetExportQueryRowLimit ( configuration ) ;
183+ int resultCount = userDataService . GetCountDelegateUserCardsForExportByCentreId ( centreId ) ;
184+
185+ int totalRun = ( int ) ( resultCount / exportQueryRowLimit ) + ( ( resultCount % exportQueryRowLimit ) > 0 ? 1 : 0 ) ;
186+ int currentRun = 1 ;
187+ List < DelegateUserCard > delegates = new List < DelegateUserCard > ( ) ;
188+ while ( totalRun >= currentRun )
189+ {
190+ delegates . AddRange ( await userDataService . GetDelegateUserCardsForExportByCentreId ( centreId , exportQueryRowLimit , currentRun ) ) ;
191+ currentRun ++ ;
192+ }
193+
185194
186- return sortedItems ;
195+ return delegates . Where ( c => ! Guid . TryParse ( c . EmailAddress , out _ ) ) ;
187196 }
188197
189198 private static void SetUpDataTableColumnsForAllDelegates (
0 commit comments