コード例 #1
0
        public virtual IList <ShoppingCartItem> GetShoppingCart(Customer customer, ShoppingCartType?shoppingCartType = null,
                                                                int storeId = 0, int?productId = null, DateTime?createdFromUtc = null, DateTime?createdToUtc = null)
        {
            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            var items = _sciRepository.Table.Where(sci => sci.CustomerId == customer.Id);

            //filter by type
            if (shoppingCartType.HasValue)
            {
                items = items.Where(item => item.ShoppingCartTypeId == (int)shoppingCartType.Value);
            }

            //filter shopping cart items by store
            if (storeId > 0 && !_shoppingCartSettings.CartsSharedBetweenStores)
            {
                items = items.Where(item => item.StoreId == storeId);
            }

            //filter shopping cart items by product
            if (productId > 0)
            {
                items = items.Where(item => item.ProductId == productId);
            }

            //filter shopping cart items by date
            if (createdFromUtc.HasValue)
            {
                items = items.Where(item => createdFromUtc.Value <= item.CreatedOnUtc);
            }
            if (createdToUtc.HasValue)
            {
                items = items.Where(item => createdToUtc.Value >= item.CreatedOnUtc);
            }

            var key = _cacheKeyService.PrepareKeyForShortTermCache(NopOrderDefaults.ShoppingCartCacheKey, customer, shoppingCartType, storeId, productId, createdFromUtc, createdToUtc);

            return(_staticCacheManager.Get(key, () => items.ToList()));
        }
