PIbd-22_Chernyshev_G.Y._LabWork05_Hard #13
@ -6,6 +6,7 @@ using GarmentFactoryFileImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -128,26 +129,38 @@ namespace GarmentFactoryFileImplement.Implements
|
||||
|
||||
public bool RestockShops(SupplyBindingModel model)
|
||||
{
|
||||
if (model == null || source.Shops.Select(x => x.TextileMaxCount - x.ShopTextiles.Select(y => y.Value.Item2).Sum()).Sum() < model.Count)
|
||||
//подсчёт свободных мест через сумму свободных мест в каждом магазине
|
||||
//в каждом магазине кол-во свободных мест = макс. кол-во - занятые места
|
||||
int totalFreeSpace = source.Shops
|
||||
.Select(x => x.TextileMaxCount - x.ShopTextiles.Select(y => y.Value.Item2).Sum())
|
||||
.Sum();
|
||||
|
||||
//если места не хватает
|
||||
if (totalFreeSpace < model.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//для каждого магазина, где есть ещё место
|
||||
foreach (Shop shop in source.Shops.Where(shop => shop.TextileMaxCount - shop.ShopTextiles.Sum(x => x.Value.Item2) > 0))
|
||||
|
||||
foreach (Shop shop in source.Shops)
|
||||
{
|
||||
//подсчёт свободных мест
|
||||
int free_places = shop.TextileMaxCount - shop.ShopTextiles.Select(x => x.Value.Item2).Sum();
|
||||
free_places = Math.Min(free_places, model.Count);
|
||||
model.Count -= free_places;
|
||||
int freeSpace = shop.TextileMaxCount - shop.ShopTextiles.Select(x => x.Value.Item2).Sum();
|
||||
|
||||
if (freeSpace <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
freeSpace = Math.Min(freeSpace, model.Count);
|
||||
model.Count -= freeSpace;
|
||||
|
||||
if (shop.Textiles.ContainsKey(model.TextileId))
|
||||
{
|
||||
shop.Textiles[model.TextileId] += free_places;
|
||||
shop.Textiles[model.TextileId] += freeSpace;
|
||||
}
|
||||
|
||||
|
||||
else
|
||||
{
|
||||
shop.Textiles.Add(model.TextileId, free_places);
|
||||
shop.Textiles.Add(model.TextileId, freeSpace);
|
||||
}
|
||||
|
||||
shop.TextilesUpdate();
|
||||
|
Loading…
Reference in New Issue
Block a user