using Admin.NET.Plugin.AiDOP.DataPlatform; using Admin.NET.Plugin.AiDOP.DataPlatform.S1Refresh; using Admin.NET.Plugin.AiDOP.FinishedWarehouse; using Admin.NET.Plugin.AiDOP.Manufacturing; using Admin.NET.Plugin.AiDOP.MaterialWarehouse; using Admin.NET.Plugin.AiDOP.Order; using Admin.NET.Plugin.AiDOP.ProcurementExecution; using Admin.NET.Plugin.AiDOP.Production; using Admin.NET.Plugin.AiDOP.Supply; using System.Text.Json; namespace Admin.NET.Plugin.AiDOP.DataPlatform.MdpRebuild; public interface IModuleRebuildHandler { string ModuleCode { get; } Task RunAsync( MdpRebuildScope scope, string triggerType, long jobId, Func report, CancellationToken cancellationToken); } public sealed class S1ModuleRebuildHandler : IModuleRebuildHandler, ITransient { public string ModuleCode => "S1"; private readonly S1MdpSyncTransformService _transform; public S1ModuleRebuildHandler(S1MdpSyncTransformService transform) => _transform = transform; public async Task RunAsync( MdpRebuildScope scope, string triggerType, long jobId, Func report, CancellationToken cancellationToken) { var result = await _transform.RunFullAsync( S1MdpRunScope.Create(scope.TenantId, scope.FactoryId), cancellationToken, triggerType, jobId, update => report(new ModuleProgressUpdate(update.Stage, update.StageIndex, update.ProgressPercent, update.Message, update.Rows, update.CompletedStage))); return Map(result.BatchId, result.RunLogId, result.StageRows, result.StandardRows, result.DwdRows, result.KpiRows, result.AtomicRows); } private static ModuleRebuildResult Map(string batchId, long runLogId, int stage, int standard, int dwd, int kpi, int atomic) => new() { BatchId = batchId, RunLogId = runLogId, StageRows = stage, StandardRows = standard, DwdRows = dwd, KpiRows = kpi, AtomicRows = atomic }; } public sealed class S2ModuleRebuildHandler : IModuleRebuildHandler, ITransient { public string ModuleCode => "S2"; private readonly S2MdpSyncTransformService _transform; public S2ModuleRebuildHandler(S2MdpSyncTransformService transform) => _transform = transform; public async Task RunAsync( MdpRebuildScope scope, string triggerType, long jobId, Func report, CancellationToken cancellationToken) { var result = await _transform.RunFullAsync(scope, cancellationToken, triggerType, jobId, report); return new ModuleRebuildResult { BatchId = result.BatchId, RunLogId = result.RunLogId, StageRows = result.StageRows, StandardRows = result.StandardRows, DwdRows = result.DwdRows, KpiRows = result.KpiRows, AtomicRows = result.AtomicRows }; } } public sealed class S3ModuleRebuildHandler : IModuleRebuildHandler, ITransient { public string ModuleCode => "S3"; private readonly S3MdpSyncTransformService _transform; public S3ModuleRebuildHandler(S3MdpSyncTransformService transform) => _transform = transform; public async Task RunAsync( MdpRebuildScope scope, string triggerType, long jobId, Func report, CancellationToken cancellationToken) { var result = await _transform.RunFullAsync(scope, cancellationToken, triggerType, jobId, report); return new ModuleRebuildResult { BatchId = result.BatchId, RunLogId = result.RunLogId, StageRows = result.StageRows, StandardRows = result.StandardRows, DwdRows = result.DwdRows, KpiRows = result.KpiRows, AtomicRows = result.AtomicRows }; } } public sealed class S4ModuleRebuildHandler : IModuleRebuildHandler, ITransient { public string ModuleCode => "S4"; private readonly S4MdpSyncTransformService _transform; public S4ModuleRebuildHandler(S4MdpSyncTransformService transform) => _transform = transform; public async Task RunAsync( MdpRebuildScope scope, string triggerType, long jobId, Func report, CancellationToken cancellationToken) { var result = await _transform.RunFullAsync(scope, cancellationToken, triggerType, jobId, report); return new ModuleRebuildResult { BatchId = result.BatchId, RunLogId = result.RunLogId, StageRows = result.StageRows, StandardRows = result.StandardRows, DwdRows = result.DwdRows, KpiRows = result.KpiRows }; } } public abstract class T8ModuleRebuildHandlerBase : IModuleRebuildHandler { public abstract string ModuleCode { get; } private readonly AidopT8KpiManualRefreshService _refresh; protected T8ModuleRebuildHandlerBase(AidopT8KpiManualRefreshService refresh) => _refresh = refresh; public async Task RunAsync( MdpRebuildScope scope, string triggerType, long jobId, Func report, CancellationToken cancellationToken) { _ = triggerType; _ = jobId; var result = await _refresh.RunModuleRefreshAsync(ModuleCode, cancellationToken, scope, report); if (string.Equals(result.OverallStatus, "REFRESHING", StringComparison.OrdinalIgnoreCase)) throw new ModuleRebuildAlreadyRunningException(ModuleCode); if (!result.Ok) throw new InvalidOperationException(result.Message ?? $"{ModuleCode} 数据重算失败"); return new ModuleRebuildResult { BatchId = result.BatchId ?? string.Empty, StageRows = result.Inbound.StageRows, StandardRows = result.Inbound.StandardRows, DwdRows = result.Transform.DwdRows, KpiRows = result.Transform.KpiRows, DetailJson = JsonSerializer.Serialize(new { perKpi = result.PerKpi, inboundOk = result.Inbound.Ok, transformOk = result.Transform.Ok, overallStatus = result.OverallStatus }) }; } } public sealed class S5ModuleRebuildHandler : T8ModuleRebuildHandlerBase, ITransient { public override string ModuleCode => "S5"; public S5ModuleRebuildHandler(AidopT8KpiManualRefreshService refresh) : base(refresh) { } } public sealed class S6ModuleRebuildHandler : T8ModuleRebuildHandlerBase, ITransient { public override string ModuleCode => "S6"; public S6ModuleRebuildHandler(AidopT8KpiManualRefreshService refresh) : base(refresh) { } } public sealed class S7ModuleRebuildHandler : T8ModuleRebuildHandlerBase, ITransient { public override string ModuleCode => "S7"; public S7ModuleRebuildHandler(AidopT8KpiManualRefreshService refresh) : base(refresh) { } }