Files
ReifenSpaeth/Lagerbestand/Models/TireInspection.cs
T

117 lines
3.4 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Lagerbestand.Models;
/// <summary>
/// Condition rating of a single tire as reported by the measurement device.
/// </summary>
public enum TireMeasureRating
{
Unknown = 0,
Ok = 1,
Warning = 2,
Critical = 3
}
/// <summary>
/// A tread depth / condition measurement for a tire stack, typically imported
/// from a Launch TTS01 report (QR code) during checkin.
/// </summary>
public class TireInspection
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
public Guid TireStackId { get; set; }
[ForeignKey(nameof(TireStackId))]
[System.Text.Json.Serialization.JsonIgnore]
public TireStack? TireStack { get; set; }
[MaxLength(50)]
public string Source { get; set; } = "Launch TTS01";
[MaxLength(100)]
public string? RecordSn { get; set; }
[MaxLength(500)]
public string? ReportUrl { get; set; }
public DateTime? MeasuredAt { get; set; }
[MaxLength(20)]
public string? LicensePlate { get; set; }
public int? Mileage { get; set; }
[Column(TypeName = "decimal(5,2)")]
public decimal? TreadDepthFL { get; set; }
[Column(TypeName = "decimal(5,2)")]
public decimal? TreadDepthFR { get; set; }
[Column(TypeName = "decimal(5,2)")]
public decimal? TreadDepthRL { get; set; }
[Column(TypeName = "decimal(5,2)")]
public decimal? TreadDepthRR { get; set; }
public TireMeasureRating RatingFL { get; set; }
public TireMeasureRating RatingFR { get; set; }
public TireMeasureRating RatingRL { get; set; }
public TireMeasureRating RatingRR { get; set; }
[MaxLength(1000)]
public string? Recommendation { get; set; }
public string? RawJson { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public Guid? CreatedByUserId { get; set; }
[NotMapped]
public decimal? MinTreadDepth
{
get
{
var values = new[] { TreadDepthFL, TreadDepthFR, TreadDepthRL, TreadDepthRR }.Where(v => v.HasValue).Select(v => v!.Value).ToList();
return values.Count == 0 ? null : values.Min();
}
}
[NotMapped]
public TireMeasureRating WorstRating
{
get
{
var ratings = new[] { RatingFL, RatingFR, RatingRL, RatingRR };
return ratings.Max();
}
}
[NotMapped]
public bool HasTreadData => TreadDepthFL.HasValue || TreadDepthFR.HasValue || TreadDepthRL.HasValue || TreadDepthRR.HasValue;
/// <summary>Legal minimum tread depth in Germany (mm).</summary>
public const decimal DefaultCriticalThreshold = 1.6m;
/// <summary>Common workshop recommendation threshold for replacement (mm).</summary>
public const decimal DefaultWarningThreshold = 3.0m;
/// <summary>
/// Derives a rating from a tread depth in mm. Values below <paramref name="critical"/>
/// are critical, values up to <paramref name="warning"/> are a warning.
/// </summary>
public static TireMeasureRating RateDepth(decimal? mm, decimal? critical = null, decimal? warning = null)
{
if (!mm.HasValue) return TireMeasureRating.Unknown;
var crit = critical ?? DefaultCriticalThreshold;
var warn = Math.Max(warning ?? DefaultWarningThreshold, crit);
if (mm.Value < crit) return TireMeasureRating.Critical;
if (mm.Value <= warn) return TireMeasureRating.Warning;
return TireMeasureRating.Ok;
}
}