コード例 #2
0
        /// <summary>
        /// Gets all customers
        /// </summary>
        /// <param name="createdFromUtc">Created date from (UTC); null to load all records</param>
        /// <param name="createdToUtc">Created date to (UTC); null to load all records</param>
        /// <param name="affiliateId">Affiliate identifier</param>
        /// <param name="vendorId">Vendor identifier</param>
        /// <param name="customerRoleIds">A list of customer role identifiers to filter by (at least one match); pass null or empty list in order to load all customers; </param>
        /// <param name="email">Email; null to load all customers</param>
        /// <param name="username">Username; null to load all customers</param>
        /// <param name="firstName">First name; null to load all customers</param>
        /// <param name="lastName">Last name; null to load all customers</param>
        /// <param name="dayOfBirth">Day of birth; 0 to load all customers</param>
        /// <param name="monthOfBirth">Month of birth; 0 to load all customers</param>
        /// <param name="company">Company; null to load all customers</param>
        /// <param name="phone">Phone; null to load all customers</param>
        /// <param name="zipPostalCode">Phone; null to load all customers</param>
        /// <param name="loadOnlyWithShoppingCart">Value indicating whther to load customers only with shopping cart</param>
        /// <param name="sct">Value indicating what shopping cart type to filter; userd when 'loadOnlyWithShoppingCart' param is 'true'</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <returns>Customer collection</returns>
        public virtual IPagedList <Customer> GetAllCustomers(DateTime?createdFromUtc       = null,
                                                             DateTime?createdToUtc         = null, int affiliateId = 0, int vendorId       = 0,
                                                             int[] customerRoleIds         = null, string email    = null, string username = null,
                                                             string firstName              = null, string lastName = null,
                                                             int dayOfBirth                = 0, int monthOfBirth   = 0,
                                                             string company                = null, string phone    = null, string zipPostalCode = null,
                                                             bool loadOnlyWithShoppingCart = false, ShoppingCartType?sct = null,
                                                             int pageIndex = 0, int pageSize = 2147483647)
        {
            var query = _customerRepository.Table;

            if (createdFromUtc.HasValue)
            {
                query = query.Where(c => createdFromUtc.Value <= c.CreatedOnUtc);
            }
            if (createdToUtc.HasValue)
            {
                query = query.Where(c => createdToUtc.Value >= c.CreatedOnUtc);
            }
            if (affiliateId > 0)
            {
                query = query.Where(c => affiliateId == c.AffiliateId);
            }
            if (vendorId > 0)
            {
                query = query.Where(c => vendorId == c.VendorId);
            }
            query = query.Where(c => !c.Deleted);
            if (customerRoleIds != null && customerRoleIds.Length > 0)
            {
                query = query.Where(c => c.CustomerRoles.Select(cr => cr.Id).Intersect(customerRoleIds).Any());
            }
            if (!String.IsNullOrWhiteSpace(email))
            {
                query = query.Where(c => c.Email.Contains(email));
            }
            if (!String.IsNullOrWhiteSpace(username))
            {
                query = query.Where(c => c.Username.Contains(username));
            }
            if (!String.IsNullOrWhiteSpace(firstName))
            {
                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.FirstName &&
                                z.Attribute.Value.Contains(firstName)))
                        .Select(z => z.Customer);
            }
            if (!String.IsNullOrWhiteSpace(lastName))
            {
                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.LastName &&
                                z.Attribute.Value.Contains(lastName)))
                        .Select(z => z.Customer);
            }
            //date of birth is stored as a string into database.
            //we also know that date of birth is stored in the following format YYYY-MM-DD (for example, 1983-02-18).
            //so let's search it as a string
            if (dayOfBirth > 0 && monthOfBirth > 0)
            {
                //both are specified
                string dateOfBirthStr = monthOfBirth.ToString("00", CultureInfo.InvariantCulture) + "-" + dayOfBirth.ToString("00", CultureInfo.InvariantCulture);
                //EndsWith is not supported by SQL Server Compact
                //so let's use the following workaround http://social.msdn.microsoft.com/Forums/is/sqlce/thread/0f810be1-2132-4c59-b9ae-8f7013c0cc00

                //we also cannot use Length function in SQL Server Compact (not supported in this context)
                //z.Attribute.Value.Length - dateOfBirthStr.Length = 5
                //dateOfBirthStr.Length = 5
                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.DateOfBirth &&
                                z.Attribute.Value.Substring(5, 5) == dateOfBirthStr))
                        .Select(z => z.Customer);
            }
            else if (dayOfBirth > 0)
            {
                //only day is specified
                string dateOfBirthStr = dayOfBirth.ToString("00", CultureInfo.InvariantCulture);
                //EndsWith is not supported by SQL Server Compact
                //so let's use the following workaround http://social.msdn.microsoft.com/Forums/is/sqlce/thread/0f810be1-2132-4c59-b9ae-8f7013c0cc00

                //we also cannot use Length function in SQL Server Compact (not supported in this context)
                //z.Attribute.Value.Length - dateOfBirthStr.Length = 8
                //dateOfBirthStr.Length = 2
                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.DateOfBirth &&
                                z.Attribute.Value.Substring(8, 2) == dateOfBirthStr))
                        .Select(z => z.Customer);
            }
            else if (monthOfBirth > 0)
            {
                //only month is specified
                string dateOfBirthStr = "-" + monthOfBirth.ToString("00", CultureInfo.InvariantCulture) + "-";
                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.DateOfBirth &&
                                z.Attribute.Value.Contains(dateOfBirthStr)))
                        .Select(z => z.Customer);
            }
            //search by company
            if (!String.IsNullOrWhiteSpace(company))
            {
                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.Company &&
                                z.Attribute.Value.Contains(company)))
                        .Select(z => z.Customer);
            }
            //search by phone
            if (!String.IsNullOrWhiteSpace(phone))
            {
                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(phone)))
                        .Select(z => z.Customer);
            }
            //search by zip
            if (!String.IsNullOrWhiteSpace(zipPostalCode))
            {
                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(zipPostalCode)))
                        .Select(z => z.Customer);
            }

            if (loadOnlyWithShoppingCart)
            {
                int?sctId = null;
                if (sct.HasValue)
                {
                    sctId = (int)sct.Value;
                }

                query = sct.HasValue ?
                        query.Where(c => c.ShoppingCartItems.Any(x => x.ShoppingCartTypeId == sctId)) :
                        query.Where(c => c.ShoppingCartItems.Any());
            }

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

            var customers = new PagedList <Customer>(query, pageIndex, pageSize);

            return(customers);
        }
