public virtual IPagedList <Customer> GetAllCustomersByPasswordFormat(PasswordFormat passwordFormat)
        {
            var q = new CustomerSearchQuery
            {
                PasswordFormat = passwordFormat,
                PageIndex      = 0,
                PageSize       = 500
            };

            var customers = SearchCustomers(q);

            return(customers);
        }
        public virtual IPagedList <Customer> GetOnlineCustomers(DateTime lastActivityFromUtc, int[] customerRoleIds, int pageIndex, int pageSize)
        {
            var q = new CustomerSearchQuery
            {
                LastActivityFromUtc = lastActivityFromUtc,
                CustomerRoleIds     = customerRoleIds,
                IsSystemAccount     = false,
                PageIndex           = pageIndex,
                PageSize            = pageSize
            };

            var customers = SearchCustomers(q);

            customers.AlterQuery(x => x.OrderByDescending(c => c.LastActivityDateUtc));

            return(customers);
        }
        public virtual IPagedList <Customer> SearchCustomers(CustomerSearchQuery q)
        {
            Guard.NotNull(q, nameof(q));

            var isOrdered = false;
            IQueryable <Customer> query = null;

            if (q.OnlyWithCart)
            {
                var cartItemQuery = _shoppingCartItemRepository.TableUntracked.Expand(x => x.Customer);

                if (q.CartType.HasValue)
                {
                    cartItemQuery = cartItemQuery.Where(x => x.ShoppingCartTypeId == (int)q.CartType.Value);
                }

                var groupQuery =
                    from sci in cartItemQuery
                    group sci by sci.CustomerId into grp
                    select grp
                    .OrderByDescending(x => x.CreatedOnUtc)
                    .Select(x => new
                {
                    x.Customer,
                    x.CreatedOnUtc
                })
                    .FirstOrDefault();

                // We have to sort again because of paging.
                query = groupQuery
                        .OrderByDescending(x => x.CreatedOnUtc)
                        .Select(x => x.Customer);

                isOrdered = true;
            }
            else
            {
                query = _customerRepository.Table;
            }

            if (q.Email.HasValue())
            {
                query = query.Where(c => c.Email.Contains(q.Email));
            }

            if (q.Username.HasValue())
            {
                query = query.Where(c => c.Username.Contains(q.Username));
            }

            if (q.CustomerNumber.HasValue())
            {
                query = query.Where(c => c.CustomerNumber.Contains(q.CustomerNumber));
            }

            if (q.AffiliateId.GetValueOrDefault() > 0)
            {
                query = query.Where(c => c.AffiliateId == q.AffiliateId.Value);
            }

            if (q.SearchTerm.HasValue())
            {
                if (_customerSettings.CompanyEnabled)
                {
                    query = query.Where(c => c.FullName.Contains(q.SearchTerm) || c.Company.Contains(q.SearchTerm));
                }
                else
                {
                    query = query.Where(c => c.FullName.Contains(q.SearchTerm));
                }
            }

            if (q.DayOfBirth.GetValueOrDefault() > 0)
            {
                query = query.Where(c => c.BirthDate.Value.Day == q.DayOfBirth.Value);
            }

            if (q.MonthOfBirth.GetValueOrDefault() > 0)
            {
                query = query.Where(c => c.BirthDate.Value.Month == q.MonthOfBirth.Value);
            }

            if (q.RegistrationFromUtc.HasValue)
            {
                query = query.Where(c => q.RegistrationFromUtc.Value <= c.CreatedOnUtc);
            }

            if (q.RegistrationToUtc.HasValue)
            {
                query = query.Where(c => q.RegistrationToUtc.Value >= c.CreatedOnUtc);
            }

            if (q.LastActivityFromUtc.HasValue)
            {
                query = query.Where(c => q.LastActivityFromUtc.Value <= c.LastActivityDateUtc);
            }

            if (q.CustomerRoleIds != null && q.CustomerRoleIds.Length > 0)
            {
                query = query.Where(c => c.CustomerRoleMappings.Select(rm => rm.CustomerRoleId).Intersect(q.CustomerRoleIds).Count() > 0);
            }

            if (q.Deleted.HasValue)
            {
                query = query.Where(c => c.Deleted == q.Deleted.Value);
            }

            if (q.Active.HasValue)
            {
                query = query.Where(c => c.Active == q.Active.Value);
            }

            if (q.IsSystemAccount.HasValue)
            {
                query = q.IsSystemAccount.Value == true
                    ? query.Where(c => !string.IsNullOrEmpty(c.SystemName))
                    : query.Where(c => string.IsNullOrEmpty(c.SystemName));
            }

            if (q.PasswordFormat.HasValue)
            {
                int passwordFormatId = (int)q.PasswordFormat.Value;
                query = query.Where(c => c.PasswordFormatId == passwordFormatId);
            }

            // Search by phone
            if (q.Phone.HasValue())
            {
                query = query
                        .Join(_gaRepository.Table, x => x.Id, y => y.EntityId, (x, y) => new { Customer = x, Attribute = y })
                        .Where(z => z.Attribute.KeyGroup == "Customer" &&
                               z.Attribute.Key == SystemCustomerAttributeNames.Phone &&
                               z.Attribute.Value.Contains(q.Phone))
                        .Select(z => z.Customer);
            }

            // Search by zip
            if (q.ZipPostalCode.HasValue())
            {
                query = query
                        .Join(_gaRepository.Table, x => x.Id, y => y.EntityId, (x, y) => new { Customer = x, Attribute = y })
                        .Where(z => z.Attribute.KeyGroup == "Customer" &&
                               z.Attribute.Key == SystemCustomerAttributeNames.ZipPostalCode &&
                               z.Attribute.Value.Contains(q.ZipPostalCode))
                        .Select(z => z.Customer);
            }

            if (!isOrdered)
            {
                query = query.OrderByDescending(c => c.CreatedOnUtc);
            }

            var customers = new PagedList <Customer>(query, q.PageIndex, q.PageSize);

            return(customers);
        }
