This commit is contained in:
Николай 2023-05-18 18:05:48 +04:00
commit 3afab0dee9
16 changed files with 362 additions and 22 deletions

View File

@ -1,4 +1,5 @@
using HardwareShopBusinessLogic.OfficePackage;
using HardwareShopBusinessLogic.MailWorker;
using HardwareShopBusinessLogic.OfficePackage;
using HardwareShopBusinessLogic.OfficePackage.HelperModels;
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts;
@ -13,16 +14,22 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper
private readonly IGoodStorage _goodStorage;
private readonly IUserStorage _userStorage;
private readonly AbstractSaveToExcel _saveToExcel;
private readonly AbstractSaveToWord _saveToWord;
public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage)
private readonly AbstractMailWorker _mailWorker;
public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage, AbstractMailWorker abstractMailWorker, IUserStorage userStorage)
{
_componentStorage = componentStorage;
_saveToExcel = abstractSaveToExcel;
_saveToWord = abstractSaveToWord;
_goodStorage = goodStorage;
_mailWorker = abstractMailWorker;
_userStorage = userStorage;
}
public List<ReportBuildGoodViewModel> GetBuildGood(List<GoodViewModel> goods)
{
@ -50,12 +57,12 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper
}
/// Получение сведений по комплектующим за период,
/// с указанием в каких товарах и сборках они использовались
public List<ReportComponentsViewModel> GetComponents(UserBindingModel user, ReportBindingModel model)
public List<ReportComponentsViewModel> GetComponents(ReportBindingModel model)
{
var result = new List<ReportComponentsViewModel>();
var components = _componentStorage.GetFilteredList(new()
{
UserId = user.Id,
UserId = model.UserId,
DateFrom = model.DateFrom,
DateTo = model.DateTo
});
@ -109,5 +116,18 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper
File.Delete(model.FileName);
return file;
}
public bool SendReportOnMail(int userId, string subject, string text)
{
var user = _userStorage.GetElement(new() { Id = userId });
if (user == null) return false;
_mailWorker.MailSendAsync(new()
{
MailAddress = user.Email,
Subject = subject,
Text = text
});
return true;
}
}
}

View File

@ -94,19 +94,19 @@ namespace HardwareShopBusinessLogic.BusinessLogics
{
return;
}
if (string.IsNullOrEmpty(model.Login) || model.Login.Length > 20)
if (string.IsNullOrEmpty(model.Login) || model.Login.Length > 40)
{
throw new ArgumentNullException("Нет логина пользователя или длина превышает 20 символов", nameof(model.Login));
throw new ArgumentNullException("Нет логина пользователя или длина превышает 40 символов", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Email) || model.Email.Length > 30)
if (string.IsNullOrEmpty(model.Email) || model.Email.Length > 40)
{
throw new ArgumentNullException("Нет почты пользователя или длина превышает 30 символов", nameof(model.Email));
throw new ArgumentNullException("Нет почты пользователя или длина превышает 40 символов", nameof(model.Email));
}
if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase))
{
throw new ArgumentException("Неправильно введенная почта", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password) || model.Password.Length > 30 || model.Password.Contains(' '))
if (string.IsNullOrEmpty(model.Password) || model.Password.Length > 40 || model.Password.Contains(' '))
{
throw new ArgumentNullException("Нет пароля пользователя или пароль содержит пробелы", nameof(model.Password));
}

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
<PackageReference Include="MailKit" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>
@ -15,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\HardwareShopContracts\HardwareShopContracts.csproj" />
<ProjectReference Include="..\HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj" />
<ProjectReference Include="..\HardwareShopDataModels\HardwareShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,62 @@
using HardwareShopContracts.BindingModels;
using Microsoft.Extensions.Logging;
namespace HardwareShopBusinessLogic.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 ILogger _logger;
public AbstractMailWorker(ILogger<AbstractMailWorker> logger)
{
_logger = logger;
}
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);
}
}

View File

@ -0,0 +1,42 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts;
using MailKit.Net.Pop3;
using MailKit.Security;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace HardwareShopBusinessLogic.MailWorker
{
public class MailKitWorker : AbstractMailWorker
{
public MailKitWorker(ILogger<MailKitWorker> logger) : base(logger) { }
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;
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;
}
}
}
}

View File

@ -418,5 +418,29 @@ namespace HardwareShopStorekeeperApp.Controllers
}
return View();
}
[HttpPost]
public List<ReportComponentsViewModel> Report([FromBody] ReportBindingModel reportModel)
{
if (APIClient.User == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
reportModel.UserId = APIClient.User.Id;
List<ReportComponentsViewModel>? list = APIClient.PostRequestWithResult
<ReportBindingModel, List<ReportComponentsViewModel>>("api/report/componentsreport", reportModel);
return list!;
}
[HttpPost]
public void ReportSendOnMail([FromBody] ReportBindingModel reportModel)
{
if (APIClient.User == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
reportModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/report/componentsreportsendonmail", reportModel);
}
}
}

View File

