public IEnumerable <SuiteCRMUserViewModel> GetUsers(int pageNumber, int pageSize)
        {
            List <SuiteCRMUserViewModel> returnValue = new List <SuiteCRMUserViewModel>();

            using (var connection = Helper.GetConnection(this.services, this.Logger))
                using (SuiteCRMUserUnitOfWork userRepository = new SuiteCRMUserUnitOfWork(connection))
                    using (SuiteCRMEmailAddressUnitOfWork emailRepository = new SuiteCRMEmailAddressUnitOfWork(connection))
                        using (SuiteCRMEmailAddressBeanUnitOfWork emailBeanRepository = new SuiteCRMEmailAddressBeanUnitOfWork(connection))
                        {
                            var suiteUsers          = userRepository.GetUsers(pageNumber, pageSize);
                            var ids                 = suiteUsers.Select(c => c.id).ToArray();
                            var suiteUserEmailBeans = emailBeanRepository.GetEmailAddressesBean(UsersBean, ids).ToArray();
                            var suiteUserEmails     = emailRepository.GetEmailAddresses(suiteUserEmailBeans.Select(c => c.email_address_id).ToArray()).ToList();

                            string[] usernames    = suiteUsers.Select(c => c.user_name).ToArray();
                            var      orchardUsers = this.services.ContentManager.HqlQuery <UserPart>().Where(c => c.ContentPartRecord <UserPartRecord>(), c => c.In("UserName", usernames)).List();

                            foreach (var suiteUser in suiteUsers)
                            {
                                var suiteCRMEmailBean = suiteUserEmailBeans.FirstOrDefault(c => c.bean_id == suiteUser.id);
                                if (suiteCRMEmailBean == null)
                                {
                                    continue;
                                }

                                var suiteCRMUserEmail = suiteUserEmails.FirstOrDefault(c => c.id == suiteCRMEmailBean.email_address_id);
                                if (suiteCRMUserEmail == null)
                                {
                                    continue;
                                }

                                SuiteCRMUserViewModel item = new SuiteCRMUserViewModel();
                                item.SuiteCRMUsername = suiteUser.user_name;
                                item.SuiteCRMUserId   = suiteUser.id;
                                item.SuiteCRMEmail    = suiteCRMUserEmail.email_address;

                                var user = orchardUsers.FirstOrDefault(c =>
                                                                       c.UserName.ToLower() == suiteUser.user_name.ToLower() &&
                                                                       c.Email != null &&
                                                                       c.Email.ToLower() == suiteCRMUserEmail.email_address.ToLower());
                                if (user != null)
                                {
                                    item.IsSync          = true;
                                    item.OrchardUsername = user.UserName;
                                    item.OrchardEmail    = user.Email;
                                    item.OrchardUserId   = user.Id;
                                }

                                returnValue.Add(item);
                            }
                        }

            return(returnValue);
        }
        public IEnumerable <SuiteCRMUserViewModel> CopySuiteCRMUsersToOrchard(CopySuiteCRMUsersToOrchardViewModel model)
        {
            List <SuiteCRMUserViewModel> returnValue = new List <SuiteCRMUserViewModel>();

            using (var connection = Helper.GetConnection(this.services, this.Logger))
                using (SuiteCRMUserUnitOfWork userRepository = new SuiteCRMUserUnitOfWork(connection))
                    using (SuiteCRMEmailAddressUnitOfWork emailRepository = new SuiteCRMEmailAddressUnitOfWork(connection))
                        using (SuiteCRMEmailAddressBeanUnitOfWork emailBeanRepository = new SuiteCRMEmailAddressBeanUnitOfWork(connection))
                        {
                            var ids                 = model.Users.Where(c => !string.IsNullOrEmpty(c.SuiteCRMUserId)).Select(c => c.SuiteCRMUserId).ToArray();
                            var suiteUsers          = userRepository.GetUsers(ids);
                            var suiteUserEmailBeans = emailBeanRepository.GetEmailAddressesBean(UsersBean, ids).ToArray();
                            var suiteUserEmails     = emailRepository.GetEmailAddresses(suiteUserEmailBeans.Select(c => c.email_address_id).ToArray()).ToList();
                            var orchardUsers        = this.services
                                                      .ContentManager
                                                      .GetMany <IUser>(model.Users.Where(c => c.OrchardUserId.HasValue).Select(c => c.OrchardUserId.Value), VersionOptions.Published, new QueryHints());

                            var operatorRole = this.GetOperatorRole();
                            foreach (var item in model.Users.Where(c => !string.IsNullOrEmpty(c.SuiteCRMUserId)))
                            {
                                var suiteCRMUser = suiteUsers.FirstOrDefault(c => c.id == item.SuiteCRMUserId);

                                if (suiteCRMUser == null)
                                {
                                    continue;
                                }

                                var suiteCRMEmailBean = suiteUserEmailBeans.FirstOrDefault(c => c.bean_id == item.SuiteCRMUserId);
                                if (suiteCRMEmailBean == null)
                                {
                                    continue;
                                }

                                var suiteCRMUserEmail = suiteUserEmails.FirstOrDefault(c => c.id == suiteCRMEmailBean.email_address_id);
                                if (suiteCRMUserEmail == null)
                                {
                                    continue;
                                }

                                IUser user = orchardUsers.FirstOrDefault(c =>
                                                                         c.UserName.ToLower() == suiteCRMUser.user_name.ToLower() ||
                                                                         c.Email.ToLower() == suiteCRMUserEmail.email_address.ToLower());

                                if (user != null)
                                {
                                    continue;
                                }

                                var newUser = this.membershipService.CreateUser(new CreateUserParams(
                                                                                    suiteCRMUser.user_name,
                                                                                    model.DefaultPassword,
                                                                                    suiteCRMUserEmail.email_address,
                                                                                    null, null, true));

                                SuiteCRMUserPart suiteCRMUserPart = newUser.As <SuiteCRMUserPart>();
                                suiteCRMUserPart.ExternalId   = suiteCRMUser.id;
                                suiteCRMUserPart.LastSyncTime = DateTime.UtcNow;

                                // Full name
                                var userPart = newUser.ContentItem.Parts.FirstOrDefault(d => d.PartDefinition.Name.ToLower(CultureInfo.InvariantCulture) == "user");
                                userPart.Store("FullName", string.Format("{0} {1}", suiteCRMUser.first_name, suiteCRMUser.last_name));

                                // role
                                if (operatorRole != null)
                                {
                                    UserRolesPartRecord newUserRole = new UserRolesPartRecord {
                                        UserId = newUser.Id, Role = new RoleRecord {
                                            Id = operatorRole.Id
                                        }
                                    };
                                    this.userRolesPartRecordRepository.Create(newUserRole);
                                }

                                this.services.ContentManager.Publish(newUser.ContentItem);
                                this.userRolesPartRecordRepository.Flush();

                                returnValue.Add(new SuiteCRMUserViewModel
                                {
                                    IsSync           = true,
                                    OrchardEmail     = newUser.Email,
                                    OrchardUserId    = newUser.Id,
                                    SuiteCRMUserId   = suiteCRMUser.id,
                                    SuiteCRMEmail    = suiteCRMUserEmail.email_address,
                                    SuiteCRMUsername = suiteCRMUser.user_name,
                                    OrchardUsername  = newUser.UserName
                                });
                            }
                        }

            return(returnValue);
        }