diff --git a/ComputerShopBusinessLogic/BusinessLogics/ReportImplementerLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ReportImplementerLogic.cs index 546f796..1d49c6c 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/ReportImplementerLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/ReportImplementerLogic.cs @@ -85,7 +85,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics _saveToPdf.CreateDoc(new PdfInfoImplementer { FileName = model.FileName, - Title = "Список участников", + Title = "Отчёт по заказам за период", DateFrom = model.DateFrom!.Value, DateTo = model.DateTo!.Value, Orders = GetReportOrdersByDates(model) diff --git a/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs b/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..082079e --- /dev/null +++ b/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,64 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + protected string _mailPassword = string.Empty; + protected string _smtpClientHost = string.Empty; + protected int _smtpClientPort; + protected string _popHost = string.Empty; + protected int _popPort; + //private readonly IOrganiserLogic _organiserLogic; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger/*, IOrganiserLogic organiserLogic*/) + { + _logger = logger; + //_organiserLogic = organiserLogic; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + { + return; + } + + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + + await SendMailAsync(info); + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + } +} diff --git a/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs b/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..81af458 --- /dev/null +++ b/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using ComputerShopContracts.BusinessLogicContracts; +using System.Net.Mime; +using ComputerShopContracts.BindingModels; + +namespace ComputerShopBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger/*, IOrganiserLogic organiserLogic*/) : base(logger/*, organiserLogic*/) { } + + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + Attachment attachment = new Attachment("C:\\!Курсовая\\Отчёт за период.pdf", new ContentType(MediaTypeNames.Application.Pdf)); + objMailMessage.Attachments.Add(attachment); + + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + + + } +} diff --git a/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdfImplementer.cs b/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdfImplementer.cs index a756ecd..c584135 100644 --- a/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdfImplementer.cs +++ b/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdfImplementer.cs @@ -16,29 +16,42 @@ namespace GarmentFactoryBusinessLogic.OfficePackage CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); //!!!МБ ТУТ НЕЛЬЗЯ ДРОБНЫЕ ЧИСЛА ИЛИ МОЖНО С ТОЧКОЙ - CreateTable(new List { "1,5cm", "3cm", "3cm", "2,5cm", "1,5cm", "4cm", "3cm", "3cm", "3cm", "3cm" }); + CreateTable(new List { "2cm", "2.5cm", "2cm", "2cm", "2cm", "4cm", "2.5cm", "3.5cm", "3.5cm", "2.5cm" }); CreateRow(new PdfRowParameters { - Texts = new List { "ID заказа", "Дата заказа", "Стоимость заказа", "Статус заказа", "ID заявки", "ФИО клиента", "Дата заявки", "Название сборки", "Каетегория сборки", "Цена сборки" }, + Texts = new List { "ID заказа", "Дата заказа", "Сумма заказа", "Статус заказа", "ID заявки", "ФИО клиента", "Дата заявки", "Название сборки", "Категория сборки", "Цена сборки" }, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); foreach (var order in info.Orders) { + //!!!СЮДА + if (order.RequestsAssemblies.Count < 1) + { + CreateRow(new PdfRowParameters + { + Texts = new List { + order.OrderId.ToString(), order.DateCreateOrder.ToShortDateString(), order.OrderSum.ToString(), order.OrderStatus.ToString(), + "Заказ без заявок", "Неизвестно", "Неизвестно", + "Неизвестно", "Неизвестно", "0" + } + }); + } foreach (var request in order.RequestsAssemblies) { CreateRow(new PdfRowParameters { - Texts = new List { order.OrderId.ToString(), order.DateCreateOrder.ToShortDateString(), order.OrderSum.ToString(), order.OrderStatus.ToString(), - request.RequestId.ToString(), request.ClientFIO, request.DateRequest.ToShortDateString(), + Texts = new List { order.OrderId.ToString(), order.DateCreateOrder.ToShortDateString(), order.OrderSum.ToString(), order.OrderStatus.ToString(), + request.RequestId.ToString(), request.ClientFIO, request.DateRequest.ToShortDateString(), string.IsNullOrEmpty(request.AssemblyName) ? "Сборка не привязана" : request.AssemblyName, - string.IsNullOrEmpty(request.AssemblyCategory) ? "Неизвестная категория" : request.AssemblyCategory, + string.IsNullOrEmpty(request.AssemblyCategory) ? "Неизвестно" : request.AssemblyCategory, request.AssemblyPrice.ToString() - }, - Style = "Normal", - ParagraphAlignment = PdfParagraphAlignmentType.Left + } + //}, + //Style = "Normal", + //ParagraphAlignment = PdfParagraphAlignmentType.Left }); } diff --git a/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdfImplementer.cs b/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdfImplementer.cs index 3fe5771..eb51978 100644 --- a/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdfImplementer.cs +++ b/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdfImplementer.cs @@ -41,9 +41,10 @@ namespace GarmentFactoryBusinessLogic.OfficePackage.Implements protected override void CreatePdf(PdfInfoImplementer info) { _document = new Document(); - DefineStyles(_document); - _section = _document.AddSection(); + DefineStyles(_document); + _document.DefaultPageSetup.Orientation = Orientation.Landscape; + _section = _document.AddSection(); } protected override void CreateParagraph(PdfParagraph pdfParagraph) @@ -100,13 +101,15 @@ namespace GarmentFactoryBusinessLogic.OfficePackage.Implements } } + //!!!ТУТ ИСКЛЮЧЕНИЕ protected override void SavePdf(PdfInfoImplementer info) { var renderer = new PdfDocumentRenderer(true) { Document = _document }; - renderer.RenderDocument(); + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + renderer.RenderDocument(); renderer.PdfDocument.Save(info.FileName); } } diff --git a/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs b/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..f82ebfe --- /dev/null +++ b/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + public string MailPassword { get; set; } = string.Empty; + public string SmtpClientHost { get; set; } = string.Empty; + public int SmtpClientPort { get; set; } + public string PopHost { get; set; } = string.Empty; + public int PopPort { get; set; } + } +} diff --git a/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs b/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..d949069 --- /dev/null +++ b/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} diff --git a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs index 895461c..d0d0df1 100644 --- a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs @@ -83,10 +83,11 @@ namespace ComputerShopDatabaseImplement.Implements public List GetOrdersInfoByDates(ReportBindingModel report) { using var context = new ComputerShopDatabase(); - return context.Orders.Include(x => x.Requests) + return context.Orders + .Where(x => x.UserId == report.UserId && DateTime.Compare(x.DateCreate, report.DateFrom.Value) >= 0 && DateTime.Compare(x.DateCreate, report.DateTo.Value) <= 0) + .Include(x => x.Requests) .ThenInclude(x => x.Request) .ThenInclude(x => x.Assembly) - .Where(x => x.UserId == report.UserId && x.DateCreate >= report.DateFrom && x.DateCreate <= report.DateTo) .ToList() .Select(x => new ReportOrdersViewModel { @@ -95,9 +96,9 @@ namespace ComputerShopDatabaseImplement.Implements OrderSum = x.Sum, OrderStatus = x.Status, RequestsAssemblies = x.Requests.Select(r => (r.Request.Id, r.Request.ClientFIO, r.Request.DateRequest, - (r.Request.Assembly != null) ? r.Request.Assembly.AssemblyName : "Без сборки", - (r.Request.Assembly != null) ? r.Request.Assembly.Category : "Без сборки", - (r.Request.Assembly != null) ? r.Request.Assembly.Price : 0)).ToList() + (r.Request.Assembly != null) ? r.Request.Assembly.AssemblyName : "", + (r.Request.Assembly != null) ? r.Request.Assembly.Category : "", + (r.Request.Assembly != null) ? r.Request.Assembly.Price : 0)).ToList(), }) .ToList(); } diff --git a/ComputerShopImplementerApp/Controllers/HomeController.cs b/ComputerShopImplementerApp/Controllers/HomeController.cs index 3a758f9..4709d1d 100644 --- a/ComputerShopImplementerApp/Controllers/HomeController.cs +++ b/ComputerShopImplementerApp/Controllers/HomeController.cs @@ -7,6 +7,7 @@ using ComputerShopDataModels.Enums; using ComputerShopDataModels.Models; using ComputerShopContracts.SearchModels; using ComputerShopContracts.BusinessLogicContracts; +using DocumentFormat.OpenXml.Bibliography; namespace ComputerShopImplementerApp.Controllers { @@ -594,10 +595,116 @@ namespace ComputerShopImplementerApp.Controllers throw; } string table = ""; + //МБ НЕ НДО ПРИСВАИВАТЬ КЛАСС u-table-entity + table += $""; + table += ""; + //ID заказа + table += ""; + //Дата заказа + table += ""; + //Стоимость заказа + table += ""; + //Статус заказа + table += ""; + //ID заявки + table += ""; + //ФИО клиента + table += ""; + //Дата заявки + table += ""; + //Название сборки + table += ""; + //Категория сборки + table += ""; + //Цена сборки + table += ""; + table += ""; + //МБ НЕ НДО ПРИСВАИВАТЬ КЛАСС + table += ""; + //МБ ИЗМЕНИТЬ ВЫСОТУ + table += ""; + //МБ ИЗМЕНИТЬ КЛАСС + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += ""; + table += ""; + //МБ НЕ ПРИСВАИВАТЬ КЛАСС ИЛИ СДЕЛАТЬ ПЕРЕД ВНУТРЕННИМ ЦИКЛОМ + table += ""; + foreach (var order in result) + { + if (order.RequestsAssemblies.Count < 1) + { + //МБ ПОМЕНЯТЬ ВЫСОТУ + table += ""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += ""; + } + foreach (var request in order.RequestsAssemblies) + { + //МБ ПОМЕНЯТЬ ВЫСОТУ + table += ""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + table += $""; + //МБ тут не будет 0 у непривязанных сборок + table += $""; + table += ""; + } + } + table += "
ID заказаДата заказаСтоимость заказаСтатус заказаID заявкиФИО клиентаДата заявкиНазвание сборкиКатегория сборкиЦена сборки
{order.OrderId.ToString()}{order.DateCreateOrder.ToShortDateString()}{order.OrderSum.ToString()}{order.OrderStatus.ToString()}{"Заказ без заявок"}{"Неизвестно"}{"Неизвестно"}{"Неизвестно"}{"Неизвестно"}{"Неизвестно"}
{order.OrderId.ToString()}{order.DateCreateOrder.ToShortDateString()}{order.OrderSum.ToString()}{order.OrderStatus.ToString()}{request.RequestId.ToString()}{request.ClientFIO.ToString()}{request.DateRequest.ToShortDateString()}{(string.IsNullOrEmpty(request.AssemblyName) ? "Сборка не привязана" : request.AssemblyName)}{(string.IsNullOrEmpty(request.AssemblyCategory) ? "Неизвестная категория" : request.AssemblyCategory)}{request.AssemblyPrice.ToString()}
"; return table; } - + [HttpPost] + public void ReportOrdersByDates(DateTime dateFrom, DateTime dateTo) + { + if (APIUser.User == null) + { + throw new Exception("Вход только авторизованным"); + } + //if (string.IsNullOrEmpty(organiserEmail)) + //{ + // throw new Exception("Email пуст"); + //} + APIUser.PostRequest("api/order/CreateReportToPDFFile", new ReportBindingModel + { + FileName = "C:\\!КУРСОВАЯ\\Отчёт за период.pdf", + DateFrom = dateFrom, + DateTo = dateTo, + UserId = APIUser.User.Id + }); + APIUser.PostRequest("api/order/SendPDFToMail", new MailSendInfoBindingModel + { + //!!!МБ СЮДА ПЕРЕДАВАТЬ ПОЧТУ, КОТОРУЮ ВВОДЯТ НА СТРАНИЦЕ + MailAddress = APIUser.User.Email, + Subject = "Отчет за период", + Text = "Отчет по заказам с " + dateFrom.ToShortDateString() + " по " + dateTo.ToShortDateString() + }); + Response.Redirect("Index"); + } // ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ @@ -652,16 +759,23 @@ namespace ComputerShopImplementerApp.Controllers [HttpPost] public void Enter(string login, string password) { - if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) + try { - throw new Exception("Введите логин и пароль"); + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIUser.User = APIUser.GetRequest($"api/user/loginimplementer?login={login}&password={password}"); + if (APIUser.User == null) + { + throw new Exception("Неверный логин/пароль"); + } + Response.Redirect("Index"); } - APIUser.User = APIUser.GetRequest($"api/user/loginimplementer?login={login}&password={password}"); - if (APIUser.User == null) + catch (Exception) { - throw new Exception("Неверный логин/пароль"); + Response.Redirect("Enter"); } - Response.Redirect("Index"); } [HttpGet] diff --git a/ComputerShopImplementerApp/Program.cs b/ComputerShopImplementerApp/Program.cs index d412fe8..ffba5e9 100644 --- a/ComputerShopImplementerApp/Program.cs +++ b/ComputerShopImplementerApp/Program.cs @@ -5,6 +5,8 @@ using ComputerShopDatabaseImplement.Implements; using ComputerShopDatabaseImplement.Models; using ComputerShopDataModels.Models; using ComputerShopImplementerApp; +using GarmentFactoryBusinessLogic.OfficePackage; +using GarmentFactoryBusinessLogic.OfficePackage.Implements; var builder = WebApplication.CreateBuilder(args); @@ -13,19 +15,25 @@ var builder = WebApplication.CreateBuilder(args); //builder.Services.AddTransient(); //builder.Services.AddTransient(); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); -//builder.Services.AddTransient(); // Add services to the container. builder.Services.AddControllersWithViews(); diff --git a/ComputerShopImplementerApp/Views/Home/ReportOrdersByDates.cshtml b/ComputerShopImplementerApp/Views/Home/ReportOrdersByDates.cshtml index 7a9864a..2eb0d91 100644 --- a/ComputerShopImplementerApp/Views/Home/ReportOrdersByDates.cshtml +++ b/ComputerShopImplementerApp/Views/Home/ReportOrdersByDates.cshtml @@ -8,25 +8,22 @@

Отчёт за период по заказам

- +
- +
-
- - @* *@ - -
-
-
-
+
+ +
+
+