示例#1
0
        /// <summary>
        /// This can be rerun any time. The service updates the last run time and will continue
        /// </summary>
        public void Sync()
        {
            try
            {
                if (!_settings.SyncWithHra)
                {
                    _logger.Info("Syncing with HRA is off ");
                    return;
                }
                //get all tags for IsHealthPlan is true
                var tags = _organizationRepository.GetMedicareSites().Where(x => x.ShowHraQuestionnaire);
                if (tags != null && tags.Any())
                {
                    foreach (var tag in tags)
                    {
                        var newLastTransactionDate = DateTime.Now; //   use the time when exporting for this tag starts, as start date for next day.
                        _logger.Info("Medicare CustomerResult Sync for tag : " + tag.Alias);
                        var path           = _medicareSyncCustomSettingsPath + "MedicareCustomerResultSync" + tag.Alias + ".xml";
                        var customSettings = _customSettingManager.Deserialize(path);

                        if (customSettings == null || !customSettings.LastTransactionDate.HasValue)
                        {
                            customSettings = new CustomSettings {
                                LastTransactionDate = DateTime.Now.AddHours(-2)
                            };
                        }

                        if (customSettings.LastTransactionDate != null)
                        {
                            var exportFromTime = customSettings.LastTransactionDate.Value;
                            var customerIds    = _customerRepository.GetRecentResultUpdatedForMedicareSync(exportFromTime, tag.Alias).ToArray();
                            if (customerIds.Any())
                            {
                                var events = customerIds.Select(x => x.EventId).Distinct().ToArray();
                                var eventIdsWithMammogramTest       = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.Mammogram).ToArray();
                                var eventIdsWithEchocardiogram      = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvEcho).ToArray();
                                var eventIdsWithAwvLipid            = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvLipid).ToArray();
                                var eventIdsWithAwvSpiro            = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvSpiro).ToArray();
                                var eventIdsWithDiabeticRetinopathy = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.DiabeticRetinopathy).ToArray();
                                var eventIdsWithUrineMicroalbumin   = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.UrineMicroalbumin).ToArray();
                                var eventIdsWithAwvEkg       = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvEkg).ToArray();
                                var eventIdsWithAwvHba1C     = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvHBA1C).ToArray();
                                var eventIdsWithAwvAaa       = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvAAA).ToArray();
                                var eventIdsWithAwvAbi       = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.AwvABI).ToArray();
                                var eventIdsWithQuantaFloAbi = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.QuantaFloABI).ToArray();
                                var eventIdsWithLead         = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.Lead).ToArray();
                                var eventIdsWithBp           = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.Hypertension).ToArray();
                                var eventList = _eventRepository.GetEvents(events).ToArray();
                                //var eventIdsWithMonoFilament = _customerRepository.GetEventWithTest(events, tag.Alias, (long)TestType.Monofilament).ToArray();
                                const int pageSize   = 25;
                                var       totalPages = Math.Ceiling((double)(customerIds.Count()) / pageSize);
                                for (int i = 0; i < totalPages; i++)
                                {
                                    var currentList = customerIds.Skip(i * pageSize).Take(pageSize).ToArray();
                                    try
                                    {
                                        foreach (var medicareResultEditModel in currentList)
                                        {
                                            var eventData       = eventList.First(e => e.Id == medicareResultEditModel.EventId);
                                            var isNewResultFlow = eventData.EventDate >= _settings.ResultFlowChangeDate;

                                            var evaluatingPhysicians = _physicianEvaluationRepsoitory.GetPhysicianEvaluation(medicareResultEditModel.EventCustomerId);
                                            if (evaluatingPhysicians != null)
                                            {
                                                var primaryEvalRecord = evaluatingPhysicians != null?evaluatingPhysicians.Where(p => p.IsPrimaryEvaluator).OrderByDescending(p => p.EvaluationEndTime ?? p.EvaluationStartTime).FirstOrDefault() : null;

                                                var primaryPhysicianId  = primaryEvalRecord != null ? primaryEvalRecord.PhysicianId : 0;
                                                var overreadPhysicianId = primaryEvalRecord != null?evaluatingPhysicians.Where(p => !p.IsPrimaryEvaluator && p.EvaluationStartTime > primaryEvalRecord.EvaluationStartTime).OrderByDescending(
                                                    p => p.EvaluationEndTime ?? p.EvaluationStartTime).Select(p => p.PhysicianId).FirstOrDefault() : 0;

                                                medicareResultEditModel.PhysicianOruId = overreadPhysicianId > 0 ? overreadPhysicianId : primaryPhysicianId;
                                            }



                                            if (eventIdsWithBp.Contains(medicareResultEditModel.EventId)) //1
                                            {
                                                // Blood Pressure Evaluation Test or  Hypertension
                                                var testResultRepository = new HypertensionTestRepository();
                                                var aaaTestResult        = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                var aaaTest = ((Core.Medical.Domain.HypertensionTestResult)aaaTestResult);
                                                if (aaaTest != null && aaaTest.TestNotPerformed == null &&
                                                    aaaTest.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.BpReading           = new MedicareResultEditModel.Bp();
                                                    medicareResultEditModel.BpReading.Systolic  = aaaTest.Systolic != null ? aaaTest.Systolic.Reading : null;
                                                    medicareResultEditModel.BpReading.Diastolic = aaaTest.Diastolic != null ? aaaTest.Diastolic.Reading : null;
                                                }
                                            }
                                            if (eventIdsWithAwvAaa.Contains(medicareResultEditModel.EventId)) //1
                                            {
                                                // Update the AAA result
                                                var testResultRepository = new AwvAaaTestRepository();
                                                var aaaTestResult        = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                var aaaTest = ((Core.Medical.Domain.AwvAaaTestResult)aaaTestResult);
                                                if (aaaTest != null && aaaTest.Finding != null && aaaTest.TestNotPerformed == null &&
                                                    aaaTest.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.AaaResult                  = new MedicareResultEditModel.AaaReadings();
                                                    medicareResultEditModel.AaaResult.AortaSize        = aaaTest.Finding != null ? aaaTest.Finding.Label : null;
                                                    medicareResultEditModel.AaaResult.AorticDissection = aaaTest.AorticDissection != null && aaaTest.AorticDissection.Reading;
                                                    medicareResultEditModel.AaaResult.Plaque           = aaaTest.Plaque != null && aaaTest.Plaque.Reading;
                                                    medicareResultEditModel.AaaResult.Thrombus         = aaaTest.Thrombus != null && aaaTest.Thrombus.Reading;

                                                    if (aaaTest.IncidentalFindings != null)
                                                    {
                                                        foreach (var standardFinding in aaaTest.IncidentalFindings)
                                                        {
                                                            if (standardFinding.Label == "Aortic Stenosis")
                                                            {
                                                                medicareResultEditModel.AaaResult.AorticStenosis = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            if (eventIdsWithAwvAbi.Contains(medicareResultEditModel.EventId))//2
                                            {
                                                // AwvAbi
                                                var testResultRepository = new AwvAbiTestRepository();
                                                var padTestResult        = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                var aaaTest = ((Core.Medical.Domain.AwvAbiTestResult)padTestResult);
                                                if (aaaTest != null && aaaTest.Finding != null && aaaTest.TestNotPerformed == null &&
                                                    aaaTest.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.PadReading         = new MedicareResultEditModel.PadTestReadings();
                                                    medicareResultEditModel.PadReading.Finding = aaaTest.Finding.Label;
                                                }
                                            }
                                            if (eventIdsWithQuantaFloAbi.Contains(medicareResultEditModel.EventId))//3
                                            {
                                                // QuantaFlo Abi
                                                var testResultRepository = new QuantaFloABITestRepository();
                                                var padTestResult        = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                var aaaTest = ((Core.Medical.Domain.QuantaFloABITestResult)padTestResult);
                                                if (aaaTest != null && aaaTest.Finding != null && aaaTest.TestNotPerformed == null &&
                                                    aaaTest.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.QuantaFloReading         = new MedicareResultEditModel.QuantaFloReadings();
                                                    medicareResultEditModel.QuantaFloReading.Finding = aaaTest.Finding.Label;
                                                }
                                            }
                                            if (eventIdsWithLead.Contains(medicareResultEditModel.EventId))//5
                                            {
                                                // Lead
                                                var testResultRepository = new LeadTestRepository();
                                                var leadTestResult       = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                var leadTest             = ((Core.Medical.Domain.LeadTestResult)leadTestResult);
                                                if (leadTest != null && leadTest.TestNotPerformed == null &&
                                                    leadTest.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.LeadReading = new MedicareResultEditModel.LeadReadings();
                                                    if (leadTest.LeftResultReadings != null)
                                                    {
                                                        medicareResultEditModel.LeadReading.LNoVisualPlaque             = leadTest.LeftResultReadings.NoVisualPlaque != null && leadTest.LeftResultReadings.NoVisualPlaque.Reading.HasValue && leadTest.LeftResultReadings.NoVisualPlaque.Reading.Value;
                                                        medicareResultEditModel.LeadReading.LVisuallyDemonstratedPlaque = leadTest.LeftResultReadings.VisuallyDemonstratedPlaque != null && leadTest.LeftResultReadings.VisuallyDemonstratedPlaque.Reading.HasValue && leadTest.LeftResultReadings.VisuallyDemonstratedPlaque.Reading.Value;
                                                        medicareResultEditModel.LeadReading.LModerateStenosis           = leadTest.LeftResultReadings.ModerateStenosis != null && leadTest.LeftResultReadings.ModerateStenosis.Reading.HasValue && leadTest.LeftResultReadings.ModerateStenosis.Reading.Value;
                                                        medicareResultEditModel.LeadReading.LPossibleOcclusion          = leadTest.LeftResultReadings.PossibleOcclusion != null && leadTest.LeftResultReadings.PossibleOcclusion.Reading.HasValue && leadTest.LeftResultReadings.PossibleOcclusion.Reading.Value;
                                                    }
                                                    if (leadTest.LowVelocityLeft != null)
                                                    {
                                                        medicareResultEditModel.LeadReading.LowVelocityLeft = true;
                                                    }
                                                    if (leadTest.RightResultReadings != null)
                                                    {
                                                        medicareResultEditModel.LeadReading.RNoVisualPlaque             = leadTest.RightResultReadings.NoVisualPlaque != null && leadTest.RightResultReadings.NoVisualPlaque.Reading.HasValue && leadTest.RightResultReadings.NoVisualPlaque.Reading.Value;
                                                        medicareResultEditModel.LeadReading.RVisuallyDemonstratedPlaque = leadTest.RightResultReadings.VisuallyDemonstratedPlaque != null && leadTest.RightResultReadings.VisuallyDemonstratedPlaque.Reading.HasValue && leadTest.RightResultReadings.VisuallyDemonstratedPlaque.Reading.Value;
                                                        medicareResultEditModel.LeadReading.RModerateStenosis           = leadTest.RightResultReadings.ModerateStenosis != null && leadTest.RightResultReadings.ModerateStenosis.Reading.HasValue && leadTest.RightResultReadings.ModerateStenosis.Reading.Value;
                                                        medicareResultEditModel.LeadReading.RPossibleOcclusion          = leadTest.RightResultReadings.PossibleOcclusion != null && leadTest.RightResultReadings.PossibleOcclusion.Reading.HasValue && leadTest.RightResultReadings.PossibleOcclusion.Reading.Value;
                                                    }
                                                    if (leadTest.LowVelocityRight != null)
                                                    {
                                                        medicareResultEditModel.LeadReading.LowVelocityRight = true;
                                                    }
                                                }
                                            }

                                            if (eventIdsWithMammogramTest.Contains(medicareResultEditModel.EventId))
                                            {
                                                // Update the mammogram result
                                                var testResultRepository = new MammogramTestRepository();
                                                var mammogramTestResult  = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                var mamoTest             = ((Core.Medical.Domain.MammogramTestResult)mammogramTestResult);
                                                if (mamoTest != null && mamoTest.Finding != null && mamoTest.TestNotPerformed == null && mamoTest.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.MammogramReading = mamoTest.Finding.Label;
                                                }
                                            }
                                            if (eventIdsWithAwvSpiro.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new AwvSpiroTestRepository();
                                                var awvSpiroTestResult   = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvSpiroTestResult != null && awvSpiroTestResult.TestNotPerformed == null && awvSpiroTestResult.UnableScreenReason == null)
                                                {
                                                    if (awvSpiroTestResult.ResultStatus != null)
                                                    {
                                                        medicareResultEditModel.AwvSpiroStateNumber = (int)awvSpiroTestResult.ResultStatus.StateNumber;
                                                    }
                                                    var spirotest = ((Core.Medical.Domain.AwvSpiroTestResult)awvSpiroTestResult);
                                                    if (spirotest.Finding != null)
                                                    {
                                                        medicareResultEditModel.AwvSpiroFindingLabel = spirotest.Finding.Label;
                                                    }
                                                    if (spirotest.Obstructive != null)
                                                    {
                                                        medicareResultEditModel.AwvSpiroObstructiveLabel = spirotest.Obstructive.Label.ToString();
                                                    }
                                                }
                                            }
                                            //if (eventIdsWithMonoFilament.Contains(medicareResultEditModel.EventId))
                                            //{
                                            //    var testResultRepository = new MonofilamentTestRepository();
                                            //    var monoTestResult = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId);
                                            //    if (monoTestResult != null && monoTestResult.ResultStatus != null)
                                            //    {
                                            //        medicareResultEditModel.MonoFilamentStateNumber = (int)monoTestResult.ResultStatus.StateNumber;
                                            //    }
                                            //}
                                            if (eventIdsWithEchocardiogram.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new AwvEchocardiogramTestRepository();
                                                var awvEchoTestResult    = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvEchoTestResult != null && awvEchoTestResult.TestNotPerformed == null && awvEchoTestResult.UnableScreenReason == null)
                                                {
                                                    try
                                                    {
                                                        if (awvEchoTestResult != null && awvEchoTestResult.ResultStatus != null)
                                                        {
                                                            medicareResultEditModel.AwvEchoStateNumber = (int)awvEchoTestResult.ResultStatus.StateNumber;
                                                        }
                                                        var echocartest = ((Core.Medical.Domain.AwvEchocardiogramTestResult)awvEchoTestResult);

                                                        medicareResultEditModel.AwvEchoFindingLabel   = echocartest.EstimatedEjactionFraction != null ? echocartest.EstimatedEjactionFraction.Label : null;
                                                        medicareResultEditModel.EchocardiogramReading = new MedicareResultEditModel.EchocardiogramReadings();

                                                        if (echocartest.EstimatedEjactionFraction != null)
                                                        {
                                                            medicareResultEditModel.EchocardiogramReading.EstimatedEjactionFractionMin = echocartest.EstimatedEjactionFraction.MinValue;
                                                            medicareResultEditModel.EchocardiogramReading.EstimatedEjactionFractionMax = echocartest.EstimatedEjactionFraction.MaxValue;
                                                        }
                                                        //Aortic
                                                        medicareResultEditModel.EchocardiogramReading.Aortic = echocartest.Aortic != null && echocartest.Aortic.Reading;
                                                        if (echocartest.AorticMorphology != null)
                                                        {
                                                            foreach (var standardFinding in echocartest.AorticMorphology)
                                                            {
                                                                if (standardFinding.Label == "Stenosis")
                                                                {
                                                                    medicareResultEditModel.EchocardiogramReading.AorticStenosis = true;
                                                                }
                                                            }
                                                        }
                                                        var aorticRegurgitation = echocartest.AorticRegurgitation != null ? echocartest.AorticRegurgitation.Label : null;
                                                        medicareResultEditModel.EchocardiogramReading.AorticRegurgitation = aorticRegurgitation != null ? (aorticRegurgitation == "Mod" ? "Moderate" : aorticRegurgitation) : null;

                                                        //Mitral
                                                        medicareResultEditModel.EchocardiogramReading.Mitral = echocartest.Mitral != null && echocartest.Mitral.Reading;
                                                        if (echocartest.MitralMorphology != null)
                                                        {
                                                            foreach (var standardFinding in echocartest.MitralMorphology)
                                                            {
                                                                if (standardFinding.Label == "Stenosis")
                                                                {
                                                                    medicareResultEditModel.EchocardiogramReading.MitralStenosis = true;
                                                                }
                                                                if (standardFinding.Label == "Mitral Prolapse")
                                                                {
                                                                    medicareResultEditModel.EchocardiogramReading.MitralProlapse = true;
                                                                }
                                                            }
                                                        }
                                                        var mitralRegurgitation = echocartest.MitralRegurgitation != null ? echocartest.MitralRegurgitation.Label : null;
                                                        medicareResultEditModel.EchocardiogramReading.MitralRegurgitation = mitralRegurgitation != null ? (mitralRegurgitation == "Mod" ? "Moderate" : mitralRegurgitation) : null;

                                                        //Pulmonic
                                                        medicareResultEditModel.EchocardiogramReading.Pulmonic = echocartest.Pulmonic != null && echocartest.Pulmonic.Reading;
                                                        var pulmonicRegurgitation = echocartest.PulmonicRegurgitation != null ? echocartest.PulmonicRegurgitation.Label : null;
                                                        medicareResultEditModel.EchocardiogramReading.PulmonicRegurgitation = pulmonicRegurgitation != null ? (pulmonicRegurgitation == "Mod" ? "Moderate" : pulmonicRegurgitation) : null;


                                                        //Tricuspid
                                                        medicareResultEditModel.EchocardiogramReading.Tricuspid     = echocartest.Tricuspid != null && echocartest.Tricuspid.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.HighOrGreater = echocartest.MorphologyTricuspidHighOrGreater != null && echocartest.MorphologyTricuspidHighOrGreater.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.TricuspidPap  = echocartest.TricuspidPap != null ? echocartest.TricuspidPap.Reading : null;
                                                        var tricuspidRegurgitation = echocartest.TricuspidRegurgitation != null ? echocartest.TricuspidRegurgitation.Label : null;
                                                        medicareResultEditModel.EchocardiogramReading.TricuspidRegurgitation = tricuspidRegurgitation != null ? (tricuspidRegurgitation == "Mod" ? "Moderate" : tricuspidRegurgitation) : null;

                                                        //DiastolicDysfunction
                                                        medicareResultEditModel.EchocardiogramReading.DiastolicDysfunction        = echocartest.DiastolicDysfunction != null && echocartest.DiastolicDysfunction.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.DistolicDysfunctionFinding  = echocartest.DistolicDysfunctionFinding != null ? echocartest.DistolicDysfunctionFinding.Label : null;
                                                        medicareResultEditModel.EchocardiogramReading.DiastolicDysfunctionEeRatio = echocartest.DiastolicDysfunctionEeRatio != null ? echocartest.DiastolicDysfunctionEeRatio.Reading : null;

                                                        medicareResultEditModel.EchocardiogramReading.AorticRoot             = echocartest.AorticRoot != null && echocartest.AorticRoot.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.Sclerotic              = echocartest.Sclerotic != null && echocartest.Sclerotic.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.Atherosclerosis        = echocartest.Atherosclerosis != null && echocartest.Atherosclerosis.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.VentricularHypertrophy = echocartest.VentricularHypertrophy != null && echocartest.VentricularHypertrophy.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.Arrythmia              = echocartest.Arrythmia != null && echocartest.Arrythmia.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.AFib     = echocartest.AFib != null && echocartest.AFib.Reading;
                                                        medicareResultEditModel.EchocardiogramReading.AFlutter = echocartest.AFlutter != null && echocartest.AFlutter.Reading;
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        _logger.Error(" error EchocardiogramReading   Customer : " + medicareResultEditModel.CustomerId + " Message: " + ex.Message + "\n stack Trace: " + ex.StackTrace);
                                                    }
                                                }
                                            }
                                            if (eventIdsWithUrineMicroalbumin.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new UrineMicroalbuminTestRepository();
                                                var awvUrinTestResult    = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvUrinTestResult != null && awvUrinTestResult.ResultStatus != null && awvUrinTestResult.TestNotPerformed == null && awvUrinTestResult.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.AwvUrinStateNumber = (int)awvUrinTestResult.ResultStatus.StateNumber;
                                                    var urintest = ((Core.Medical.Domain.UrineMicroalbuminTestResult)awvUrinTestResult);
                                                    if (urintest != null && urintest.Finding != null)
                                                    {
                                                        medicareResultEditModel.AwvUrinFindingLabel = urintest.Finding.Label;
                                                    }
                                                }
                                            }


                                            if (eventIdsWithAwvLipid.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new AwvLipidTestRepository();
                                                var awvLipidTestResult   = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvLipidTestResult != null && awvLipidTestResult.ResultStatus != null && awvLipidTestResult.TestNotPerformed == null &&
                                                    awvLipidTestResult.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.AwvLipidStateNumber = (int)awvLipidTestResult.ResultStatus.StateNumber;
                                                    var awvLipid = ((Core.Medical.Domain.AwvLipidTestResult)awvLipidTestResult);
                                                    if (awvLipid != null)
                                                    {
                                                        medicareResultEditModel.LipidReading = new MedicareResultEditModel.Lipid();
                                                        if (awvLipid.TotalCholestrol != null &&
                                                            awvLipid.TotalCholestrol.Finding != null)
                                                        {
                                                            medicareResultEditModel.AwvLipidTotalCholestrol = awvLipid.TotalCholestrol.Reading;
                                                            medicareResultEditModel.LipidReading.Tc         = awvLipid.TotalCholestrol.Reading;
                                                        }
                                                        if (awvLipid.HDL != null && awvLipid.HDL.Finding != null)
                                                        {
                                                            medicareResultEditModel.LipidReading.Hdl = awvLipid.HDL.Reading;
                                                        }
                                                        if (awvLipid.LDL != null && awvLipid.LDL.Finding != null)
                                                        {
                                                            medicareResultEditModel.LipidReading.Ldl = awvLipid.LDL.Reading;
                                                        }
                                                        if (awvLipid.TriGlycerides != null && awvLipid.TriGlycerides.Finding != null)
                                                        {
                                                            medicareResultEditModel.LipidReading.Trig = awvLipid.TriGlycerides.Reading;
                                                        }
                                                    }
                                                }
                                            }
                                            if (eventIdsWithDiabeticRetinopathy.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new DiabeticRetinopathyTestRepository();
                                                var awvDrsTestResult     = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvDrsTestResult != null && awvDrsTestResult.ResultStatus != null && awvDrsTestResult.TestNotPerformed == null && awvDrsTestResult.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.AwvDrsStateNumber = (int)awvDrsTestResult.ResultStatus.StateNumber;
                                                }
                                            }
                                            if (eventIdsWithAwvEkg.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new AwvEkgTestRepository();
                                                var awvEkgTestResult     = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvEkgTestResult != null && awvEkgTestResult.ResultStatus != null && awvEkgTestResult.TestNotPerformed == null && awvEkgTestResult.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.AwvEkgStateNumber = (int)awvEkgTestResult.ResultStatus.StateNumber;
                                                }
                                            }
                                            if (eventIdsWithAwvHba1C.Contains(medicareResultEditModel.EventId))
                                            {
                                                var testResultRepository = new AwvHemaglobinTestRepository();
                                                var awvHba1CTestResult   = testResultRepository.GetTestResults(medicareResultEditModel.CustomerId, medicareResultEditModel.EventId, isNewResultFlow);
                                                if (awvHba1CTestResult != null && awvHba1CTestResult.ResultStatus != null && awvHba1CTestResult.TestNotPerformed == null && awvHba1CTestResult.UnableScreenReason == null)
                                                {
                                                    medicareResultEditModel.AwvHbA1CStateNumber = (int)awvHba1CTestResult.ResultStatus.StateNumber;
                                                }
                                            }
                                        }
                                        _logger.Info("Medicare Customer Sync Starting  for tag: " + tag.Alias + " Page : " + (i + 1));

                                        var phyids = currentList.Where(x => x.PhysicianOruId > 0).Select(x => x.PhysicianOruId).ToArray();
                                        var names  = _organizationRoleUserRepository.GetNameIdPairofUsers(phyids);
                                        if (names.Any())
                                        {
                                            foreach (var medicareResultEditModel in currentList.Where(x => x.PhysicianOruId > 0))
                                            {
                                                var n = names.FirstOrDefault(x => x.FirstValue == medicareResultEditModel.PhysicianOruId);
                                                if (n != null)
                                                {
                                                    medicareResultEditModel.PhysicianName = n.SecondValue;
                                                }
                                            }
                                        }

                                        var model = new MedicareResultListmodel {
                                            EhrResultEditmodels = currentList.ToList()
                                        };

                                        var result = _medicareApiService.PostAnonymous <bool>(_settings.MedicareApiUrl + MedicareApiUrl.UpdateCustomerResult, model);
                                        if (result)
                                        {
                                            _logger.Info("Medicare Customer Sync for tag: " + tag.Alias + " Page : " + (i + 1));
                                        }
                                    }
                                    catch (Exception exception)
                                    {
                                        _logger.Error(" error occur Medicare Customer Sync for tag: " + tag.Alias + " Customer : " + (i + 1) + " Message: " + exception.Message + "\n stack Trace: " + exception.StackTrace);
                                    }
                                }
                            }

                            var kynCustomerIds = _kyaLabValuesRepository.GetRecentKynLabValuesForMedicareSync(exportFromTime, tag.Alias).ToArray();
                            if (kynCustomerIds.Any())
                            {
                                const int pageSize   = 25;
                                var       totalPages = Math.Ceiling((double)(kynCustomerIds.Count()) / pageSize);
                                for (var i = 0; i < totalPages; i++)
                                {
                                    var currentList = kynCustomerIds.Skip(i * pageSize).Take(pageSize).ToArray();
                                    try
                                    {
                                        _logger.Info("Medicare Customer Sync Starting  for tag: " + tag.Alias + " Page : " + (i + 1));

                                        var model = new MedicareKynResultListmodel()
                                        {
                                            EhrResultEditmodels = currentList.ToList()
                                        };
                                        var result = _medicareApiService.PostAnonymous <bool>(_settings.MedicareApiUrl + MedicareApiUrl.UpdateCustomerKynResult, model);
                                        if (result)
                                        {
                                            _logger.Info("Medicare Customer Sync for tag: " + tag.Alias + " Page : " + (i + 1));
                                        }
                                    }
                                    catch (Exception exception)
                                    {
                                        _logger.Error(" error occur Medicare Customer Sync for tag: " + tag.Alias + " Customer : " + (i + 1) + " Message: " + exception.Message + "\n stack Trace: " + exception.StackTrace);
                                    }
                                }
                            }
                        }


                        _customSettingManager.SerializeandSave(path, new CustomSettings {
                            LastTransactionDate = newLastTransactionDate
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.Error("Error occurred for SyncCustomerResultPollingAgent Message: " + exception.Message + "\n stack Trace: " + exception.StackTrace);
            }
        }