コード例 #3
0
        /// <summary>
        /// Selects only customers with shopping carts and sorts by <see cref="Customer.CreatedOnUtc"/> descending.
        /// </summary>
        /// <param name="cartType">Type of cart to match. <c>null</c> to match any type.</param>
        public static IQueryable <Customer> ApplyHasCartFilter(this IQueryable <Customer> query, ShoppingCartType?cartType = null)
        {
            Guard.NotNull(query, nameof(query));

            var cartItemQuery = query
                                .GetDbContext <SmartDbContext>()
                                .ShoppingCartItems
                                .AsNoTracking()
                                .Include(x => x.Customer)
                                .AsQueryable();

            if (cartType.HasValue)
            {
                cartItemQuery = cartItemQuery.Where(x => x.ShoppingCartTypeId == (int)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);

            return(query);
        }
コード例 #4
0
        /// <summary>
        /// Gets all customers
        /// </summary>
        /// <param name="registrationFrom">Customer registration from; null to load all customers</param>
        /// <param name="registrationTo">Customer registration to; null to load all customers</param>
        /// <param name="customerRoleIds">A list of customer role identifiers to filter by (at least one match); pass null or empty list in order to load all customers; </param>
        /// <param name="email">Email; null to load all customers</param>
        /// <param name="username">Username; null to load all customers</param>
        /// <param name="firstName">First name; null to load all customers</param>
        /// <param name="lastName">Last name; null to load all customers</param>
        /// <param name="dayOfBirth">Day of birth; 0 to load all customers</param>
        /// <param name="monthOfBirth">Month of birth; 0 to load all customers</param>
        /// <param name="loadOnlyWithShoppingCart">Value indicating whther to load customers only with shopping cart</param>
        /// <param name="sct">Value indicating what shopping cart type to filter; userd when 'loadOnlyWithShoppingCart' param is 'true'</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <returns>Customer collection</returns>
        public virtual IPagedList <Customer> GetAllCustomers(DateTime?registrationFrom,
                                                             DateTime?registrationTo, int[] customerRoleIds, string email, string username,
                                                             string firstName, string lastName, int dayOfBirth, int monthOfBirth,
                                                             bool loadOnlyWithShoppingCart, ShoppingCartType?sct, int pageIndex, int pageSize)
        {
            var query = _customerRepository.Table;

            if (registrationFrom.HasValue)
            {
                query = query.Where(c => registrationFrom.Value <= c.CreatedOnUtc);
            }
            if (registrationTo.HasValue)
            {
                query = query.Where(c => registrationTo.Value >= c.CreatedOnUtc);
            }
            query = query.Where(c => !c.Deleted);
            if (customerRoleIds != null && customerRoleIds.Length > 0)
            {
                query = query.Where(c => c.CustomerRoles.Select(cr => cr.Id).Intersect(customerRoleIds).Count() > 0);
            }
            if (!String.IsNullOrWhiteSpace(email))
            {
                query = query.Where(c => c.Email.Contains(email));
            }
            if (!String.IsNullOrWhiteSpace(username))
            {
                query = query.Where(c => c.Username.Contains(username));
            }
            if (!String.IsNullOrWhiteSpace(firstName))
            {
                query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.FirstName && ca.Value.Contains(firstName)).Count() > 0);
            }
            if (!String.IsNullOrWhiteSpace(lastName))
            {
                query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.LastName && ca.Value.Contains(lastName)).Count() > 0);
            }
            //date of birth is stored as a string into database.
            //we also know that date of birth is stored in the following format YYYY-MM-DD (for example, 1983-02-18).
            //so let's search it as a string
            if (dayOfBirth > 0 && monthOfBirth > 0)
            {
                //both are specified
                string dateOfBirthStr = monthOfBirth.ToString("00", CultureInfo.InvariantCulture) + "-" + dayOfBirth.ToString("00", CultureInfo.InvariantCulture);
                //EndsWith is not supported by SQL Server Compact
                //query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.DateOfBirth && ca.Value.EndsWith(dateOfBirthStr)).Count() > 0);
                //so let's use the following workaround http://social.msdn.microsoft.com/Forums/is/sqlce/thread/0f810be1-2132-4c59-b9ae-8f7013c0cc00
                query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.DateOfBirth && ca.Value.Substring(ca.Value.Length - dateOfBirthStr.Length, dateOfBirthStr.Length) == dateOfBirthStr).Count() > 0);
            }
            else if (dayOfBirth > 0)
            {
                //only day is specified
                string dateOfBirthStr = dayOfBirth.ToString("00", CultureInfo.InvariantCulture);
                //EndsWith is not supported by SQL Server Compact
                //query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.DateOfBirth && ca.Value.EndsWith(dateOfBirthStr)).Count() > 0);
                //so let's use the following workaround http://social.msdn.microsoft.com/Forums/is/sqlce/thread/0f810be1-2132-4c59-b9ae-8f7013c0cc00
                query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.DateOfBirth && ca.Value.Substring(ca.Value.Length - dateOfBirthStr.Length, dateOfBirthStr.Length) == dateOfBirthStr).Count() > 0);
            }
            else if (monthOfBirth > 0)
            {
                //only month is specified
                string dateOfBirthStr = "-" + monthOfBirth.ToString("00", CultureInfo.InvariantCulture) + "-";
                query = query.Where(c => c.CustomerAttributes.Where(ca => ca.Key == SystemCustomerAttributeNames.DateOfBirth && ca.Value.Contains(dateOfBirthStr)).Count() > 0);
            }


            if (loadOnlyWithShoppingCart)
            {
                int?sctId = null;
                if (sct.HasValue)
                {
                    sctId = (int)sct.Value;
                }

                query = sct.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, pageIndex, pageSize);

            return(customers);
        }
