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)
            {
            }
        }