94 lines
2.8 KiB
C#
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 Int32 Add(PruefmittelPruefung model)
|
|
{
|
|
model.PruefdatumSoll = model.PruefdatumIst.AddDays(model.Pruefintervall);
|
|
|
|
return this.equipments.Add(model);
|
|
}
|
|
|
|
public PruefmittelPruefung FindById(Int32 pruefmittelId)
|
|
=> this.equipments.FindById(pruefmittelId)
|
|
?? new PruefmittelPruefung();
|
|
|
|
public IEnumerable<Int32> GetDaysInSchedule(PruefmittelPruefung equipment)
|
|
{
|
|
var matches = Regex.Matches($"{equipment?.Schedule}", "\\d+", RegexOptions.Multiline);
|
|
var daysInSchedule = new List<Int32>();
|
|
|
|
foreach (Match match in matches)
|
|
{
|
|
if (match.Success && Int32.TryParse(match.Value, out var day))
|
|
{
|
|
daysInSchedule.Add(day);
|
|
}
|
|
}
|
|
|
|
return daysInSchedule;
|
|
}
|
|
|
|
public Int32 Remove(Int32 pruefmittelId)
|
|
=> this.equipments.Remove(pruefmittelId);
|
|
|
|
public IEnumerable<PruefmittelPruefung> SelectAll()
|
|
=> this.equipments.SelectAll();
|
|
|
|
public IEnumerable<PruefmittelPruefung> SelectAll(Int32 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(Int32 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 Int32 Update(PruefmittelPruefung model)
|
|
=> this.equipments.Update(model);
|
|
}
|
|
}
|