コード例 #5
0
 /// <summary>
 /// Gets all customers
 /// </summary>
 /// <param name="createdFromUtc">Created date from (UTC); null to load all records</param>
 /// <param name="createdToUtc">Created date to (UTC); null to load all records</param>
 /// <param name="affiliateId">Affiliate identifier</param>
 /// <param name="vendorId">Vendor identifier</param>
 /// <param name="customerRoleIds">A list of customer role identifiers to filter by (at least one match); pass null or empty list in order to load all customers; </param>
 /// <param name="email">Email; null to load all customers</param>
 /// <param name="username">Username; null to load all customers</param>
 /// <param name="firstName">First name; null to load all customers</param>
 /// <param name="lastName">Last name; null to load all customers</param>
 /// <param name="dayOfBirth">Day of birth; 0 to load all customers</param>
 /// <param name="monthOfBirth">Month of birth; 0 to load all customers</param>
 /// <param name="company">Company; null to load all customers</param>
 /// <param name="phone">Phone; null to load all customers</param>
 /// <param name="zipPostalCode">Phone; null to load all customers</param>
 /// <param name="ipAddress">IP address; null to load all customers</param>
 /// <param name="loadOnlyWithShoppingCart">Value indicating whether to load customers only with shopping cart</param>
 /// <param name="sct">Value indicating what shopping cart type to filter; userd when 'loadOnlyWithShoppingCart' param is 'true'</param>
 /// <param name="pageIndex">Page index</param>
 /// <param name="pageSize">Page size</param>
 /// <returns>Customers</returns>
 public IAPIPagedList <Customer> GetAllCustomers(DateTime?createdFromUtc = null,
                                                 DateTime?createdToUtc   = null, int affiliateId               = 0, int vendorId       = 0,
                                                 string customerRoleIds  = null, string email                  = null, string username = null,
                                                 string firstName        = null, string lastName               = null,
                                                 int dayOfBirth          = 0, int monthOfBirth                 = 0,
                                                 string company          = null, string phone                  = null, string zipPostalCode = null,
                                                 string ipAddress        = null, bool loadOnlyWithShoppingCart = false, ShoppingCartType?sct = null,
                                                 int pageIndex           = 0, int pageSize = int.MaxValue)
 {
     int[] arrCustomerRoleIds = null;
     if (customerRoleIds != null)
     {
         arrCustomerRoleIds = customerRoleIds.Split(',').Select(x => Int32.Parse(x)).ToArray();
     }
     return(_customerService.GetAllCustomers(createdFromUtc, createdToUtc, affiliateId, vendorId, arrCustomerRoleIds, email, username, firstName, lastName, dayOfBirth, monthOfBirth, company, phone, zipPostalCode, ipAddress, loadOnlyWithShoppingCart, sct, pageIndex, pageSize).ConvertPagedListToAPIPagedList());
 }
