Промежуточное сохранение.
This commit is contained in:
parent
a0d1981bee
commit
d377511cba
@ -20,10 +20,16 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
|
||||
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
private readonly IShopLogic _shopLogic;
|
||||
|
||||
private readonly IManufactureStorage _manufactureStorage;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IManufactureStorage manufactureStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_shopLogic = shopLogic;
|
||||
_manufactureStorage = manufactureStorage;
|
||||
}
|
||||
|
||||
//вывод отфильтрованного списка компонентов
|
||||
@ -122,7 +128,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
|
||||
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Order. OrderId:{Id}. Sun:{Sum}. ManufactureId:{Id}", model.Id, model.Sum, model.ManufactureId);
|
||||
_logger.LogInformation("Order. OrderId:{Id}. Sun:{Sum}. ManufactureId:{Id}. Sum:{Sum}", model.Id, model.Sum, model.ManufactureId, model.Sum);
|
||||
}
|
||||
|
||||
//обновление статуса заказа
|
||||
@ -146,9 +152,21 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
|
||||
model.Status = newOrderStatus;
|
||||
|
||||
//проверка на выдачу
|
||||
if (model.Status == OrderStatus.Выдан)
|
||||
if (model.Status == OrderStatus.Готов)
|
||||
{
|
||||
model.DateImplement = DateTime.Now;
|
||||
|
||||
var manufacture = _manufactureStorage.GetElement(new() { Id = viewModel.ManufactureId });
|
||||
|
||||
if (manufacture == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(manufacture));
|
||||
}
|
||||
|
||||
if (!_shopLogic.AddManufactures(manufacture, viewModel.Count))
|
||||
{
|
||||
throw new Exception($"AddManufactures operation failed. Shop is full.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -72,10 +72,10 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Количество добавляемого мороженого должно быть больше 0", nameof(count));
|
||||
throw new ArgumentNullException("Количество добавляемых изделий должно быть больше 0", nameof(count));
|
||||
}
|
||||
|
||||
_logger.LogInformation("AddIceCream. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id);
|
||||
_logger.LogInformation("AddManufacture. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id);
|
||||
var shop = _shopStorage.GetElement(model);
|
||||
|
||||
if (shop == null)
|
||||
@ -99,6 +99,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
|
||||
ShopName = shop.ShopName,
|
||||
Address = shop.Address,
|
||||
DateOpen = shop.DateOpen,
|
||||
MaxCountManufactures = shop.MaxCountManufactures,
|
||||
Manufactures = shop.Manufactures
|
||||
});
|
||||
|
||||
@ -181,5 +182,61 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
|
||||
throw new InvalidOperationException("Магазин с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
|
||||
public bool AddManufactures(IManufactureModel model, int count)
|
||||
{
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Количество добавляемых изделий должно быть больше 0", nameof(count));
|
||||
}
|
||||
|
||||
_logger.LogInformation("AddManufactures. Manufacture: {Manufacture}. Count: {Count}", model?.ManufactureName, count);
|
||||
|
||||
var capacity = _shopStorage.GetFullList().Select(x => x.MaxCountManufactures - x.Manufactures.Select(x => x.Value.Item2).Sum()).Sum() - count;
|
||||
|
||||
if (capacity < 0)
|
||||
{
|
||||
_logger.LogWarning("AddManufactures operation failed. Sell {count} Manufactures ", -capacity);
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var shop in _shopStorage.GetFullList())
|
||||
{
|
||||
if (shop.MaxCountManufactures - shop.Manufactures.Select(x => x.Value.Item2).Sum() < count)
|
||||
{
|
||||
if (!AddManufacture(new() { Id = shop.Id }, model, shop.MaxCountManufactures - shop.Manufactures.Select(x => x.Value.Item2).Sum()))
|
||||
{
|
||||
_logger.LogWarning("AddManufactures operation failed.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
count -= shop.MaxCountManufactures - shop.Manufactures.Select(x => x.Value.Item2).Sum();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AddManufacture(new() { Id = shop.Id }, model, count))
|
||||
{
|
||||
_logger.LogWarning("AddManufactures operation failed.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
count -= count;
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SellManufatures(IManufactureModel model, int count)
|
||||
{
|
||||
return _shopStorage.SellManufactures(model, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,6 @@ namespace BlacksmithWorkshopContracts.StoragesContracts
|
||||
|
||||
ShopViewModel? Delete(ShopBindingModel model);
|
||||
|
||||
bool SellManufactures(IManufactureModel, int count);
|
||||
bool SellManufactures(IManufactureModel model, int count);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ namespace BlacksmithWorkshopFileImplement
|
||||
|
||||
public List<Manufacture> Manufactures { get; private set; }
|
||||
|
||||
public List<Manufacture> Shops { get; private set; }
|
||||
public List<Shop> Shops { get; private set; }
|
||||
|
||||
public static DataFileSingleton GetInstance()
|
||||
{
|
||||
|
@ -96,25 +96,25 @@ namespace BlacksmithWorkshopFileImplement.Implements
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool SellIceCreams(IIceCreamModel model, int count)
|
||||
public bool SellManufactures(IManufactureModel model, int count)
|
||||
{
|
||||
if (_source.Shops.Select(x => x.IceCreams.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count)
|
||||
if (_source.Shops.Select(x => x.Manufactures.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var list = _source.Shops.Where(x => x.IceCreams.ContainsKey(model.Id));
|
||||
var list = _source.Shops.Where(x => x.Manufactures.ContainsKey(model.Id));
|
||||
|
||||
foreach (var shop in list)
|
||||
{
|
||||
if (shop.IceCreams[model.Id].Item2 < count)
|
||||
if (shop.Manufactures[model.Id].Item2 < count)
|
||||
{
|
||||
count -= shop.IceCreams[model.Id].Item2;
|
||||
shop.IceCreams[model.Id] = (shop.IceCreams[model.Id].Item1, 0);
|
||||
count -= shop.Manufactures[model.Id].Item2;
|
||||
shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.IceCreams[model.Id] = (shop.IceCreams[model.Id].Item1, shop.IceCreams[model.Id].Item2 - count);
|
||||
shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, shop.Manufactures[model.Id].Item2 - count);
|
||||
count -= count;
|
||||
}
|
||||
|
||||
@ -123,7 +123,7 @@ namespace BlacksmithWorkshopFileImplement.Implements
|
||||
ShopName = shop.ShopName,
|
||||
Address = shop.Address,
|
||||
DateOpen = shop.DateOpen,
|
||||
MaxCountManufactures = shop.MaxCountManufacturess,
|
||||
MaxCountManufactures = shop.MaxCountManufactures,
|
||||
Manufactures = shop.Manufactures
|
||||
});
|
||||
|
||||
|
@ -103,7 +103,7 @@ namespace BlacksmithWorkshopFileImplement.Models
|
||||
new XElement("Address", Address),
|
||||
new XElement("DateOpen", DateOpen.ToString()),
|
||||
new XElement("MaxCountManufactures", MaxCountManufactures.ToString()),
|
||||
new XElement("Manufactures", countManufature.Select(x => new XElement("Manufactures",
|
||||
new XElement("Manufactures", countManufacture.Select(x => new XElement("Manufactures",
|
||||
new XElement("Key", x.Key),
|
||||
new XElement("Value", x.Value))).ToArray()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user