public CustomerEventDetailViewModel GetPatients(PatientSearchFilter filter, long technicianId) { var list = new List <ShortPatientInfoViewModel>(); var patientList = _customerRepository.GetPatientList(filter); if (patientList.IsNullOrEmpty()) { return(new CustomerEventDetailViewModel()); } var customerIds = patientList.Select(x => x.CustomerId); var eventCustomers = _eventCustomerRepository.GetByCustomerIdEventDate(customerIds, DateTime.Today, DateTime.Now.AddDays(-1), technicianId).Where(x => x.AppointmentId.HasValue).ToArray(); if (eventCustomers.IsNullOrEmpty()) { return(new CustomerEventDetailViewModel()); } customerIds = eventCustomers.Select(x => x.CustomerId).Distinct().ToList(); var appointmentIds = eventCustomers.Where(x => x.AppointmentId.HasValue).Select(x => x.AppointmentId.Value).ToArray(); var appointments = _appointmentRepository.GetByIds(appointmentIds); var eventIds = eventCustomers.Select(x => x.EventId).Distinct().ToArray(); var events = _eventRepository.GetEventsByIds(eventIds); var eventModels = _eventService.GetShortEventInfoList(events); var hosts = _hostRepository.GetEventHosts(eventIds); var eventCustomerIds = eventCustomers.Select(x => x.Id); var orders = _orderRepository.GetAllOrdersByEventCustomerIds(eventCustomerIds); var participationConsentSignatures = _participationConsentSignatureRepository.GetByEventCustomerIds(eventCustomerIds); var fluConsentSignatures = _fluConsentSignatureRepository.GetByEventCustomerIds(eventCustomerIds); var physicianRecordRequestSignatures = _physicianRecordRequestSignatureRepository.GetByEventCustomerIds(eventCustomerIds); var primaryCarePhysicians = _primaryCarePhysicianRepository.GetByCustomerIds(customerIds); foreach (var eventCustomer in eventCustomers) { var patient = patientList.First(x => x.CustomerId == eventCustomer.CustomerId); var appointment = appointments.First(x => x.Id == eventCustomer.AppointmentId); var theEvent = events.First(x => x.Id == eventCustomer.EventId); var host = hosts.First(x => x.Id == theEvent.HostId); var order = orders.Single(o => o.CustomerId == eventCustomer.CustomerId); var eventPackages = _eventPackageRepository.GetPackagesForEvent(eventCustomer.EventId); var eventTests = _eventTestRepository.GetTestsForEvent(eventCustomer.EventId); var eventpackageId = order.OrderDetails.Where(od => od.OrderItemStatus.OrderStatusState == OrderStatusState.FinalSuccess && od.OrderItem.OrderItemType == OrderItemType.EventPackageItem) .Select(od => od.OrderItem.ItemId).SingleOrDefault(); var eventPackage = eventPackages.SingleOrDefault(ep => eventpackageId == ep.Id); var eventTestIds = order.OrderDetails.Where(od => od.OrderItemStatus.OrderStatusState == OrderStatusState.FinalSuccess && od.OrderItem.OrderItemType == OrderItemType.EventTestItem).Select(od => od.OrderItem.ItemId).ToArray(); var eventTestsonOrder = eventTests.Where(et => eventTestIds.Contains(et.Id)).ToArray(); var chaperoneSignature = _chaperoneSignatureRepository.GetByEventCustomerId(eventCustomer.Id); var model = new ShortPatientInfoViewModel { CustomerId = patient.CustomerId, EventCustomerId = eventCustomer.Id, FirstName = patient.Name.FirstName, MiddleName = patient.Name.MiddleName, LastName = patient.Name.LastName, Email = patient.Email.ToString(), HomePhone = patient.HomePhoneNumber.FormatPhoneNumber, MobileNumber = patient.MobilePhoneNumber.FormatPhoneNumber, EventId = eventCustomer.EventId, AppointmentTime = appointment.StartTime, Packages = eventPackage != null ? eventPackage.Package.Name : "", Tests = !eventTestsonOrder.IsNullOrEmpty() ? string.Join(", ", eventTestsonOrder.Select(t => t.Test.Name)) : "", HipaaConsent = eventCustomer.HIPAAStatus, CheckInTime = appointment.CheckInTime, CheckOutTime = appointment.CheckOutTime, MatrixConsent = participationConsentSignatures != null && participationConsentSignatures.SingleOrDefault(x => x.EventCustomerId == eventCustomer.Id) != null, PhysicianRecordRequest = physicianRecordRequestSignatures != null && physicianRecordRequestSignatures.SingleOrDefault(x => x.EventCustomerId == eventCustomer.Id) != null, FluVaccine = fluConsentSignatures != null && fluConsentSignatures.SingleOrDefault(x => x.EventCustomerId == eventCustomer.Id) != null, NoShow = eventCustomer.AppointmentId.HasValue && eventCustomer.NoShow, LeftWithoutScreening = eventCustomer.AppointmentId.HasValue && eventCustomer.LeftWithoutScreeningReasonId.HasValue, ChaperoneConsent = chaperoneSignature != null?true: false }; var pcp = primaryCarePhysicians != null?primaryCarePhysicians.FirstOrDefault(x => x.CustomerId == eventCustomer.CustomerId) : null; if (pcp != null) { var pcpAddress = pcp.Address != null?Mapper.Map <Address, AddressViewModel>(pcp.Address) : null; model.PrimaryCarePhysician = new PcpInfoViewModel { Name = pcp.Name.FullName, Address = pcpAddress, PhoneNumber = pcp.Primary != null ? pcp.Primary.FormatPhoneNumber : pcp.Secondary != null ? pcp.Secondary.FormatPhoneNumber : "", Fax = pcp.Fax != null ? pcp.Fax.FormatPhoneNumber : "" }; } list.Add(model); } return(new CustomerEventDetailViewModel { Customers = list, Events = eventModels }); }