コード例 #6
0
        /// <summary>
        /// Gets all customers
        /// </summary>
        /// <param name="createdFromUtc">Created date from (UTC); null to load all records</param>
        /// <param name="createdToUtc">Created date to (UTC); null to load all records</param>
        /// <param name="affiliateId">Affiliate identifier</param>
        /// <param name="vendorId">Vendor identifier</param>
        /// <param name="storeId">Store identifier</param>
        /// <param name="ownerId">Owner identifier</param>
        /// <param name="salesEmployeeId">Sales employee identifier</param>
        /// <param name="customerGroupIds">A list of customer group identifiers to filter by (at least one match); pass null or empty list in order to load all customers; </param>
        /// <param name="email">Email; null to load all customers</param>
        /// <param name="username">Username; null to load all customers</param>
        /// <param name="firstName">First name; null to load all customers</param>
        /// <param name="lastName">Last name; null to load all customers</param>
        /// <param name="dayOfBirth">Day of birth; 0 to load all customers</param>
        /// <param name="monthOfBirth">Month of birth; 0 to load all customers</param>
        /// <param name="company">Company; null to load all customers</param>
        /// <param name="phone">Phone; null to load all customers</param>
        /// <param name="zipPostalCode">Phone; null to load all customers</param>
        /// <param name="loadOnlyWithShoppingCart">Value indicating whether to load customers only with shopping cart</param>
        /// <param name="sct">Value indicating what shopping cart type to filter; userd when 'loadOnlyWithShoppingCart' param is 'true'</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <returns>Customers</returns>
        public virtual async Task <IPagedList <Customer> > GetAllCustomers(DateTime?createdFromUtc       = null,
                                                                           DateTime?createdToUtc         = null, string affiliateId      = "", string vendorId = "", string storeId = "", string ownerId = "",
                                                                           string salesEmployeeId        = "", string[] customerGroupIds = null, string[] customerTagIds = null, string email = null, string username = null,
                                                                           string firstName              = null, string lastName = null,
                                                                           string company                = null, string phone    = null, string zipPostalCode = null,
                                                                           bool loadOnlyWithShoppingCart = false, ShoppingCartType?sct = null,
                                                                           int pageIndex = 0, int pageSize = 2147483647, Expression <Func <Customer, object> > orderBySelector = null)
        {
            var querymodel = new GetCustomerQuery()
            {
                CreatedFromUtc           = createdFromUtc,
                CreatedToUtc             = createdToUtc,
                AffiliateId              = affiliateId,
                VendorId                 = vendorId,
                StoreId                  = storeId,
                OwnerId                  = ownerId,
                SalesEmployeeId          = salesEmployeeId,
                CustomerGroupIds         = customerGroupIds,
                CustomerTagIds           = customerTagIds,
                Email                    = email,
                Username                 = username,
                FirstName                = firstName,
                LastName                 = lastName,
                Company                  = company,
                Phone                    = phone,
                ZipPostalCode            = zipPostalCode,
                LoadOnlyWithShoppingCart = loadOnlyWithShoppingCart,
                Sct             = sct,
                PageIndex       = pageIndex,
                PageSize        = pageSize,
                OrderBySelector = orderBySelector
            };
            var query = await _mediator.Send(querymodel);

            return(await PagedList <Customer> .Create(query, pageIndex, pageSize));
        }
