ModuleRebuildHandlers.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Admin.NET.Plugin.AiDOP.DataPlatform;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.S1Refresh;
  3. using Admin.NET.Plugin.AiDOP.FinishedWarehouse;
  4. using Admin.NET.Plugin.AiDOP.Manufacturing;
  5. using Admin.NET.Plugin.AiDOP.MaterialWarehouse;
  6. using Admin.NET.Plugin.AiDOP.Order;
  7. using Admin.NET.Plugin.AiDOP.ProcurementExecution;
  8. using Admin.NET.Plugin.AiDOP.Production;
  9. using Admin.NET.Plugin.AiDOP.Supply;
  10. using System.Text.Json;
  11. namespace Admin.NET.Plugin.AiDOP.DataPlatform.MdpRebuild;
  12. public interface IModuleRebuildHandler
  13. {
  14. string ModuleCode { get; }
  15. Task<ModuleRebuildResult> RunAsync(
  16. MdpRebuildScope scope,
  17. string triggerType,
  18. long jobId,
  19. Func<ModuleProgressUpdate, Task> report,
  20. CancellationToken cancellationToken);
  21. }
  22. public sealed class S1ModuleRebuildHandler : IModuleRebuildHandler, ITransient
  23. {
  24. public string ModuleCode => "S1";
  25. private readonly S1MdpSyncTransformService _transform;
  26. public S1ModuleRebuildHandler(S1MdpSyncTransformService transform) => _transform = transform;
  27. public async Task<ModuleRebuildResult> RunAsync(
  28. MdpRebuildScope scope, string triggerType, long jobId, Func<ModuleProgressUpdate, Task> report, CancellationToken cancellationToken)
  29. {
  30. var result = await _transform.RunFullAsync(
  31. S1MdpRunScope.Create(scope.TenantId, scope.FactoryId),
  32. cancellationToken,
  33. triggerType,
  34. jobId,
  35. update => report(new ModuleProgressUpdate(update.Stage, update.StageIndex, update.ProgressPercent, update.Message, update.Rows, update.CompletedStage)));
  36. return Map(result.BatchId, result.RunLogId, result.StageRows, result.StandardRows, result.DwdRows, result.KpiRows, result.AtomicRows);
  37. }
  38. private static ModuleRebuildResult Map(string batchId, long runLogId, int stage, int standard, int dwd, int kpi, int atomic) => new()
  39. {
  40. BatchId = batchId,
  41. RunLogId = runLogId,
  42. StageRows = stage,
  43. StandardRows = standard,
  44. DwdRows = dwd,
  45. KpiRows = kpi,
  46. AtomicRows = atomic
  47. };
  48. }
  49. public sealed class S2ModuleRebuildHandler : IModuleRebuildHandler, ITransient
  50. {
  51. public string ModuleCode => "S2";
  52. private readonly S2MdpSyncTransformService _transform;
  53. public S2ModuleRebuildHandler(S2MdpSyncTransformService transform) => _transform = transform;
  54. public async Task<ModuleRebuildResult> RunAsync(
  55. MdpRebuildScope scope, string triggerType, long jobId, Func<ModuleProgressUpdate, Task> report, CancellationToken cancellationToken)
  56. {
  57. var result = await _transform.RunFullAsync(scope, cancellationToken, triggerType, jobId, report);
  58. return new ModuleRebuildResult
  59. {
  60. BatchId = result.BatchId,
  61. RunLogId = result.RunLogId,
  62. StageRows = result.StageRows,
  63. StandardRows = result.StandardRows,
  64. DwdRows = result.DwdRows,
  65. KpiRows = result.KpiRows,
  66. AtomicRows = result.AtomicRows
  67. };
  68. }
  69. }
  70. public sealed class S3ModuleRebuildHandler : IModuleRebuildHandler, ITransient
  71. {
  72. public string ModuleCode => "S3";
  73. private readonly S3MdpSyncTransformService _transform;
  74. public S3ModuleRebuildHandler(S3MdpSyncTransformService transform) => _transform = transform;
  75. public async Task<ModuleRebuildResult> RunAsync(
  76. MdpRebuildScope scope, string triggerType, long jobId, Func<ModuleProgressUpdate, Task> report, CancellationToken cancellationToken)
  77. {
  78. var result = await _transform.RunFullAsync(scope, cancellationToken, triggerType, jobId, report);
  79. return new ModuleRebuildResult
  80. {
  81. BatchId = result.BatchId,
  82. RunLogId = result.RunLogId,
  83. StageRows = result.StageRows,
  84. StandardRows = result.StandardRows,
  85. DwdRows = result.DwdRows,
  86. KpiRows = result.KpiRows,
  87. AtomicRows = result.AtomicRows
  88. };
  89. }
  90. }
  91. public sealed class S4ModuleRebuildHandler : IModuleRebuildHandler, ITransient
  92. {
  93. public string ModuleCode => "S4";
  94. private readonly S4MdpSyncTransformService _transform;
  95. public S4ModuleRebuildHandler(S4MdpSyncTransformService transform) => _transform = transform;
  96. public async Task<ModuleRebuildResult> RunAsync(
  97. MdpRebuildScope scope, string triggerType, long jobId, Func<ModuleProgressUpdate, Task> report, CancellationToken cancellationToken)
  98. {
  99. var result = await _transform.RunFullAsync(scope, cancellationToken, triggerType, jobId, report);
  100. return new ModuleRebuildResult
  101. {
  102. BatchId = result.BatchId,
  103. RunLogId = result.RunLogId,
  104. StageRows = result.StageRows,
  105. StandardRows = result.StandardRows,
  106. DwdRows = result.DwdRows,
  107. KpiRows = result.KpiRows
  108. };
  109. }
  110. }
  111. public abstract class T8ModuleRebuildHandlerBase : IModuleRebuildHandler
  112. {
  113. public abstract string ModuleCode { get; }
  114. private readonly AidopT8KpiManualRefreshService _refresh;
  115. protected T8ModuleRebuildHandlerBase(AidopT8KpiManualRefreshService refresh) => _refresh = refresh;
  116. public async Task<ModuleRebuildResult> RunAsync(
  117. MdpRebuildScope scope, string triggerType, long jobId, Func<ModuleProgressUpdate, Task> report, CancellationToken cancellationToken)
  118. {
  119. _ = triggerType;
  120. _ = jobId;
  121. var result = await _refresh.RunModuleRefreshAsync(ModuleCode, cancellationToken, scope, report);
  122. if (string.Equals(result.OverallStatus, "REFRESHING", StringComparison.OrdinalIgnoreCase))
  123. throw new ModuleRebuildAlreadyRunningException(ModuleCode);
  124. if (!result.Ok)
  125. throw new InvalidOperationException(result.Message ?? $"{ModuleCode} 数据重算失败");
  126. return new ModuleRebuildResult
  127. {
  128. BatchId = result.BatchId ?? string.Empty,
  129. StageRows = result.Inbound.StageRows,
  130. StandardRows = result.Inbound.StandardRows,
  131. DwdRows = result.Transform.DwdRows,
  132. KpiRows = result.Transform.KpiRows,
  133. DetailJson = JsonSerializer.Serialize(new
  134. {
  135. perKpi = result.PerKpi,
  136. inboundOk = result.Inbound.Ok,
  137. transformOk = result.Transform.Ok,
  138. overallStatus = result.OverallStatus
  139. })
  140. };
  141. }
  142. }
  143. public sealed class S5ModuleRebuildHandler : T8ModuleRebuildHandlerBase, ITransient
  144. {
  145. public override string ModuleCode => "S5";
  146. public S5ModuleRebuildHandler(AidopT8KpiManualRefreshService refresh) : base(refresh) { }
  147. }
  148. public sealed class S6ModuleRebuildHandler : T8ModuleRebuildHandlerBase, ITransient
  149. {
  150. public override string ModuleCode => "S6";
  151. public S6ModuleRebuildHandler(AidopT8KpiManualRefreshService refresh) : base(refresh) { }
  152. }
  153. public sealed class S7ModuleRebuildHandler : T8ModuleRebuildHandlerBase, ITransient
  154. {
  155. public override string ModuleCode => "S7";
  156. public S7ModuleRebuildHandler(AidopT8KpiManualRefreshService refresh) : base(refresh) { }
  157. }