public override Task ProcessInScope(IServiceProvider serviceProvider)
        {
            using (IServiceScope scope = _serviceProvider.CreateScope())
            {
                var context         = scope.ServiceProvider.GetRequiredService <AspergillosisContext>();
                var externalContext = scope.ServiceProvider.GetRequiredService <ExternalImportDbContext>();

                var allPatients = context.Patients
                                  .Include(p => p.PatientTestResults);

                foreach (var code in TestType.Codes().Keys)
                {
                    var testType = context.TestTypes
                                   .Where(it => it.Name == TestType.LabTestFromCode(code))
                                   .FirstOrDefault();

                    foreach (var patient in allPatients)
                    {
                        var results = externalContext.PathologyReports
                                      .Where(r => r.OrderItemCode.Equals(code) && r.RM2Number == "RM2" + patient.RM2Number);
                        if (!results.Any())
                        {
                            continue;
                        }
                        var existingDates = patient.PatientTestResults
                                            .Where(pi => pi.TestTypeId == testType.ID)
                                            .Select(pi => pi.DateTaken.Date)
                                            .ToList();

                        foreach (var result in results)
                        {
                            if (existingDates.FindAll(d => d.Date == result.DatePerformed.Date).ToList().Count == 0)
                            {
                                if (result.Result == null)
                                {
                                    continue;
                                }
                                var patientTestResult = new PatientTestResult();
                                patientTestResult.PatientId           = patient.ID;
                                patientTestResult.DateTaken           = result.DatePerformed;
                                patientTestResult.TestTypeId          = testType.ID;
                                patientTestResult.SourceSystemGUID    = result.ObservationGUID;
                                patientTestResult.UnitOfMeasurementId = testType.UnitOfMeasurementId;
                                patientTestResult.CreatedDate         = DateTime.Now;

                                try
                                {
                                    patientTestResult.Value = Decimal.Parse(result.Result
                                                                            .Replace("<", String.Empty)
                                                                            .Replace("*", String.Empty)
                                                                            .Replace(">", String.Empty));
                                }
                                catch (System.FormatException e)
                                {
                                    Console.WriteLine("TEST VALUE ERROR::::::::::::" + result.Result);
                                    continue;
                                }
                                patientTestResult.Range = result.NormalRange;
                                context.PatientTestResult.Add(patientTestResult);
                            }
                        }
                    }
                }
                context.SaveChanges();
            }
            return(Task.CompletedTask);
        }
        public async Task <IActionResult> LabTests()
        {
            var allPatients = _context.Patients
                              .Include(p => p.PatientTestResults);

            //var uom = _context.UnitOfMeasurements.Where(u => u.Name == "mg/L").FirstOrDefault();
            foreach (var code in TestType.Codes().Keys)
            {
                var testType = _context.TestTypes
                               .Where(it => it.Name == TestType.LabTestFromCode(code))
                               .FirstOrDefault();

                foreach (var patient in allPatients)
                {
                    var results = _externalImportDbContext.PathologyReports
                                  .Where(r => r.OrderItemCode.Equals(code) &&
                                         r.RM2Number == "RM2" + patient.RM2Number);
                    if (!results.Any())
                    {
                        continue;
                    }
                    var existingDates = patient.PatientTestResults
                                        .Where(pi => pi.TestTypeId == testType.ID)
                                        .Select(pi => pi.DateTaken.Date)
                                        .ToList();

                    foreach (var result in results)
                    {
                        if (existingDates.FindAll(d => d.Date == result.DatePerformed.Date).ToList().Count == 0)
                        {
                            if (result.Result == null)
                            {
                                continue;
                            }
                            var patientTestResult = new PatientTestResult();
                            patientTestResult.PatientId           = patient.ID;
                            patientTestResult.DateTaken           = result.DatePerformed;
                            patientTestResult.TestTypeId          = testType.ID;
                            patientTestResult.SourceSystemGUID    = result.ObservationGUID;
                            patientTestResult.UnitOfMeasurementId = testType.UnitOfMeasurementId;
                            patientTestResult.CreatedDate         = DateTime.Now;

                            try
                            {
                                patientTestResult.Value = Decimal.Parse(result.Result
                                                                        .Replace("<", String.Empty)
                                                                        .Replace("*", String.Empty)
                                                                        .Replace(">", String.Empty));
                            }
                            catch (System.FormatException e)
                            {
                                Console.WriteLine("VALUE::::::::::::" + result.Result);
                                continue;
                            }
                            patientTestResult.Range = result.NormalRange;

                            await _context.PatientTestResult.AddAsync(patientTestResult);
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            return(Ok());
        }