Exemplo n.º 4
0
        public virtual IPagedList <Customer> SearchCustomers(CustomerSearchQuery q)
        {
            Guard.NotNull(q, nameof(q));

            var query = _customerRepository.Table;

            if (q.Email.HasValue())
            {
                query = query.Where(c => c.Email.Contains(q.Email));
            }

            if (q.Username.HasValue())
            {
                query = query.Where(c => c.Username.Contains(q.Username));
            }

            if (q.CustomerNumber.HasValue())
            {
                query = query.Where(c => c.CustomerNumber.Contains(q.CustomerNumber));
            }

            if (q.AffiliateId.GetValueOrDefault() > 0)
            {
                query = query.Where(c => c.AffiliateId == q.AffiliateId.Value);
            }

            if (q.SearchTerm.HasValue())
            {
                if (_customerSettings.CompanyEnabled)
                {
                    query = query.Where(c => c.FullName.Contains(q.SearchTerm) || c.Company.Contains(q.SearchTerm));
                }
                else
                {
                    query = query.Where(c => c.FullName.Contains(q.SearchTerm));
                }
            }

            if (q.DayOfBirth.GetValueOrDefault() > 0)
            {
                query = query.Where(c => c.BirthDate.Value.Day == q.DayOfBirth.Value);
            }

            if (q.MonthOfBirth.GetValueOrDefault() > 0)
            {
                query = query.Where(c => c.BirthDate.Value.Month == q.MonthOfBirth.Value);
            }

            if (q.RegistrationFromUtc.HasValue)
            {
                query = query.Where(c => q.RegistrationFromUtc.Value <= c.CreatedOnUtc);
            }

            if (q.RegistrationToUtc.HasValue)
            {
                query = query.Where(c => q.RegistrationToUtc.Value >= c.CreatedOnUtc);
            }

            if (q.LastActivityFromUtc.HasValue)
            {
                query = query.Where(c => q.LastActivityFromUtc.Value <= c.LastActivityDateUtc);
            }

            if (q.CustomerRoleIds != null && q.CustomerRoleIds.Length > 0)
            {
                query = query.Where(c => c.CustomerRoles.Select(cr => cr.Id).Intersect(q.CustomerRoleIds).Count() > 0);
            }

            if (q.Deleted.HasValue)
            {
                query = query.Where(c => c.Deleted == q.Deleted.Value);
            }

            if (q.IsSystemAccount.HasValue)
            {
                query = q.IsSystemAccount.Value == true
                                        ? query.Where(c => !string.IsNullOrEmpty(c.SystemName))
                                        : query.Where(c => string.IsNullOrEmpty(c.SystemName));
            }

            if (q.PasswordFormat.HasValue)
            {
                int passwordFormatId = (int)q.PasswordFormat.Value;
                query = query.Where(c => c.PasswordFormatId == passwordFormatId);
            }

            // Search by phone
            if (q.Phone.HasValue())
            {
                query = query
                        .Join(_gaRepository.Table, x => x.Id, y => y.EntityId, (x, y) => new { Customer = x, Attribute = y })
                        .Where((z => z.Attribute.KeyGroup == "Customer" &&
                                z.Attribute.Key == SystemCustomerAttributeNames.Phone &&
                                z.Attribute.Value.Contains(q.Phone)))
                        .Select(z => z.Customer);
            }

            // Search by zip
            if (q.ZipPostalCode.HasValue())
            {
                query = query
                        .Join(_gaRepository.Table, x => x.Id, y => y.EntityId, (x, y) => new { Customer = x, Attribute = y })
                        .Where((z => z.Attribute.KeyGroup == "Customer" &&
                                z.Attribute.Key == SystemCustomerAttributeNames.ZipPostalCode &&
                                z.Attribute.Value.Contains(q.ZipPostalCode)))
                        .Select(z => z.Customer);
            }

            if (q.OnlyWithCart)
            {
                int?sctId = null;
                if (q.CartType.HasValue)
                {
                    sctId = (int)q.CartType.Value;
                }

                query = q.CartType.HasValue ?
                        query.Where(c => c.ShoppingCartItems.Where(x => x.ShoppingCartTypeId == sctId).Count() > 0) :
                        query.Where(c => c.ShoppingCartItems.Count() > 0);
            }

            query = query.OrderByDescending(c => c.CreatedOnUtc);

            var customers = new PagedList <Customer>(query, q.PageIndex, q.PageSize);

            return(customers);
        }