//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;
            }
        }
        //Delete Contact by Id
        public static void Delete(int Id)
        {
            try
            {
                ContactDTO ContactDTO = new ContactDTO();
                ContactDTO = GetById(Id);

                GlobalSettings.LoggedInClientId = ContactDTO.ClientId;
                int PartnerId = ClientService.GetById(ContactDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                if (ContactDTO.Groups.Count > 0)
                {
                    foreach (var item in ContactDTO.Groups)
                    {
                        RemoveGroupFromContact(Id, item.Id);
                    }
                }

                UnitOfWork uow = new UnitOfWork();
                uow.ContactRepo.Delete(Id);
                uow.SaveChanges();

            }
            catch
            {
                throw;
            }
        }
 public static Contact ContactToDomain(ContactDTO ContactDTO)
 {
     if (ContactDTO == null) return null;
      Mapper.CreateMap<ContactDTO, Contact>();
      Mapper.CreateMap<GroupDTO, Group>();
      Contact Contact = Mapper.Map<Contact>(ContactDTO);
      return Contact;
 }
 public int CreateContact(ContactDTO contactDTO)
 {
     try
     {
         return (ContactService.Create(contactDTO));
     }
     catch (Exception)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent("An error occurred, please try again or contact the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
 public ContactDTO GetById(int Id)
 {
     ContactDTO ContactDTO = new ContactDTO();
     try
     {
         ContactDTO = (ContactService.GetById(Id));
         return ContactDTO;
     }
     catch (Exception)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent("An error occurred, please try again or contact the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
 public void EditContact(ContactDTO contactDTO)
 {
     try
     {
         ContactService.Edit(contactDTO);
     }
     catch (TimeoutException)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.RequestTimeout)
         {
             Content = new StringContent("An error occurred, please try again or contact the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
     catch (Exception)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent("An error occurred, please try again or contact the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
        public static int GetContactsCountbyGroupId(int ClientId, int GroupId, string Search)
        {
            List<ContactDTO> contactDTO = new List<ContactDTO>();
            List<ContactDTO> contactDTOList = new List<ContactDTO>();
            try
            {
                UnitOfWork uow = new UnitOfWork();

                IEnumerable<Contact> Contact = uow.ContactRepo.GetAll().Where(e => e.ClientId == ClientId).ToList();

                if (Contact != null)
                {
                    foreach (var item in Contact)
                    {
                        ContactDTO ContactDTO = new ContactDTO();
                        ContactDTO = GetById(item.Id);

                        if (ContactDTO.Groups.Count() > 0)
                        {
                            foreach (var itemContactList in ContactDTO.Groups)
                            {
                                if (itemContactList.Id == GroupId)
                                {
                                    contactDTOList.Add(ContactDTO);
                                }
                            }
                        }

                    }

                    if (Search != "" && Search != null)
                    {

                        bool IsDate = CommonService.IsDate(Search);
                        if (IsDate != true)
                        {
                            // string search

                            var Contactsearch = contactDTOList.Where(e => (e.Email != null ? (e.Email.ToLower().Contains(Search.ToLower())) : false) || e.MobileNumber.Contains(Search) || (e.Name != null ? (e.Name.ToLower().Contains(Search.ToLower())) : false) || (e.FirstName != null ? (e.FirstName.ToLower().Contains(Search.ToLower())) : false) || (e.LastName != null ? (e.LastName.ToLower().Contains(Search.ToLower())) : false) || (e.AnniversaryDate.ToString() != null ? (Convert.ToDateTime(e.AnniversaryDate).ToString("dd-MMM-yyyy").ToLower().Contains(Search.ToLower())) : false) || (e.BirthDate.ToString() != null ? (Convert.ToDateTime(e.BirthDate).ToString("dd-MMM-yyyy").ToLower().Contains(Search.ToLower())) : false));//.OrderBy(e => e.Name);

                            if (Contactsearch != null)
                            {
                                foreach (var item in Contactsearch)
                                {
                                    ContactDTO ContactDTOObj = new ContactDTO();
                                    //ContactDTOObj = GetById(item.Id);
                                    contactDTO.Add(item);

                                    //ContactDTOObj =Transform.ContactToDTO(item);
                                    // contactDTO.Add(Transform.ContactToDTO(item));

                                }

                            } return contactDTO.Count();

                        }
                        else
                        {
                            //date wise search
                            DateTime date = Convert.ToDateTime(Search);
                            var Contactsearch = contactDTOList.Where(e => e.AnniversaryDate >= date && e.AnniversaryDate < date.AddDays(1) || e.BirthDate >= date && e.BirthDate < date.AddDays(1));//.OrderBy(e => e.Name);

                            if (Contactsearch != null)
                            {
                                foreach (var item in Contactsearch)
                                {
                                    ContactDTO ContactDTOObj = new ContactDTO();
                                    //ContactDTOObj = GetById(item.Id);
                                    contactDTO.Add(item);
                                    // contactDTO.Add(Transform.ContactToDTO(item));

                                }
                            }
                            return contactDTO.Count();

                        }

                    }

                }

                return contactDTOList.Count();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static List<ContactDTO> GetContactsbyClientId(int ClientId, string search, PagingInfo pagingInfo)
        {
            List<ContactDTO> contactDTO = new List<ContactDTO>();
            GroupDTO GroupDTO = new GroupDTO();
            try
            {
                UnitOfWork uow = new UnitOfWork();
                int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                int take = pagingInfo.ItemsPerPage;
                IQueryable<Contact> Contact = uow.ContactRepo.GetAll().Where(e => e.ClientId == ClientId).AsQueryable();// ToList().Skip(skip).Take(take); //.OrderBy(e => e.Name)

                Contact = PagingService.Sorting<Contact>(Contact, pagingInfo.SortBy, pagingInfo.Reverse);
                Contact = Contact.Skip(skip).Take(take);
                if (Contact != null)
                {
                    if (search != "" && search != null)
                    {

                        bool IsDate = CommonService.IsDate(search);
                        if (IsDate != true)
                        {
                            // string search

                            IQueryable<Contact> Contactsearch = uow.ContactRepo.GetAll().Where(e => (e.ClientId == ClientId) && ((e.Email != null ? (e.Email.ToLower().Contains(search.ToLower())) : false) || e.MobileNumber.Contains(search) || (e.Name != null ? (e.Name.ToLower().Contains(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();// .ToList().Skip(skip).Take(take); //.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)
                                {
                                    ContactDTO ContactDTOObj = new ContactDTO();
                                    ContactDTOObj = GetById(item.Id);
                                    contactDTO.Add(ContactDTOObj);

                                    //ContactDTOObj =Transform.ContactToDTO(item);
                                    // contactDTO.Add(Transform.ContactToDTO(item));

                                }

                            }

                        }
                        else
                        {
                            //date wise search
                            DateTime date = Convert.ToDateTime(search);
                            IQueryable<Contact> Contactsearch = uow.ContactRepo.GetAll().Where(e => (e.ClientId == ClientId) && 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)
                                {
                                    ContactDTO ContactDTOObj = new ContactDTO();
                                    ContactDTOObj = GetById(item.Id);
                                    contactDTO.Add(ContactDTOObj);
                                    // contactDTO.Add(Transform.ContactToDTO(item));

                                }
                            }
                            return contactDTO;

                        }

                    }
                    else
                    {
                        foreach (var item in Contact)
                        {
                            ContactDTO ContactDTOObj = new ContactDTO();
                            ContactDTOObj = GetById(item.Id);
                            contactDTO.Add(ContactDTOObj);
                            //contactDTO.Add(Transform.ContactToDTO(item));

                        }
                    }
                }

                return contactDTO;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Returns the sheet collection
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>                  
        private static List<ContactDTO> GetExcelSheetNames(string strFileName, bool IsValidContact)
        {
            OleDbConnection Con = new OleDbConnection();
            Con = Connect(strFileName);
            DataTable dtTable;
            dtTable = new DataTable();
            DataTable dtTable1 = new DataTable();
            DataTable dtTable2 = new DataTable();
            List<ContactDTO> ContactDTOList = new List<ContactDTO>();
            try
            {
                Con.Open();

                dtTable = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                dtTable1 = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                dtTable2 = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                if ((dtTable == null))
                {
                    return null;
                }

                int cnt = dtTable.Rows.Count;
                //string[cnt] ExcelSheets;
                string[] ExcelSheets = new string[cnt];

                int i = 0, k = 0;
                int totalCount = 0;
                foreach (DataRow Row1 in dtTable1.Rows)
                {
                    string strTableName = Row1["TABLE_NAME"].ToString();
                    ExcelSheets[k] = strTableName.Substring(0, (strTableName.Length - 1)).ToString();
                    if (ExcelSheets[k].Contains("$"))
                    {
                        int count = ExcelSheets[k].IndexOf("$");
                        string s = ExcelSheets[k].Substring(0, count);
                        //s = GetStringValue2(s);
                        ExcelSheets[k] = s;

                    }

                    DataTable DataTable1 = new DataTable();
                    DataTable1 = GetDataTable(ExcelSheets[k], strFileName);
                    totalCount = totalCount + DataTable1.Rows.Count;
                    k++;
                }

                foreach (DataRow Row in dtTable.Rows)
                {
                    string strTableName = Row["TABLE_NAME"].ToString();
                    ExcelSheets[i] = strTableName.Substring(0, (strTableName.Length - 1)).ToString();
                    if (ExcelSheets[i].Contains("$"))
                    {
                        int count = ExcelSheets[i].IndexOf("$");
                        string s = ExcelSheets[i].Substring(0, count);
                        //s = GetStringValue2(s);
                        ExcelSheets[i] = s;

                    }

                    DataTable DataTable = new DataTable();
                    DataTable = GetDataTable(ExcelSheets[i], strFileName);

                    for (int j = 0; (j <= (DataTable.Rows.Count - 1)); j++)
                    {

                        ContactDTO ContactDTO = new ContactDTO();
                        string FirstName = null;
                        string LastName = null;

                        if (!Convert.IsDBNull(DataTable.Rows[j]["FirstName"].ToString()))
                        {
                            FirstName = DataTable.Rows[j]["FirstName"].ToString();
                            if (FirstName == "")
                            {
                                FirstName = null;
                            }
                            ContactDTO.FirstName = FirstName;
                        }
                        else
                        {
                            FirstName = null;
                            ContactDTO.FirstName = FirstName;
                        }

                        if (!Convert.IsDBNull(DataTable.Rows[j]["LastName"].ToString()))
                        {
                            LastName = DataTable.Rows[j]["LastName"].ToString();
                            if (LastName == "")
                            {
                                LastName = null;
                            }
                            ContactDTO.LastName = LastName;
                        }
                        else
                        {
                            LastName = null;
                            ContactDTO.LastName = LastName;
                        }

                        if (FirstName != null || LastName != null)
                        {
                            ContactDTO.Name = FirstName + " " + LastName;
                        }

                        if (FirstName != null && LastName != null)
                        {
                            ContactDTO.Name = FirstName + " " + LastName;
                            ContactDTO.IsValid = true;
                        }
                        else //if (ContactDTO.FirstName == null && ContactDTO.LastName == null)
                        {
                            ContactDTO.Name = FirstName + " " + LastName;
                            ContactDTO.IsValid = false;
                        }

                        //if (!Convert.IsDBNull(DataTable.Rows[j]["Contact Name"].ToString()))
                        //{
                        //    ContactDTO.Name = DataTable.Rows[j]["Contact Name"].ToString();
                        //}
                        //else
                        //{
                        //    ContactDTO.Name = "";
                        //}

                        if (!Convert.IsDBNull(DataTable.Rows[j]["Mobile Number"].ToString()))
                        {
                            ContactDTO.MobileNumber = DataTable.Rows[j]["Mobile Number"].ToString();
                            ContactDTO.IsMobileValid = true;
                            if (ContactDTO.MobileNumber.Length != 10)
                            {
                                ContactDTO.MobileNumber = ContactDTO.MobileNumber;
                                ContactDTO.IsValid = false;
                                ContactDTO.IsMobileValid = false;
                            }

                            bool IsValid = IsValidMobile(ContactDTO.MobileNumber);
                            if (IsValid != true)
                            {
                                ContactDTO.MobileNumber = ContactDTO.MobileNumber;
                                ContactDTO.IsValid = false;
                                ContactDTO.IsMobileValid = false;
                            }
                        }
                        else
                        {
                            ContactDTO.MobileNumber = ContactDTO.MobileNumber;
                            ContactDTO.IsValid = false;
                            ContactDTO.IsMobileValid = false;
                        }

                        if (!Convert.IsDBNull(DataTable.Rows[j]["Gender"].ToString()))
                        {
                            String Gender = DataTable.Rows[j]["Gender"].ToString().ToLower();

                            if (Gender.ToLower() == "male" || Gender.ToLower() == "female")
                            {
                                ContactDTO.Gender = DataTable.Rows[j]["Gender"].ToString();
                            }
                            else
                            {
                                ContactDTO.Gender = "Male";
                            }

                        }
                        else
                        {
                            ContactDTO.Gender = "Male";
                        }

                        if (!Convert.IsDBNull(DataTable.Rows[j]["Email Address"].ToString()))
                        {
                            ContactDTO.Email = DataTable.Rows[j]["Email Address"].ToString();
                            if (ContactDTO.Email == "")
                            {
                                ContactDTO.Email = null;
                            }
                        }
                        else
                        {
                            ContactDTO.Email = null;
                        }

                        if (ContactDTO.FirstName == null && ContactDTO.LastName == null && ContactDTO.MobileNumber == "" && ContactDTO.Email == null)
                        {
                            continue;
                        }

                        if (IsValidContact == true)
                        {

                            if (ContactDTO.IsValid == true)  //if (ContactDTO.Name != "" && ContactDTO.MobileNumber != "")
                            {
                                if (ContactDTO.Name.Length <= 51) //100
                                {
                                    ContactDTO.TotalCount = totalCount;
                                    ContactDTO.IsValid = true;
                                    ContactDTOList.Add(ContactDTO);
                                }
                                else
                                {
                                    ContactDTO.TotalCount = totalCount;
                                    ContactDTO.IsValid = false;
                                    ContactDTOList.Add(ContactDTO);
                                }

                            }
                        }
                        else
                        {
                            if (ContactDTO.IsValid == false)  //if (ContactDTO.Name != "" && ContactDTO.MobileNumber != "")
                            {
                                ContactDTO.TotalCount = totalCount;
                                ContactDTO.IsValid = false;
                                ContactDTOList.Add(ContactDTO);
                            }

                        }

                    }

                    i = (i + 1);
                }
                //return ExcelSheets;
                Con.Close();
                return ContactDTOList;

            }
            catch (Exception)
            {
                Con.Close();
                throw;
            }
        }
        public static ContactDTO GetContactByMobileNumberAndClientId(string MobileNumber, int ClientId)
        {
            try
            {
                ContactDTO ContactDTO = new ContactDTO();
                UnitOfWork uow = new UnitOfWork();
                IEnumerable<Contact> Contact = uow.ContactRepo.GetAll().Where(e => e.MobileNumber == MobileNumber && e.ClientId == ClientId);

                if (Contact.ToList().Count > 0)
                {
                    foreach (var item in Contact)
                    {
                        //ContactDTO.Id = item.Id;
                        //ContactDTO.Name = item.Name;
                        ContactDTO = Transform.ContactToDTO(item);
                    }
                    return ContactDTO;

                }
                else return ContactDTO;
            }
            catch
            {
                throw;
            }
        }
        //int GroupId,
        public static bool ImportContactsFromExcelFile(int ClientId, List<ContactDTO> ContactDTOList)
        {
            //List<ContactDTO> ContactDTOListNew = new List<ContactDTO>();
            //ContactDTOListNew = ContactDTOList;

            bool IsImported = false;
            try
            {
                ContactDTO ContactDTO = new ContactDTO();
                GroupDTO GroupDTO = new GroupDTO();
                // Create Group
                GroupDTO.Name = "MyGroup " + string.Format("{0:G}", System.DateTime.Now);
                GroupDTO.ClientID = ClientId;
                int GroupId = GroupService.Create(GroupDTO);

                if (GroupId != 0)
                {
                    GroupDTO = GroupService.GetById(GroupId);
                }
                else
                {
                    GroupDTO = null;
                }

                foreach (var item in ContactDTOList)
                {

                    ContactDTO = item;
                    ContactDTO.ClientId = ClientId;
                    if (ContactDTO.Groups == null && GroupDTO != null)
                    {
                        ContactDTO.Groups = new List<GroupDTO>();
                        ContactDTO.Groups.Add(GroupDTO);
                    }

                    bool IsMobileExist = SearchMobileNumber(ContactDTO.MobileNumber, ClientId, ContactDTO.Id);
                    if (IsMobileExist != true)
                    {
                        //Insert Contact information to Contact.
                        try
                        {
                            if (ContactDTO.IsValid == true)
                            {
                                ////Check Contact Present or not
                                //ContactDTO ContactDTOpresent = new ContactDTO();
                                //ContactDTOpresent = ContactService.GetContactByMobileNumberAndClientId(ContactDTO.MobileNumber, ClientId);
                                if (ContactDTO.Id == 0)
                                {
                                    int ContactId = Create(ContactDTO);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }

                    IsImported = true;
                }

                GroupDTO GroupDTOTemp = new GroupDTO();
                GroupDTOTemp.ContactCount = GroupContactService.GetGroupIdWiseContactsCount(GroupId, "");
                if (GroupDTOTemp.ContactCount == 0)
                {
                    GroupService.Delete(GroupId);
                }

                return IsImported;
            }
            catch (Exception e)
            {
                throw;
            }
        }
        public static List<ContactDTO> GetGroupIdWisePresentContactsByClientId(int ClientId, int GroupId, PagingInfo pagingInfo)
        {
            List<ContactDTO> contactDTOList = new List<ContactDTO>();
            List<ContactDTO> contactDTO = new List<ContactDTO>();
            try
            {
                UnitOfWork uow = new UnitOfWork();
                int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                int take = pagingInfo.ItemsPerPage;
                //IEnumerable<Contact> Contact = uow.ContactRepo.GetAll().Where(e => e.ClientId == ClientId).ToList();
                //var contacts = from c in Contact  where ( from g in c.Groups where g.Id ==GroupId select g ).Any() select c;

                Group Group = new Group();
                Group = uow.GroupRepo.GetById(GroupId); //, true
                IQueryable<ContactDTO> Contact = GroupContactService.GetGroupIdWiseContacts(Group.Id).AsQueryable();// Group.Contacts.AsQueryable();
                Contact = PagingService.Sorting<ContactDTO>(Contact, pagingInfo.SortBy, pagingInfo.Reverse);
                //Contact = Contact.Skip(skip).Take(take);
                if (Contact != null)
                {
                    foreach (var item in Contact)
                    {
                        ContactDTO ContactDTOobj = new ContactDTO();
                        ContactDTOobj = item;// Transform.ContactToDTO(item);
                        contactDTOList.Add(ContactDTOobj);
                    }

                    if (pagingInfo.Search != "" && pagingInfo.Search != null)
                    {
                        bool IsDate = CommonService.IsDate(pagingInfo.Search);
                        if (IsDate != true)
                        {
                            IQueryable<ContactDTO> Contactsearch = Contact.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();
                            //Group.Contacts
                            Contactsearch = PagingService.Sorting<ContactDTO>(Contactsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                            Contactsearch = Contactsearch.Skip(skip).Take(take);
                            foreach (var item in Contactsearch)
                            {
                                contactDTO.Add(item); //Transform.ContactToDTO(item)
                            }
                            return contactDTO;
                        }
                        else
                        {
                            DateTime date = Convert.ToDateTime(pagingInfo.Search);
                            IQueryable<ContactDTO> Contactsearch = Contact.Where(e => e.AnniversaryDate >= date && e.AnniversaryDate < date.AddDays(1) || e.BirthDate >= date && e.BirthDate < date.AddDays(1)).AsQueryable(); // .Skip(skip).Take(take); //.OrderBy(e => e.Name)
                            //Group.Contacts
                            Contactsearch = PagingService.Sorting<ContactDTO>(Contactsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                            Contactsearch = Contact.Skip(skip).Take(take);
                            foreach (var item in Contactsearch)
                            {
                                contactDTO.Add(item); //Transform.ContactToDTO(item)
                            }
                            return contactDTO;
                        }
                    }
                    else

                        return contactDTOList.Skip(skip).Take(take).ToList();
                }
                else
                {
                    //foreach (var item in Contact)
                    //{
                    //    contactDTOList.Add(Transform.ContactToDTO(item));
                    //}
                    return contactDTOList.Skip(skip).Take(take).ToList();
                }

                //GroupContactDTO GroupContactDTO = GroupService.GetGroupContactById(pagingInfo.GroupId);// uow.GroupRepo.GetAll().Where(e => e.ClientID == ClientId).ToList();
                //IQueryable<ContactDTO> ContactDTO = GroupContactDTO.Contacts.AsQueryable();// .Skip(skip).Take(take).ToList();

                //ContactDTO = PagingService.Sorting<ContactDTO>(ContactDTO, pagingInfo.SortBy, pagingInfo.Reverse);
                //ContactDTO = ContactDTO.Skip(skip).Take(take);

                //if (ContactDTO != null)
                //{
                //    contactDTOList = ContactDTO.ToList() ;
                //    if (pagingInfo.Search != "" && pagingInfo.Search != null)
                //    {
                //        bool IsDate = CommonService.IsDate(pagingInfo.Search);
                //        if (IsDate != true)
                //        {
                //            // string search
                //            IQueryable<ContactDTO> Contactsearch = contactDTOList.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();// .Skip(skip).Take(take); //.OrderBy(e => e.Name)
                //            Contactsearch = PagingService.Sorting<ContactDTO>(Contactsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                //            Contactsearch = Contactsearch.Skip(skip).Take(take);

                //            if (Contactsearch != null)
                //            {
                //                foreach (var item in Contactsearch)
                //                {
                //                    ContactDTO ContactDTOObj = new ContactDTO();
                //                    contactDTO.Add(item);
                //                }
                //            } return contactDTO;

                //        }
                //        else
                //        {
                //            //date wise search
                //            DateTime date = Convert.ToDateTime(pagingInfo.Search);
                //            IQueryable<ContactDTO> Contactsearch = contactDTOList.Where(e => e.AnniversaryDate >= date && e.AnniversaryDate < date.AddDays(1) || e.BirthDate >= date && e.BirthDate < date.AddDays(1)).AsQueryable(); // .Skip(skip).Take(take); //.OrderBy(e => e.Name)
                //            Contactsearch = PagingService.Sorting<ContactDTO>(Contactsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                //            Contactsearch = Contactsearch.Skip(skip).Take(take);
                //            if (Contactsearch != null)
                //            {
                //                foreach (var item in Contactsearch)
                //                {
                //                    ContactDTO ContactDTOObj = new ContactDTO();
                //                    contactDTO.Add(item);
                //                }
                //            }
                //            return contactDTO;
                //        }
                //    }
                //}
                //return contactDTOList ;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static String SplitMobile(string Mobile, EcouponCampaignDTO EcouponCampaignDTO)
        {
            string result = "";
            bool IsSent = false;
            int FalseCount = 0;
            try
            {
                string finalstring = "";

                // Input string contain separators.
                string value1 = Mobile;

                char[] delimiter1 = new char[] { ',', ';' };   // <-- Split on these
                // ... Use StringSplitOptions.RemoveEmptyEntries.
                string[] array2 = value1.Split(delimiter1,
                    StringSplitOptions.RemoveEmptyEntries);

                //Console.WriteLine();
                foreach (string mobile in array2)
                {
                    Console.WriteLine(mobile);
                    bool isMessageSent = CheckCampainLogByCampaingIdAndMobile(EcouponCampaignDTO.Id, Mobile);

                    ClientDTO ClientDTO = new ClientDTO();
                    ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);

                    //SMSGatewayDTO SMSGatewayDTO = new SMSGatewayDTO();
                    //SMSGatewayDTO = SMSGatewayService.GetById(ClientDTO.SMSGatewayId);

                    if (isMessageSent == false)
                    {
                        Console.Write("Send SMS");
                        //if (ClientDTO.SMSCredit > 0)
                        //{
                            int ecouponcode = 0;
                        createnew:
                            CommonService CommonService = new CommonService();
                            ecouponcode = CommonService.GetRandomNumber();
                            string ecouponcodelength = ecouponcode.ToString();
                            if (ecouponcodelength.Length < 6 || ecouponcodelength.Length > 6)
                            {
                                goto createnew;
                            }
                            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
                            CouponDTOList = CouponService.GetCouponListByCodeAndMobile(ecouponcode.ToString(), mobile);
                            if (CouponDTOList.Count == 0)
                            {
                                string Message = "";
                                EcouponCampaignDTO.Message = EcouponCampaignService.GetById(EcouponCampaignDTO.Id).Message;

                                //macros
                                List<Macros> MacrosList = Enum.GetValues(typeof(Macros)).Cast<Macros>().ToList();

                                ContactDTO ContactDTO = new ContactDTO();
                                ContactDTO = ContactService.GetContactByMobileNumberAndClientId(mobile, EcouponCampaignDTO.ClientId);

                                if (MacrosList.Count() > 0)
                                {
                                    foreach (var item in MacrosList)
                                    {

                                        if (item.ToString() == "FirstName")
                                        {
                                            string FirstName = "";
                                            FirstName = ContactDTO.FirstName;// CommonService.GetFirstname(ContactDTO.Name);
                                            EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", FirstName);
                                        }

                                        if (item.ToString() == "LastName")
                                        {
                                            string LastName = "";
                                            LastName = ContactDTO.LastName;// CommonService.GetLastname(ContactDTO.Name);
                                            EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", LastName);
                                        }

                                        if (item.ToString() == "BirthDate")
                                        {
                                            if (ContactDTO.BirthDate != null)
                                            {
                                                DateTime BirthDate = Convert.ToDateTime(ContactDTO.BirthDate);
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", BirthDate.ToString("dd-MMM"));
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "AnniversaryDate")
                                        {
                                            if (ContactDTO.AnniversaryDate != null)
                                            {
                                                DateTime AnniversaryDate = Convert.ToDateTime(ContactDTO.AnniversaryDate);
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", AnniversaryDate.ToString("dd-MMM"));
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "Email")
                                        {
                                            if (ContactDTO.Email != null)
                                            {
                                                string Email = ContactDTO.Email;
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", Email);
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "MobileNumber")
                                        {
                                            if (ContactDTO.MobileNumber != null)
                                            {
                                                string MobileNumber = ContactDTO.MobileNumber;
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", MobileNumber);
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "Gender")
                                        {
                                            if (ContactDTO.Gender != null)
                                            {
                                                string Gender = ContactDTO.Gender;

                                                //if (Gender == "0")
                                                //{
                                                //    Gender = "Male";
                                                //}
                                                //else
                                                //{
                                                //    Gender = "Female";
                                                //}

                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", Gender);
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "ExpiresOn")
                                        {
                                            if (EcouponCampaignDTO.ExpiresOn != null)
                                            {
                                                DateTime ExpiresOn = Convert.ToDateTime(EcouponCampaignDTO.ExpiresOn);
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ExpiresOn.ToString("dd-MMM-yy"));
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "Code")
                                        {
                                            string Code = ecouponcode.ToString();
                                            EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", Code);
                                        }

                                    }

                                    Message = EcouponCampaignDTO.Message;
                                    // Check the Message required credits and actual client credits
                                    double SMSMsgCount = GetMessageCount(Message);

                                    SettingDTO SettingDTO = new SettingDTO();
                                    SettingDTO = SettingService.GetById(1);
                                    SMSMsgCount = SMSMsgCount * SettingDTO.NationalCouponSMSCount;

                                    ////Check Credits
                                    //if (ClientDTO.SMSCredit >= SMSMsgCount)
                                    //{
                                        string sender = "";

                                        List<CouponDTO> CouponDTOListDuplicate = new List<CouponDTO>();
                                        CouponDTOListDuplicate = CouponService.GetCouponListByEcouponCampaignIdAndMobile(EcouponCampaignDTO.Id, mobile);
                                        if (CouponDTOListDuplicate.Count != 0)
                                        {
                                            ////If already sent then skip
                                            continue;
                                            ////foreach (var item in CouponDTOListDuplicate)
                                            ////{
                                            ////    if (item.IsExpired != true)
                                            ////    {
                                            ////        string MobileDuplicate = null;
                                            ////        CouponDTO CouponDTO = new CouponDTO();
                                            ////        CouponDTO = item;
                                            ////        CouponDTO.IsExpired=true;
                                            ////        CouponService.Edit(CouponDTO);

                                            ////        MobileDuplicate = item.MobileNumber;
                                            ////        Message = item.Message;
                                            ////        ecouponcode = Convert.ToInt32(item.Code);

                                            ////        if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                                            ////        {
                                            ////            sender = ClientDTO.SenderCode;
                                            ////        }
                                            ////        else
                                            ////        {

                                            ////            sender = "022751";
                                            ////        }

                                            ////        IsSent = ActualSmsSend(mobile, Message, sender, EcouponCampaignDTO, ClientDTO, ecouponcode.ToString());
                                            ////        continue;
                                            ////    }

                                            ////}

                                        }

                                        if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                                        {
                                            sender = ClientDTO.SenderCode;
                                        }
                                        else
                                        {

                                            sender = "022751";
                                        }

                                        IsSent = ActualSmsSend(mobile, Message, sender, EcouponCampaignDTO, ClientDTO, ecouponcode.ToString());
                                    //}
                                    //else goto nextprocess;

                                }

                                // Message = ReformatMsg(EcouponCampaignDTO.Message + " Your ecoupon code is " + ecouponcode + "");

                            }
                            else if (CouponDTOList.Count >= 1)
                            {
                                goto createnew;
                            }

                        //}
                        //else
                        //{
                        //    goto nextprocess;
                        //}

                    }
                    else
                    {

                    }
                }

                int TotatCouponSent = CouponService.GetCouponCountByEcouponCampaignId(EcouponCampaignDTO.Id);

                //if (EcouponCampaignDTO.RecipientsCount == TotatCouponSent)
                if (EcouponCampaignDTO.RecipientsCount <= TotatCouponSent)
                {
                    // Modify EcouponCampaign IsSent status
                    EcouponCampaignDTO.IsSent = true;
                    EcouponCampaignDTO.Message = EcouponCampaignService.GetById(EcouponCampaignDTO.Id).Message;
                    EcouponCampaignDTO.RequiredCredits = CouponService.GetECouponCampaignRequiredCreditsByEcouponCampaignId(EcouponCampaignDTO.Id);
                    EcouponCampaignDTO.CreditsDiffrence = EcouponCampaignDTO.ConsumedCredits - EcouponCampaignDTO.RequiredCredits;

                    if (EcouponCampaignDTO.ConsumedCredits != EcouponCampaignDTO.RequiredCredits)
                    {
                        if (EcouponCampaignDTO.CreditsDiffrence < 0)
                        {
                            //// deduct clients balance
                            ClientDTO ClientDTOUpdate = new ClientDTO();
                            ClientDTOUpdate = ClientService.GetById(EcouponCampaignDTO.ClientId);
                            ClientDTOUpdate.SMSCredit = ClientDTOUpdate.SMSCredit - (-(EcouponCampaignDTO.CreditsDiffrence));
                            ClientService.Edit(ClientDTOUpdate);

                            ////Reconcile Ecoupon Campaign
                            EcouponCampaignDTO.IsReconcile = true;
                            EcouponCampaignDTO.ReconcileDate = System.DateTime.Now;
                            EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);

                        }
                        else if (EcouponCampaignDTO.CreditsDiffrence > 0)
                        {
                            ////Add clients balance
                            ClientDTO ClientDTOUpdate = new ClientDTO();
                            ClientDTOUpdate = ClientService.GetById(EcouponCampaignDTO.ClientId);
                            ClientDTOUpdate.SMSCredit = ClientDTOUpdate.SMSCredit + EcouponCampaignDTO.CreditsDiffrence;
                            ClientService.Edit(ClientDTOUpdate);

                            ////Reconcile Ecoupon Campaign
                            EcouponCampaignDTO.IsReconcile = true;
                            EcouponCampaignDTO.ReconcileDate = System.DateTime.Now;
                            EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);

                        }
                    }
                    else if (EcouponCampaignDTO.CreditsDiffrence == 0)
                    {
                        EcouponCampaignDTO.IsReconcile = true;
                        EcouponCampaignDTO.ReconcileDate = System.DateTime.Now;
                        EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);
                    }

                   // EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);
                }

            nextprocess:
                result = finalstring;
            }

            catch (Exception ex)
            {

                result = "";

                using (FileStream file = new FileStream(Directory.GetCurrentDirectory() + "\\msgBlasterBackendService_Log.txt", FileMode.Append, FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(file);
                    streamWriter.WriteLine(System.DateTime.Now + " - " + "  SplitMobile()" + " - " + ex.Message);
                    streamWriter.Close();
                }

            }
            return result;
        }
        public static void ReadSqlite(int ClientId, string sqlitefile)
        {
            int NewGroupId = 0;
            int GrpId = 0;
            try
            {

                DataTable dtGroupContacts = GetGroupContacts(sqlitefile);
                int GroupId = 0;
                for (int i = 0; (i <= (dtGroupContacts.Rows.Count - 1)); i++)
                {
                    try
                    {
                        int ContactId = 0;

                        if (!Convert.IsDBNull(dtGroupContacts.Rows[i]["GroupID"].ToString()))
                        {
                            GroupId = Convert.ToInt32( dtGroupContacts.Rows[i]["GroupID"].ToString());
                        }
                        else
                        {
                            GroupId = 0;
                        }

                        if (!Convert.IsDBNull(dtGroupContacts.Rows[i]["ContactID"].ToString()))
                        {
                            ContactId = Convert.ToInt32(dtGroupContacts.Rows[i]["ContactID"].ToString());
                        }
                        else
                        {
                            ContactId = 0;
                        }

                        DataTable dtGroups = GetGroups(sqlitefile, GroupId);
                        for (int j = 0; (j <= (dtGroups.Rows.Count - 1)); j++)
                        {
                            try
                            {
                                //int chkGroupid = 0;
                                    string Groupname = "";
                                    if (!Convert.IsDBNull(dtGroups.Rows[j]["GroupName"].ToString()))
                                    {
                                        Groupname = dtGroups.Rows[j]["GroupName"].ToString();
                                    }
                                    else
                                    {
                                        Groupname = "";
                                    }

                                    //if (!Convert.IsDBNull(dtGroups.Rows[j]["GroupID"].ToString()))
                                    //{
                                    //    GrpId = Convert.ToInt32(dtGroups.Rows[j]["GroupID"].ToString());
                                    //}
                                    //else
                                    //{
                                    //    GrpId = 0;
                                    //}

                                    if (GroupId != GrpId)
                                    {
                                        // Check group name already present or not
                                        GroupDTO GroupDTOPresent = new GroupDTO();
                                        GroupDTOPresent = GroupService.GetGroupByNameAndClientId(Groupname, ClientId);
                                        if (GroupDTOPresent.Id == 0)
                                        {
                                            ////CreateGroup
                                            GroupDTO GroupDTO = new GroupDTO();
                                            GroupDTO.Name = Groupname;
                                            GroupDTO.ClientID = ClientId;
                                            NewGroupId = GroupService.Create(GroupDTO);
                                        }
                                        else
                                        {
                                            NewGroupId = GroupDTOPresent.Id;
                                        }

                                    }

                                    DataTable dtContacts = GetContactsById(sqlitefile, ContactId);

                                for (int k = 0; (k <= (dtContacts.Rows.Count - 1)); k++)
                                {
                                    try
                                    {
                                         string FirstName;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["FirstName"].ToString()))
                                         {
                                             FirstName = dtContacts.Rows[k]["FirstName"].ToString();
                                         }
                                         else
                                         {
                                             FirstName = "";
                                         }

                                         string LastName;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["LastName"].ToString()))
                                         {
                                             LastName = dtContacts.Rows[k]["LastName"].ToString();
                                         }
                                         else
                                         {
                                             LastName = "";
                                         }

                                         string FullName = FirstName + " " + LastName;

                                         string Mobile;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["MobileNumber"].ToString()))
                                         {
                                             Mobile = dtContacts.Rows[k]["MobileNumber"].ToString();
                                             if (Mobile == "") { continue; }
                                         }
                                         else
                                         {
                                             Mobile = "";
                                         }

                                         string Gender;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["Gender"].ToString()))
                                         {
                                             Gender = dtContacts.Rows[k]["Gender"].ToString();
                                             if (Gender == "")
                                             {
                                                 Gender = "Male";
                                             }
                                         }
                                         else
                                         {
                                             Gender = "Male";
                                         }

                                         string BirthDate;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["BirthDate"].ToString()))
                                         {
                                             BirthDate = dtContacts.Rows[k]["BirthDate"].ToString();
                                         }
                                         else
                                         {
                                             BirthDate =  null;
                                         }

                                         string Anniversary;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["Anniversary"].ToString()))
                                         {
                                             Anniversary = dtContacts.Rows[k]["Anniversary"].ToString();
                                         }
                                         else
                                         {
                                             Anniversary =  null;
                                         }

                                         string Email;
                                         if (!Convert.IsDBNull(dtContacts.Rows[k]["EmailID"].ToString()))
                                         {
                                             Email = dtContacts.Rows[k]["EmailID"].ToString();
                                         }
                                         else
                                         {
                                             Email = null;
                                         }

                                         //Check Contact Present or not
                                         ContactDTO ContactDTO = new ContactDTO();
                                         ContactDTO = ContactService.GetContactByMobileNumberAndClientId(Mobile, ClientId);
                                         if (ContactDTO.Id == 0)
                                         {
                                             ////Create Contact
                                             ContactDTO ContactDTONew = new ContactDTO();
                                             ContactDTONew.Name = FullName;
                                             if (CommonService.IsDate(BirthDate))
                                             {
                                                DateTime? bdate = Convert.ToDateTime( BirthDate);
                                                 ContactDTONew.BirthDate = bdate;
                                             }

                                             if (CommonService.IsDate(Anniversary))
                                             {
                                                 DateTime? anniversarydate = Convert.ToDateTime(Anniversary);
                                                 ContactDTONew.BirthDate = anniversarydate;
                                             }
                                             ContactDTONew.Email = Email;
                                             ContactDTONew.ClientId = ClientId;

                                             ContactDTONew.Gender = Gender;
                                             ContactDTONew.MobileNumber = Mobile;

                                             ContactDTONew.FirstName = FirstName;
                                             ContactDTONew.LastName = LastName;
                                             GroupDTO GroupDTO = GroupService.GetById(NewGroupId);
                                             List<GroupDTO> Groups = new List<GroupDTO>();
                                             Groups.Add(GroupDTO);
                                             ContactDTONew.Groups = Groups;

                                             if (ContactDTONew.FirstName.Length > 50)
                                             {
                                                 continue;
                                             }

                                             if (ContactDTONew.LastName.Length > 50)
                                             {
                                                 continue;
                                             }
                                             ContactService.Create(ContactDTONew);

                                         }
                                    }
                                    catch
                                    {
                                        continue;
                                    }

                                }

                                GrpId = GroupId;
                            }
                            catch
                            {
                                continue;
                            }
                        }

                    }
                    catch
                    {
                        continue;
                    }
                }

            }
            catch(Exception ex)
            {
                using (FileStream file = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\" + System.DateTime.Now.Date.ToString("dd-MMM-yyyy") + "_Log.txt", FileMode.Append, FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(file);
                    streamWriter.WriteLine(System.DateTime.Now + " - " + "ReadSqlite" + " - " + ex.Message.ToString());
                    streamWriter.Close();
                }
            }
        }
        //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;
        }
        public static String SplitMobile(string Mobile, CampaignDTO CampaignDTO)
        {
            string result = "";

            try
            {
                string finalstring = "";

                // Input string contain separators.
                string value1 = Mobile;

                char[] delimiter1 = new char[] { ',', ';' };   // <-- Split on these
                // ... Use StringSplitOptions.RemoveEmptyEntries.
                string[] array2 = value1.Split(delimiter1,
                    StringSplitOptions.RemoveEmptyEntries);

                //Console.WriteLine();
                foreach (string mobile in array2)
                {
                    Console.WriteLine(mobile);
                    bool isMessageSent = CheckCampainLogByCampaingIdAndMobile(CampaignDTO.Id, Mobile);

                    ClientDTO ClientDTO = new ClientDTO();
                    ClientDTO = ClientService.GetById(CampaignDTO.ClientId);

                    //SMSGatewayDTO SMSGatewayDTO = new SMSGatewayDTO();
                    //SMSGatewayDTO = SMSGatewayService.GetById(ClientDTO.SMSGatewayId);

                    if (isMessageSent == false)
                    {
                        Console.Write("Send SMS");
                        // Check the Message required credits and actual client credits
                        string message = "";
                        CampaignDTO.Message = CampaignService.GetById(CampaignDTO.Id).Message;
                        //// macros

                               List<Macros> MacrosList = Enum.GetValues(typeof(Macros)).Cast<Macros>().ToList();
                               ContactDTO ContactDTO = new ContactDTO();
                               ContactDTO = ContactService.GetContactByMobileNumberAndClientId(mobile, CampaignDTO.ClientId);

                                 if (MacrosList.Count() > 0)
                                    {
                                   foreach (var item in MacrosList)
                                   {

                                       if (item.ToString() == "FirstName")
                                       {
                                           string FirstName = "";
                                           FirstName = CommonService.GetFirstname(ContactDTO.Name);
                                           CampaignDTO.Message = CampaignDTO.Message.Replace("[" + item.ToString() + "]", FirstName);
                                       }

                                       if (item.ToString() == "LastName")
                                       {
                                           string LastName = "";
                                           LastName = CommonService.GetLastname(ContactDTO.Name);

                                           CampaignDTO.Message = CampaignDTO.Message.Replace("[" + item.ToString() + "]", LastName);
                                       }

                                   }

                                   message = CampaignDTO.Message;

                                   CampaignDTO.Message = ReformatMsg(message);
                                   int SMSMsgCount = GetMessageCount(message);

                                   if (ClientDTO.SMSCredit >= SMSMsgCount)
                                   {
                                       if (CampaignLogService.GetCampaignLogListByCampaignIdAndMobile(CampaignDTO.Id, mobile).Count != 0)
                                       {
                                           continue;
                                       }

                                       //CampaignDTO.Message
                                       string sender = "";
                                       if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                                       {
                                           sender = ClientDTO.SenderCode;
                                       }
                                       else
                                       {

                                           sender = "022751";
                                       }

                                       ActualSmsSend(mobile, CampaignDTO.Message, sender, CampaignDTO, ClientDTO);

                                   }
                                   else
                                   {
                                       goto nextprocess;
                                   }

                               }

                        //CampaignDTO.Message = ReformatMsg(message);
                        //int SMSMsgCount = GetMessageCount(message);

                        //if (ClientDTO.SMSCredit >= SMSMsgCount)
                        //{
                        //     //CampaignDTO.Message
                        //    ActualSmsSend(mobile, CampaignDTO.Message, SMSGatewayDTO.Name, CampaignDTO, ClientDTO);

                        //}
                        //else
                        //{
                        //    goto nextprocess;
                        //}

                    }
                    else
                    {

                    }
                }

                // Modify Campaign IsSent status
                CampaignDTO.IsSent = true;
                CampaignDTO.Message = CampaignService.GetById(CampaignDTO.Id).Message;
                CampaignService.EditCampaignFromBackend(CampaignDTO);

                nextprocess:
                result = finalstring;
            }

            catch (Exception ex)
            {

                result = "";

                using (FileStream file = new FileStream(Directory.GetCurrentDirectory() + "\\msgBlasterBackendService_Log.txt", FileMode.Append, FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(file);
                    streamWriter.WriteLine(System.DateTime.Now + " - " + "  SplitMobile()" + " - " + ex.Message);
                    streamWriter.Close();
                }

            }
            return result;
        }
        public static void SaveOtherContact(int Clientid, string sqlitefile)
        {
            DataTable dtContacts = GetAllContacts(sqlitefile);
            for (int k = 0; (k <= (dtContacts.Rows.Count - 1)); k++)
            {
                try
                {

                    string FirstName;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["FirstName"].ToString()))
                    {
                        FirstName = dtContacts.Rows[k]["FirstName"].ToString();
                    }
                    else
                    {
                        FirstName = "";
                    }

                    string LastName;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["LastName"].ToString()))
                    {
                        LastName = dtContacts.Rows[k]["LastName"].ToString();
                    }
                    else
                    {
                        LastName = "";
                    }

                    string FullName = FirstName + " " + LastName;

                    string Mobile;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["MobileNumber"].ToString()))
                    {
                        Mobile = dtContacts.Rows[k]["MobileNumber"].ToString();
                        if (Mobile == "") { continue; }
                    }
                    else
                    {
                        Mobile = "";
                    }

                    string Gender;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["Gender"].ToString()))
                    {
                        Gender = dtContacts.Rows[k]["Gender"].ToString();
                        if (Gender == "")
                        {
                            Gender = "Male";
                        }
                    }
                    else
                    {
                        Gender = "Male";
                    }

                    string BirthDate;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["BirthDate"].ToString()))
                    {
                        BirthDate = dtContacts.Rows[k]["BirthDate"].ToString();
                    }
                    else
                    {
                        BirthDate = null;
                    }

                    string Anniversary;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["Anniversary"].ToString()))
                    {
                        Anniversary = dtContacts.Rows[k]["Anniversary"].ToString();
                    }
                    else
                    {
                        Anniversary = null;
                    }

                    string Email;
                    if (!Convert.IsDBNull(dtContacts.Rows[k]["EmailID"].ToString()))
                    {
                        Email = dtContacts.Rows[k]["EmailID"].ToString();
                    }
                    else
                    {
                        Email = null;
                    }

                    //Check Contact Present or not
                    ContactDTO ContactDTO = new ContactDTO();
                    ContactDTO = ContactService.GetContactByMobileNumberAndClientId(Mobile, Clientid);
                    if (ContactDTO.Id == 0)
                    {
                        ////Create Contact
                        ContactDTO ContactDTONew = new ContactDTO();
                        ContactDTONew.Name = FullName;
                        if (CommonService.IsDate(BirthDate))
                        {
                            DateTime? bdate = Convert.ToDateTime(BirthDate);
                            ContactDTONew.BirthDate = bdate;
                        }

                        if (CommonService.IsDate(Anniversary))
                        {
                            DateTime? anniversarydate = Convert.ToDateTime(Anniversary);
                            ContactDTONew.BirthDate = anniversarydate;
                        }

                        ContactDTONew.FirstName = FirstName;
                        ContactDTONew.LastName = LastName;
                        ContactDTONew.Email = Email;
                        ContactDTONew.ClientId = Clientid;
                        ContactDTONew.Gender = Gender;
                        ContactDTONew.MobileNumber = Mobile;

                        if (ContactDTONew.FirstName.Length > 50)
                        {
                            continue;
                        }

                        if (ContactDTONew.LastName.Length > 50)
                        {
                            continue;
                        }

                        ContactService.Create(ContactDTONew);

                    }
                }
                catch
                {
                    continue;
                }

            }
        }
        //Edit Contact
        public static void Edit(ContactDTO contactDTO)
        {
            try
            {

                ContactDTO ContactDTORemoveGroup = new ContactDTO();
                ContactDTORemoveGroup = GetById(contactDTO.Id);

                GlobalSettings.LoggedInClientId = contactDTO.ClientId;
                int PartnerId = ClientService.GetById(contactDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                if (ContactDTORemoveGroup.Groups != null)
                {
                    foreach (var item in ContactDTORemoveGroup.Groups)
                    {
                        RemoveGroupFromContact(ContactDTORemoveGroup.Id, item.Id);
                    }
                }

                UnitOfWork uow = new UnitOfWork();
                Contact contact = Transform.ContactToDomain(contactDTO); ;
                uow.ContactRepo.Update(contact);

                if (contactDTO.Groups.Count != 0)
                {
                    //foreach (var item in contactDTO.Groups)
                    //{
                    //    RemoveGroupFromContact(contact.Id, item.Id);
                    //}

                    foreach (var item in contactDTO.Groups)
                    {
                        AddGroupToContact(contact.Id, item.Id);
                    }
                }

                uow.SaveChanges();
            }
            catch
            {
                throw;
            }
        }