| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- using SqlSugar;
- namespace Admin.NET.Plugin.AiDOP.Service.S8.Rules.DataAccess;
- /// <summary>
- /// LEGACY_SQL 取数抽象。抽出接口是为了让"STANDARD_DATASET 绝不回落 Legacy"
- /// 这条边界可以被测试真实验证(注入探针后断言调用次数为 0),而不是只靠代码审阅。
- /// </summary>
- public interface IS8LegacySqlDataProvider
- {
- Task<S8MonitoringDataResult> LoadAsync(
- long tenantId,
- long factoryId,
- AdoS8WatchRule rule,
- string ruleType,
- int timeoutSeconds,
- CancellationToken cancellationToken = default);
- }
- /// <summary>
- /// LEGACY_SQL 取数适配器:把既有"规则自带 expression + data_source_id"的取数结果
- /// 转换为统一的 canonical 行集合,使 evaluator 不再直接依赖 <see cref="System.Data.DataTable"/>。
- ///
- /// 本类**逐行保留迁移前的行为**(数据源查询条件、失败 reason 码、异常消息措辞),
- /// 132 条历史规则的运行结果因此不发生变化。
- /// </summary>
- public class S8LegacySqlDataProvider : IS8LegacySqlDataProvider, ITransient
- {
- private readonly SqlSugarRepository<AdoS8DataSource> _dataSourceRep;
- private readonly S8DataSourceRowLoader _rowLoader;
- public S8LegacySqlDataProvider(
- SqlSugarRepository<AdoS8DataSource> dataSourceRep,
- S8DataSourceRowLoader rowLoader)
- {
- _dataSourceRep = dataSourceRep;
- _rowLoader = rowLoader;
- }
- /// <summary>
- /// 按规则的 data_source_id + expression 取数。
- /// 数据源不可用 → data_source_unavailable;SQL/API 执行失败 → query_failed。两者 reason 码与迁移前一致。
- /// </summary>
- public async Task<S8MonitoringDataResult> LoadAsync(
- long tenantId,
- long factoryId,
- AdoS8WatchRule rule,
- string ruleType,
- int timeoutSeconds,
- CancellationToken cancellationToken = default)
- {
- var dataSource = await _dataSourceRep.AsQueryable()
- .Where(x => x.Id == rule.DataSourceId
- && x.TenantId == tenantId
- && x.FactoryId == factoryId
- && x.Enabled)
- .FirstAsync();
- if (dataSource == null
- || string.IsNullOrWhiteSpace(dataSource.Endpoint)
- || !S8DataSourceRowLoader.IsSupportedType(dataSource.Type))
- {
- throw new S8RuleEvaluatorException(
- "data_source_unavailable",
- $"{ruleType} 规则 {rule.RuleCode} 数据源不可用(id={rule.DataSourceId})");
- }
- System.Data.DataTable table;
- try
- {
- table = await _rowLoader.LoadAsync(
- dataSource,
- rule.Expression!,
- _dataSourceRep.Context.CurrentConnectionConfig.DbType,
- timeoutSeconds,
- cancellationToken);
- }
- catch (Exception ex)
- {
- throw new S8RuleEvaluatorException(
- "query_failed",
- $"{ruleType} 规则 {rule.RuleCode} 取数失败:{ex.Message}",
- ex);
- }
- return new S8MonitoringDataResult
- {
- RowSet = S8MonitoringRowSet.FromDataTable(table),
- DataSourceId = dataSource.Id,
- DataAccessMode = S8DataAccessMode.LegacySql
- };
- }
- }
|