common/Stichproben/Cordonel/StichprobenManager.cs
2026-04-23 17:50:07 +02:00

65 lines
2.1 KiB
C#

namespace Stichproben.Cordonel
{
public class StichprobenManager
{
private readonly StichprobenDbContext stichprobenDb;
public StichprobenManager(int leakStationId)
=> this.stichprobenDb = new StichprobenDbContext(leakStationId);
public StichprobeResult ShouldBePressureTested(int pcbId)
{
var isAnyUndicht = this.stichprobenDb.IsAnyStichprobeUndicht();
var previousStichprobe = this.stichprobenDb.GetLastStichprobeOrDefault();
if (previousStichprobe is null)
{
previousStichprobe = new Stichprobe();
}
var stichprobenRule = this.stichprobenDb.GetMinRule();
var currentStichprobe = new Stichprobe
{
PcbId = pcbId
};
if (isAnyUndicht == false)
{
stichprobenRule = this.stichprobenDb.FindRuleOrMin(previousStichprobe.Prozentsatz);
if (previousStichprobe.Nummer >= stichprobenRule.Count)
{
stichprobenRule = this.stichprobenDb.GetNextRuleOrMin(previousStichprobe.Prozentsatz);
}
if (previousStichprobe.Nummer != 0)
{
currentStichprobe.Nummer = previousStichprobe.Nummer + 1;
}
}
else
{
stichprobenRule = this.stichprobenDb.GetMaxRule();
}
var percent = stichprobenRule.Percent;
var moduloDevider = 100 / percent;
currentStichprobe.Prozentsatz = percent;
currentStichprobe.IstStichprobe = currentStichprobe.Nummer % moduloDevider == 0;
if (currentStichprobe.Nummer == 0)
{
currentStichprobe.Nummer = 1;
}
if (previousStichprobe.PcbId != currentStichprobe.PcbId)
{
this.stichprobenDb.AddStichprobe(currentStichprobe);
}
return new StichprobeResult(currentStichprobe.IstStichprobe, currentStichprobe.Nummer, stichprobenRule.Percent, stichprobenRule.Count);
}
}
}