public ListModelBase <InterviewInboundViewModel, InterviewInboundFilter> GetInterviewInboundReportList(int pageNumber, int pageSize, ModelFilterBase filter, out int totalRecords)
        {
            filter = filter ?? new InterviewInboundFilter();
            var interviewInboundFilter = filter as InterviewInboundFilter ?? new InterviewInboundFilter();

            var responseVendorReportFilter = new ResponseVendorReportFilter
            {
                StartDate  = interviewInboundFilter.StartDate,
                EndDate    = interviewInboundFilter.EndDate,
                AccountId  = interviewInboundFilter.AccountId,
                CustomTags = interviewInboundFilter.CustomTags
            };

            var customerIds = _customerRepository.GetCustomersByCustomTag(responseVendorReportFilter, pageNumber, pageSize, out totalRecords);

            //var customerIds = _callRepository.GetForInterviewReport(filter as InterviewInboundFilter, pageNumber, pageSize, out totalRecords);
            if (customerIds.IsNullOrEmpty())
            {
                return(null);
            }

            var calls = _callRepository.GetCallsForInterviewReport(filter as InterviewInboundFilter, customerIds);

            var customers = customerIds.Any() ? _customerRepository.GetCustomers(customerIds.ToArray()) : new List <Customer>();

            //var eventCustomers = _eventCustomerRepository.GetEventCustomersByEventIdsCustomerIds(eventIds, customerIds) ?? new List<EventCustomer>();
            var eventCustomers   = _eventCustomerRepository.GetByEventIdsOrCustomerIds(interviewInboundFilter.StartDate, customerIds) ?? new List <EventCustomer>();
            var eventCustomerIds = eventCustomers.Select(x => x.Id);

            var eventIds = eventCustomers.Select(x => x.EventId).ToArray();
            var events   = _eventRepository.GetEvents(eventIds);

            var chaseOutbounds = _chaseOutboundRepository.GetByCustomerIds(customerIds);

            var customerChaseCampaigns = _chaseCampaignRepository.GetCustomerChaseCampaignsByCustomerIds(customerIds.ToArray());
            var chaseCampaignIds       = customerChaseCampaigns.Select(x => x.ChaseCampaignId).Distinct();
            var chaseCampaigns         = _chaseCampaignRepository.GetByIds(chaseCampaignIds);

            var appointmentIds = eventCustomers.Where(x => x.AppointmentId.HasValue).Select(x => x.AppointmentId.Value);
            var appointments   = _appointmentRepository.GetByIds(appointmentIds);

            var eventAppointmentCancellatonLogs = _eventAppointmentCancellationLogRepository.GetByEventCustomerIds(eventCustomerIds);

            var noteIds           = eventAppointmentCancellatonLogs.Where(x => x.NoteId.HasValue).Select(x => x.NoteId.Value);
            var customerCallNotes = _customerCallNotesRepository.GetByIds(noteIds);

            var eventIdStaffIdPairs = _eventRepository.GetEventStaffPairs(eventIds);

            var orgRoleUserIds        = eventIdStaffIdPairs.Select(x => x.SecondValue).Distinct().ToArray();
            var organizationRoleUsers = _organizationRoleUserRepository.GetOrganizationRoleUsers(orgRoleUserIds);

            var userIds = organizationRoleUsers.Select(x => x.UserId).Distinct().ToList();
            var users   = _userRepository.GetUsers(userIds);

            return(_interviewInboundReportFactory.Create(eventCustomers, customers, chaseOutbounds, customerChaseCampaigns, chaseCampaigns, calls, appointments, events, eventAppointmentCancellatonLogs, customerCallNotes, eventIdStaffIdPairs,
                                                         organizationRoleUsers, users));
        }
        private void SaveCallQueueCustomerAndAssignment(IEnumerable <CallQueueCustomer> callQueueCustomers, IEnumerable <CallQueueCustomer> existingCustomers, long criteriaId, long healthPlanId, long callQueueId,
                                                        IEnumerable <long> customerIds)//, IEnumerable<long> eventids
        {
            int       totalRecords    = customerIds.Count();
            var       pageNumber      = 1;
            const int PageSize        = 100;
            var       firstDateOfYear = new DateTime(DateTime.Today.Year, 1, 1);

            var accountSetting = _callQueueSettingRepository.GetByAccountIdAndCallQueueId(healthPlanId, callQueueId);
            var maxDayCount    = (from asetting in accountSetting
                                  where asetting.SuppressionTypeId == (long)CallQueueSuppressionType.Others
                                  select asetting.NoOfDays).FirstOrDefault();

            try
            {
                while (true)
                {
                    var pagedCusotmerIds       = customerIds.Skip((pageNumber - 1) * PageSize).Take(PageSize).ToArray();
                    var pagedcallQueueCusomers = callQueueCustomers.Where(x => pagedCusotmerIds.Contains(x.CustomerId.Value));

                    var callDetails = _centerCallRepository.GetCallByCustomerId(pagedCusotmerIds, firstDateOfYear, true);

                    var customers         = _customerRepository.GetCustomers(pagedCusotmerIds);
                    var prospectCustomers = _prospectCustomerRepository.GetProspectCustomersByCustomerIds(pagedCusotmerIds);

                    var eventCustomers = _eventCustomerRepository.GetByEventIdsOrCustomerIds(firstDateOfYear, pagedCusotmerIds);

                    var customerEligibilities = _customerEligibilityRepository.GetCustomerEligibilityByCustomerIdsAndYear(pagedCusotmerIds, DateTime.Today.Year);

                    var targetedCustomers = _customerTargetedRepository.GetCustomerTargetedByCustomerIdsAndYear(pagedCusotmerIds, DateTime.Today.Year);

                    eventCustomers = eventCustomers ?? new List <EventCustomer>();

                    var cancelledCustomersLog = (_eventAppointmentCancellationLogRepository.GetCancellationByCusomerIds(pagedCusotmerIds, DateTime.Today.AddDays(-maxDayCount)));


                    var appointmentIds = (from ec in eventCustomers
                                          where ec.AppointmentId.HasValue
                                          select ec.AppointmentId.Value).ToArray();

                    var appointments = _appointmentRepository.GetByIds(appointmentIds);

                    foreach (var callQueueCustomer in pagedcallQueueCusomers)
                    {
                        try
                        {
                            var customerId = callQueueCustomer.CustomerId;
                            var existingCallQueueCustomer = existingCustomers.FirstOrDefault(cq => (cq.CustomerId == customerId));
                            var customer = customers.First(x => x.CustomerId == callQueueCustomer.CustomerId);

                            var prospectCustomer = prospectCustomers.FirstOrDefault(p => p.CustomerId.Value == customerId);

                            var customerCalls = (from cd in callDetails
                                                 where cd.CalledCustomerId == customerId && cd.Status != (long)CallStatus.CallSkipped
                                                 select cd).ToArray();

                            var callAttempts = (from cd in callDetails
                                                where cd.CalledCustomerId == customerId && cd.CallQueueId.Value == callQueueId && cd.Status != (long)CallStatus.CallSkipped &&
                                                (cd.IsContacted.HasValue && cd.IsContacted.Value)
                                                select cd).Count();

                            var customerswithAppointment = (from ec in eventCustomers
                                                            where ec.AppointmentId.HasValue
                                                            select ec).ToArray();

                            var appointmentDate = (from ec in customerswithAppointment
                                                   join a in appointments on ec.AppointmentId.Value equals a.Id
                                                   where ec.AppointmentId.HasValue && ec.CustomerId == customerId && /*&& ec.AppointmentId.HasValue*/
                                                   ec.NoShow == false
                                                   orderby a.StartTime descending
                                                   select a).FirstOrDefault();


                            var eacl = (from ccl in cancelledCustomersLog
                                        join ec in eventCustomers on ccl.EventCustomerId equals ec.Id
                                        where ec.AppointmentId == null && ec.CustomerId == customerId
                                        orderby ccl.DateCreated descending
                                        select ccl).FirstOrDefault();

                            var customerEligibility = customerEligibilities.FirstOrDefault(x => x.CustomerId == customerId);

                            var targetedCustomer = targetedCustomers != null?targetedCustomers.FirstOrDefault(x => x.CustomerId == customerId) : null;

                            var noShowMarkedEventCustomer = eventCustomers.Where(x => x.CustomerId == customerId);

                            existingCallQueueCustomer = CreateCustomerCallQueueCustomerModel(existingCallQueueCustomer, callQueueCustomer, customer, customerCalls,
                                                                                             callAttempts, appointmentDate, prospectCustomer, eacl, false, customerEligibility, noShowMarkedEventCustomer, targetedCustomer);

                            /*if (!eventids.IsNullOrEmpty())
                             * {
                             *  existingCallQueueCustomer.EventIds = string.Join(",", eventids.ToArray()) + ",";
                             * }*/
                            existingCallQueueCustomer.LastUpdatedOn = DateTime.Today;

                            var isNew = !(existingCallQueueCustomer.Id > 0);

                            existingCallQueueCustomer = _callQueueCustomerRepository.Save(existingCallQueueCustomer, isNew);

                            _healthPlanCallQueueAssignmentRepository.Save(new HealthPlanCallQueueCriteriaAssignment
                            {
                                CallQueueCustomerId = existingCallQueueCustomer.Id,
                                CallQueueId         = callQueueCustomer.CallQueueId,
                                CriteriaId          = criteriaId
                            });
                        }
                        catch (Exception ex)
                        {
                            logger.Error(string.Format("Error saving Call Queue Customer. \nMessage : {0} \nStack Trace : {1}", ex.Message, ex.StackTrace));
                        }
                    }

                    if ((pageNumber * PageSize) >= totalRecords)
                    {
                        break;
                    }

                    pageNumber++;
                }
            }
            catch (Exception)
            {
            }
        }
        public ListModelBase <ResponseVendorReportViewModel, ResponseVendorReportFilter> GetResponseVendorReportList(int pageNumber, int pageSize, ModelFilterBase filter, out int totalRecords)
        {
            filter = filter ?? new ResponseVendorReportFilter();
            var responseVendorReportFilter = filter as ResponseVendorReportFilter ?? new ResponseVendorReportFilter();

            var customerIds = _customerRepository.GetCustomersByCustomTag(filter as ResponseVendorReportFilter, pageNumber, pageSize, out totalRecords);

            //var customerIds = _callRepository.GetForResponseVendorReport(filter as ResponseVendorReportFilter, pageNumber, pageSize, out totalRecords);
            if (customerIds.IsNullOrEmpty())
            {
                return(null);
            }

            var customerEligibilities = _customerEligibilityRepository.GetCustomerEligibilityByCustomerIdsAndYear(customerIds, DateTime.Today.Year);

            if (customerEligibilities.IsNullOrEmpty())
            {
                customerEligibilities = new List <CustomerEligibility>();
            }

            var corporateAccount = _corporateAccountRepository.GetById(((ResponseVendorReportFilter)filter).AccountId);

            var calls = _callRepository.GetCallsForResponseVendorReport(filter as ResponseVendorReportFilter, customerIds);

            var customers = customerIds.Any() ? _customerRepository.GetCustomers(customerIds.ToArray()) : new List <Customer>();

            var eventCustomers = _eventCustomerRepository.GetByEventIdsOrCustomerIds(responseVendorReportFilter.StartDate, customerIds) ?? new List <EventCustomer>();

            var eventIds = eventCustomers.Select(x => x.EventId).ToArray();
            var events   = _eventRepository.GetByIds(eventIds);

            var languages = _languageRepository.GetAll();

            var appointmentIds = eventCustomers.Where(x => x.AppointmentId.HasValue).Select(x => x.AppointmentId.Value);
            var appointments   = _appointmentRepository.GetByIds(appointmentIds);

            var eventCustomerIds = eventCustomers.Select(x => x.Id);
            var pcpAppointments  = _pcpAppointmentRepository.GetByEventCustomerIds(eventCustomerIds);
            var pcpDispositions  = _pcpDispositionRepository.GetByEventCustomerIds(eventCustomerIds);

            var eventCustomerBarriers = _barrierRepository.GetCustomerBarrierByEventCustomerIds(eventCustomerIds);

            var barrierIds = eventCustomerBarriers.Select(x => x.BarrierId).ToArray();
            var barriers   = _barrierRepository.GetByIds(barrierIds);

            var chaseOutbounds = _chaseOutboundRepository.GetByCustomerIds(customerIds);

            var customerChaseCampaigns = _chaseCampaignRepository.GetCustomerChaseCampaignsByCustomerIds(customerIds.ToArray());
            var chaseCampaignIds       = customerChaseCampaigns.Select(x => x.ChaseCampaignId).Distinct();
            var chaseCampaigns         = _chaseCampaignRepository.GetByIds(chaseCampaignIds);

            var campaignTypeIds = chaseCampaigns.Select(x => x.ChaseCampaignTypeId).ToArray();
            var campaignTypes   = _chaseCampaignTypeRepository.GetByIds(campaignTypeIds);

            var eventAppointmentCancellatonLogs = _eventAppointmentCancellationLogRepository.GetByEventCustomerIds(eventCustomerIds);

            var resultPostedToPlanFileName = Path.Combine(_settings.ResultPostedToPlanPath, string.Format("resultPostedto_{0}.xml", corporateAccount.Tag));
            var resultPosted = _resultPdfPostedSerializer.Deserialize(resultPostedToPlanFileName);

            var resultPostedCustomers = resultPosted != null ? resultPosted.Customer : new List <CustomerInfo>();

            return(_responseVendorReportFactory.Create(customers, languages, eventCustomers, appointments, events, calls, pcpAppointments, pcpDispositions, eventCustomerBarriers, barriers, chaseOutbounds, customerChaseCampaigns,
                                                       chaseCampaigns, campaignTypes, eventAppointmentCancellatonLogs, resultPostedCustomers, customerEligibilities));
        }