Exemple #1
0
 private void GetVendors()
 {
     Vendors.AddRange(new List <Vendor>()
     {
         new Models.Vendor
         {
             BusinessEntityID = 1,
             AccountNumber    = " ",
             Name             = "Special One",
             CreditRating     = 0,
             ActiveFlag       = true
         },
         new Models.Vendor
         {
             BusinessEntityID = 2,
             AccountNumber    = " ",
             Name             = "Special Two",
             CreditRating     = 0,
             ActiveFlag       = true
         },
         new Models.Vendor
         {
             BusinessEntityID = 3,
             AccountNumber    = " ",
             Name             = "Special Three",
             CreditRating     = 0,
             ActiveFlag       = false
         },
         new Models.Vendor
         {
             BusinessEntityID = 4,
             AccountNumber    = " ",
             Name             = "Special Four",
             CreditRating     = 0,
             ActiveFlag       = false
         },
         new Models.Vendor
         {
             BusinessEntityID = 5,
             AccountNumber    = " ",
             Name             = "Special Five",
             CreditRating     = 0,
             ActiveFlag       = true
         }
     });
 }
        public void Init()
        {
            if (Items.Any())
            {
                return;
            }

            var items = new[]
            {
                new Item {
                    MinimumAmount = 10, Name = "Eggs", Units = "Dozen"
                },
                new Item {
                    MinimumAmount = 15, Name = "Milk", Units = "Gallons"
                },
                new Item {
                    MinimumAmount = 20, Name = "Flour", Units = "Lbs"
                },
                new Item {
                    MinimumAmount = 5, Name = "Syrup", Units = "Gallons"
                },
                new Item {
                    MinimumAmount = 2, Name = "Chocolate", Units = "Lbs"
                },
                new Item {
                    MinimumAmount = 25, Name = "Strawberries", Units = "Bunches"
                },
                new Item {
                    MinimumAmount = 3, Name = "Ice Cream", Units = "Gallons"
                },
                new Item {
                    MinimumAmount = 10, Name = "Sugar", Units = "Lbs"
                },
                new Item {
                    MinimumAmount = 4, Name = "Corn Syrup", Units = "Gallons"
                },
                new Item {
                    MinimumAmount = 5, Name = "Sugar (Powdered)", Units = "Lbs"
                },
                new Item {
                    MinimumAmount = 10, Name = "Salt", Units = "Oz"
                }
            };
            var vendors = new[]
            {
                new Vendor
                {
                    City          = "Sacramento",
                    Name          = "Costco",
                    Country       = "United States",
                    Phone         = "123-456-7890",
                    State         = "CA",
                    StreetAddress = "1000 J St.",
                    ZipCode       = "93103"
                },
                new Vendor
                {
                    City          = "San Fransisco",
                    Name          = "USA Food",
                    Country       = "United States",
                    Phone         = "123-666-7890",
                    State         = "CA",
                    StreetAddress = "1300 Main St.",
                    ZipCode       = "90043"
                },
                new Vendor
                {
                    City          = "Sacramento",
                    Name          = "Whole Foods",
                    Country       = "United States",
                    Phone         = "555-456-0987",
                    State         = "CA",
                    StreetAddress = "100 H St.",
                    ZipCode       = "93103"
                }
            };

            var orders = new[]
            {
                new Order {
                    Status = OrderStatus.None, OrderDate = DateTime.Now
                },
                new Order {
                    Status = OrderStatus.Approved, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(1))
                },
                new Order {
                    Status = OrderStatus.Approved, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(2))
                },
                new Order {
                    Status = OrderStatus.Denied, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(3))
                },
                new Order {
                    Status = OrderStatus.Fulfilled, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(4))
                },
                new Order {
                    Status = OrderStatus.Denied, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(5))
                },
                new Order {
                    Status = OrderStatus.Fulfilled, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(6))
                },
                new Order {
                    Status = OrderStatus.Approved, OrderDate = DateTime.Now.Subtract(TimeSpan.FromDays(7))
                }
            };

            // Add dummy rows
            Items.AddRange(items);
            Vendors.AddRange(vendors);
            Orders.AddRange(orders);
            SaveChanges();

            // Go ahead and add the items to each vendor and such
            var dbItems   = Items.ToList();
            var dbVendors = Vendors.ToList();
            var dbOrders  = Orders.ToList();
            var products  = new List <Product>();

            for (var i = 0; i < dbItems.Count; ++i)
            {
                products.AddRange(from v in dbVendors.Where((x, ij) => ij % 10 != 0 || i % 2 == 0)
                                  let pkgAmt                                                       = Random.Next(6, 24)
                                                                     let price                     = Random.Next(25, 200) * pkgAmt
                                                                                           let sku = Guid.NewGuid().ToString()
                                                                                                     select
                                                                                                     new Product {
                    Item = dbItems[i], Vendor = v, PackageAmount = pkgAmt, Price = price, SKU = sku
                });
            }

            var orderItems = new List <OrderItem>();

            foreach (var t in dbOrders)
            {
                orderItems.AddRange(from t1 in dbItems
                                    select products.OrderBy(x => Guid.NewGuid()).First(x => x.Item == t1)
                                    into randProd
                                    let orderAmt                       = Random.Next(1, 4)
                                                              let paid = orderAmt * randProd.Price
                                                                         let totalAmt = orderAmt * randProd.PackageAmount
                                                                                        select new OrderItem
                {
                    Item        = randProd.Item,
                    ItemId      = randProd.Item.ItemId,
                    Vendor      = randProd.Vendor,
                    VendorId    = randProd.Vendor.VendorId,
                    Order       = t,
                    OrderId     = t.OrderId,
                    OrderAmount = orderAmt,
                    PaidPrice   = (int)Math.Ceiling((double)paid),
                    TotalAmount = (int)Math.Ceiling(totalAmt)
                });
            }

            var q =
                orderItems.GroupBy(x => new { x.Item, x.Order, x.Vendor })
                .Where(g => g.Count() > 1)
                .Select(y => y.Key)
                .ToList();

            foreach (var k in q)
            {
                Console.WriteLine($"Order {k.Order.OrderId} Name {k.Item.Name} Vendor {k.Vendor.Name}");
            }

            var newestFulfilledOrder = orders.OrderBy(x => x.OrderDate).First(x => x.Status == OrderStatus.Fulfilled);
            var stocks = (
                from oi in orderItems
                where oi.OrderId == newestFulfilledOrder.OrderId
                let prod = products.First(x => x.Item == oi.Item && x.Vendor == oi.Vendor)
                           select new Stock
            {
                Amount = oi.OrderAmount * prod.PackageAmount,
                Item = oi.Item,
                ItemId = oi.ItemId,
                Vendor = oi.Vendor,
                VendorId = oi.VendorId
            }).ToList();

            var shelfNums = GenerateSequence(1, stocks.Count + 1).ToList();

            foreach (var s in stocks)
            {
                s.Location = $"Shelf {shelfNums[Random.Next(0, shelfNums.Count)]}";
            }

            Products.AddRange(products);
            OrderItems.AddRange(orderItems);
            Stocks.AddRange(stocks);
            SaveChanges();
        }