| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- namespace Admin.NET.Plugin.AiDOP.Rule;
- /// <summary>
- /// 全模块规则配置只读服务骨架。P21 阶段不访问数据库,仅提供默认值解析入口。
- /// </summary>
- public class RuleConfigService : ITransient
- {
- private const string DefaultSource = "default";
- private const string ConfiguredSource = "configured";
- private readonly IRuleConfigRepository _repository;
- public RuleConfigService(IRuleConfigRepository? repository = null)
- {
- _repository = repository ?? new EmptyRuleConfigRepository();
- }
- public async Task<RuleConfigResolveResult<TOptions>> ResolveOptionsAsync<TOptions>(
- string moduleCode,
- string scenarioCode,
- long tenantId,
- long? factoryId,
- TOptions defaults,
- CancellationToken cancellationToken = default)
- where TOptions : class, new()
- {
- cancellationToken.ThrowIfCancellationRequested();
- var layers = await _repository.GetEffectiveLayersAsync(moduleCode, scenarioCode, tenantId, factoryId, cancellationToken);
- return RuleConfigResolver.Resolve(defaults, layers);
- }
- public Task<RuleConfigResolveResult<S3DeliveryGenerateRuleOptions>> ResolveS3DeliveryGenerateOptionsAsync(
- long tenantId,
- long? factoryId,
- CancellationToken cancellationToken = default)
- {
- return ResolveOptionsAsync(
- "S3",
- "DELIVERY_GENERATE",
- tenantId,
- factoryId,
- new S3DeliveryGenerateRuleOptions(),
- cancellationToken);
- }
- public async Task<RuleConfigSnapshot<S3DeliveryGenerateRuleOptions>> ResolveS3DeliveryGenerateSnapshotAsync(
- long tenantId,
- long? factoryId,
- DateTime? resolvedAt = null,
- CancellationToken cancellationToken = default)
- {
- var result = await ResolveS3DeliveryGenerateOptionsAsync(tenantId, factoryId, cancellationToken);
- if (!result.Success)
- {
- throw Oops.Oh($"S3交货单生成规则配置无效:{string.Join(";", result.Errors)}");
- }
- return CreateSnapshot("S3", "DELIVERY_GENERATE", tenantId, factoryId, result, resolvedAt ?? DateTime.Now);
- }
- public RuleConfigSnapshot<TOptions> CreateSnapshot<TOptions>(
- string moduleCode,
- string scenarioCode,
- long tenantId,
- long? factoryId,
- RuleConfigResolveResult<TOptions> result,
- DateTime resolvedAt)
- where TOptions : class, new()
- {
- return new RuleConfigSnapshot<TOptions>
- {
- ModuleCode = moduleCode,
- ScenarioCode = scenarioCode,
- Source = result.AppliedItems.Count == 0 ? DefaultSource : ConfiguredSource,
- ResolvedAt = resolvedAt,
- TenantId = tenantId,
- FactoryId = factoryId,
- Options = result.Options,
- AppliedItems = result.AppliedItems,
- Warnings = result.Warnings
- };
- }
- }
|