2024-04-28 23:09:52 +04:00
|
|
|
|
using ElectronicsShopContracts.BindingModels;
|
|
|
|
|
using ElectronicsShopContracts.BusinessLogicContracts;
|
|
|
|
|
using ElectronicsShopContracts.SearchModels;
|
|
|
|
|
using ElectronicsShopContracts.StorageContracts;
|
|
|
|
|
using ElectronicsShopContracts.ViewModels;
|
|
|
|
|
using ElectronicsShopDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ElectronicsShopBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-04-29 01:28:41 +04:00
|
|
|
|
private readonly IOrderStorage _storage;
|
2024-04-28 23:09:52 +04:00
|
|
|
|
|
2024-04-29 01:28:41 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage storage) {
|
2024-04-28 23:09:52 +04:00
|
|
|
|
_logger = logger;
|
2024-04-29 01:28:41 +04:00
|
|
|
|
_storage = storage;
|
2024-04-28 23:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
2024-04-29 01:28:41 +04:00
|
|
|
|
if (_storage.Insert(model) == null) {
|
2024-04-28 23:09:52 +04:00
|
|
|
|
_logger.LogInformation("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"ReadList:ID:{model?.ID}");
|
2024-04-29 01:28:41 +04:00
|
|
|
|
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
|
2024-04-28 23:09:52 +04:00
|
|
|
|
if (list == null) {
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-29 01:28:41 +04:00
|
|
|
|
_logger.LogInformation($"ReadList.Count:{list.Count}");
|
|
|
|
|
return list;
|
2024-04-28 23:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true) {
|
|
|
|
|
if (model == null) {
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty((model.ID).ToString())) {
|
|
|
|
|
throw new ArgumentNullException("Нет ID заказа", nameof(model.ID));
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0) {
|
|
|
|
|
throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.Status:{model.Status}.PaymeantOption:{model.PaymeantOption}." +
|
2024-04-29 01:28:41 +04:00
|
|
|
|
$"DateCreate:{model.DateCreate}.DataImplement:{model.DateImplement}");
|
2024-04-28 23:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
2024-04-29 01:28:41 +04:00
|
|
|
|
var viewModel = _storage.GetElement(new OrderSearchModel { ID = model.ID });
|
|
|
|
|
if (viewModel == null) {
|
2024-04-28 23:09:52 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-04-29 01:28:41 +04:00
|
|
|
|
if (viewModel.Status + 1 != newOrderStatus) {
|
2024-04-28 23:09:52 +04:00
|
|
|
|
_logger.LogWarning("Status update to " + newOrderStatus.ToString() + " operation failed.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = newOrderStatus;
|
2024-04-29 01:28:41 +04:00
|
|
|
|
if (model.Status == OrderStatus.Готов) {
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
2024-04-28 23:09:52 +04:00
|
|
|
|
}
|
2024-04-29 01:28:41 +04:00
|
|
|
|
else {
|
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
2024-04-28 23:09:52 +04:00
|
|
|
|
}
|
2024-04-29 01:28:41 +04:00
|
|
|
|
if (_storage.Update(model) == null) {
|
2024-04-28 23:09:52 +04:00
|
|
|
|
_logger.LogWarning("Update operarion failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|