Files
laatzen/LaaProductionWeb/LaaProduction.Personalization/EquipmentsManager.cs
T
2023-09-01 08:50:08 +02:00

125 lines
3.9 KiB
C#

namespace LaaProduction.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using LaaProductionSMTP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class EquipmentsManager : IEquipmentsManager
{
private readonly IEquipmentsRepository equipments;
private readonly SMTPClient smtpClient;
public EquipmentsManager(IEquipmentsRepository equipments, SMTPClient smtpClient)
{
this.equipments = equipments;
this.smtpClient = smtpClient;
}
public int Add(PruefmittelPruefung model)
{
model.PruefdatumSoll = model.PruefdatumIst.AddDays(model.Pruefintervall);
return this.equipments.Add(model);
}
public PruefmittelPruefung FindById(int pruefmittelId)
=> this.equipments.FindById(pruefmittelId)
?? new PruefmittelPruefung();
public int Remove(int pruefmittelId)
=> this.equipments.Remove(pruefmittelId);
public IEnumerable<PruefmittelPruefung> SelectAll()
=> this.equipments.SelectAll();
public IEnumerable<PruefmittelPruefung> SelectAll(int softwareId)
{
var equipments = this.equipments
.SelectAll(softwareId)
.ToList();
foreach (var equipment in equipments)
{
var daysInSchedule = this.GetDaysInSchedule(equipment);
equipment.DaysLeft = equipment.PruefdatumSoll.Subtract(DateTime.Now).Days;
if (daysInSchedule.Any())
{
equipment.InSchedule = daysInSchedule.Max(x => x) >= equipment.DaysLeft;
}
if (equipment.Erforderlich && daysInSchedule.Contains(equipment.DaysLeft))
{
this.SendPruefmittelPruefungReminderEmail(equipment);
}
}
return equipments;
}
public void SetInspected(int pruefmittelId)
{
var existing = this.equipments.FindById(pruefmittelId);
if (existing is null)
{
return;
}
existing.PruefdatumIst = DateTime.Now;
existing.PruefdatumSoll = existing.PruefdatumIst.AddDays(existing.Pruefintervall);
this.equipments.Update(existing);
}
public int Update(PruefmittelPruefung model)
=> this.equipments.Update(model);
private IEnumerable<int> GetDaysInSchedule(PruefmittelPruefung equipment)
{
var matches = Regex.Matches($"{equipment?.Schedule}", "\\d+", RegexOptions.Multiline);
var daysInSchedule = new List<int>();
foreach (Match match in matches)
{
if (match.Success && int.TryParse(match.Value, out var day))
{
daysInSchedule.Add(day);
}
}
return daysInSchedule;
}
private void SendPruefmittelPruefungReminderEmail(PruefmittelPruefung equipment)
{
var separator = new[] { ',', ';', ' ' };
var options = StringSplitOptions.RemoveEmptyEntries;
var emailsTo = equipment.EmailAn.Split(separator, options);
var emailsCC = equipment.InCcAn.Split(separator, options);
var sender = equipment.EmailFrom;
foreach (var x in separator)
{
sender = sender.Trim(x);
}
this.smtpClient
.CreateMessage()
.Subject(equipment.Betreff)
.Body(equipment.Nachricht)
.From(sender)
.To(emailsTo)
.CC(emailsCC)
.Send();
}
}
}