PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairBusinessLogic/BusinessLogics/StoreLogic.cs

238 lines
8.2 KiB
C#
Raw Normal View History

2024-05-05 00:19:00 +04:00
using Microsoft.Extensions.Logging;
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlumbingRepairBusinessLogic.BusinessLogics
{
public class StoreLogic : IStoreLogic
{
private readonly ILogger _logger;
private readonly IStoreStorage _storeStorage;
public StoreLogic(ILogger<StoreLogic> logger, IStoreStorage storeStorage)
{
_logger = logger;
_storeStorage = storeStorage;
}
public bool AddWork(StoreSearchModel model, IWorkModel work, int quantity)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (quantity <= 0)
{
2024-05-05 21:39:31 +04:00
throw new ArgumentException("Количество изделий должно быть больше 0", nameof(quantity));
2024-05-05 00:19:00 +04:00
}
2024-05-05 21:39:31 +04:00
_logger.LogInformation("AddWork. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
2024-05-05 00:19:00 +04:00
var element = _storeStorage.GetElement(model);
if (element == null)
{
2024-05-05 21:39:31 +04:00
_logger.LogWarning("AddWork element not found");
2024-05-05 00:19:00 +04:00
return false;
}
2024-05-05 21:39:31 +04:00
if (element.WorkMaxCount - element.StoreWorks.Select(x => x.Value.Item2).Sum() < quantity)
{
throw new ArgumentNullException("Магазин переполнен", nameof(quantity));
}
if (element.StoreWorks.TryGetValue(work.Id, out var pair))
2024-05-05 00:19:00 +04:00
{
2024-05-05 21:39:31 +04:00
element.StoreWorks[work.Id] = (work, quantity + pair.Item2);
_logger.LogInformation("AddWork. Added {quantity} {work} to '{StoreName}' store", quantity, work.WorkName, element.StoreName);
2024-05-05 00:19:00 +04:00
}
else
{
2024-05-05 21:39:31 +04:00
element.StoreWorks[work.Id] = (work, quantity);
2024-05-06 11:24:06 +04:00
_logger.LogInformation("AddWork. Added {quantity} new work {work} to '{StoreName}' store", quantity, work.WorkName, element.StoreName);
2024-05-05 00:19:00 +04:00
}
_storeStorage.Update(new()
{
Id = element.Id,
StoreAdress = element.StoreAdress,
StoreName = element.StoreName,
OpeningDate = element.OpeningDate,
2024-05-05 21:39:31 +04:00
WorkMaxCount = element.WorkMaxCount,
StoreWorks = element.StoreWorks
2024-05-05 00:19:00 +04:00
});
return true;
}
2024-05-05 21:39:31 +04:00
public bool AddWork(IWorkModel work, int quantity)
{
if (work == null)
{
throw new ArgumentNullException(nameof(work));
}
if (quantity <= 0)
{
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
}
var freePlaces = _storeStorage.GetFullList()
.Select(x => x.WorkMaxCount - x.StoreWorks
.Select(p => p.Value.Item2).Sum()).Sum() - quantity;
if (freePlaces < 0)
{
_logger.LogInformation("AddWork. Failed to add work to store. It's full.");
return false;
}
foreach (var store in _storeStorage.GetFullList())
{
var temp = Math.Min(quantity, store.WorkMaxCount - store.StoreWorks.Select(x => x.Value.Item2).Sum());
if (temp <= 0)
{
continue;
}
if (!AddWork(new() { Id = store.Id }, work, temp))
{
_logger.LogWarning("An error occurred while adding work to stores");
return false;
}
quantity -= temp;
if (quantity == 0)
{
return true;
}
}
return true;
}
2024-05-05 00:19:00 +04:00
public bool Create(StoreBindingModel model)
{
CheckModel(model);
2024-05-05 21:39:31 +04:00
model.StoreWorks = new();
2024-05-05 00:19:00 +04:00
if (_storeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(StoreBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_storeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public StoreViewModel? ReadElement(StoreSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
var element = _storeStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<StoreViewModel>? ReadList(StoreSearchModel? model)
{
_logger.LogInformation("ReadList. StoreName:{StoreName}.Id:{ Id} ", model?.StoreName, model?.Id);
var list = (model == null) ? _storeStorage.GetFullList() : _storeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2024-05-05 21:39:31 +04:00
public bool SellWork(IWorkModel work, int quantity)
{
return _storeStorage.SellWork(work, quantity);
}
2024-05-05 00:19:00 +04:00
public bool Update(StoreBindingModel model)
{
CheckModel(model, false);
if (string.IsNullOrEmpty(model.StoreName))
{
throw new ArgumentNullException("Нет названия магазина", nameof(model.StoreName));
}
if (_storeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(StoreBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.StoreName))
{
throw new ArgumentNullException("Нет названия магазина", nameof(model.StoreName));
}
2024-05-05 21:39:31 +04:00
if (model.WorkMaxCount < 0)
{
throw new ArgumentException("Максимальное количество изделий в магазине не может быть меньше нуля", nameof(model.WorkMaxCount));
}
2024-05-05 00:19:00 +04:00
_logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAdress, model.Id);
var element = _storeStorage.GetElement(new StoreSearchModel
{
StoreName = model.StoreName
});
if (element != null && element.Id != model.Id && element.StoreName == model.StoreName)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
}
}