public Admission OpportuniticlyLoadAdmission(TreatmentEpisode episode, string recordDate) { DateTime date = DateTime.Parse(recordDate); using (var db = new fasams_db()) { List <Admission> existing = db.Admissions .Include(x => x.PerformanceOutcomeMeasures.Select(p => p.SubstanceUseDisorders)) .Include(x => x.Evaluations) .Include(x => x.Diagnoses) .Include(x => x.Discharge) .Where(a => a.TreatmentSourceId == episode.SourceRecordIdentifier) .ToList(); if (existing == null || existing.Count == 0) { return(new Admission { SourceRecordIdentifier = Guid.NewGuid().ToString(), Evaluations = new List <Evaluation>(), Diagnoses = new List <Diagnosis>() }); } if (existing.Any(a => a.InternalAdmissionDate <= date && a.Discharge == null)) { return(existing.Where(a => a.InternalAdmissionDate <= date && a.Discharge == null).FirstOrDefault()); } if (existing.Any(a => a.InternalAdmissionDate <= date && a.Discharge.InternalDischargeDate >= date)) { return(existing.Where(a => a.InternalAdmissionDate <= date && a.Discharge.InternalDischargeDate >= date).FirstOrDefault()); } return(new Admission { SourceRecordIdentifier = Guid.NewGuid().ToString(), Evaluations = new List <Evaluation>(), Diagnoses = new List <Diagnosis>() }); } }
public void ProcessAdmission(TreatmentEpisode episode, Admission admission) { switch (admission.TypeCode) { case "1": { if (episode.Admissions.Any(a => a.TypeCode == "1")) { if (episode.Admissions.Any(a => a.SourceRecordIdentifier == admission.SourceRecordIdentifier)) { //same record so just replace it. var existingItem = episode.Admissions.Where(a => a.SourceRecordIdentifier == admission.SourceRecordIdentifier).FirstOrDefault(); int id = episode.Admissions.IndexOf(existingItem); episode.Admissions[id] = admission; return; } else { return; } } episode.Admissions.Add(admission); return; } case "2": { return; } default: break; } }
public void ProcessImmediateDischarge(TreatmentEpisode episode, ImmediateDischarge discharge) { if (episode.ImmediateDischarges.Any(i => i.SourceRecordIdentifier == discharge.SourceRecordIdentifier)) { var existingItem = episode.ImmediateDischarges.Where(a => a.SourceRecordIdentifier == discharge.SourceRecordIdentifier).FirstOrDefault(); int id = episode.ImmediateDischarges.IndexOf(existingItem); episode.ImmediateDischarges[id] = discharge; return; } episode.ImmediateDischarges.Add(discharge); return; }
public Admission OpportuniticlyLoadAdmission(TreatmentEpisode episode, UpdateType type, string recordDate) { DateTime date = DateTime.Parse(recordDate); switch (type) { case UpdateType.Admission: { return(OpportuniticlyLoadAdmission(episode, recordDate)); } case UpdateType.Update: { if (episode.Admissions.Exists(a => a.TreatmentSourceId == episode.SourceRecordIdentifier && a.PerformanceOutcomeMeasures != null && a.InternalAdmissionDate <= date && (a.Discharge == null || a.Discharge.InternalDischargeDate >= date))) { return(episode.Admissions.Where(a => a.TreatmentSourceId == episode.SourceRecordIdentifier && a.PerformanceOutcomeMeasures != null && a.InternalAdmissionDate <= date && (a.Discharge == null || a.Discharge.InternalDischargeDate >= date)).SingleOrDefault()); } else { return(OpportuniticlyLoadAdmission(episode, recordDate)); } } case UpdateType.Discharge: { if (episode.Admissions.Exists(a => a.TreatmentSourceId == episode.SourceRecordIdentifier && a.PerformanceOutcomeMeasures != null && a.InternalAdmissionDate <= date && (a.Discharge == null || a.Discharge.InternalDischargeDate >= date))) { return(episode.Admissions.Where(a => a.TreatmentSourceId == episode.SourceRecordIdentifier && a.PerformanceOutcomeMeasures != null && a.InternalAdmissionDate <= date && (a.Discharge == null || a.Discharge.InternalDischargeDate >= date)).SingleOrDefault()); } else { return(OpportuniticlyLoadAdmission(episode, recordDate)); } } default: return(null); } }
public ImmediateDischarge OpportuniticlyLoadImmediateDischarge(TreatmentEpisode episode, UpdateType type, string recordDate) { DateTime date = DateTime.Parse(recordDate); switch (type) { case UpdateType.ImDischarge: { if (episode.ImmediateDischarges.Exists(i => i.TreatmentSourceId == episode.SourceRecordIdentifier && i.InternalEvaluationDate == date)) { return(episode.ImmediateDischarges.Where(i => i.TreatmentSourceId == episode.SourceRecordIdentifier && i.InternalEvaluationDate == date).SingleOrDefault()); } else { return(OpportuniticlyLoadImmediateDischarge(episode, recordDate)); } } default: return(null); } }
public ImmediateDischarge OpportuniticlyLoadImmediateDischarge(TreatmentEpisode episode, string recordDate) { DateTime date = DateTime.Parse(recordDate); using (var db = new fasams_db()) { List <ImmediateDischarge> existing = db.ImmediateDischarges .Where(i => i.TreatmentSourceId == episode.SourceRecordIdentifier) .ToList(); if (existing == null || existing.Count == 0) { return(new ImmediateDischarge { SourceRecordIdentifier = Guid.NewGuid().ToString(), TreatmentSourceId = episode.SourceRecordIdentifier }); } if (existing.Any(i => i.InternalEvaluationDate == date)) { return(existing.Where(i => i.InternalEvaluationDate == date).FirstOrDefault()); } return(new ImmediateDischarge { SourceRecordIdentifier = Guid.NewGuid().ToString(), TreatmentSourceId = episode.SourceRecordIdentifier }); } }
public void UpsertTreatmentSession(TreatmentEpisode treatmentEpisode) { using (var db = new fasams_db()) { TreatmentEpisode existing = db.TreatmentEpisodes .Include(x => x.Admissions.Select(a => a.Discharge)) .Include(x => x.ImmediateDischarges) .SingleOrDefault(e => e.SourceRecordIdentifier == treatmentEpisode.SourceRecordIdentifier && e.FederalTaxIdentifier == treatmentEpisode.FederalTaxIdentifier); if (existing == null) { db.TreatmentEpisodes.Add(treatmentEpisode); if (treatmentEpisode.Admissions != null) { foreach (var row in treatmentEpisode.Admissions) { db.Admissions.Add(row); if (row.PerformanceOutcomeMeasures != null) { foreach (var perf in row.PerformanceOutcomeMeasures) { db.PerformanceOutcomeMeasures.Add(perf); if (perf.SubstanceUseDisorders != null) { foreach (var sad in perf.SubstanceUseDisorders) { db.SubstanceUseDisorders.Add(sad); } } } } if (row.Evaluations != null) { foreach (var item in row.Evaluations) { db.Evaluations.Add(item); } } if (row.Diagnoses != null) { foreach (var item in row.Diagnoses) { db.Diagnoses.Add(item); } } if (row.Discharge != null && row.Discharge.SourceRecordIdentifier != null) { db.Discharges.Add(row.Discharge); } } } } else { db.Entry(existing).CurrentValues.SetValues(treatmentEpisode); if (treatmentEpisode.Admissions != null) { //db.Entry(existing.Admissions).CurrentValues.SetValues(treatmentEpisode.Admissions); foreach (var row in treatmentEpisode.Admissions) { var exAdmit = db.Admissions.Find(row.SourceRecordIdentifier); db.Entry(exAdmit).CurrentValues.SetValues(row); if (row.PerformanceOutcomeMeasures != null) { foreach (var perf in row.PerformanceOutcomeMeasures) { var exPerf = db.PerformanceOutcomeMeasures.Find(perf.SourceRecordIdentifier); if (exPerf != null) { db.Entry(exPerf).CurrentValues.SetValues(perf); } else { db.PerformanceOutcomeMeasures.Add(perf); } } } if (row.Evaluations != null) { foreach (var item in row.Evaluations) { var exItem = db.Evaluations.Find(item.SourceRecordIdentifier); if (exItem != null) { db.Entry(exItem).CurrentValues.SetValues(item); } else { db.Evaluations.Add(item); } } } if (row.Diagnoses != null) { foreach (var item in row.Diagnoses) { var exItem = db.Diagnoses.Find(item.SourceRecordIdentifier); if (exItem != null) { db.Entry(exItem).CurrentValues.SetValues(item); } else { db.Diagnoses.Add(item); } } } if (row.Discharge != null) { var exItem = db.Discharges.Find(row.Discharge.SourceRecordIdentifier); if (exItem != null) { db.Entry(exItem).CurrentValues.SetValues(row.Discharge); } else { db.Discharges.Add(row.Discharge); } if (row.Discharge.Diagnoses != null) { foreach (var dx in row.Discharge.Diagnoses) { var exDx = db.Diagnoses.Find(dx.SourceRecordIdentifier); if (exDx != null) { db.Entry(exDx).CurrentValues.SetValues(dx); } else { db.Diagnoses.Add(dx); } } } if (row.Discharge.Evaluations != null) { foreach (var eval in row.Discharge.Evaluations) { var exEval = db.Evaluations.Find(eval.SourceRecordIdentifier); if (exEval != null) { db.Entry(exEval).CurrentValues.SetValues(eval); } else { db.Evaluations.Add(eval); } } } } } } } db.SaveChanges(); } }