using SCM.Application.Common; using SCM.Domain.Medicao; using SCM.Shared.Exceptions; namespace SCM.Application.Medicao; public sealed record LinhaBasePontoRequest(int Semana, DateOnly Data, decimal Percentual); public sealed record DefinirLinhaBaseRequest(IReadOnlyList Pontos); public sealed record LinhaBasePontoResponse(Guid Id, int Semana, DateOnly Data, decimal Percentual, string Origem); /// Contrato de persistência da linha de base (implementado em Infra). public interface ILinhaBaseRepository { Task> GetByEapAsync(Guid eapId, CancellationToken ct = default); Task EapExistsAsync(Guid eapId, CancellationToken ct = default); Task ReplaceAsync(Guid eapId, IReadOnlyList pontos, CancellationToken ct = default); Task DeletePontoAsync(Guid eapId, int semana, CancellationToken ct = default); } public interface ILinhaBaseService { Task> GetByEapAsync(Guid eapId, CancellationToken ct = default); Task> DefinirAsync(Guid eapId, DefinirLinhaBaseRequest request, CancellationToken ct = default); Task RemoverPontoAsync(Guid eapId, int semana, CancellationToken ct = default); } public sealed class LinhaBaseService : ILinhaBaseService { private readonly ILinhaBaseRepository _repository; private readonly IUnitOfWork _unitOfWork; public LinhaBaseService(ILinhaBaseRepository repository, IUnitOfWork unitOfWork) { _repository = repository; _unitOfWork = unitOfWork; } public async Task> GetByEapAsync(Guid eapId, CancellationToken ct = default) { if (!await _repository.EapExistsAsync(eapId, ct)) throw new NotFoundException($"EAP {eapId} não encontrada."); return (await _repository.GetByEapAsync(eapId, ct)).Select(ToResponse).ToList(); } public async Task> DefinirAsync( Guid eapId, DefinirLinhaBaseRequest request, CancellationToken ct = default) { if (!await _repository.EapExistsAsync(eapId, ct)) throw new NotFoundException($"EAP {eapId} não encontrada."); if (request.Pontos.Select(p => p.Semana).Distinct().Count() != request.Pontos.Count) throw new DomainException("Cada semana só pode ter um ponto na linha de base."); var pontos = request.Pontos .OrderBy(p => p.Semana) .Select(p => LinhaBasePonto.Create(eapId, p.Semana, p.Data, p.Percentual)) .ToList(); // Curva S: o percentual acumulado não pode decrescer com o avanço das semanas. for (var i = 1; i < pontos.Count; i++) if (pontos[i].Percentual < pontos[i - 1].Percentual) throw new DomainException( $"A curva é acumulada: a semana {pontos[i].Semana} ({pontos[i].Percentual}%) não pode ser menor que a semana {pontos[i - 1].Semana} ({pontos[i - 1].Percentual}%)."); await ExecuteInTransactionAsync(() => _repository.ReplaceAsync(eapId, pontos, ct)); return pontos.Select(ToResponse).ToList(); } public async Task RemoverPontoAsync(Guid eapId, int semana, CancellationToken ct = default) { if (!await _repository.EapExistsAsync(eapId, ct)) throw new NotFoundException($"EAP {eapId} não encontrada."); var removed = false; await ExecuteInTransactionAsync(async () => removed = await _repository.DeletePontoAsync(eapId, semana, ct)); if (!removed) throw new NotFoundException($"Ponto da semana {semana} não encontrado na linha de base."); } private static LinhaBasePontoResponse ToResponse(LinhaBasePonto p) => new(p.Id, p.Semana, p.Data, p.Percentual, p.Origem); private async Task ExecuteInTransactionAsync(Func action) { _unitOfWork.BeginTransaction(); try { await action(); _unitOfWork.Commit(); } catch { _unitOfWork.Rollback(); throw; } } }