@ -4,13 +4,82 @@
Layout = "~/Views/Shared/_LayoutStorekeeper.cshtml";
}
<form method="post" class="d-flex flex-column align-items-center">
<div class="col-sm-3">
<label class="form-label">С</label>
<input type="date" class="form-control" name="dateFrom">
<label class="form-label">По</label>
<input type="date" class="form-control" name="dateTo">
</div>
<button type="submit" class="btn btn-primary mt-3 px-4">Вывод на страницу</button>
<button type="submit" class="btn btn-primary mt-3 px-4">Отправить на почту</button>
</form>
<div class="d-flex flex-column align-items-center">
<div class="col-sm-3">
<label class="form-label">С</label>
<input type="date" class="form-control" name="dateFrom" id="dateFrom">
<label class="form-label">По</label>
<input type="date" class="form-control" name="dateTo" id="dateTo">
</div>
<button type="submit" class="btn btn-primary mt-3 px-4" id="page">Вывод на страницу</button>
<button type="submit" class="btn btn-primary mt-3 px-4" id="mail">Отправить на почту</button>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Комплектующее</th>
<th scope="col">Товар/Сборка</th>
<th scope="col">Количество</th>
</tr>
</thead>
<tbody id="result">
</tbody>
</table>
@section Scripts
{
<script>
let list = []
const from = document.getElementById("dateFrom");
const to = document.getElementById("dateTo");
const onpage = document.getElementById("page");
const onmail = document.getElementById("mail");
const resultTable = document.getElementById("result");
onpage.addEventListener("click", () => {
console.log('try to get report')
if (from.value && to.value && from.value !== '' && to.value !== '') {
const dateFrom = new Date(from.value);
const dateTo = new Date(to.value);
const reportModel = { "DateFrom": dateFrom, "DateTo": dateTo }
$.ajax({
method: "POST",
contentType: "application/json",
url: `/Storekeeper/Report`,
data: JSON.stringify(reportModel),
success: function (result) {
list = result
console.log(list)
reloadTable()
}
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
})
} else { alert("empty fields") }
})
onmail.addEventListener("click" () =>{
console.log('try to send email')
$.ajax({
method: "POST",
contentType: "application/json",
url: `/Storekeeper/ReportSendOnMail`,
data: JSON.stringify(reportModel)
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
})
})
function reloadTable() {
resultString = '';
list.forEach((elem) => {
resultString += `<tr><td>${elem.componentName}</td><td></td><td></td></tr>`;
elem.goodOrBuilds.forEach((goodOrBuild) => {
resultString += `<tr><td></td><td>${goodOrBuild.item1}</td><td>${goodOrBuild.item2}</td></tr>`;
})
resultString += `<tr><td>Итого</td><td></td><td>${elem.totalCount}</td></tr>`;
})
resultTable.innerHTML = resultString
}
</script>
}

View File

@ -0,0 +1,17 @@
namespace HardwareShopContracts.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; }
}
}

View File

@ -0,0 +1,11 @@
namespace HardwareShopContracts.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;
}
}

View File

@ -0,0 +1,19 @@
using HardwareShopDataModels.Models;
namespace HardwareShopContracts.BindingModels
{
public class MessageInfoBindingModel : IMessageInfoModel
{
public string MessageId { get; set; } = string.Empty;
public int? UserId { get; set; }
public string SenderName { get; set; } = string.Empty;
public string Subject { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public DateTime DateDelivery { get; set; }
}
}

View File

@ -18,7 +18,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts
/// <param name="model"></param>
/// <param name="user"></param>
/// <returns></returns>
List<ReportComponentsViewModel> GetComponents(UserBindingModel user, ReportBindingModel model);
List<ReportComponentsViewModel> GetComponents(ReportBindingModel model);
/// <summary>
/// Сохранение списка сборок по выбранным товарам в файл-Word
@ -31,5 +31,11 @@ namespace HardwareShopContracts.BusinessLogicsContracts
/// </summary>
/// <param name="model"></param>
byte[] SaveBuildGoodToExcelFile(ReportBindingModel model, List<GoodViewModel> goods);
/// <summary>
/// Отправление отчета на почту
/// </summary>
/// <param name="model"></param>
bool SendReportOnMail(int userId, string subject, string text);
}
}

View File

@ -0,0 +1,17 @@
namespace HardwareShopDataModels.Models
{
public interface IMessageInfoModel
{
string MessageId { get; }
int? UserId { get; }
string SenderName { get; }
DateTime DateDelivery { get; }
string Subject { get; }
string Body { get; }
}
}

View File

@ -42,7 +42,36 @@ namespace HardwareShopRestApi.Controllers
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения списка сборок по выбранным товарам");
_logger.LogError(ex, "Ошибка получения списка сборок по выбранным товарам");
throw;
}
}
[HttpPost]
public List<ReportComponentsViewModel>? ComponentsReport(ReportBindingModel model)
{
try
{
var list = _reportStorekeeperLogic.GetComponents(model);
return list;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения сведений по полученным пользователем комплектующим за период");
throw;
}
}
[HttpPost]
public void ComponentsReportSendOnMail(ReportBindingModel model)
{
try
{
_reportStorekeeperLogic.SendReportOnMail(model.UserId, "заголовок", "текст");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения сведений по полученным пользователем комплектующим за период");
throw;
}
}

View File

@ -1,7 +1,9 @@
using HardwareShopBusinessLogic.BusinessLogics;
using HardwareShopBusinessLogic.BusinessLogics.Storekeeper;
using HardwareShopBusinessLogic.MailWorker;
using HardwareShopBusinessLogic.OfficePackage;
using HardwareShopBusinessLogic.OfficePackage.Implements;
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BuisnessLogicsContracts;
using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.StoragesContracts;
@ -36,6 +38,8 @@ builder.Services.AddTransient<IWorkerReportLogic, WorkerReportLogic>();
builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractMailWorker, MailKitWorker>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
@ -46,6 +50,17 @@ builder.Services.AddSwaggerGen(c =>
var app = builder.Build();
var mailSender = app.Services.GetService<AbstractMailWorker>();
mailSender?.MailConfig(new MailConfigBindingModel
{
MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty,
MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty,
SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty,
SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()),
PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty,
PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString())
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

View File

@ -5,5 +5,12 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"SmtpClientHost": "smtp.gmail.com",
"SmtpClientPort": "587",
"PopHost": "pop.gmail.com",
"PopPort": "995",
"MailLogin": "orderbuyerzxc@gmail.com",
"MailPassword": "sjxa uvgn pepe tatl"
}