Compare commits

...

6 Commits

4 changed files with 81 additions and 83 deletions

View File

@ -4,6 +4,7 @@ using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts; using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels; using LawFirmContracts.ViewModels;
using LawFirmDataModels.Enums; using LawFirmDataModels.Enums;
using LawFirmDataModels.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -17,16 +18,18 @@ namespace LawFirmBusinessLogic.BusinessLogics
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic; private readonly IShopLogic _shopLogic;
private readonly IDocumentStorage _documentStorage; private readonly IDocumentStorage _documentStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
_shopLogic = shopLogic; _shopLogic = shopLogic;
_documentStorage = documentStorage; _documentStorage = documentStorage;
_shopStorage = shopStorage;
} }
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
@ -86,7 +89,7 @@ namespace LawFirmBusinessLogic.BusinessLogics
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found.");
return false; return false;
} }
if (_shopLogic.CheckThenSupplyMany(document, model.Count) == false) if (CheckThenSupplyMany(document, model.Count) == false)
{ {
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error.");
return false; return false;
@ -104,6 +107,67 @@ namespace LawFirmBusinessLogic.BusinessLogics
return true; return true;
} }
public bool CheckThenSupplyMany(IDocumentModel document, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Document count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new docs in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.SupplyDocuments(new() { Id = shop.Id }, document, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool TakeOrderInWork(OrderBindingModel model) public bool TakeOrderInWork(OrderBindingModel model)
{ {
return StatusUpdate(model, OrderStatus.Выполняется); return StatusUpdate(model, OrderStatus.Выполняется);

View File

@ -116,66 +116,6 @@ namespace LawFirmBusinessLogic.BusinessLogics
} }
return true; return true;
} }
public bool CheckThenSupplyMany(IDocumentModel document, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Document count < 0.");
return false;
}
int freeSpace = 0;
foreach(var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new docs in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (SupplyDocuments(new() { Id = shop.Id}, document, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count-= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool SellDocument(IDocumentModel document, int count) public bool SellDocument(IDocumentModel document, int count)
{; {;

View File

@ -19,6 +19,5 @@ namespace LawFirmContracts.BusinessLogicContracts
bool Delete(ShopBindingModel model); bool Delete(ShopBindingModel model);
bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count); bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count);
bool SellDocument(IDocumentModel document, int count); bool SellDocument(IDocumentModel document, int count);
bool CheckThenSupplyMany(IDocumentModel document, int count);
} }
} }

View File

@ -85,25 +85,22 @@ namespace LawFirmFileImplement.Implements
{ {
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
var countStore = count;
if (document == null) if (document == null)
{ {
return false; return false;
} }
foreach (var shop in source.Shops) var countStore = count;
var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id));
foreach (var doc in shopDocuments)
{ {
foreach (var doc in shop.ShopDocuments) count -= doc.Value.Item2;
if (count <= 0)
{ {
if (doc.Value.Item1.Id == document.Id) break;
{
count -= doc.Value.Item2;
}
if (count <= 0)
{
break;
}
} }
} }
@ -114,9 +111,8 @@ namespace LawFirmFileImplement.Implements
count = countStore; count = countStore;
for (int i = 0; i < source.Shops.Count; i++) foreach (var shop in source.Shops)
{ {
var shop = source.Shops[i];
var documents = shop.ShopDocuments; var documents = shop.ShopDocuments;
foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id)) foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id))
@ -140,14 +136,13 @@ namespace LawFirmFileImplement.Implements
MaxCountDocuments = shop.MaxCountDocuments, MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents ShopDocuments = documents
}); });
source.SaveShops(); source.SaveShops();
if (count <= 0) break;
} }
if (count > 0) return count <= 0;
{
return false;
}
return true;
} }
} }
} }