public async Task <int> Load(Guid extractId, int found) { try { DomainEvents.Dispatch( new ExtractActivityNotification(extractId, new DwhProgress( nameof(PatientStatusExtract), nameof(ExtractStatus.Loading), found, 0, 0, 0, 0))); //load temp extracts without errors StringBuilder query = new StringBuilder(); query.Append($" SELECT * FROM {nameof(TempPatientStatusExtract)}s s"); query.Append($" INNER JOIN PatientExtracts p ON "); query.Append($" s.PatientPK = p.PatientPK AND "); query.Append($" s.SiteCode = p.SiteCode "); query.Append($" WHERE s.CheckError = 0"); var tempPatientStatusExtracts = _tempPatientStatusExtractRepository.GetFromSql(query.ToString()); const int take = 1000; int skip = 0; var count = tempPatientStatusExtracts.Count(); while (skip < count) { var batch = tempPatientStatusExtracts.Skip(skip).Take(take).ToList(); //Auto mapper var extractRecords = Mapper.Map <List <TempPatientStatusExtract>, List <PatientStatusExtract> >(batch); foreach (var record in extractRecords) { record.Id = LiveGuid.NewGuid(); } //Batch Insert var inserted = _patientStatusExtractRepository.BatchInsert(extractRecords); if (!inserted) { Log.Error($"Extract {nameof(PatientStatusExtract)} not Loaded"); return(0); } Log.Debug("saved batch"); skip = skip + take; DomainEvents.Dispatch( new ExtractActivityNotification(extractId, new DwhProgress( nameof(PatientStatusExtract), nameof(ExtractStatus.Loading), found, skip, 0, 0, 0))); } return(count); } catch (Exception e) { Log.Error(e, $"Extract {nameof(PatientStatusExtract)} not Loaded"); return(0); } }
public async Task <int> Load(Guid extractId, int found) { int count = 0; try { DomainEvents.Dispatch( new ExtractActivityNotification(extractId, new DwhProgress( nameof(PatientStatusExtract), nameof(ExtractStatus.Loading), found, 0, 0, 0, 0))); StringBuilder query = new StringBuilder(); query.Append($" SELECT s.* FROM {nameof(TempPatientStatusExtract)}s s"); query.Append($" INNER JOIN PatientExtracts p ON "); query.Append($" s.PatientPK = p.PatientPK AND "); query.Append($" s.SiteCode = p.SiteCode "); const int take = 500; var eCount = await _tempPatientStatusExtractRepository.GetCount(query.ToString()); var pageCount = _tempPatientStatusExtractRepository.PageCount(take, eCount); int page = 1; while (page <= pageCount) { var tempPatientStatusExtracts = await _tempPatientStatusExtractRepository.GetAll(query.ToString(), page, take); var batch = tempPatientStatusExtracts.ToList(); count += batch.Count; //Auto mapper var extractRecords = Mapper.Map <List <TempPatientStatusExtract>, List <PatientStatusExtract> >(batch); foreach (var record in extractRecords) { record.Id = LiveGuid.NewGuid(); } //Batch Insert var inserted = _patientStatusExtractRepository.BatchInsert(extractRecords); if (!inserted) { Log.Error($"Extract {nameof(PatientStatusExtract)} not Loaded"); return(0); } Log.Debug("saved batch"); page++; DomainEvents.Dispatch( new ExtractActivityNotification(extractId, new DwhProgress( nameof(PatientStatusExtract), nameof(ExtractStatus.Loading), found, count, 0, 0, 0))); } return(count); } catch (Exception e) { Log.Error(e, $"Extract {nameof(PatientStatusExtract)} not Loaded"); return(0); } }