예제 #1
0
        public override IViewComponentResult Invoke(object data = null)
        {
            var dataAsDict = data as Dictionary <string, object>;

            if (dataAsDict == null)
            {
                return(R.Success.ComponentResult);
            }
            var searchModel = dataAsDict["model"] as ProductSearchModel;

            if (searchModel == null)
            {
                return(R.Success.ComponentResult);
            }
            var startPrice = searchModel.AvailableFromPrice;
            var endPrice   = searchModel.AvailableToPrice;

            var model = new PriceFilterModel()
            {
                AvailableFromPrice = startPrice.FloorTen(),
                AvailableToPrice   = endPrice.CeilTen(),
            };

            model.FromPrice = searchModel.FromPrice ?? model.AvailableFromPrice;
            model.ToPrice   = searchModel.ToPrice ?? model.AvailableToPrice;
            return(R.Success.With("prices", model).ComponentResult);
        }
예제 #2
0
        public PricelistModel GetPricelist([FromBody] PriceFilterModel filter)
        {
            Argument.NotNull(filter, "Pricelist filter is required.");

            var productNames  = new List <string>();
            var productPrices = new List <PriceModel[]>();

            //var productRetailPrices = new List<decimal?[]>();

            using (var storage = new Storage())
            {
                var products = storage
                               .Products
                               .LoadWith(p => p.RawMaterial.Manufacturer)
                               .Where(
                    p => p.ManufacturerId == filter.ManufacturerId &&
                    p.RawMaterial.RawMaterialType.AlloyType == filter.AlloyType &&
                    p.RawMaterial.RawMaterialType.RollType == filter.RollType)
                               .Select(
                    p => new
                {
                    ProductId = p.ProductId,
                    Name      = p.ManufacturerId == p.RawMaterial.Manufacturer.ManufacturerId
                                                                ? p.Name
                                                                : $"{p.Name} ({p.RawMaterial.Manufacturer.Name})",
                    Thickness = p.Thickness,
                    Price     = 0,
                })
                               .ToList();

                var colNames = products
                               .Select(p => p.Thickness)
                               .Distinct()
                               .OrderBy(p => p)
                               .ToArray();

                foreach (var row in products.GroupBy(p => p.Name))
                {
                    var rowPrices = colNames
                                    .Select(
                        thickness =>
                    {
                        var product = row.SingleOrDefault(p => p.Thickness == thickness);
                        if (null == product)
                        {
                            return new PriceModel
                            {
                                Price       = null,
                                RetailPrice = null,
                            }
                        }
                        ;

                        return(new PriceModel
                        {
                            Price = ProductService.GetProductPrice(product.ProductId, filter.Date),
                            RetailPrice = ProductService.GetProductRetailPrice(product.ProductId, filter.Date),
                        });
                    })
                                    .ToArray();

                    if (rowPrices.All(p => null != p))
                    {
                        productNames.Add(row.Key);
                        productPrices.Add(rowPrices);
                    }
                }

                return(new PricelistModel
                {
                    Columns = colNames.Select(p => p.ToString()).ToArray(),
                    Rows = productNames.ToArray(),
                    Prices = productPrices.ToArray(),
                });
            }
        }