2024-07-04 11:02:45 +05:00
|
|
|
using System;
|
2024-01-19 17:48:45 +05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
namespace AsbCloudApp.Data;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Плановое значение и максимально допустимое ограничение
|
|
|
|
/// </summary>
|
|
|
|
public class PlanLimitDto
|
2023-03-21 12:41:43 +05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
/// План
|
2023-03-21 12:41:43 +05:00
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
[Required]
|
|
|
|
public double Plan { get; set; }
|
2023-03-21 12:41:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
/// <summary>
|
|
|
|
/// Максимальное ограничение
|
|
|
|
/// </summary>
|
|
|
|
[Required]
|
|
|
|
public double LimitMax { get; set; }
|
2023-03-21 12:41:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
/// <summary>
|
|
|
|
/// Валидация
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="commonRange">Общий диапазон для плана и ограничения</param>
|
|
|
|
/// <param name="paramName"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public virtual IEnumerable<ValidationResult> Validate((double GE, double LE) commonRange, string paramName)
|
|
|
|
=> Validate(commonRange, commonRange, paramName);
|
2024-01-19 17:48:45 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
/// <summary>
|
|
|
|
/// Валидация
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="planRange"></param>
|
|
|
|
/// <param name="limitMaxRange"></param>
|
|
|
|
/// <param name="paramName">Название параметра для которого задается план и ограничение</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public virtual IEnumerable<ValidationResult> Validate((double GE, double LE) planRange, (double GE, double LE) limitMaxRange, string paramName)
|
|
|
|
{
|
|
|
|
if (Plan < planRange.GE || Plan > planRange.LE)
|
|
|
|
yield return new ValidationResult($"{paramName} плановое значение должно быть в диапазоне [{planRange.GE}; {planRange.LE}].");
|
2024-01-19 17:48:45 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
if (Plan < planRange.GE || Plan > planRange.LE)
|
|
|
|
yield return new ValidationResult($"{paramName} ограничивающее значение должно быть в диапазоне [{limitMaxRange.GE}; {limitMaxRange.LE}].");
|
2024-01-19 17:48:45 +05:00
|
|
|
}
|
2024-08-19 10:01:07 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Реализация RangeAttribute для PlanLimitDto
|
|
|
|
/// </summary>
|
|
|
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
|
|
|
|
AllowMultiple = false)]
|
|
|
|
public class RangePlanLimitAttribute : ValidationAttribute
|
|
|
|
{
|
|
|
|
private readonly double minimum;
|
|
|
|
private readonly double maximum;
|
2024-01-19 17:48:45 +05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Реализация RangeAttribute для PlanLimitDto
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
/// <param name="minimum"></param>
|
|
|
|
/// <param name="maximum"></param>
|
|
|
|
public RangePlanLimitAttribute(double minimum, double maximum)
|
2024-01-19 17:48:45 +05:00
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
this.minimum = minimum;
|
|
|
|
this.maximum = maximum;
|
|
|
|
}
|
2024-01-19 17:48:45 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
/// <inheritdoc/>
|
|
|
|
public override bool IsValid(object? value)
|
|
|
|
{
|
|
|
|
try
|
2024-01-19 17:48:45 +05:00
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
if(value is PlanLimitDto dto)
|
2024-01-19 17:48:45 +05:00
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
var isPlanValid = dto.Plan <= maximum && dto.Plan >= minimum;
|
|
|
|
var isLimitMaxValid = dto.LimitMax <= maximum && dto.LimitMax >= minimum;
|
|
|
|
return isPlanValid && isLimitMaxValid;
|
|
|
|
}
|
|
|
|
}catch{}
|
|
|
|
return false;
|
2023-03-21 12:41:43 +05:00
|
|
|
}
|
|
|
|
}
|