コード例 #7
0
 //BUGFIX 3.814
 public virtual IPagedList <Customer> GetAllCustomers(DateTime?createdFromUtc = null,
                                                      DateTime?createdToUtc   = null, int affiliateId               = 0, int vendorId       = 0,
                                                      int[] customerRoleIds   = null, string email                  = null, string username = null,
                                                      string firstName        = null, string lastName               = null,
                                                      int dayOfBirth          = 0, int monthOfBirth                 = 0,
                                                      string company          = null, string phone                  = null, string zipPostalCode = null,
                                                      string ipAddress        = null, bool loadOnlyWithShoppingCart = false, ShoppingCartType?sct = null,
                                                      int pageIndex           = 0, int pageSize = int.MaxValue)
 {
     return(GetAllCustomers(null, createdFromUtc,
                            createdToUtc, affiliateId, vendorId,
                            customerRoleIds, email, username,
                            firstName, lastName,
                            dayOfBirth, monthOfBirth,
                            company, phone, zipPostalCode,
                            ipAddress, loadOnlyWithShoppingCart, sct,
                            pageIndex, pageSize
                            ));
 }
コード例 #8
0
        /// <summary>
        /// Gets all customers
        /// </summary>
        /// <param name="createdFromUtc">Created date from (UTC); null to load all records</param>
        /// <param name="createdToUtc">Created date to (UTC); null to load all records</param>
        /// <param name="affiliateId">Affiliate identifier</param>
        /// <param name="vendorId">Vendor identifier</param>
        /// <param name="customerRoleIds">A list of customer role identifiers to filter by (at least one match); pass null or empty list in order to load all customers; </param>
        /// <param name="email">Email; null to load all customers</param>
        /// <param name="username">Username; null to load all customers</param>
        /// <param name="firstName">First name; null to load all customers</param>
        /// <param name="lastName">Last name; null to load all customers</param>
        /// <param name="dayOfBirth">Day of birth; 0 to load all customers</param>
        /// <param name="monthOfBirth">Month of birth; 0 to load all customers</param>
        /// <param name="company">Company; null to load all customers</param>
        /// <param name="phone">Phone; null to load all customers</param>
        /// <param name="zipPostalCode">Phone; null to load all customers</param>
        /// <param name="ipAddress">IP address; null to load all customers</param>
        /// <param name="loadOnlyWithShoppingCart">Value indicating whether to load customers only with shopping cart</param>
        /// <param name="sct">Value indicating what shopping cart type to filter; userd when 'loadOnlyWithShoppingCart' param is 'true'</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="getOnlyTotalCount">A value in indicating whether you want to load only total number of records. Set to "true" if you don't want to load data from database</param>
        /// <returns>Customers</returns>
        public virtual IPagedList <Customer> GetAllCustomers(DateTime?createdFromUtc = null,
                                                             DateTime?createdToUtc   = null, int affiliateId               = 0, int vendorId       = 0,
                                                             int[] customerRoleIds   = null, string email                  = null, string username = null,
                                                             string firstName        = null, string lastName               = null,
                                                             int dayOfBirth          = 0, int monthOfBirth                 = 0,
                                                             string company          = null, string phone                  = null, string zipPostalCode = null,
                                                             string ipAddress        = null, bool loadOnlyWithShoppingCart = false, ShoppingCartType?sct = null,
                                                             int pageIndex           = 0, int pageSize = int.MaxValue, bool getOnlyTotalCount = false)
        {
            var query = _customerRepository.Table;

            if (createdFromUtc.HasValue)
            {
                query = query.Where(c => createdFromUtc.Value <= c.CreatedOnUtc);
            }
            if (createdToUtc.HasValue)
            {
                query = query.Where(c => createdToUtc.Value >= c.CreatedOnUtc);
            }
            if (affiliateId > 0)
            {
                query = query.Where(c => affiliateId == c.AffiliateId);
            }
            if (vendorId > 0)
            {
                query = query.Where(c => vendorId == c.VendorId);
            }
            query = query.Where(c => !c.Deleted);

            if (customerRoleIds != null && customerRoleIds.Length > 0)
            {
                query = query.Join(_customerCustomerRoleMappingRepository.Table, x => x.Id, y => y.CustomerId,
                                   (x, y) => new { Customer = x, Mapping = y })
                        .Where(z => customerRoleIds.Contains(z.Mapping.CustomerRoleId))
                        .Select(z => z.Customer)
                        .Distinct();
            }

            if (!string.IsNullOrWhiteSpace(email))
            {
                query = query.Where(c => c.Email.Contains(email));
            }
            if (!string.IsNullOrWhiteSpace(username))
            {
                query = query.Where(c => c.Username.Contains(username));
            }
            if (!string.IsNullOrWhiteSpace(firstName))
            {
                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.FirstName &&
                                z.Attribute.Value.Contains(firstName)))
                        .Select(z => z.Customer);
            }
            if (!string.IsNullOrWhiteSpace(lastName))
            {
                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.LastName &&
                                z.Attribute.Value.Contains(lastName)))
                        .Select(z => z.Customer);
            }
            //date of birth is stored as a string into database.
            //we also know that date of birth is stored in the following format YYYY-MM-DD (for example, 1983-02-18).
            //so let's search it as a string
            if (dayOfBirth > 0 && monthOfBirth > 0)
            {
                //both are specified
                var dateOfBirthStr = monthOfBirth.ToString("00", CultureInfo.InvariantCulture) + "-" + dayOfBirth.ToString("00", CultureInfo.InvariantCulture);

                //z.Attribute.Value.Length - dateOfBirthStr.Length = 5
                //dateOfBirthStr.Length = 5
                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.DateOfBirth &&
                                z.Attribute.Value.Substring(5, 5) == dateOfBirthStr))
                        .Select(z => z.Customer);
            }
            else if (dayOfBirth > 0)
            {
                //only day is specified
                var dateOfBirthStr = dayOfBirth.ToString("00", CultureInfo.InvariantCulture);

                //z.Attribute.Value.Length - dateOfBirthStr.Length = 8
                //dateOfBirthStr.Length = 2
                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.DateOfBirth &&
                                z.Attribute.Value.Substring(8, 2) == dateOfBirthStr))
                        .Select(z => z.Customer);
            }
            else if (monthOfBirth > 0)
            {
                //only month is specified
                var dateOfBirthStr = "-" + monthOfBirth.ToString("00", CultureInfo.InvariantCulture) + "-";
                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.DateOfBirth &&
                                z.Attribute.Value.Contains(dateOfBirthStr)))
                        .Select(z => z.Customer);
            }
            //search by company
            if (!string.IsNullOrWhiteSpace(company))
            {
                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.Company &&
                                z.Attribute.Value.Contains(company)))
                        .Select(z => z.Customer);
            }
            //search by phone
            if (!string.IsNullOrWhiteSpace(phone))
            {
                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(phone)))
                        .Select(z => z.Customer);
            }
            //search by zip
            if (!string.IsNullOrWhiteSpace(zipPostalCode))
            {
                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(zipPostalCode)))
                        .Select(z => z.Customer);
            }

            //search by IpAddress
            if (!string.IsNullOrWhiteSpace(ipAddress) && CommonHelper.IsValidIpAddress(ipAddress))
            {
                query = query.Where(w => w.LastIpAddress == ipAddress);
            }

            if (loadOnlyWithShoppingCart)
            {
                int?sctId = null;
                if (sct.HasValue)
                {
                    sctId = (int)sct.Value;
                }

                query = sct.HasValue ?
                        query.Where(c => c.ShoppingCartItems.Any(x => x.ShoppingCartTypeId == sctId)) :
                        query.Where(c => c.ShoppingCartItems.Any());
            }

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

            var customers = new PagedList <Customer>(query, pageIndex, pageSize, getOnlyTotalCount);

            return(customers);
        }