示例#2
0
        private LabsInboundViewModel SetLabResults(LabsInboundViewModel model, long customerId, long eventId, long testId, bool isNewResultFlow, bool getLeftReadings = false)
        {
            var isTestPurchased = false;
            ITestResultRepository testResultRepository;

            if (testId == (long)LabInboundTestType.Echo)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.AwvEcho);
                if (isTestPurchased)
                {
                    testResultRepository = new AwvEchocardiogramTestRepository();
                    var awvEchoTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (awvEchoTestResult != null && (awvEchoTestResult.TestNotPerformed == null || awvEchoTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = awvEchoTestResult as AwvEchocardiogramTestResult;
                        if (result != null && result.Finding != null)
                        {
                            model.LabResult = result.Finding.Label;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.A1C)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.AwvHBA1C);
                if (isTestPurchased)
                {
                    testResultRepository = new AwvHemaglobinTestRepository();
                    var awvHemaglobinTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (awvHemaglobinTestResult != null && (awvHemaglobinTestResult.TestNotPerformed == null || awvHemaglobinTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = awvHemaglobinTestResult as AwvHemaglobinTestResult;
                        if (result != null && result.Hba1c != null)
                        {
                            model.LabResult = result.Hba1c.Reading;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.Ekg)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.AwvEkg);
                if (isTestPurchased)
                {
                    testResultRepository = new AwvEkgTestRepository();
                    var awvEkgTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (awvEkgTestResult != null && (awvEkgTestResult.TestNotPerformed == null || awvEkgTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = awvEkgTestResult as AwvEkgTestResult;
                        if (result != null && result.Finding != null)
                        {
                            model.LabResult = result.Finding.Label;
                        }
                    }
                }
            }

            //FL Blue asked us to Leave the Test with blank result field

            /*else if (testId == (long)LabInboundTestType.DiabeticRetinopathy)
             * {
             *  isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.DiabeticRetinopathy);
             *  if (isTestPurchased)
             *  {
             *      testResultRepository = new DiabeticRetinopathyTestRepository();
             *      var diabeticRetinopathyTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
             *      if (diabeticRetinopathyTestResult != null && (diabeticRetinopathyTestResult.TestNotPerformed == null || diabeticRetinopathyTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
             *      {
             *          var result = diabeticRetinopathyTestResult as DiabeticRetinopathyTestResult;
             *          if (result != null && result.Finding != null)
             *              model.LabResult = result.Finding.Label;
             *      }
             *  }
             * }*/

            else if (testId == (long)LabInboundTestType.UrineMicroalbumin)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.UrineMicroalbumin);
                if (isTestPurchased)
                {
                    testResultRepository = new UrineMicroalbuminTestRepository();
                    var urineMicroalbuminTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (urineMicroalbuminTestResult != null && (urineMicroalbuminTestResult.TestNotPerformed == null || urineMicroalbuminTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = urineMicroalbuminTestResult as UrineMicroalbuminTestResult;
                        if (result != null && result.MicroalbuminValue != null)
                        {
                            model.LabResult = result.MicroalbuminValue.Reading;
                        }
                        else
                        {
                            model.LabResult = KitDistributedString;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.AwvAbi)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.AwvABI);
                if (isTestPurchased)
                {
                    testResultRepository = new AwvAbiTestRepository();
                    var awvAbiTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (awvAbiTestResult != null && (awvAbiTestResult.TestNotPerformed == null || awvAbiTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = awvAbiTestResult as AwvAbiTestResult;
                        if (result != null && result.Finding != null)
                        {
                            model.LabResult = result.Finding.Label;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.ScreeningMammographyDigital)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.Mammogram);
                if (isTestPurchased)
                {
                    testResultRepository = new MammogramTestRepository();
                    var mammogramTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (mammogramTestResult != null && (mammogramTestResult.TestNotPerformed == null || mammogramTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = mammogramTestResult as MammogramTestResult;
                        if (result != null && result.Finding != null && !string.IsNullOrWhiteSpace(result.Finding.Label))
                        {
                            model.LabResult = "BIRAD-" + result.Finding.Label.Substring(0, 1);
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.AwvSpirometry)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.AwvSpiro);
                if (isTestPurchased)
                {
                    testResultRepository = new AwvSpiroTestRepository();
                    var awvSpiroTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (awvSpiroTestResult != null && (awvSpiroTestResult.TestNotPerformed == null || awvSpiroTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = awvSpiroTestResult as AwvSpiroTestResult;
                        if (result != null && result.Finding != null)
                        {
                            model.LabResult = result.Finding.Label;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.BloodPressure)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.Hypertension);
                if (isTestPurchased)
                {
                    testResultRepository = new HypertensionTestRepository();
                    var hyperTenstionTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (hyperTenstionTestResult != null && (hyperTenstionTestResult.TestNotPerformed == null || hyperTenstionTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var basicBiometric = _basicBiometricRepository.Get(eventId, customerId);
                        if (basicBiometric != null)
                        {
                            model.LabResult = (basicBiometric.SystolicPressure.HasValue ? basicBiometric.SystolicPressure.Value.ToString() : "0") + "/" + (basicBiometric.DiastolicPressure.HasValue ? basicBiometric.DiastolicPressure.Value.ToString() : "0");
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.AwvAaa)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.AwvAAA);
                if (isTestPurchased)
                {
                    testResultRepository = new AwvAaaTestRepository();
                    var awvAaaTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (awvAaaTestResult != null && (awvAaaTestResult.TestNotPerformed == null || awvAaaTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = awvAaaTestResult as AwvAaaTestResult;
                        if (result != null && result.Finding != null)
                        {
                            model.LabResult = result.Finding.Label;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.Ifobt)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.IFOBT);
                if (isTestPurchased)
                {
                    testResultRepository = new IFOBTTestRepository();
                    var ifobtTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (ifobtTestResult != null && (ifobtTestResult.TestNotPerformed == null || ifobtTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = ifobtTestResult as IFOBTTestResult;
                        if (result != null && result.Finding != null && !string.IsNullOrWhiteSpace(result.Finding.Label) && (result.Finding.Label.ToLower() == PositiveString.ToLower() ||
                                                                                                                             result.Finding.Label.ToLower() == NegativeString.ToLower()))
                        {
                            model.LabResult = result.Finding.Label;
                        }
                        else
                        {
                            model.LabResult = KitDistributedString;
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.Lead)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.Lead);
                if (isTestPurchased)
                {
                    testResultRepository = new LeadTestRepository();
                    var leadTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (leadTestResult != null && (leadTestResult.TestNotPerformed == null || leadTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = leadTestResult as LeadTestResult;
                        if (result != null)
                        {
                            var readings      = testResultRepository.GetAllReadings((int)TestType.Lead);
                            var readingLabels = new List <string>();
                            if (getLeftReadings)
                            {
                                model.LabType = EnumExtension.GetDescription(LabInboundTestType.Lead) + " L";
                                if (result.LeftResultReadings != null)
                                {
                                    var leftResultReadings = result.LeftResultReadings;

                                    if (leftResultReadings.NoVisualPlaque != null && leftResultReadings.NoVisualPlaque.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.LeftNoVisualPlaque);
                                        readingLabels.Add(reading.LableText);
                                    }

                                    if (leftResultReadings.VisuallyDemonstratedPlaque != null && leftResultReadings.VisuallyDemonstratedPlaque.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.LeftVisuallyDemonstratedPlaque);
                                        readingLabels.Add(reading.LableText);
                                    }

                                    if (leftResultReadings.ModerateStenosis != null && leftResultReadings.ModerateStenosis.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.LeftModerateStenosis);
                                        readingLabels.Add(reading.LableText);
                                    }

                                    if (leftResultReadings.PossibleOcclusion != null && leftResultReadings.PossibleOcclusion.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.LeftPossibleOcclusion);
                                        readingLabels.Add(reading.LableText);
                                    }
                                }
                            }
                            else
                            {
                                model.LabType = EnumExtension.GetDescription(LabInboundTestType.Lead) + " R";
                                if (result.RightResultReadings != null)
                                {
                                    var rightResultReading = result.RightResultReadings;

                                    if (rightResultReading.NoVisualPlaque != null && rightResultReading.NoVisualPlaque.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.RightNoVisualPlaque);
                                        readingLabels.Add(reading.LableText);
                                    }

                                    if (rightResultReading.VisuallyDemonstratedPlaque != null && rightResultReading.VisuallyDemonstratedPlaque.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.RightVisuallyDemonstratedPlaque);
                                        readingLabels.Add(reading.LableText);
                                    }

                                    if (rightResultReading.ModerateStenosis != null && rightResultReading.ModerateStenosis.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.RightModerateStenosis);
                                        readingLabels.Add(reading.LableText);
                                    }

                                    if (rightResultReading.PossibleOcclusion != null && rightResultReading.PossibleOcclusion.Reading != null)
                                    {
                                        var reading = readings.First(x => x.Label == ReadingLabels.RightPossibleOcclusion);
                                        readingLabels.Add(reading.LableText);
                                    }
                                }
                            }

                            model.LabResult = string.Join(", ", readingLabels);
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.Monofilament)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.Monofilament);
                if (isTestPurchased)
                {
                    testResultRepository = new MonofilamentTestRepository();
                    var monofilamentTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (monofilamentTestResult != null && (monofilamentTestResult.TestNotPerformed == null || monofilamentTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = monofilamentTestResult as MonofilamentTestResult;
                        if (result != null)
                        {
                            var readings = testResultRepository.GetAllReadings((int)TestType.Monofilament);
                            if (getLeftReadings)
                            {
                                model.LabType = EnumExtension.GetDescription(LabInboundTestType.Monofilament) + " L";
                                if (result.LeftPositive != null)
                                {
                                    var reading = readings.First(x => x.Label == ReadingLabels.MonofilamentLeftFootSensationIntact);
                                    model.LabResult = reading.LableText;
                                }
                                else if (result.LeftNegative != null)
                                {
                                    var reading = readings.First(x => x.Label == ReadingLabels.MonofilamentLeftFootSensationNotIntact);
                                    model.LabResult = reading.LableText;
                                }
                            }
                            else
                            {
                                model.LabType = EnumExtension.GetDescription(LabInboundTestType.Monofilament) + " R";
                                if (result.RightPositive != null)
                                {
                                    var reading = readings.First(x => x.Label == ReadingLabels.MonofilamentRightFootSensationIntact);
                                    model.LabResult = reading.LableText;
                                }
                                else if (result.RightNegative != null)
                                {
                                    var reading = readings.First(x => x.Label == ReadingLabels.MonofilamentRightFootSensationNotIntact);
                                    model.LabResult = reading.LableText;
                                }
                            }
                        }
                    }
                }
            }

            else if (testId == (long)LabInboundTestType.QuantaFloAbi)
            {
                isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)TestType.QuantaFloABI);
                if (isTestPurchased)
                {
                    testResultRepository = new QuantaFloABITestRepository();
                    var quantaFloAbiTestResult = testResultRepository.GetTestResults(customerId, eventId, isNewResultFlow);
                    if (quantaFloAbiTestResult != null && (quantaFloAbiTestResult.TestNotPerformed == null || quantaFloAbiTestResult.TestNotPerformed.TestNotPerformedReasonId <= 0))
                    {
                        var result = quantaFloAbiTestResult as QuantaFloABITestResult;
                        if (result != null && result.Finding != null)
                        {
                            model.LabResult = result.Finding.Label;
                        }
                    }
                }
            }

            return(model);
        }