using System; using System.Data.Common; using System.Linq; using System.Threading; using System.Threading.Tasks; using GB5Shared.Attachment; using GB5Shared.DTO.Framework.Login; using GB5Shared.FileUpload; using Microsoft.Extensions.Logging; using AttachmentResolutionEnvelope = FrameworkDAL.DTO.ECM.AttachmentResolutionEnvelope; namespace FrameworkBLL.Attachment { public class AttachmentResolutionService : IAttachmentResolutionService { private readonly IAttachmentDAL _attachmentDAL; private readonly IAttachmentPathResolutionService _pathResolution; private readonly ILogger _logger; public AttachmentResolutionService( IAttachmentDAL attachmentDAL, IAttachmentPathResolutionService pathResolution, ILogger logger) { _attachmentDAL = attachmentDAL; _pathResolution = pathResolution; _logger = logger; } public async Task ResolveHeaderAsync( AttachmentResolutionEnvelope envelope, int entityId, LoginDTO login, DbTransaction tx, CancellationToken ct = default) { foreach (var mapping in envelope.LevelMappings) mapping.ObjectId = entityId; await ResolveAsync(envelope, login, tx, ct).ConfigureAwait(false); } public async Task ResolveAsync( AttachmentResolutionEnvelope envelope, LoginDTO login, DbTransaction tx, CancellationToken ct = default) { if (envelope is null) throw new ArgumentNullException(nameof(envelope)); var valid = envelope.LevelMappings .Where(m => m.ObjectId > 0 && m.AttachmentIds.Count > 0) .ToList(); if (valid.Count == 0) return; await _attachmentDAL .BulkResolveObjectIdAsync(valid, login, tx, ct) .ConfigureAwait(false); // Phase 2: move deferred attachment files to their real ObjectId paths if (envelope.ObjectHeaderTypeId > 0 && envelope.HeaderRowGuid != Guid.Empty) { await _pathResolution .ResolvePhase2Async(envelope.ObjectHeaderTypeId, envelope.HeaderRowGuid, login, tx, ct) .ConfigureAwait(false); } _logger.LogInformation( "AttachmentResolution: resolved {Count} level(s) for user {UserId}", valid.Count, login.UserId); } } }