This commit is contained in:
Алексей Крюков 2024-04-22 11:48:48 +04:00
parent 558f70c3d8
commit af3573a8d8
3 changed files with 91 additions and 101 deletions

View File

@ -57,96 +57,22 @@ namespace TypographyBusinessLogic.BusinessLogics
}
return true;
}
public bool CheckThenSupplyMany(IPrintedModel printed, int count)
private void CheckModel(OrderBindingModel model)
{
if (count <= 0)
if (model == null)
{
_logger.LogWarning("Check then supply operation error. Printed count < 0.");
return false;
throw new ArgumentNullException(nameof(model));
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
if (model.Count <= 0)
{
freeSpace += shop.MaxCountPrinteds;
foreach (var c in shop.ShopPrinteds)
throw new ArgumentNullException("Количество заказов должно быть больше нуля");
}
if (model.Sum <= 0)
{
freeSpace -= c.Value.Item2;
throw new ArgumentNullException("Цена заказа должна быть больше нуля");
}
_logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}", model.Id, model.Sum);
}
if (freeSpace < count)
{
_logger.LogWarning("Check then supply operation error. There's no place for new printeds in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountPrinteds;
foreach (var c in shop.ShopPrinteds)
freeSpace -= c.Value.Item2;
if (freeSpace <= 0)
continue;
if (freeSpace >= count)
{
if (_shopLogic.MakeShipment(new ShopSearchModel() { Id = shop.Id }, printed, count))
count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace < count)
{
if (_shopLogic.MakeShipment(new ShopSearchModel() { Id = shop.Id }, printed, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != status - 1)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
OrderStatus oldStatus = model.Status;
model.Status = status;
if (model.Status == OrderStatus.Выдан)
model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
model.Status = oldStatus;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выполняется);
@ -162,33 +88,97 @@ namespace TypographyBusinessLogic.BusinessLogics
return ChangeStatus(model, OrderStatus.Выдан);
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
public bool CheckSupply(IPrintedModel printed, int count)
{
if (model == null)
if (count <= 0)
{
throw new ArgumentNullException(nameof(model));
_logger.LogWarning("Check supply operation error. Printed count < 0");
return false;
}
if (!withParams)
int Capacity = _shopStorage.GetFullList().Select(x => x.MaxCountPrinteds).Sum();
int currentCount = _shopStorage.GetFullList().Select(x => x.ShopPrinteds.Select(y => y.Value.Item2).Sum()).Sum();
int freeSpace = Capacity - currentCount;
if (freeSpace < count)
{
return;
_logger.LogWarning("Check supply error. No place for new Printeds");
return false;
}
if (model.PrintedId < 0)
foreach (var shop in _shopStorage.GetFullList())
{
throw new ArgumentNullException("Некорректный идентификатор изделий", nameof(model.PrintedId));
}
if (model.Count <= 0)
freeSpace = shop.MaxCountPrinteds;
foreach (var doc in shop.ShopPrinteds)
{
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
freeSpace -= doc.Value.Item2;
}
if (model.Sum <= 0)
if (freeSpace == 0)
{
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum));
continue;
}
if (model.Count <= 0)
if (freeSpace >= count)
{
throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count));
if (_shopLogic.MakeShipment(new()
{
Id = shop.Id
}, printed, count))
{
count = 0;
}
_logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id);
else
{
_logger.LogWarning("Supply error");
return false;
}
}
else
{
if (_shopLogic.MakeShipment(new() { Id = shop.Id }, printed, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
private bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{
_logger.LogWarning("Find order failed");
return false;
}
if (element.Status != status - 1)
{
_logger.LogWarning("Status change failed");
throw new InvalidOperationException("Невозможно перевести состояние заказа");
}
if (status == OrderStatus.Готов)
{
var printed = _printedStorage.GetElement(new PrintedSearchModel() { Id = model.PrintedId });
if (printed == null)
{
_logger.LogWarning("Status change error. Printed not found");
return false;
}
if (!CheckSupply(printed, model.Count))
{
_logger.LogWarning("Status change error. Shop doesnt have printedes");
return false;
}
}
model.Status = status;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
return true;
}
}
}

View File

@ -182,7 +182,7 @@ namespace TypographyBusinessLogic
if (model.MaxCountPrinteds < 0)
{
throw new InvalidOperationException("Отрицательное максимальное количество авто!");
throw new InvalidOperationException("Отрицательное максимальное количество изделий!");
}
_logger.LogInformation("Shop. Name: {0}, Address: {1}, ID: {2}", model.ShopName, model.Address, model.Id);

View File

@ -93,10 +93,10 @@ namespace TypographyFileImplement.Implements
var shopPrinteds = source.Shops.SelectMany(shop => shop.ShopPrinteds.Where(c => c.Value.Item1.Id == printed.Id));
int countStore = 0;
foreach (var it in shopPrinteds)
countStore += it.Value.Item2;
int countStore = source.Shops
.SelectMany(x => x.ShopPrinteds)
.Where(y => y.Key == model.Id)
.Sum(y => y.Value.Item2);
if (count > countStore)
return false;