예제 #1
0
        public void Build(SalesRecord salesRecord, IEnumerable <Price> prices, decimal subTotal, int batchId)
        {
            var rand = RandomContext.Instance.RandomNumber;

            var subPrices = prices.Where(p => p.Category == Category).ToArray();
            var total     = salesRecord.Prices.Count();

            if (subPrices.Length == 0)
            {
                return;
            }

            foreach (var item in subPrices)
            {
                for (int i = 0; i < total; i++)
                {
                    var picker = rand.Next(0, 100);
                    if (picker < item.ShowRate)
                    {
                        salesRecord.AddPrice(new OutputPrice()
                        {
                            BatchId   = batchId,
                            ProductId = item.Id,
                            Service   = item.Service,
                            UnitPrice = 0,
                            Category  = item.Category,
                            Quantity  = 1,
                            Discount  = item.UnitPrice
                        });
                        _logger.Info(
                            $"picker: {picker} Picked item: {item.Service} price: {item.UnitPrice}");
                    }
                }
            }
        }
예제 #2
0
        public void Build(SalesRecord salesRecord, IEnumerable <Price> prices, ProductCategory category, int batchId)
        {
            _logger.Info($"Processing Ramaining: {salesRecord.Remaining}");
            while (true)
            {
                var item = prices.OrderBy(p => p.UnitPrice).FirstOrDefault(p => p.Category == category && p.UnitPrice > salesRecord.Remaining);
                if (item == null)
                {
                    _logger.Info($"Couldn't find best price item, now try to pick again.");

                    var picker = _categoryPickers.Single(p => p.Category == category);

                    picker.Build(salesRecord, prices, salesRecord.Remaining, batchId);
                }
                else
                {
                    var pickedItem = new OutputPrice()
                    {
                        BatchId   = batchId,
                        Service   = item.Service,
                        UnitPrice = salesRecord.Remaining,
                        Category  = item.Category,
                        Quantity  = 1,
                        Discount  = item.UnitPrice - salesRecord.Remaining
                    };

                    salesRecord.AddPrice(pickedItem);
                    _logger.Info(
                        $"Remaining picker picked item: {pickedItem.Service} price: {pickedItem.UnitPrice} " +
                        $"after discount {pickedItem.Discount} remaining: {salesRecord.Remaining}");
                    return;
                }
            }
        }
예제 #3
0
        public void Build(SalesRecord salesRecord, IEnumerable <Price> prices, decimal subTotal, int batchId)
        {
            var rand      = RandomContext.Instance.RandomNumber;
            var remaining = subTotal;
            var subPrices = prices.Where(p => p.Category == Category).ToArray();

            var totalChance = subPrices.Sum(p => p.ShowRate);

            while (remaining > 0)
            {
                var picker  = rand.Next(0, totalChance);
                var pointer = 0;
                for (int i = 0; i < subPrices.Length; i++)
                {
                    pointer += subPrices[i].ShowRate;
                    if (picker < pointer)
                    {
                        var item = subPrices[i];
                        if (item.UnitPrice > remaining)
                        {
                            return;
                        }
                        salesRecord.AddPrice(new OutputPrice()
                        {
                            BatchId   = batchId,
                            ProductId = item.Id,
                            Service   = item.Service,
                            UnitPrice = item.UnitPrice,
                            Category  = item.Category,
                            Quantity  = 1,
                            Discount  = 0
                        });
                        remaining -= item.UnitPrice;
                        _logger.Info(
                            $"picker: {picker} Picked item: {item.Service} price: {item.UnitPrice} remaining: {remaining}");
                        break;
                    }
                }
            }

            return;
        }