private void GenerateReportForShortName(IEnumerable <CorporateAccount> accounts) { var distinctAcesClientShortName = accounts.Where(x => !string.IsNullOrWhiteSpace(x.AcesClientShortName)).Select(t => t.AcesClientShortName) .Distinct(); foreach (var acesClientShortName in distinctAcesClientShortName) { var accountShortNamewise = accounts.Where(a => a.AcesClientShortName == acesClientShortName); if (!accountShortNamewise.IsNullOrEmpty()) { var destinationFoler = _folderPath; //Path.Combine(_folderPath, clientId); DirectoryOperationsHelper.CreateDirectoryIfNotExist(destinationFoler); DirectoryOperationsHelper.CreateDirectoryIfNotExist(_folderArchivedPath); var fileNameWithoutExtention = acesClientShortName + "_Universal_Member"; var fileName = fileNameWithoutExtention + ".txt"; var destinationFile = Path.Combine(destinationFoler, fileName); if (DirectoryOperationsHelper.IsFileExist(destinationFile)) { var archiveFileName = fileNameWithoutExtention + "_" + DateTime.Today.AddDays(-1).ToString("ddMMyyyy") + ".txt"; var archiveFilePath = Path.Combine(_folderArchivedPath, archiveFileName); DirectoryOperationsHelper.DeleteFileIfExist(archiveFilePath); DirectoryOperationsHelper.Move(destinationFile, archiveFilePath); } var suppressedRecordsFile = Path.Combine(destinationFoler, acesClientShortName + "_Suppressed_Member_Records.txt"); if (DirectoryOperationsHelper.IsFileExist(suppressedRecordsFile)) { var archivesuppressedRecordsFileName = acesClientShortName + "_Suppressed_Member_Records_" + DateTime.Today.AddDays(-1).ToString("ddMMyyyy") + ".txt"; var archiveFilePath = Path.Combine(_folderArchivedPath, archivesuppressedRecordsFileName); DirectoryOperationsHelper.Move(suppressedRecordsFile, Path.Combine(_folderArchivedPath, archivesuppressedRecordsFileName)); } WriteCsvHeader(destinationFile); _logger.Info("starting for account AcesClientShortName " + acesClientShortName); foreach (var corporateAccount in accountShortNamewise) { try { _logger.Info("starting for account Tag " + corporateAccount.Tag); var filter = new UniversalMemberListModelFilter { AccountId = corporateAccount.Id, Tag = corporateAccount.Tag }; var pageNumber = 1; while (true) { int totalRecords = 0; var customers = _customerRepository.GetCustomerForUniversalMemberReport(filter, pageNumber, PageSize, out totalRecords); if (customers.IsNullOrEmpty()) { break; } var customerIds = customers.Select(x => x.CustomerId); var pcpCollection = _primaryCarePhysicianRepository.GetCustomerPcpWithoutAddress( customers.Select(x => x.CustomerId)); var customerEligilityCollection = _customerEligibilityRepository.GetCustomerEligibilityByCustomerIds(customerIds); var languages = _languageRepository.GetAll(); var collection = new List <UniversalMemberViewModel>(); var suppressedRecordsCollection = new List <UniversalMemberViewModel>(); foreach (var customer in customers) { var customerPcps = pcpCollection.Where(pcp => pcp.CustomerId == customer.CustomerId).ToArray(); var customerEligibilityStatus = customerEligilityCollection.Where(x => x.CustomerId == customer.CustomerId).ToArray(); var pcpStartDate = (DateTime?)null; var pcpEndDate = (DateTime?)null; var pcpId = (long?)null; if (!customerPcps.IsNullOrEmpty()) { var activePcp = customerPcps.SingleOrDefault(x => x.IsActive); if (activePcp != null) { pcpStartDate = activePcp.DataRecorderMetaData.DateCreated; pcpId = activePcp.Id; } } var eligibilityStartDate = (DateTime?)null; var eligibilityEndDate = (DateTime?)null; if (!customerEligibilityStatus.IsNullOrEmpty()) { var customerEligibilityforCurrentYear = customerEligibilityStatus.FirstOrDefault(x => x.IsEligible.HasValue && x.IsEligible.Value && x.ForYear == DateTime.Today.Year); if (customerEligibilityforCurrentYear != null) { var year = customerEligibilityforCurrentYear.ForYear; eligibilityStartDate = new DateTime(year, 1, 1); eligibilityEndDate = new DateTime(year, 12, 31); } } var language = languages.SingleOrDefault(x => x.Id == customer.LanguageId); var model = SetUniversalMemberViewModelData(customer, corporateAccount, language, pcpStartDate, pcpEndDate, eligibilityStartDate, eligibilityEndDate, pcpId); if (string.IsNullOrWhiteSpace(model.FirstName) || string.IsNullOrWhiteSpace(model.LastName) || model.DoB == null || string.IsNullOrWhiteSpace(model.ClientMemberId)) { suppressedRecordsCollection.Add(model); } else { collection.Add(model); } } WriteCsv(collection, destinationFile); if (!suppressedRecordsCollection.IsNullOrEmpty()) { WriteCsvHeader(suppressedRecordsFile); WriteCsv(suppressedRecordsCollection, suppressedRecordsFile); } _logger.Info((pageNumber * PageSize) + " out of " + totalRecords); if ((pageNumber * PageSize) >= totalRecords) { break; } pageNumber++; } _logger.Info("completed for Account " + corporateAccount.Tag); } catch (Exception exception) { _logger.Error("some error occurred while created Member List for Account " + corporateAccount.Tag); _logger.Error("Message: " + exception.Message); _logger.Error("Stack Trace: " + exception.StackTrace); } } if (DirectoryOperationsHelper.IsFileExist(destinationFile)) { _logger.Info("Universal Member Report generated : " + destinationFile); DirectoryOperationsHelper.CreateDirectoryIfNotExist(_sftpFolderPath); _logger.Info("Deleting old file from SFTP."); DirectoryOperationsHelper.DeleteFileIfExist(Path.Combine(_sftpFolderPath, acesClientShortName + "_Universal_Member.txt")); _logger.Info("Old file deleted from SFTP."); _logger.Info("Copying file to SFTP location : " + _sftpFolderPath); var sftpFilePath = Path.Combine(_sftpFolderPath, fileName); DirectoryOperationsHelper.Copy(destinationFile, sftpFilePath); _logger.Info("File copied : " + sftpFilePath); } } } }