//Create Contact
        public static int Create(ContactDTO ContactDTO)
        {
            try
            {
                GlobalSettings.LoggedInClientId = ContactDTO.ClientId;
                int PartnerId = ClientService.GetById(ContactDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                var contact = new Contact();
                using (var uow = new UnitOfWork())
                {
                    contact = Transform.ContactToDomain(ContactDTO);

                    uow.ContactRepo.Insert(contact);
                    uow.SaveChanges();
                    if (ContactDTO.Groups != null)
                    {
                        foreach (var item in ContactDTO.Groups)
                        {
                            AddGroupToContact(contact.Id, item.Id);
                        }
                    }
                    return (contact.Id);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Returns Contact paged list as per campaign and client with search criteria
        public static PageData<ContactDTO> GetContactsByCampaignId(PagingInfo pagingInfo, int CampaignId, int ClientId)
        {
            UnitOfWork uow = new UnitOfWork();
            CampaignDTO CampaignDTO = new CampaignDTO();
            PageData<ContactDTO> pageList = new PageData<ContactDTO>();

            CampaignDTO = GetById(CampaignId);

            if (CampaignDTO != null)
            {
                string[] array;
                if (CampaignDTO.RecipientsNumber != "")
                {
                    string value = CampaignDTO.RecipientsNumber;
                    array = value.Replace(" ", "").Split(',');

                    List<ContactDTO> ContactDTOList = new List<ContactDTO>();
                    Contact Contact = new Contact();
                    ContactDTO ContactDTO = new ContactDTO();

                    int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                    int take = pagingInfo.ItemsPerPage;

                    var newlist = array.Skip(skip).Take(take).ToArray();

                    if (pagingInfo.Search != "")
                    {
                        ContactDTOList = SearchContacts(pagingInfo.Search, array, CampaignId, ClientId);
                        pageList.Data = ContactDTOList;
                        pageList.Count = ContactDTOList.Count;
                    }
                    else
                    {
                        for (int i = 0; i < newlist.Length; i++)
                        {
                            Contact = uow.ContactRepo.GetAll().Where(e => e.MobileNumber == newlist[i] && e.ClientId == ClientId).FirstOrDefault();
                            if (Contact == null)
                            {
                                Contact = new Contact();
                                Contact.MobileNumber = newlist.ToList()[i];
                                Contact.Gender = null;
                                Contact.Id = 0;
                                Contact.ClientId = 0;
                                //Contact.Name = null;
                            }

                            ContactDTO = Transform.ContactToDTO(Contact);

                            ContactDTOList.Add(ContactDTO);
                        }
                        pageList.Data = ContactDTOList;
                        pageList.Count = array.Length;
                    }
                }
                else if (CampaignDTO.GroupId != null)
                {
                    // GroupContactDTO GroupContactDTO =  GroupService.GetGroupContactById((int)CampaignDTO.GroupId);
                    pagingInfo.GroupId = (int)CampaignDTO.GroupId;
                    pageList = ContactService.GetGroupIdWisePresentContactsPagedListByClientId(pagingInfo, ClientId);
                }

            }
            else
            {
                pageList.Data = null;
            }

            return pageList;
        }
        //Returns Contact list as per receipent numbers, campaign and client with search criteria
        public static List<ContactDTO> SearchContacts(string search, string[] receipients, int CampaignId, int ClientId)
        {
            List<ContactDTO> ContactDTOList = new List<ContactDTO>();
            List<Contact> Contacts = new List<Contact>();
            UnitOfWork uow = new UnitOfWork();
            Contact Contact = new Contact();

            if (receipients.Contains(search))
            {
                int count = 0;
                Contacts = uow.ContactRepo.GetAll().Where(e => e.MobileNumber == search && e.ClientId == ClientId).ToList();

                if (Contacts.Count != 0)
                {
                    foreach (var item in Contacts)
                    {
                        count = count + 1;
                        ContactDTOList.Add(Transform.ContactToDTO(item));
                    }
                }
                else
                {
                    Contact = new Contact();
                    ContactDTO ContactDTO = new ContactDTO();

                    Contact.MobileNumber = search;
                    Contact.Gender = null;
                    Contact.Id = 0;
                    Contact.ClientId = 0;
                    //Contact.Name = null;

                    ContactDTO = Transform.ContactToDTO(Contact);
                    ContactDTOList.Add(ContactDTO);
                }
            }
            else
            {
                int count = 0;
                Contacts = uow.ContactRepo.GetAll().Where(e => e.Name.ToLower().Equals(search.ToLower()) || (e.Email != null ? (e.Email.ToLower().Contains(search.ToLower())) : false) && e.ClientId == ClientId).ToList();

                if (Contacts != null)
                {
                    foreach (var item in Contacts)
                    {
                        count = count + 1;
                        ContactDTOList.Add(Transform.ContactToDTO(item));
                    }
                }
            }
            return ContactDTOList;
        }
        //Get Client's Latest Activities
        public static List<ActivityLogDTO> GetClientLatestActivities(int ClientId, int Top)
        {
            List<ActivityLogDTO> ActivityLogDTOList = new List<ActivityLogDTO>();
            try
            {
                using (var uow = new UnitOfWork())
                {
                    // DateTime yesterdayDate = DateTime.Now.AddDays(-Top).Date;
                    //var LogList = uow.ActivityLogRepo.Get(c => c.Date > yesterdayDate).ToList();
                    var LogList = uow.ActivityLogRepo.GetAll().Where(e => e.ClientId == Convert.ToInt32(ClientId)).OrderByDescending(e => e.Id).ToList();
                    int i = 0;
                    int j = 0;
                    foreach (var log in LogList)
                    {
                        j = 0;
                        if (log.EntityType == "GroupContact" || log.EntityType == "CampaignLogXML" || log.EntityType == "CampaignLog" || log.EntityType == "Coupon")
                        {
                            continue;
                        }

                        if (log.EntityType == "Campaign") //&& log.OperationType == "Added" || log.OperationType == "Modified"
                        {
                            Campaign Campaign = new Campaign();
                            Campaign = uow.CampaignRepo.GetById(log.EntityId);
                            if (Campaign != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Campaign.Name;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        if (log.EntityType == "Client") //&& log.OperationType == "Added" || log.OperationType == "Modified"
                        {
                            Client Client = new Client();
                            Client = uow.ClientRepo.GetById(log.EntityId);
                            if (Client != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Client.Company;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        if (log.EntityType == "Contact") //&& log.OperationType == "Added" || log.OperationType == "Modified"
                        {
                            Contact Contact = new Contact();
                            Contact = uow.ContactRepo.GetById(log.EntityId);
                            if (Contact != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Contact.Name;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        if (log.EntityType == "CreditRequest") //&& log.OperationType == "Added" || log.OperationType == "Modified"
                        {
                            CreditRequest CreditRequest = new CreditRequest();
                            CreditRequest = uow.CreditRequestRepo.GetById(log.EntityId);
                            if (CreditRequest != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = "";// "Credit Request";// uow.CreditRequestRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        if (log.EntityType == "Document")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            Document Document = new Document();
                            Document = uow.DocumentRepo.GetById(log.EntityId);
                            if (Document != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Document.FileName;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        if (log.EntityType == "EcouponCampaign")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            EcouponCampaign EcouponCampaign = new EcouponCampaign();
                            EcouponCampaign = uow.EcouponCampaignRepo.GetById(log.EntityId);
                            if (EcouponCampaign != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = EcouponCampaign.Title;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        if (log.EntityType == "Group")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            Group Group = new Group();
                            Group = uow.GroupRepo.GetById(log.EntityId);
                            if (Group != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Group.Name;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;

                            }
                        }

                        //if (log.EntityType == "Partner")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        //{
                        //    Partner Partner = new Partner();
                        //    Partner = uow.PartnerRepo.GetById(log.EntityId);
                        //    if (Partner != null)
                        //    {
                        //        ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                        //        if (log.UserId != null)
                        //        {
                        //            ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                        //        }

                        //        if (log.PartnerId != null)
                        //        {
                        //            ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                        //        }

                        //        if (log.ClientId != null)
                        //        {
                        //            ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                        //        }

                        //        ActivityLogDTO.EntityName = Partner.Name;
                        //        ActivityLogDTO.Date = log.Date;
                        //        ActivityLogDTO.EntityType = log.EntityType;
                        //        ActivityLogDTO.OperationType = log.OperationType;
                        //        ActivityLogDTO.Id = log.Id;
                        //        ActivityLogDTOList.Add(ActivityLogDTO);
                        //        j = 1;
                        //    }
                        //}

                        //if (log.EntityType == "Plan")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        //{
                        //    Plan Plan = new Plan();
                        //    Plan = uow.PlanRepo.GetById(log.EntityId);
                        //    if (Plan != null)
                        //    {
                        //        ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                        //        if (log.UserId != null)
                        //        {
                        //            ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                        //        }

                        //        if (log.PartnerId != null)
                        //        {
                        //            ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                        //        }

                        //        if (log.ClientId != null)
                        //        {
                        //            ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                        //        }

                        //        ActivityLogDTO.EntityName = Plan.Title;
                        //        ActivityLogDTO.Date = log.Date;
                        //        ActivityLogDTO.EntityType = log.EntityType;
                        //        ActivityLogDTO.OperationType = log.OperationType;
                        //        ActivityLogDTO.Id = log.Id;
                        //        ActivityLogDTOList.Add(ActivityLogDTO);
                        //        j = 1;
                        //    }
                        //}

                        if (log.EntityType == "RedeemedCount")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            RedeemedCount RedeemedCount = new RedeemedCount();
                            RedeemedCount = uow.RedeemedCountRepo.GetById(log.EntityId);
                            if (RedeemedCount != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = "";// "Redeemed Count";// uow.RedeemedCountRepo.GetById(Convert.ToInt32(log.UserId)).Title;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;
                            }
                        }

                        if (log.EntityType == "Setting")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            Setting Setting = new Setting();
                            Setting = uow.SettingRepo.GetById(log.EntityId);
                            if (Setting != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = "";// "Setting";//uow.SettingRepo.GetById(Convert.ToInt32(log.UserId)).Title;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;
                            }
                        }

                        if (log.EntityType == "Template")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            Template Template = new Template();
                            Template = uow.TemplateRepo.GetById(log.EntityId);
                            if (Template != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Template.Title;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;
                            }
                        }

                        if (log.EntityType == "User")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            User User = new User();
                            User = uow.UserRepo.GetById(log.EntityId);
                            if (User != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = User.Name;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;
                            }
                        }

                        if (log.EntityType == "Location")// && log.OperationType == "Added" || log.OperationType == "Modified")
                        {
                            Location Location = new Location();
                            Location = uow.LocationRepo.GetById(log.EntityId);
                            if (Location != null)
                            {
                                ActivityLogDTO ActivityLogDTO = new ActivityLogDTO();
                                if (log.UserId != null)
                                {
                                    ActivityLogDTO.User = uow.UserRepo.GetById(Convert.ToInt32(log.UserId)).Name;
                                }

                                if (log.PartnerId != null)
                                {
                                    ActivityLogDTO.Partner = uow.PartnerRepo.GetById(Convert.ToInt32(log.PartnerId)).Name;
                                }

                                if (log.ClientId != null)
                                {
                                    ActivityLogDTO.Client = uow.ClientRepo.GetById(Convert.ToInt32(log.ClientId)).Company;
                                }

                                ActivityLogDTO.EntityName = Location.Name;
                                ActivityLogDTO.Date = log.Date;
                                ActivityLogDTO.EntityType = log.EntityType;
                                ActivityLogDTO.OperationType = log.OperationType;
                                ActivityLogDTO.Id = log.Id;
                                ActivityLogDTOList.Add(ActivityLogDTO);
                                j = 1;
                            }
                        }

                        i = i + j;
                        if (i >= Top)
                        {
                            return ActivityLogDTOList;
                        }

                    }

                }
                return ActivityLogDTOList;

            }
            catch (Exception)
            {
                //return ActivityLogDTOList;
                throw;
            }
        }
        //Get contact present list as per group id
        public static List<ContactDTO> GetGroupIdWisePresentContacts(PagingInfo pagingInfo)
        {
            List<GroupContactDTO> GroupContactDTOList = new List<GroupContactDTO>();
            List<ContactDTO> ContactDTOList = new List<ContactDTO>();
            List<Contact> ContactList = new List<Contact>();

            int ClientId = 0;
            if (pagingInfo.GroupId != 0)
            {
                ClientId = GroupService.GetById(pagingInfo.GroupId).ClientID;
            }

            try
            {
                int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                int take = pagingInfo.ItemsPerPage;

                // string search
                //string ContactIdStringall = ContactService.GetContactIdarrayByName(pagingInfo.Search, ClientId);

                UnitOfWork uow = new UnitOfWork();
                IQueryable<GroupContact> GroupContact = uow.GroupContactRepo.GetAll().Where(e => e.GroupId == pagingInfo.GroupId).AsQueryable(); //&& (ContactIdStringall != null ? (e.ContactId.ToString().Split(',').Any(ContactId => ContactIdStringall.Contains(ContactId.ToString()))) : false)
                //GroupContact = PagingService.Sorting<GroupContact>(GroupContact, pagingInfo.SortBy, pagingInfo.Reverse);
                GroupContact = GroupContact.Skip(skip).Take(take);
                if (GroupContact != null)
                {
                    foreach (var item in GroupContact)
                    {
                        Contact Contact = new Contact();
                        Contact = uow.ContactRepo.GetById(item.ContactId);
                        ContactList.Add(Contact);
                        //ContactDTO ContactDTO = new ContactDTO();
                        //ContactDTO = ContactService.GetById(item.ContactId);
                        //ContactDTOList.Add(ContactDTO);
                    }

                    if (ContactList != null)
                    {
                        if (pagingInfo.Search != "" && pagingInfo.Search != null)
                        {
                            List<ContactDTO> ContactDTOSearchList = new List<ContactDTO>();

                            bool IsDate = CommonService.IsDate(pagingInfo.Search);
                            if (IsDate != true)
                            {

                                // string search
                                string ContactIdString = ContactService.GetContactIdarrayByName(pagingInfo.Search, ClientId);
                                IQueryable<GroupContact> GroupContactSearch = uow.GroupContactRepo.GetAll().Where(e => e.GroupId == pagingInfo.GroupId && (ContactIdString != null ? (e.ContactId.ToString().Split(',').Any(ContactId => ContactIdString.Contains(ContactId.ToString()))) : false)).AsQueryable();
                                //GroupContact = PagingService.Sorting<GroupContact>(GroupContact, pagingInfo.SortBy, pagingInfo.Reverse);
                                GroupContactSearch = GroupContactSearch.Skip(skip).Take(take);
                                if (GroupContactSearch != null)
                                {
                                    List<Contact> ContactListSearch = new List<Contact>();
                                    foreach (var item in GroupContactSearch)
                                    {
                                        Contact Contact = new Contact();
                                        Contact = uow.ContactRepo.GetById(item.ContactId);
                                        ContactListSearch.Add(Contact);
                                    }

                                    IQueryable<Contact> Contactsearch = ContactListSearch.Where(e => (e.Email != null ? (e.Email.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || e.MobileNumber.Contains(pagingInfo.Search) || (e.Name != null ? (e.Name.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.FirstName != null ? (e.FirstName.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.LastName != null ? (e.LastName.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.AnniversaryDate.ToString() != null ? (Convert.ToDateTime(e.AnniversaryDate).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.BirthDate.ToString() != null ? (Convert.ToDateTime(e.BirthDate).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false)).AsQueryable();//.OrderBy(e => e.Name);
                                    Contactsearch = PagingService.Sorting<Contact>(Contactsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                                    Contactsearch = Contactsearch.Skip(skip).Take(take);
                                    if (Contactsearch != null)
                                    {
                                        foreach (var item in Contactsearch)
                                        {
                                            ContactDTOSearchList.Add(Transform.ContactToDTO(item));
                                        }

                                    } return ContactDTOSearchList.Skip(skip).Take(take).ToList();

                                }

                            }
                            else
                            {
                                //date wise search
                                string ContactIdString = ContactService.GetContactIdarrayByName(pagingInfo.Search, ClientId);

                                IQueryable<GroupContact> GroupContactSearch = uow.GroupContactRepo.GetAll().Where(e => e.GroupId == pagingInfo.GroupId && (ContactIdString != null ? (e.ContactId.ToString().Split(',').Any(ContactId => ContactIdString.Contains(ContactId.ToString()))) : false)).AsQueryable();
                                //GroupContact = PagingService.Sorting<GroupContact>(GroupContact, pagingInfo.SortBy, pagingInfo.Reverse);
                                GroupContactSearch = GroupContactSearch.Skip(skip).Take(take);
                                if (GroupContactSearch != null)
                                {
                                    List<Contact> ContactListSearch = new List<Contact>();
                                    foreach (var item in GroupContactSearch)
                                    {
                                        Contact Contact = new Contact();
                                        Contact = uow.ContactRepo.GetById(item.ContactId);
                                        ContactListSearch.Add(Contact);
                                    }

                                    DateTime date = Convert.ToDateTime(pagingInfo.Search);
                                    IQueryable<Contact> Contactsearch = ContactListSearch.Where(e => e.AnniversaryDate >= date && e.AnniversaryDate < date.AddDays(1) || e.BirthDate >= date && e.BirthDate < date.AddDays(1)).AsQueryable();//.OrderBy(e => e.Name);
                                    Contactsearch = PagingService.Sorting<Contact>(Contactsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                                    Contactsearch = Contactsearch.Skip(skip).Take(take);
                                    if (Contactsearch != null)
                                    {
                                        foreach (var item in Contactsearch)
                                        {
                                            ContactDTOSearchList.Add(Transform.ContactToDTO(item));

                                        }
                                    }
                                    return ContactDTOSearchList.Skip(skip).Take(take).ToList();
                                }
                            }
                        }

                        //ContactList = ContactList.Skip(skip).Take(take).ToList();
                        foreach (var item in ContactList)
                        {
                            ContactDTOList.Add(Transform.ContactToDTO(item));
                        }
                    }

                    return ContactDTOList;// ContactDTOList.Skip(skip).Take(take).ToList();
                }

                return ContactDTOList.Skip(skip).Take(take).ToList();
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Get contact list as per group id
        public static List<ContactDTO> GetGroupIdWiseContacts(int GroupId)
        {
            List<GroupContactDTO> GroupContactDTOList = new List<GroupContactDTO>();
            List<ContactDTO> ContactDTOList = new List<ContactDTO>();
            List<Contact> ContactList = new List<Contact>();

            try
            {

                UnitOfWork uow = new UnitOfWork();
                IEnumerable<GroupContact> GroupContact = uow.GroupContactRepo.GetAll().Where(e => e.GroupId == GroupId).AsQueryable();

                if (GroupContact != null)
                {
                    foreach (var item in GroupContact)
                    {
                        Contact Contact = new Contact();
                        Contact = uow.ContactRepo.GetById(item.ContactId);
                        ContactList.Add(Contact);
                    }

                    if (ContactList != null)
                    {
                        foreach (var item in ContactList)
                        {
                            ContactDTOList.Add(Transform.ContactToDTO(item));
                        }
                    }

                    return ContactDTOList;// ContactDTOList.Skip(skip).Take(take).ToList();
                }
                return ContactDTOList;
            }
            catch (Exception)
            {
                throw;
            }
        }
 public static ContactDTO ContactToDTO(Contact Contact)
 {
     if (Contact == null) return null;
      Mapper.CreateMap<Contact, ContactDTO>();
      Mapper.CreateMap<Group, GroupDTO>();
      ContactDTO ContactDTO = Mapper.Map<ContactDTO>(Contact);
      //GroupDTO GroupDTO = Mapper.Map<GroupDTO>(Contact.Groups);
      //var ContactDTO = AutoMapper.Mapper.Map<ContactDTO>(Contact);
      //ContactDTO.Name = Contact.FirstName + " " + Contact.LastName;
      return ContactDTO;
 }