S8KpiTargetConfigService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Admin.NET.Plugin.AiDOP.Dto.S8;
  2. using Admin.NET.Plugin.AiDOP.Entity.S8;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  5. /// <summary>
  6. /// S9 KPI 目标值配置服务(S8-CONFIG-GLOBAL-ROW-SEMANTICS-AND-KPI-TARGET-1)。
  7. ///
  8. /// 复用 <c>ado_s8_monitor_metric</c>,不新建配置表;只维护 <c>default_target_ratio</c>(目标值)。
  9. /// 作用域语义与步骤 1 完全一致:平台默认(0/0)租户侧只读,需要调整走「自定义本工厂」覆盖,
  10. /// 「恢复平台默认」只删覆盖行。生效读取:工厂覆盖 &gt; 平台默认。
  11. ///
  12. /// 边界:
  13. /// - 只暴露 Result KPI(mechanism='RATIO' AND is_result_kpi=1),当前 5 项;
  14. /// 表里其余 8 行是监控指标字典,不属于业务目标值配置,不向业务前端暴露。
  15. /// - <b>只配置 TargetRatio。</b>KPI 当前值(CurrentValue)不是配置项,由业务计算/接口提供,本服务不涉及。
  16. /// </summary>
  17. public class S8KpiTargetConfigService : ITransient
  18. {
  19. /// <summary>目标值为百分比,合法区间 [0, 100]。</summary>
  20. public const decimal MinTargetRatio = 0m;
  21. public const decimal MaxTargetRatio = 100m;
  22. private readonly SqlSugarRepository<AdoS8MonitorMetric> _rep;
  23. public S8KpiTargetConfigService(SqlSugarRepository<AdoS8MonitorMetric> rep) => _rep = rep;
  24. /// <summary>Result KPI 判定:与 <see cref="S8MonitoringService.GetResultKpiSummaryAsync"/> 同口径。</summary>
  25. private ISugarQueryable<AdoS8MonitorMetric> ResultKpiQuery() =>
  26. _rep.AsQueryable().Where(x => x.Mechanism == "RATIO" && x.IsResultKpi);
  27. /// <summary>
  28. /// 列表:返回 5 项 Result KPI 的「生效目标值」视图。
  29. /// 有工厂覆盖 → Scope=FACTORY 且 TargetRatio 取覆盖值;否则 Scope=GLOBAL 取平台默认值。
  30. /// 不返回 TenantId / FactoryId / data_source 等技术字段。
  31. /// </summary>
  32. public async Task<List<AdoS8KpiTargetItemDto>> ListAsync(S8TrustedScope scope)
  33. {
  34. var rows = await ResultKpiQuery()
  35. .Where(x => x.TenantId == S8ConfigScope.GlobalTenantId || x.TenantId == scope.TenantId)
  36. .ToListAsync();
  37. var globals = rows
  38. .Where(x => S8ConfigScope.IsGlobal(x.TenantId, x.FactoryId))
  39. .ToDictionary(x => x.MetricCode);
  40. var overrides = rows
  41. .Where(x => !S8ConfigScope.IsGlobal(x.TenantId, x.FactoryId))
  42. .ToDictionary(x => x.MetricCode);
  43. // 以平台默认行为骨架:平台没有定义的指标不应凭工厂覆盖凭空出现。
  44. return globals.Values
  45. .OrderBy(x => x.SortNo).ThenBy(x => x.MetricCode)
  46. .Select(g =>
  47. {
  48. overrides.TryGetValue(g.MetricCode, out var ov);
  49. return new AdoS8KpiTargetItemDto
  50. {
  51. MetricCode = g.MetricCode,
  52. MetricName = g.MetricName,
  53. Unit = string.IsNullOrWhiteSpace(g.Unit) ? "%" : g.Unit!,
  54. TargetRatio = ov?.DefaultTargetRatio ?? g.DefaultTargetRatio,
  55. GlobalTargetRatio = g.DefaultTargetRatio,
  56. Remark = ov?.Remark ?? g.Remark,
  57. Scope = ov != null ? S8ConfigScope.Factory : S8ConfigScope.Global,
  58. HasFactoryOverride = ov != null,
  59. };
  60. })
  61. .ToList();
  62. }
  63. /// <summary>
  64. /// 自定义本工厂 / 编辑本工厂目标值(同一入口,按业务键 metricCode 定位,不暴露 DB Id)。
  65. /// 无覆盖行 → 复制平台默认并盖章可信作用域后新建;已有覆盖行 → 就地更新目标值。
  66. /// 归属列一律由服务端盖章,不接受客户端传入。
  67. /// </summary>
  68. public async Task<AdoS8KpiTargetItemDto> UpsertFactoryOverrideAsync(
  69. string metricCode, decimal targetRatio, string? remark, S8TrustedScope scope)
  70. {
  71. if (string.IsNullOrWhiteSpace(metricCode))
  72. throw new S8BizException("指标编码必填");
  73. ValidateTarget(targetRatio);
  74. var code = metricCode.Trim();
  75. var global = await ResultKpiQuery()
  76. .Where(x => x.MetricCode == code
  77. && x.TenantId == S8ConfigScope.GlobalTenantId
  78. && x.FactoryId == S8ConfigScope.GlobalFactoryId)
  79. .FirstAsync() ?? throw new S8NotFoundException("平台默认指标不存在");
  80. var existing = await _rep.AsQueryable()
  81. .Where(x => x.MetricCode == code
  82. && x.TenantId == scope.TenantId)
  83. .FirstAsync();
  84. if (existing != null)
  85. {
  86. existing.DefaultTargetRatio = targetRatio;
  87. if (remark != null) existing.Remark = NormalizeOrNull(remark);
  88. existing.UpdatedAt = DateTime.Now;
  89. await _rep.UpdateAsync(existing);
  90. }
  91. else
  92. {
  93. var copy = CloneForFactory(global, scope);
  94. copy.DefaultTargetRatio = targetRatio;
  95. if (remark != null) copy.Remark = NormalizeOrNull(remark);
  96. copy.Id = await _rep.AsInsertable(copy).ExecuteReturnBigIdentityAsync();
  97. }
  98. return (await ListAsync(scope)).First(x => x.MetricCode == code);
  99. }
  100. /// <summary>
  101. /// 恢复平台默认:只删除当前工厂覆盖行,平台默认行不受影响;删除后读取自动回落平台默认。
  102. /// 本就没有覆盖行时按「不存在」处理(幂等语义由调用方 404 表达)。
  103. /// </summary>
  104. public async Task<AdoS8KpiTargetItemDto> ResetToGlobalDefaultAsync(string metricCode, S8TrustedScope scope)
  105. {
  106. if (string.IsNullOrWhiteSpace(metricCode))
  107. throw new S8BizException("指标编码必填");
  108. var code = metricCode.Trim();
  109. var existing = await _rep.AsQueryable()
  110. .Where(x => x.MetricCode == code
  111. && x.TenantId == scope.TenantId)
  112. .FirstAsync() ?? throw new S8NotFoundException("当前工厂没有覆盖配置,无需恢复");
  113. await _rep.DeleteByIdAsync(existing.Id);
  114. return (await ListAsync(scope)).First(x => x.MetricCode == code);
  115. }
  116. private static void ValidateTarget(decimal targetRatio)
  117. {
  118. if (targetRatio < MinTargetRatio || targetRatio > MaxTargetRatio)
  119. throw new S8BizException($"目标值必须在 {MinTargetRatio:0}–{MaxTargetRatio:0} 之间");
  120. }
  121. private static string? NormalizeOrNull(string? v) =>
  122. string.IsNullOrWhiteSpace(v) ? null : v.Trim();
  123. /// <summary>复制平台默认行的字典字段,归属由服务端盖章;不复制 Id / 时间戳。</summary>
  124. private static AdoS8MonitorMetric CloneForFactory(AdoS8MonitorMetric g, S8TrustedScope scope) => new()
  125. {
  126. Id = 0,
  127. TenantId = scope.TenantId,
  128. FactoryId = scope.LegacyFactoryId,
  129. ObjectCode = g.ObjectCode,
  130. MetricCode = g.MetricCode,
  131. MetricName = g.MetricName,
  132. Mechanism = g.Mechanism,
  133. Unit = g.Unit,
  134. DueAtField = g.DueAtField,
  135. StatusField = g.StatusField,
  136. MeasuredValueField = g.MeasuredValueField,
  137. ObjectIdField = g.ObjectIdField,
  138. ObjectCodeField = g.ObjectCodeField,
  139. ObjectNameField = g.ObjectNameField,
  140. DefaultGraceMinutes = g.DefaultGraceMinutes,
  141. DefaultCompletedStates = g.DefaultCompletedStates,
  142. DefaultTargetRatio = g.DefaultTargetRatio,
  143. DefaultLowerBound = g.DefaultLowerBound,
  144. DefaultUpperBound = g.DefaultUpperBound,
  145. IsResultKpi = g.IsResultKpi,
  146. Enabled = g.Enabled,
  147. SortNo = g.SortNo,
  148. Remark = g.Remark,
  149. CreatedAt = DateTime.Now,
  150. UpdatedAt = null,
  151. };
  152. }