KpiTargetRules.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Admin.NET.Plugin.AiDOP.Dto.SmartOps;
  2. using Admin.NET.Plugin.AiDOP.Entity;
  3. namespace Admin.NET.Plugin.AiDOP.SmartOps;
  4. public static class KpiTargetRules
  5. {
  6. public static readonly DateTime OpenEnd = new(9999, 12, 31);
  7. public static DateTime Normalize(DateTime value) => value.Date;
  8. public static DateTime EndOrMax(DateTime? effectiveTo) => (effectiveTo ?? OpenEnd).Date;
  9. public static bool Overlaps(DateTime aFrom, DateTime? aTo, DateTime bFrom, DateTime? bTo)
  10. {
  11. var aStart = aFrom.Date;
  12. var aEnd = EndOrMax(aTo);
  13. var bStart = bFrom.Date;
  14. var bEnd = EndOrMax(bTo);
  15. return aStart <= bEnd && bStart <= aEnd;
  16. }
  17. public static bool Covers(DateTime from, DateTime? to, DateTime bizDate)
  18. {
  19. var day = bizDate.Date;
  20. return from.Date <= day && day <= EndOrMax(to);
  21. }
  22. public static void EnsureTargetPositive(decimal targetValue)
  23. {
  24. if (targetValue <= 0)
  25. throw new KpiTargetValidationException("目标值必须大于 0");
  26. }
  27. public static void EnsureRange(DateTime from, DateTime? to)
  28. {
  29. if (to.HasValue && to.Value.Date < from.Date)
  30. throw new KpiTargetValidationException("失效日期不能早于生效日期");
  31. }
  32. public static AdoSmartOpsKpiTargetConfig PickSingle(
  33. IReadOnlyList<AdoSmartOpsKpiTargetConfig> matches,
  34. string scopeLabel)
  35. {
  36. if (matches.Count == 0)
  37. throw new InvalidOperationException("PickSingle 需要至少一条配置");
  38. if (matches.Count > 1)
  39. throw new KpiTargetDuplicateScopeException(
  40. $"{scopeLabel} 命中 {matches.Count} 条重叠目标,拒绝自动选择最新一条");
  41. return matches[0];
  42. }
  43. public static List<AdoSmartOpsKpiTargetConfig> ActiveOn(
  44. IEnumerable<AdoSmartOpsKpiTargetConfig> configs,
  45. DateTime bizDate)
  46. {
  47. return configs
  48. .Where(x => x.Status == 1 && Covers(x.EffectiveFrom, x.EffectiveTo, bizDate))
  49. .OrderBy(x => x.Id)
  50. .ToList();
  51. }
  52. public static KpiTargetDto ToDto(AdoSmartOpsKpiTargetConfig x) => new()
  53. {
  54. Id = x.Id,
  55. FactoryId = x.FactoryId,
  56. MetricCode = x.MetricCode,
  57. TargetValue = x.TargetValue,
  58. EffectiveFrom = x.EffectiveFrom.ToString("yyyy-MM-dd"),
  59. EffectiveTo = x.EffectiveTo?.ToString("yyyy-MM-dd"),
  60. SourceType = x.SourceType,
  61. SourceBatchId = x.SourceBatchId,
  62. Status = x.Status,
  63. Remark = x.Remark,
  64. CreateTime = x.CreateTime,
  65. UpdateTime = x.UpdateTime
  66. };
  67. }
  68. public sealed class KpiTargetValidationException : Exception
  69. {
  70. public KpiTargetValidationException(string message) : base(message) { }
  71. }
  72. public sealed class KpiTargetConflictException : Exception
  73. {
  74. public KpiTargetConflictException(string message) : base(message) { }
  75. }
  76. public sealed class KpiTargetDuplicateScopeException : Exception
  77. {
  78. public KpiTargetDuplicateScopeException(string message) : base(message) { }
  79. }
  80. public sealed class KpiTargetNotFoundException : Exception
  81. {
  82. public KpiTargetNotFoundException(string message) : base(message) { }
  83. }
  84. public sealed class KpiDemoImportForbiddenException : Exception
  85. {
  86. public KpiDemoImportForbiddenException(string message) : base(message) { }
  87. }