Example #1
0
        public async Task <int> Load(Guid extractId, int found)
        {
            try
            {
                DomainEvents.Dispatch(
                    new ExtractActivityNotification(extractId, new DwhProgress(
                                                        nameof(PatientLaboratoryExtract),
                                                        nameof(ExtractStatus.Loading),
                                                        found, 0, 0, 0, 0)));

                //load temp extracts without errors
                StringBuilder query = new StringBuilder();
                query.Append($" SELECT * FROM {nameof(TempPatientLaboratoryExtract)}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 tempPatientLaboratoryExtracts = _tempPatientLaboratoryExtractRepository.GetFromSql(query.ToString());

                const int take  = 1000;
                int       skip  = 0;
                var       count = tempPatientLaboratoryExtracts.Count();
                while (skip < count)
                {
                    var batch = tempPatientLaboratoryExtracts.Skip(skip).Take(take).ToList();
                    //Auto mapper
                    var extractRecords = Mapper.Map <List <TempPatientLaboratoryExtract>, List <PatientLaboratoryExtract> >(batch);
                    foreach (var record in extractRecords)
                    {
                        record.Id = LiveGuid.NewGuid();
                    }
                    //Batch Insert
                    var inserted = _patientLaboratoryExtractRepository.BatchInsert(extractRecords);
                    if (!inserted)
                    {
                        Log.Error($"Extract {nameof(PatientLaboratoryExtract)} not Loaded");
                        return(0);
                    }
                    Log.Debug("saved batch");
                    skip = skip + take;
                    DomainEvents.Dispatch(
                        new ExtractActivityNotification(extractId, new DwhProgress(
                                                            nameof(PatientLaboratoryExtract),
                                                            nameof(ExtractStatus.Loading),
                                                            found, skip, 0, 0, 0)));
                }
                return(count);
            }
            catch (Exception e)
            {
                Log.Error(e, $"Extract {nameof(PatientLaboratoryExtract)} not Loaded");
                return(0);
            }
        }
        public async Task <int> Load(Guid extractId, int found, bool diffSupport)
        {
            int count = 0; var mapper = diffSupport ? ExtractDiffMapper.Instance : ExtractMapper.Instance;

            try
            {
                DomainEvents.Dispatch(
                    new ExtractActivityNotification(extractId, new DwhProgress(
                                                        nameof(PatientLaboratoryExtract),
                                                        nameof(ExtractStatus.Loading),
                                                        found, 0, 0, 0, 0)));

                StringBuilder query = new StringBuilder();
                query.Append($" SELECT s.* FROM {nameof(TempPatientLaboratoryExtract)}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 _tempPatientLaboratoryExtractRepository.GetCount(query.ToString());

                var pageCount = _tempPatientLaboratoryExtractRepository.PageCount(take, eCount);

                int page = 1;
                while (page <= pageCount)
                {
                    var tempPatientLaboratoryExtracts = await
                                                        _tempPatientLaboratoryExtractRepository.ReadAll(query.ToString(), page, take);

                    var batch = tempPatientLaboratoryExtracts.ToList();
                    count += batch.Count();

                    //Auto mapper
                    var extractRecords = mapper.Map <List <TempPatientLaboratoryExtract>, List <PatientLaboratoryExtract> >(batch);
                    foreach (var record in extractRecords)
                    {
                        record.Id = LiveGuid.NewGuid();
                    }
                    //Batch Insert
                    var inserted = _patientLaboratoryExtractRepository.BatchInsert(extractRecords);
                    if (!inserted)
                    {
                        Log.Error($"Extract {nameof(PatientLaboratoryExtract)} not Loaded");
                        return(0);
                    }
                    Log.Debug("saved batch");
                    page++;
                    DomainEvents.Dispatch(
                        new ExtractActivityNotification(extractId, new DwhProgress(
                                                            nameof(PatientLaboratoryExtract),
                                                            nameof(ExtractStatus.Loading),
                                                            found, count, 0, 0, 0)));
                }



                await _mediator.Publish(new DocketExtractLoaded("NDWH", nameof(PatientLaboratoryExtract)));

                return(count);
            }
            catch (Exception e)
            {
                Log.Error(e, $"Extract {nameof(PatientLaboratoryExtract)} not Loaded");
                return(0);
            }
        }