using OrpaonVision.Core.LayerRecognition; using OrpaonVision.Core.PartRecognition; using OrpaonVision.Core.Results; namespace OrpaonVision.Core.QuantityValidation; /// /// 数量校验服务接口。 /// public interface IQuantityValidationService { /// /// 校验精确数量。 /// /// 部件列表。 /// 期望数量。 /// 部件类型(可选)。 /// 取消令牌。 /// 精确数量校验结果。 Task> ValidateExactQuantityAsync(IReadOnlyList parts, int expectedCount, PartType? partType = null, CancellationToken cancellationToken = default); /// /// 校验最少数量。 /// /// 部件列表。 /// 最少数量。 /// 部件类型(可选)。 /// 取消令牌。 /// 最少数量校验结果。 Task> ValidateMinimumQuantityAsync(IReadOnlyList parts, int minCount, PartType? partType = null, CancellationToken cancellationToken = default); /// /// 校验范围数量。 /// /// 部件列表。 /// 最小数量。 /// 最大数量。 /// 部件类型(可选)。 /// 取消令牌。 /// 范围数量校验结果。 Task> ValidateRangeQuantityAsync(IReadOnlyList parts, int minCount, int maxCount, PartType? partType = null, CancellationToken cancellationToken = default); /// /// 校验唯一数量。 /// /// 部件列表。 /// 唯一属性名。 /// 期望唯一数量。 /// 部件类型(可选)。 /// 取消令牌。 /// 唯一数量校验结果。 Task> ValidateUniqueQuantityAsync(IReadOnlyList parts, string uniqueProperty, int expectedUniqueCount, PartType? partType = null, CancellationToken cancellationToken = default); /// /// 批量数量校验。 /// /// 部件列表。 /// 校验规则列表。 /// 取消令牌。 /// 批量校验结果。 Task> BatchValidateQuantityAsync(IReadOnlyList parts, IReadOnlyList validationRules, CancellationToken cancellationToken = default); /// /// 获取数量校验统计信息。 /// /// 开始时间。 /// 结束时间。 /// 取消令牌。 /// 统计信息。 Task> GetValidationStatisticsAsync(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default); /// /// 创建数量校验规则。 /// /// 校验规则。 /// 取消令牌。 /// 创建结果。 Task> CreateValidationRuleAsync(QuantityValidationRule rule, CancellationToken cancellationToken = default); /// /// 更新数量校验规则。 /// /// 校验规则。 /// 取消令牌。 /// 更新结果。 Task UpdateValidationRuleAsync(QuantityValidationRule rule, CancellationToken cancellationToken = default); /// /// 删除数量校验规则。 /// /// 规则ID。 /// 取消令牌。 /// 删除结果。 Task DeleteValidationRuleAsync(Guid ruleId, CancellationToken cancellationToken = default); /// /// 获取数量校验规则列表。 /// /// 产品类型编码(可选)。 /// 层级编号(可选)。 /// 部件类型(可选)。 /// 取消令牌。 /// 规则列表。 Task>> GetValidationRulesAsync(string? productTypeCode = null, int? layerNumber = null, PartType? partType = null, CancellationToken cancellationToken = default); } /// /// 精确数量校验结果。 /// public sealed class ExactQuantityValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 实际数量。 /// public int ActualCount { get; init; } /// /// 期望数量。 /// public int ExpectedCount { get; init; } /// /// 数量偏差。 /// public int Deviation => ActualCount - ExpectedCount; /// /// 部件类型(如果指定)。 /// public PartType? PartType { get; init; } /// /// 匹配的部件列表。 /// public IReadOnlyList MatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 最少数量校验结果。 /// public sealed class MinimumQuantityValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 实际数量。 /// public int ActualCount { get; init; } /// /// 最少数量。 /// public int MinimumCount { get; init; } /// /// 超出数量。 /// public int ExcessCount => Math.Max(0, ActualCount - MinimumCount); /// /// 部件类型(如果指定)。 /// public PartType? PartType { get; init; } /// /// 匹配的部件列表。 /// public IReadOnlyList MatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 范围数量校验结果。 /// public sealed class RangeQuantityValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 实际数量。 /// public int ActualCount { get; init; } /// /// 最小数量。 /// public int MinimumCount { get; init; } /// /// 最大数量。 /// public int MaximumCount { get; init; } /// /// 是否低于最小值。 /// public bool IsBelowMinimum => ActualCount < MinimumCount; /// /// 是否高于最大值。 /// public bool IsAboveMaximum => ActualCount > MaximumCount; /// /// 部件类型(如果指定)。 /// public PartType? PartType { get; init; } /// /// 匹配的部件列表。 /// public IReadOnlyList MatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 唯一数量校验结果。 /// public sealed class UniqueQuantityValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 实际唯一数量。 /// public int ActualUniqueCount { get; init; } /// /// 期望唯一数量。 /// public int ExpectedUniqueCount { get; init; } /// /// 唯一属性名。 /// public string UniqueProperty { get; init; } = string.Empty; /// /// 唯一值列表。 /// public IReadOnlyList UniqueValues { get; init; } = Array.Empty(); /// /// 重复值列表。 /// public IReadOnlyList DuplicateValues { get; init; } = Array.Empty(); /// /// 部件类型(如果指定)。 /// public PartType? PartType { get; init; } /// /// 匹配的部件列表。 /// public IReadOnlyList MatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 重复值信息。 /// public sealed class DuplicateValue { /// /// 重复值。 /// public object Value { get; init; } = string.Empty; /// /// 出现次数。 /// public int Count { get; init; } /// /// 相关部件ID列表。 /// public IReadOnlyList PartIds { get; init; } = Array.Empty(); } /// /// 批量数量校验结果。 /// public sealed class BatchQuantityValidationResult { /// /// 校验结果列表。 /// public IReadOnlyList ValidationResults { get; init; } = Array.Empty(); /// /// 总规则数量。 /// public int TotalRules { get; init; } /// /// 通过的规则数量。 /// public int PassedRules { get; init; } /// /// 失败的规则数量。 /// public int FailedRules { get; init; } /// /// 总体通过率。 /// public double OverallPassRate => TotalRules > 0 ? (double)PassedRules / TotalRules * 100 : 0.0; /// /// 批量处理时间。 /// public DateTime BatchProcessingTimeUtc { get; init; } /// /// 总耗时(毫秒)。 /// public long TotalElapsedMs { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 数量校验结果基类。 /// public abstract class QuantityValidationResult { /// /// 规则ID。 /// public Guid RuleId { get; init; } /// /// 规则名称。 /// public string RuleName { get; init; } = string.Empty; /// /// 校验类型。 /// public QuantityValidationType ValidationType { get; init; } /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 错误消息。 /// public string? ErrorMessage { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 数量校验规则。 /// public sealed class QuantityValidationRule { /// /// 规则ID。 /// public Guid RuleId { get; init; } /// /// 规则名称。 /// public string RuleName { get; init; } = string.Empty; /// /// 规则描述。 /// public string RuleDescription { get; init; } = string.Empty; /// /// 产品类型编码。 /// public string ProductTypeCode { get; init; } = string.Empty; /// /// 层级编号。 /// public int LayerNumber { get; init; } /// /// 部件类型(可选)。 /// public PartType? PartType { get; init; } /// /// 校验类型。 /// public QuantityValidationType ValidationType { get; init; } /// /// 最小数量。 /// public int MinimumCount { get; init; } /// /// 最大数量。 /// public int MaximumCount { get; init; } /// /// 期望数量。 /// public int ExpectedCount { get; init; } /// /// 唯一属性名。 /// public string UniqueProperty { get; init; } = string.Empty; /// /// 期望唯一数量。 /// public int ExpectedUniqueCount { get; init; } /// /// 是否启用。 /// public bool IsEnabled { get; init; } = true; /// /// 优先级。 /// public int Priority { get; init; } = 1; /// /// 创建时间。 /// public DateTime CreatedAtUtc { get; init; } /// /// 更新时间。 /// public DateTime UpdatedAtUtc { get; init; } /// /// 版本号。 /// public string Version { get; init; } = "1.0"; /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 数量校验统计信息。 /// public sealed class QuantityValidationStatistics { /// /// 总校验次数。 /// public int TotalValidations { get; init; } /// /// 成功校验次数。 /// public int SuccessfulValidations { get; init; } /// /// 失败校验次数。 /// public int FailedValidations { get; init; } /// /// 总体成功率。 /// public double OverallSuccessRate => TotalValidations > 0 ? (double)SuccessfulValidations / TotalValidations * 100 : 0.0; /// /// 按校验类型分组的统计。 /// public Dictionary ByValidationType { get; init; } = new(); /// /// 按部件类型分组的统计。 /// public Dictionary ByPartType { get; init; } = new(); /// /// 按产品类型分组的统计。 /// public Dictionary ByProductType { get; init; } = new(); /// /// 按层级分组的统计。 /// public Dictionary ByLayer { get; init; } = new(); /// /// 统计开始时间。 /// public DateTime StartTimeUtc { get; init; } /// /// 统计结束时间。 /// public DateTime EndTimeUtc { get; init; } } /// /// 校验类型统计。 /// public sealed class ValidationTypeStatistics { /// /// 校验类型。 /// public QuantityValidationType ValidationType { get; init; } /// /// 校验次数。 /// public int ValidationCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 成功率。 /// public double SuccessRate => ValidationCount > 0 ? (double)SuccessCount / ValidationCount * 100 : 0.0; /// /// 平均耗时(毫秒)。 /// public double AverageElapsedMs { get; init; } } /// /// 部件类型校验统计。 /// public sealed class PartTypeValidationStatistics { /// /// 部件类型。 /// public PartType PartType { get; init; } /// /// 校验次数。 /// public int ValidationCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 成功率。 /// public double SuccessRate => ValidationCount > 0 ? (double)SuccessCount / ValidationCount * 100 : 0.0; /// /// 平均部件数量。 /// public double AveragePartCount { get; init; } } /// /// 产品类型校验统计。 /// public sealed class ProductTypeValidationStatistics { /// /// 产品类型编码。 /// public string ProductTypeCode { get; init; } = string.Empty; /// /// 校验次数。 /// public int ValidationCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 成功率。 /// public double SuccessRate => ValidationCount > 0 ? (double)SuccessCount / ValidationCount * 100 : 0.0; /// /// 平均部件数量。 /// public double AveragePartCount { get; init; } } /// /// 层级校验统计。 /// public sealed class LayerValidationStatistics { /// /// 层级编号。 /// public int LayerNumber { get; init; } /// /// 校验次数。 /// public int ValidationCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 成功率。 /// public double SuccessRate => ValidationCount > 0 ? (double)SuccessCount / ValidationCount * 100 : 0.0; /// /// 平均部件数量。 /// public double AveragePartCount { get; init; } } /// /// 数量校验类型枚举。 /// public enum QuantityValidationType { /// /// 精确数量。 /// Exact = 0, /// /// 最少数量。 /// Minimum = 1, /// /// 范围数量。 /// Range = 2, /// /// 唯一数量。 /// Unique = 3 }