コード例 #9
0
        /// <summary>
        /// 获取所有顾客
        /// </summary>
        /// <param name="createdFromUtc">创建起始日期; 为空加载全部记录</param>
        /// <param name="createdToUtc">创建截止日期; 为空加载全部记录</param>
        /// <param name="affiliateId">从属标识符</param>
        /// <param name="vendorId">供应商标识符</param>
        /// <param name="customerRoleIds">A list of customer role identifiers to filter by (at least one match); pass null or empty list in order to load all customers; </param>
        /// <param name="email">邮箱; 为空加载全部</param>
        /// <param name="username">用户; 为空加载全部</param>
        /// <param name="firstName">第一名称; 为空加载全部</param>
        /// <param name="lastName">第二名称; 为空加载全部</param>
        /// <param name="dayOfBirth">出生日; 0 加载全部</param>
        /// <param name="monthOfBirth">出生月; 0 加载全部</param>
        /// <param name="company">公司; 为空加载全部</param>
        /// <param name="phone">电话; 为空加载全部</param>
        /// <param name="zipPostalCode">邮编; 为空加载全部</param>
        /// <param name="loadOnlyWithShoppingCart">一个值,指示是否加载顾客,仅有购物车时。</param>
        /// <param name="sct">一个值,标识购物车类型,用于过滤。仅当“loadOnlyWithShoppingCartValue”为真时。 </param>
        /// <param name="pageIndex">页面索引</param>
        /// <param name="pageSize">页面大小 默认值为Int32.MaxValue</param>
        /// <returns>顾客分页列表</returns>
        public virtual IPagedList <Customer> GetAllCustomers(DateTime?createdFromUtc       = null,
                                                             DateTime?createdToUtc         = null, int affiliateId = 0, int vendorId       = 0,
                                                             int[] customerRoleIds         = null, string email    = null, string username = null,
                                                             string firstName              = null, string lastName = null,
                                                             int dayOfBirth                = 0, int monthOfBirth   = 0,
                                                             string company                = null, string phone    = null, string zipPostalCode = null,
                                                             bool loadOnlyWithShoppingCart = false, ShoppingCartType?sct = null,
                                                             int pageIndex = 0, int pageSize = 2147483647)
        {
            var query = _customerRepository.Table;

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

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

            if (affiliateId > 0)
            {
                query = query.Where(c => affiliateId == c.AffiliateId);
            }

            if (vendorId > 0)
            {
                query = query.Where(c => vendorId == c.VendorId);
            }

            //过滤,已删除
            query = query.Where(c => !c.Deleted);

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


            if (!String.IsNullOrWhiteSpace(email))
            {
                query = query.Where(c => c.Email.Contains(email));
            }

            if (!String.IsNullOrWhiteSpace(username))
            {
                query = query.Where(c => c.UserName.Contains(username));
            }

            if (!String.IsNullOrWhiteSpace(firstName))
            {
            }

            if (loadOnlyWithShoppingCart)
            {
                int?sctId = null;
                if (sct.HasValue)
                {
                    sctId = (int)sct.Value;
                }

                query = sct.HasValue ?
                        query.Where(c => c.ShoppingCartItems.Any(x => x.ShoppingCartTypeId == sctId)) :
                        query.Where(c => c.ShoppingCartItems.Any());
            }
            //排序
            query.OrderByDescending(c => c.CreatedOnUtc);

            var customers = new PagedList <Customer>(query, pageIndex, pageSize);

            return(customers);
        }