Compare commits

..

No commits in common. "4e7e88cb3f33296312b658d86e3ae6adcbe1e649" and "0fb113ffbda7539a60fdc3c4c63cfd55bbeb84f6" have entirely different histories.

4 changed files with 83 additions and 81 deletions

View File

@ -4,7 +4,6 @@ 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;
@ -18,18 +17,16 @@ 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, IShopStorage shopStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage)
{ {
_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)
@ -89,7 +86,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 (CheckThenSupplyMany(document, model.Count) == false) if (_shopLogic.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;
@ -107,67 +104,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 (_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,6 +116,66 @@ 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,5 +19,6 @@ 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,24 +85,27 @@ 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;
} }
var countStore = count; foreach (var shop in source.Shops)
{
var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id)); foreach (var doc in shop.ShopDocuments)
{
foreach (var doc in shopDocuments) if (doc.Value.Item1.Id == document.Id)
{ {
count -= doc.Value.Item2; count -= doc.Value.Item2;
}
if (count <= 0) if (count <= 0)
{ {
break; break;
} }
} }
}
if (count > 0) if (count > 0)
{ {
@ -111,8 +114,9 @@ namespace LawFirmFileImplement.Implements
count = countStore; count = countStore;
foreach (var shop in source.Shops) for (int i = 0; i < source.Shops.Count; i++)
{ {
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))
@ -136,13 +140,14 @@ namespace LawFirmFileImplement.Implements
MaxCountDocuments = shop.MaxCountDocuments, MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents ShopDocuments = documents
}); });
source.SaveShops(); source.SaveShops();
if (count <= 0) break;
} }
return count <= 0; if (count > 0)
{
return false;
}
return true;
} }
} }
} }