コード例 #1
0
        /// <summary>
        /// Finds top 10 customers who have the highest amount of transactions for a specific product inside the time period.
        /// Also returns a 11th item that contains the amount of transactions for the rest of the customers that didn't make it into the top 10.
        /// </summary>
        /// <param name="mvanumber">required</param>
        /// <param name="productID">required</param>
        /// <param name="startDateVal">required</param>
        /// <param name="endDateVal">required</param>
        /// <returns></returns>
        public List<CustomerTransactionCount> GetTransactionCountsForTopCustomers(int mvanumber, string productID, int startDateVal, int endDateVal)
        {
            var customers = new CustomerRepository().GetCustomersByMva(mvanumber);

            DateTime dtLimit = DateTime.UtcNow.Date.AddDays(-Constants.DaysToKeepTransactions);

            List<string> filters = new List<string>();
            filters.Add("mvaNumber eq " + mvanumber);
            filters.Add("date ge " + startDateVal);
            filters.Add("date le " + endDateVal);
            filters.Add("dateTime ge " + dtLimit.ToString(Constants.ODataDateFormat));
            filters.Add("productID eq '" + productID + "'");

            var sp = new SearchParameters();
            sp.Top = 0;
            sp.Filter = string.Join(" and ", filters);
            sp.Facets = new List<string> { "clientInternalID,count:1000" };

            DocumentSearchResult<TransactionSearchItem> response = TransactionIndexClient.Documents.Search<TransactionSearchItem>("*", sp);
            var facetItems = response.Facets.First().Value;
            var counts = facetItems.Select(f => new CustomerTransactionCount
            {
                CustomerInternalID = f.Value.ToString(),
                CustomerName = customers.FirstOrDefault(c => c.InternalID == new Guid(f.Value.ToString()))?.Name,
                Total = f.Count ?? 0
            }).ToList();
            
            filters = new List<string>();
            filters.Add("mvaNumber eq " + mvanumber);
            filters.Add("date ge " + startDateVal);
            filters.Add("date le " + endDateVal);
            filters.Add("productID eq '" + productID + "'");

            sp = new SearchParameters();
            sp.Top = 10000;
            sp.Filter = string.Join(" and ", filters);

            DocumentSearchResult<TotalsSearchItem> response2 = TotalsIndexClient.Documents.Search<TotalsSearchItem>("*", sp);

            while (true)
            {
                foreach (var res in response2.Results)
                {
                    string customerId = res.Document.ClientInternalID;

                    var cItem = counts.FirstOrDefault(itm => itm.CustomerInternalID == customerId);
                    if (cItem == null)
                    {
                        cItem = new CustomerTransactionCount
                        {
                            CustomerInternalID = customerId,
                            CustomerName = customers.FirstOrDefault(c => c.InternalID == new Guid(customerId))?.Name,
                        };

                        counts.Add(cItem);
                    }

                    cItem.Total += res.Document.Amount.Value;
                }

                if (response2.ContinuationToken == null)
                {
                    break;
                }

                response2 = TotalsIndexClient.Documents.ContinueSearch<TotalsSearchItem>(response2.ContinuationToken);
            }

            var orderedTotals = counts.OrderByDescending(c => c.Total);
            var retVal = orderedTotals.Take(10).ToList();

            retVal.Add(new CustomerTransactionCount
            {
                CustomerInternalID = null,
                CustomerName = null,
                Total = orderedTotals.Skip(10).Sum(f => f.Total)
            });
            
            return retVal;
        }