RuleConfigService.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. namespace Admin.NET.Plugin.AiDOP.Rule;
  2. /// <summary>
  3. /// 全模块规则配置只读服务骨架。P21 阶段不访问数据库,仅提供默认值解析入口。
  4. /// </summary>
  5. public class RuleConfigService : ITransient
  6. {
  7. private const string DefaultSource = "default";
  8. private const string ConfiguredSource = "configured";
  9. private readonly IRuleConfigRepository _repository;
  10. public RuleConfigService(IRuleConfigRepository? repository = null)
  11. {
  12. _repository = repository ?? new EmptyRuleConfigRepository();
  13. }
  14. public async Task<RuleConfigResolveResult<TOptions>> ResolveOptionsAsync<TOptions>(
  15. string moduleCode,
  16. string scenarioCode,
  17. long tenantId,
  18. long? factoryId,
  19. TOptions defaults,
  20. CancellationToken cancellationToken = default)
  21. where TOptions : class, new()
  22. {
  23. cancellationToken.ThrowIfCancellationRequested();
  24. var layers = await _repository.GetEffectiveLayersAsync(moduleCode, scenarioCode, tenantId, factoryId, cancellationToken);
  25. return RuleConfigResolver.Resolve(defaults, layers);
  26. }
  27. public Task<RuleConfigResolveResult<S3DeliveryGenerateRuleOptions>> ResolveS3DeliveryGenerateOptionsAsync(
  28. long tenantId,
  29. long? factoryId,
  30. CancellationToken cancellationToken = default)
  31. {
  32. return ResolveOptionsAsync(
  33. "S3",
  34. "DELIVERY_GENERATE",
  35. tenantId,
  36. factoryId,
  37. new S3DeliveryGenerateRuleOptions(),
  38. cancellationToken);
  39. }
  40. public async Task<RuleConfigSnapshot<S3DeliveryGenerateRuleOptions>> ResolveS3DeliveryGenerateSnapshotAsync(
  41. long tenantId,
  42. long? factoryId,
  43. DateTime? resolvedAt = null,
  44. CancellationToken cancellationToken = default)
  45. {
  46. var result = await ResolveS3DeliveryGenerateOptionsAsync(tenantId, factoryId, cancellationToken);
  47. if (!result.Success)
  48. {
  49. throw Oops.Oh($"S3交货单生成规则配置无效:{string.Join(";", result.Errors)}");
  50. }
  51. return CreateSnapshot("S3", "DELIVERY_GENERATE", tenantId, factoryId, result, resolvedAt ?? DateTime.Now);
  52. }
  53. public RuleConfigSnapshot<TOptions> CreateSnapshot<TOptions>(
  54. string moduleCode,
  55. string scenarioCode,
  56. long tenantId,
  57. long? factoryId,
  58. RuleConfigResolveResult<TOptions> result,
  59. DateTime resolvedAt)
  60. where TOptions : class, new()
  61. {
  62. return new RuleConfigSnapshot<TOptions>
  63. {
  64. ModuleCode = moduleCode,
  65. ScenarioCode = scenarioCode,
  66. Source = result.AppliedItems.Count == 0 ? DefaultSource : ConfiguredSource,
  67. ResolvedAt = resolvedAt,
  68. TenantId = tenantId,
  69. FactoryId = factoryId,
  70. Options = result.Options,
  71. AppliedItems = result.AppliedItems,
  72. Warnings = result.Warnings
  73. };
  74. }
  75. }