public HealthPlanCallQueueCriteriaEditModel SaveHealthPlanCallQueueCriteria(HealthPlanCallQueueCriteriaEditModel model, long orgRoleId, bool isCriteriaSameAsPervious)
        {
            var callQueue = _callQueueRepository.GetCallQueueByCategory(model.CallQueue);

            model.CallQueueId = callQueue.Id;

            model.IsCriteriaSameAsPervious = isCriteriaSameAsPervious;

            if (model.CallQueue == HealthPlanCallQueueCategory.MailRound)
            {
                model.IsCriteriaSameAsPervious = true;
                var healthPlanCallQueueCriterias = new List <HealthPlanCallQueueCriteria>();

                HealthPlanCallQueueCriteria existingCriteria = model.Id > 0 ? _healthPlanCallQueueCriteriaRepository.GetById(model.Id) : null;

                foreach (var campaign in model.CampaignDirectMailDates)
                {
                    var criteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForMailRound(model, campaign.CampaignId, orgRoleId);

                    if (existingCriteria != null)
                    {
                        if (existingCriteria.CampaignId.HasValue && model.CampaignDirectMailDates.Any(x => x.CampaignId == existingCriteria.CampaignId.Value))
                        {
                            if (campaign.CampaignId == existingCriteria.CampaignId.Value)
                            {
                                criteria.Id = model.Id;
                                criteria.IsQueueGenerated = existingCriteria.IsQueueGenerated;
                            }
                        }
                        else
                        {
                            if (campaign.CampaignId == model.CampaignDirectMailDates.First().CampaignId)
                            {
                                criteria.Id = model.Id;
                            }
                        }
                    }
                    if (criteria.Id > 0)
                    {
                        var directmailActivities = _healthPlanCriteriaDirectMailRepository.GetByCriteriaId(criteria.Id);

                        if (directmailActivities.IsNullOrEmpty() && !campaign.DirectMailDateActivities.IsNullOrEmpty())
                        {
                            criteria.IsQueueGenerated      = false;
                            model.IsCriteriaSameAsPervious = false;
                        }
                        else if (!directmailActivities.IsNullOrEmpty() && campaign.DirectMailDateActivities.IsNullOrEmpty())
                        {
                            criteria.IsQueueGenerated      = false;
                            model.IsCriteriaSameAsPervious = false;
                        }
                        else if (!directmailActivities.IsNullOrEmpty() && !campaign.DirectMailDateActivities.IsNullOrEmpty())
                        {
                            if (directmailActivities.Count() != campaign.DirectMailDateActivities.Count())
                            {
                                criteria.IsQueueGenerated      = false;
                                model.IsCriteriaSameAsPervious = false;
                            }
                            else
                            {
                                var activtyIdsInDB     = directmailActivities.Select(x => x.CampaignActivityId);
                                var activityIdsInModel = campaign.DirectMailDateActivities.Select(x => x.ActivityId);

                                if (!(activtyIdsInDB.All(activityIdsInModel.Contains) && activityIdsInModel.All(activtyIdsInDB.Contains)))
                                {
                                    criteria.IsQueueGenerated      = false;
                                    model.IsCriteriaSameAsPervious = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        criteria.IsQueueGenerated      = false;
                        model.IsCriteriaSameAsPervious = false;
                    }


                    criteria = _healthPlanCallQueueCriteriaRepository.Save(criteria);

                    if (!campaign.DirectMailDateActivities.IsNullOrEmpty())
                    {
                        var activityIds = campaign.DirectMailDateActivities.Select(x => x.ActivityId);
                        _healthPlanCriteriaDirectMailRepository.Save(activityIds, criteria.Id);
                    }

                    healthPlanCallQueueCriterias.Add(criteria);
                }

                if (!model.IsTeamAssignment)
                {
                    foreach (var healthPlanCallQueueCriteria in healthPlanCallQueueCriterias)
                    {
                        SaveFileInfoForCsvUpload(model, healthPlanCallQueueCriteria);
                    }
                    if (!model.Assignments.IsNullOrEmpty())
                    {
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaAssignmentForMailRound(model.Assignments, healthPlanCallQueueCriterias, orgRoleId);
                        _healthPlanCriteriaAssignmentRepository.Save(model.HealthPlanId, model.CallQueueId, model.Id, assignments);
                    }
                }
                else
                {
                    if (!model.CallCenterTeamAssignments.IsNullOrEmpty())
                    {
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaTeamAssignmentForMailRound(model.CallCenterTeamAssignments, healthPlanCallQueueCriterias, orgRoleId);
                        _healthPlanCriteriaTeamAssignmentRepository.Save(assignments);
                    }
                }
            }
            else if (model.CallQueue == HealthPlanCallQueueCategory.LanguageBarrier)
            {
                var healthPlanCallQueueCriteria = _healthPlanCallQueueCriteriaRepository.GetQueueCriteriaForQueue(model.CallQueueId, model.HealthPlanId);

                if (!model.IsTeamAssignment)
                {
                    if (!model.Assignments.IsNullOrEmpty())
                    {
                        SaveFileInfoForCsvUpload(model, healthPlanCallQueueCriteria);
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaAssignment(model.Assignments, healthPlanCallQueueCriteria.Id, orgRoleId);
                        _healthPlanCriteriaAssignmentRepository.Save(model.HealthPlanId, model.CallQueueId, model.Id, assignments);
                    }
                }
                else
                {
                    if (!model.CallCenterTeamAssignments.IsNullOrEmpty())
                    {
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaTeamAssignment(model.CallCenterTeamAssignments, healthPlanCallQueueCriteria, orgRoleId);
                        _healthPlanCriteriaTeamAssignmentRepository.Save(assignments);
                    }
                }
            }
            else if (model.CallQueue == HealthPlanCallQueueCategory.AppointmentConfirmation)
            {
                var healthPlanCallQueueCriteria = model.Id > 0 ? _healthPlanCallQueueCriteriaRepository.GetById(model.Id) : _healthPlanCallQueueCriteriaRepository.GetQueueCriteriaForQueueByLanguage(model.CallQueue, model.HealthPlanId, model.LanguageId);

                var organization = _organizationRepository.GetOrganizationbyId(model.HealthPlanId);
                var language     = model.LanguageId.HasValue ? _languageRepository.GetById(model.LanguageId.Value) : null;
                model.CriteriaName = organization.Name + " Confirmation" + (language != null ? " for " + language.Name : "");
                if (healthPlanCallQueueCriteria == null)
                {
                    healthPlanCallQueueCriteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForConfirmation(model, orgRoleId);
                    healthPlanCallQueueCriteria = _healthPlanCallQueueCriteriaRepository.Save(healthPlanCallQueueCriteria);
                }
                else
                {
                    /*if (healthPlanCallQueueCriteria.LanguageId != model.LanguageId)
                     *  healthPlanCallQueueCriteria.IsQueueGenerated = false;*/
                    healthPlanCallQueueCriteria.LanguageId   = model.LanguageId;
                    healthPlanCallQueueCriteria.CriteriaName = model.CriteriaName;
                    healthPlanCallQueueCriteria = _healthPlanCallQueueCriteriaRepository.Save(healthPlanCallQueueCriteria);
                }

                if (!model.IsTeamAssignment)
                {
                    if (!model.Assignments.IsNullOrEmpty())
                    {
                        SaveFileInfoForCsvUpload(model, healthPlanCallQueueCriteria);
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaAssignment(model.Assignments, healthPlanCallQueueCriteria.Id, orgRoleId);
                        _healthPlanCriteriaAssignmentRepository.Save(model.HealthPlanId, model.CallQueueId, model.Id, assignments);
                    }
                }
                else
                {
                    if (!model.CallCenterTeamAssignments.IsNullOrEmpty())
                    {
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaTeamAssignment(model.CallCenterTeamAssignments, healthPlanCallQueueCriteria, orgRoleId);
                        _healthPlanCriteriaTeamAssignmentRepository.Save(assignments);
                    }
                }
            }
            else
            {
                HealthPlanCallQueueCriteria criteria = null;

                if (!isCriteriaSameAsPervious)
                {
                    if (model.CallQueue == HealthPlanCallQueueCategory.FillEventsHealthPlan)
                    {
                        criteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForFillEvent(model, orgRoleId);
                    }
                    #region not in use
                    //switch (model.CallQueue)
                    //{
                    //    //case HealthPlanCallQueueCategory.CallRound:
                    //    //    criteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForCallRound(model, orgRoleId);
                    //    //    break;
                    //    case HealthPlanCallQueueCategory.FillEventsHealthPlan:
                    //        criteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForFillEvent(model, orgRoleId);
                    //        break;
                    //    //case HealthPlanCallQueueCategory.NoShows:
                    //    //    criteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForNoShow(model, orgRoleId);
                    //    //    break;
                    //    //case HealthPlanCallQueueCategory.ZipRadius:
                    //    //    criteria = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCallQueueCriteriaForZipRadius(model, orgRoleId);
                    //    //    break;
                    //}
                    #endregion
                    _healthPlanCallQueueCriteriaRepository.MarkForDelete(model.Id, true);
                    criteria = _healthPlanCallQueueCriteriaRepository.Save(criteria);
                    model.Id = criteria.Id;
                }
                else if (model.Id > 0)
                {
                    criteria = _healthPlanCallQueueCriteriaRepository.GetById(model.Id);
                    criteria.DataRecorderMetaData.DateModified         = DateTime.Now;
                    criteria.DataRecorderMetaData.DataRecorderModifier = new OrganizationRoleUser(orgRoleId);
                    criteria.CriteriaName = model.CriteriaName;
                    criteria = _healthPlanCallQueueCriteriaRepository.Save(criteria);
                }

                if (!model.IsTeamAssignment)
                {
                    SaveFileInfoForCsvUpload(model, criteria);

                    if (!model.Assignments.IsNullOrEmpty())
                    {
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaAssignment(model.Assignments, model.Id, orgRoleId);
                        _healthPlanCriteriaAssignmentRepository.Save(model.HealthPlanId, model.CallQueueId, model.Id, assignments);
                    }
                }
                else
                {
                    if (!model.CallCenterTeamAssignments.IsNullOrEmpty())
                    {
                        var assignments = _healthPlanCallQueueCriteriaEditModelFactory.GetHealthPlanCriteriaTeamAssignment(model.CallCenterTeamAssignments, criteria, orgRoleId);
                        _healthPlanCriteriaTeamAssignmentRepository.Save(assignments);
                    }
                }
            }

            if (!model.IsTeamAssignment)
            {
                if (model.Assignments != null && model.Assignments.Any())
                {
                    var list = new List <CallQueueAssignmentEditModel>();
                    foreach (var assignment in model.Assignments)
                    {
                        assignment.IsExistInOtherCriteria = false;
                        list.Add(assignment);
                    }
                    model.Assignments = list;
                }
                return(model);
            }
            else
            {
                model.IsTeamAssignment = true;
                model.Assignments      = null;
                return(model);
            }
        }