async Task<Ward> GetWard(string campusId, string wardId) { var id = string.Concat(campusId, "-", wardId); var ward = await httpClient.GetAsync<Ward>(string.Format(wardUri, id)).ConfigureAwait(false); if (ward == null) { wardIds.Add(id); //add to wardIds cache ward = new Ward() { CampusId = campusId, Id = wardId }; } if (ward.Beds == null) ward.Beds = new List<Bed>(); if (string.IsNullOrEmpty(ward.Description)) ward.Description = await GetWardDescription(campusId, wardId).ConfigureAwait(false); return ward; }
async Task StoreWard(Ward ward) { if (ward != null) { if (ward.Beds != null && !ward.Beds.Any()) ward.Beds = null; var id = string.Concat(ward.CampusId, "-", ward.Id); if (ward.Beds != null) { ward.Beds = (from bed in ward.Beds let orderbyKey = string.Concat(bed.RoomId, "-", bed.BedId, "-", bed.FromDate.GetValueOrDefault().ToString("yyyy-MM-dd HH:mm")) orderby orderbyKey select bed).ToList(); await httpClient.PutAsync(string.Format(wardUri, id), ward).ConfigureAwait(false); } else if (deleteEmptyWards) { wardIds.Remove(id); //remove from wardIds cache await httpClient.DeleteAsync(string.Format(wardUri, id)).ConfigureAwait(false); } } }
void RemoveVisitFromWard(Ward ward) { if (ward == null || ward.Beds == null || !ward.Beds.Any()) return; var bedsToRemove = (from b in ward.Beds where b.VisitId == visit.Id && b.PatientId == patient.Id select b).ToList(); foreach (var item in bedsToRemove) { if (log.IsInfoEnabled) log.InfoFormat("Ward {0}: removed bed {1}-{2}-{3}-{4} fromDate '{5}' for visit {6}", ward.Id, item.CampusId, item.WardId, item.RoomId, item.BedId, item.FromDate.Maybe(d => d.Value.ToString("yyyy-MM-dd HH:mm")), item.VisitId); ward.Beds.Remove(item); } }
void RemoveBedFromWard(Transfer transfer, Ward ward, bool checkFromDate = true, bool checkLocation = true, bool removePendingDischarge = true) { if (ward == null || ward.Beds == null || !ward.Beds.Any()) return; var bedsToRemove = (from b in ward.Beds where b.VisitId == visit.Id && b.PatientId == patient.Id && (checkFromDate ? b.FromDate == transfer.FromDate : true) && (checkLocation ? (b.CampusId == transfer.CampusId && b.WardId == transfer.WardId && b.RoomId == transfer.RoomId && b.BedId == transfer.BedId) : true) && (removePendingDischarge ? true : !b.PendingDischarge.GetValueOrDefault()) select b).ToList(); foreach (var item in bedsToRemove) { if (log.IsInfoEnabled) log.InfoFormat("Ward {0}: removed bed {1}-{2}-{3}-{4} fromDate '{5}' for visit {6}", ward.Id, item.CampusId, item.WardId, item.RoomId, item.BedId, item.FromDate.Maybe(d => d.Value.ToString("yyyy-MM-dd HH:mm")), item.VisitId); ward.Beds.Remove(item); } }
void AddBedToWard(Bed bed, Ward ward) { if (ward != null && ward.Beds.FindIndex(x => x.PatientId == bed.PatientId && x.VisitId == bed.VisitId && x.FromDate == bed.FromDate && x.CampusId == bed.CampusId && x.WardId == bed.WardId && x.RoomId == bed.RoomId && x.BedId == bed.BedId) == -1) { if (log.IsInfoEnabled) log.InfoFormat("Ward {0}: added bed {1}-{2}-{3}-{4} fromDate '{5}' for visit {6}", ward.Id, bed.CampusId, bed.WardId, bed.RoomId, bed.BedId, bed.FromDate.Maybe(d => d.Value.ToString("yyyy-MM-dd HH:mm")), bed.VisitId); ward.Beds.Add(bed); } }
async Task ProcessBedOccupation() { if (!string.IsNullOrEmpty(transfer.DepartmentId) && skippedDepartments.Contains(transfer.DepartmentId)) return; //DO NOT STORE BED OCCUPATION FOR SKIPPED DEPARTMENTS bed = new Bed(transfer, patient, visit) { ConsultingDoctorLastName = consultingDoctorLastName, ConsultingDoctorFirstName = consultingDoctorFirstName }; if (!string.IsNullOrEmpty(transfer.WardId)) assignedWard = await GetWard(transfer.CampusId, transfer.WardId).ConfigureAwait(false); if (!string.IsNullOrEmpty(transfer.PriorWardId)) { if (transfer.WardId == transfer.PriorWardId) priorWard = assignedWard; else priorWard = await GetWard(transfer.PriorCampusId, transfer.PriorWardId).ConfigureAwait(false); } switch (eventType) { case "A01": //Admit a patient case "A04": //Register a patient case "A14": //Pending admit await RemoveVisitFromAllWard(); AddBedToWard(bed, assignedWard); break; case "A02": //Transfer a patient if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value > bed.FromDate.GetValueOrDefault(DateTime.Now)) break; //transfer before dichargedate if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value < bed.FromDate.GetValueOrDefault(DateTime.Now).AddMonths(-1)) break; //discharged more than 1 month ago if (visit.Transfers != null) { var lastFromDate = (from t in visit.Transfers where t.FromDate < DateTime.Now && !t.Pending.GetValueOrDefault() orderby t.FromDate descending select t.FromDate).FirstOrDefault(); if (bed.FromDate < lastFromDate) { assignedWard = null; // transfer in the past break; } } if (bed.LocationStatus != "1" && bed.LocationStatus != "2" && visit.Transfers != null) //back to original bed!! { foreach (var t in visit.Transfers) { if (string.IsNullOrEmpty(t.WardId) || t.Pending.GetValueOrDefault() || t.FromDate > DateTime.Now) continue; var ward = await httpClient.GetAsync<Ward>(string.Format(wardUri, string.Concat(t.CampusId, "-", t.WardId))).ConfigureAwait(false); if (ward != null) { RemoveBedFromWard(t, ward, checkFromDate: false, removePendingDischarge: false); await StoreWard(ward).ConfigureAwait(false); } } priorWard = null; } if (!visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.HasValue && visit.DischargeDate <= DateTime.Now) assignedWard = null; AddBedToWard(bed, assignedWard); break; case "A15": //Pending transfer AddBedToWard(bed, assignedWard); break; case "A03": //Discharge a patient await RemoveVisitFromAllWard(); RemoveBedFromWard(transfer, assignedWard, checkFromDate: false); break; case "A05": //Preadmit a patient break; case "A08": //Update patient information break; case "A11": //Cancel admit case "A27": //Cancel pending admit RemoveBedFromWard(transfer, assignedWard, checkFromDate: false); break; case "A12": //Cancel transfer if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value > bed.FromDate.GetValueOrDefault(DateTime.Now)) break; //transfer before dichargedate if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value < bed.FromDate.GetValueOrDefault(DateTime.Now).AddMonths(-1)) break; //discharged more than 1 month ago RemoveBedFromWard(transfer, assignedWard); if (visit.Transfers != null) { var lastFromDate = (from t in visit.Transfers where t.FromDate < DateTime.Now orderby t.FromDate descending select t.FromDate).FirstOrDefault(); if (bed.FromDate < lastFromDate) { assignedWard = null; // transfer in the past break; } } if (!visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.HasValue && visit.DischargeDate <= DateTime.Now) { //discharged in the past assignedWard = null; break; } if (visit.Transfers != null && visit.Transfers.Count > 1) { //more than 1 transferline bed.CampusId = bed.PriorCampusId; bed.WardId = bed.PriorWardId; bed.RoomId = bed.PriorRoomId; bed.BedId = bed.PriorBedId; bed.LocationStatus = bed.PriorLocationStatus; bed.PriorCampusId = null; bed.PriorWardId = null; bed.PriorRoomId = null; bed.PriorBedId = null; bed.PriorLocationStatus = null; bed.FromDate = (from t in visit.Transfers where t.FromDate < bed.FromDate orderby t.FromDate descending select t.FromDate).FirstOrDefault() ?? bed.FromDate; AddBedToWard(bed, priorWard); } break; case "A26": //Cancel pending transfer RemoveBedFromWard(transfer, assignedWard); //check if next visit is pending discharge and has the same bed // THAN change bed to previous bed break; case "A13": //Cancel discharge if (visit.Transfers != null) { var lastFromDate = (from t in visit.Transfers where t.FromDate < bed.FromDate orderby t.FromDate descending select t.FromDate).FirstOrDefault(); if (lastFromDate.HasValue) bed.FromDate = lastFromDate; AddBedToWard(bed, assignedWard); } bed.DischargeDate = null; bed.PendingDischarge = null; break; case "A25": //Cancel pending discharge await RemoveDischargeDateFromAllBeds(visit.Id); RemoveBedFromWard(transfer, assignedWard, checkLocation: false); break; case "A16": //Pending discharge bed.PendingDischarge = true; AddBedToWard(bed, assignedWard); break; case "A24": //Link Patient Information break; case "A28": //Add person information break; case "A31": //Update person information break; case "A37": //Unlink Patient Information break; case "A38": //Cancel preadmit break; case "A40": //Merge patient break; default: break; } await StoreWard(assignedWard); if (priorWard != null && assignedWard != priorWard) await StoreWard(priorWard); }