laatzen/LaaProductionWeb/LaaProduction.Personalization/EquipmentsManager.cs
Stoyan Zlatev e32e553bfc clr types
2024-12-18 15:25:14 +01:00

94 lines
2.8 KiB
C#

namespace LaaProduction.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class EquipmentsManager : IEquipmentsManager
{
private readonly IEquipmentsRepository equipments;
public EquipmentsManager(IEquipmentsRepository equipments)
{
this.equipments = equipments;
}
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 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;
}
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;
}
}
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);
}
}