227 lines
6.5 KiB
C#
227 lines
6.5 KiB
C#
using DocumentFormat.OpenXml.Drawing.Charts;
|
||
using DocumentFormat.OpenXml.Drawing.Diagrams;
|
||
using DocumentFormat.OpenXml.Packaging;
|
||
using ElectronicsShopBusinessLogic.BusinessLogic;
|
||
using ElectronicsShopBusinessLogic.MailWorker;
|
||
using ElectronicsShopContracts.BindingModels;
|
||
using ElectronicsShopContracts.BusinessLogicContracts;
|
||
using ElectronicsShopContracts.SearchModels;
|
||
using ElectronicsShopContracts.ViewModels;
|
||
using ElectronicsShopDataBaseImplement.Models;
|
||
using ElectronicsShopDataModels.Enums;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.EntityFrameworkCore.Migrations;
|
||
using Microsoft.Identity.Client;
|
||
using MigraDoc.Rendering;
|
||
|
||
namespace ElectronicsShopRestAPI.Controllers {
|
||
|
||
[Route("api/[controller]/[action]")]
|
||
[ApiController]
|
||
|
||
public class ClientController : Controller {
|
||
|
||
private readonly ILogger _logger;
|
||
private readonly IClientLogic _logic;
|
||
private readonly IPaymeantLogic _payLogic;
|
||
private readonly IReportClientLogic _reportLogic;
|
||
private readonly IOrderLogic _orderLogic;
|
||
private readonly AbstractMailWorker _mailWorker;
|
||
|
||
public ClientController(ILogger<ClientController> logger, IClientLogic logic, IPaymeantLogic payLogic, IReportClientLogic reportlogic,
|
||
IOrderLogic orderLogic, AbstractMailWorker mailWorker) {
|
||
_logger = logger;
|
||
_logic = logic;
|
||
_payLogic = payLogic;
|
||
_reportLogic = reportlogic;
|
||
_orderLogic = orderLogic;
|
||
_mailWorker = mailWorker;
|
||
}
|
||
|
||
[HttpGet]
|
||
public ClientViewModel? Login(string email, string password) {
|
||
try {
|
||
return _logic.ReadElemet(new ClientSearchModel {
|
||
Email = email,
|
||
Passwrod = password
|
||
});
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, "Ошибка входа в систему");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public void Register(ClientBindingModel model) {
|
||
try {
|
||
_logic.Create(model);
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, "Ошибка регистрации");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public void UpdateData(ClientBindingModel model) {
|
||
try {
|
||
_logic.Update(model);
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, "Ошибка обновления данных");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public void CreatePaymeant (PaymeantBindingModel model) {
|
||
try {
|
||
if (_payLogic.ReadList(new PaymeantSearchModel { OrderID = model.OrderID })?.Count > 0) {
|
||
var order = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID });
|
||
|
||
if (order == null) {
|
||
throw new Exception("Ошибка получения данных");
|
||
}
|
||
|
||
_orderLogic.Update(new OrderBindingModel {
|
||
ID = order.ID,
|
||
ClientID = order.ClientID,
|
||
DateCreate = order.DateCreate,
|
||
Sum = order.Sum - model.SumPayment,
|
||
ProductList = order.ProductList,
|
||
});
|
||
|
||
var payemeant = _payLogic.ReadElement(new PaymeantSearchModel { OrderID = model.OrderID })
|
||
?? throw new Exception("Ошибка получения оплаты");
|
||
payemeant.SumPayment += model.SumPayment;
|
||
|
||
_payLogic.SetStatus(new PaymeantBindingModel {
|
||
ID = payemeant.ID,
|
||
OrderID = payemeant.OrderID,
|
||
SumPayment = payemeant.SumPayment,
|
||
PayOption = payemeant.PayOption,
|
||
ClientID = payemeant.ClientID,
|
||
DatePaymeant = payemeant.DatePaymeant,
|
||
}, model.PayOption);
|
||
}
|
||
else {
|
||
var products = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID })?.ProductList;
|
||
|
||
if (products == null) {
|
||
throw new Exception("Ошибка получения данных");
|
||
}
|
||
|
||
_payLogic.CreatePay(model);
|
||
var order = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID });
|
||
|
||
if (order == null) {
|
||
throw new Exception("Ошибка получения данных");
|
||
}
|
||
|
||
_orderLogic.Update(new OrderBindingModel {
|
||
ID = order.ID,
|
||
ClientID = order.ClientID,
|
||
DateCreate = order.DateCreate,
|
||
Sum = order.Sum - model.SumPayment,
|
||
ProductList = order.ProductList,
|
||
});
|
||
}
|
||
|
||
if (model.PayOption == 0) {
|
||
var order = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID });
|
||
|
||
if (order == null) {
|
||
throw new Exception("Ошибка получения данных");
|
||
}
|
||
|
||
_orderLogic.Update(new OrderBindingModel {
|
||
ID = model.OrderID,
|
||
ClientID = order.ClientID,
|
||
DateCreate = order.DateCreate,
|
||
Sum = order.Sum,
|
||
ProductList = order.ProductList,
|
||
Status = OrderStatus.Оплачено
|
||
});
|
||
}
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, "Ошибка создания оплаты");
|
||
}
|
||
}
|
||
[HttpGet]
|
||
public List<PaymeantViewModel>? GetPaymeants(int _clientID) {
|
||
try {
|
||
return _payLogic?.ReadList(new PaymeantSearchModel { ClientID = _clientID });
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, $"Ошибка получения списка оплат клиента id = {_clientID}");
|
||
throw;
|
||
}
|
||
}
|
||
[HttpGet]
|
||
public List<ReportPaymeantsViewModel>? GetReport(string _start,string _end) {
|
||
try {
|
||
var dataSource = _reportLogic.GetPaymeants(new ReportBindingModel {
|
||
DateFrom = DateTime.Parse(_start),
|
||
DateTo = DateTime.Parse(_end)
|
||
});
|
||
return dataSource;
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, $"Ошибка получения данных");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpGet]
|
||
public byte[]? CreateXlsxReport (int _clientID) {
|
||
try {
|
||
var document = _reportLogic.SavePaymeantToExcelFile (_clientID);
|
||
return document;
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, $"Ошибка создания файла");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpGet]
|
||
public byte[]? CreateDocxReport (int _clientID) {
|
||
try {
|
||
var document = _reportLogic.SavePaymeantToWordFile (_clientID);
|
||
return document;
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, $"Ошибка создания файла");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public void SendReportMail (ReportBindingModel model) {
|
||
try {
|
||
|
||
var doc = _reportLogic.SaveProductToPdfFile(model);
|
||
|
||
MemoryStream stream = new MemoryStream();
|
||
doc.Save(stream, true);
|
||
byte[] data = stream.ToArray();
|
||
|
||
|
||
_mailWorker.MailSendAsync(new() {
|
||
MailAddress = model.ClientEmail,
|
||
Subject = "Отчет",
|
||
Text = $"Отчет оплаченных товаров с {model.DateFrom} по {model.DateTo}",
|
||
document = data
|
||
});
|
||
|
||
}
|
||
catch (Exception ex) {
|
||
_logger.LogError(ex, $"Ошибка создания файла");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
}
|
||
} |