PIbd-21 Potapov N.S. LabWork07 #8
@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SecuritySystemBusinessLogic.MailWorker;
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.BusinessLogicsContracts;
|
||||
using SecuritySystemContracts.SearchModels;
|
||||
@ -12,12 +13,16 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
private readonly IClientStorage _clientStorage;
|
||||
static readonly object locker = new object();
|
||||
private readonly AbstractMailWorker _abstractMailWorker;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, AbstractMailWorker abstractMailWorker, IClientStorage clientStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_abstractMailWorker = abstractMailWorker;
|
||||
_clientStorage = clientStorage;
|
||||
}
|
||||
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||
@ -49,20 +54,20 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||
if (element == null)
|
||||
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||
if (order == null)
|
||||
{
|
||||
_logger.LogWarning("Read operation failed");
|
||||
return false;
|
||||
}
|
||||
if (element.Status != status - 1)
|
||||
if (order.Status != status - 1)
|
||||
{
|
||||
_logger.LogWarning("Status change operation failed");
|
||||
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
|
||||
}
|
||||
if (element.ImplementerId.HasValue)
|
||||
if (order.ImplementerId.HasValue)
|
||||
{
|
||||
model.ImplementerId = element.ImplementerId;
|
||||
model.ImplementerId = order.ImplementerId;
|
||||
}
|
||||
OrderStatus oldStatus = model.Status;
|
||||
model.Status = status;
|
||||
@ -74,6 +79,11 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
var orderClient = _clientStorage.GetElement(new ClientSearchModel { Id = order.ClientId });
|
||||
if (orderClient != null)
|
||||
{
|
||||
SendMail(orderClient, order);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -140,5 +150,37 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||||
return element;
|
||||
}
|
||||
|
||||
private void SendMail(ClientViewModel clientView, OrderViewModel orderView)
|
||||
{
|
||||
if (clientView == null || orderView == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
MailSendInfoBindingModel mailSendInfoBindingModel;
|
||||
|
||||
string subject = $"Заказ #{orderView.Id}";
|
||||
string orderInfo = $"Ваш заказ #{orderView.Id} от {orderView.DateCreate} стоимостью {orderView.Sum}";
|
||||
|
||||
if (orderView.Status == OrderStatus.Принят)
|
||||
{
|
||||
mailSendInfoBindingModel = new MailSendInfoBindingModel
|
||||
{
|
||||
MailAddress = clientView.Email,
|
||||
Subject = subject,
|
||||
Text = orderInfo + " был принят"
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
mailSendInfoBindingModel = new MailSendInfoBindingModel
|
||||
{
|
||||
MailAddress = clientView.Email,
|
||||
Subject = subject,
|
||||
Text = orderInfo + $" поменял статус на {orderView.Status}"
|
||||
};
|
||||
}
|
||||
_abstractMailWorker.MailSendAsync(mailSendInfoBindingModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user