From bb6f1fc0447a3acc0c07383205cbfe124d151ffc Mon Sep 17 00:00:00 2001 From: Eduardo Lopes <155753879+eduardolopesx03@users.noreply.github.com> Date: Fri, 30 Jan 2026 10:57:21 -0300 Subject: [PATCH] Add bulk line creation quantity --- Controllers/LinesController.cs | 108 +++++++++++++++++++-------------- Dtos/CreateMobileLineDto.cs | 3 +- 2 files changed, 64 insertions(+), 47 deletions(-) diff --git a/Controllers/LinesController.cs b/Controllers/LinesController.cs index ce0d9bf..fcea6e7 100644 --- a/Controllers/LinesController.cs +++ b/Controllers/LinesController.cs @@ -350,68 +350,78 @@ namespace line_gestao_api.Controllers if (string.IsNullOrWhiteSpace(req.Cliente)) return BadRequest(new { message = "O nome do Cliente é obrigatório." }); - if (string.IsNullOrWhiteSpace(req.Linha)) - return BadRequest(new { message = "O número da Linha é obrigatório." }); + var quantidade = req.QtdLinhas.GetValueOrDefault(1); + if (quantidade < 1) + return BadRequest(new { message = "A quantidade de linhas deve ser maior que zero." }); var linhaLimpa = OnlyDigits(req.Linha); var chipLimpo = OnlyDigits(req.Chip); - if (string.IsNullOrWhiteSpace(linhaLimpa)) + if (quantidade == 1 && string.IsNullOrWhiteSpace(req.Linha)) + return BadRequest(new { message = "O número da Linha é obrigatório." }); + + if (!string.IsNullOrWhiteSpace(req.Linha) && string.IsNullOrWhiteSpace(linhaLimpa)) return BadRequest(new { message = "Número de linha inválido." }); - var exists = await _db.MobileLines.AsNoTracking().AnyAsync(x => x.Linha == linhaLimpa); - if (exists) - return Conflict(new { message = $"A linha {req.Linha} já está cadastrada no sistema." }); + if (!string.IsNullOrWhiteSpace(linhaLimpa)) + { + var exists = await _db.MobileLines.AsNoTracking().AnyAsync(x => x.Linha == linhaLimpa); + if (exists) + return Conflict(new { message = $"A linha {req.Linha} já está cadastrada no sistema." }); + } var maxItem = await _db.MobileLines.MaxAsync(x => (int?)x.Item) ?? 0; - var nextItem = maxItem + 1; - var now = DateTime.UtcNow; + var created = new List(quantidade); - var newLine = new MobileLine + for (var i = 0; i < quantidade; i++) { - Id = Guid.NewGuid(), - Item = nextItem, - Cliente = req.Cliente.Trim().ToUpper(), - Linha = linhaLimpa, - Chip = string.IsNullOrWhiteSpace(chipLimpo) ? null : chipLimpo, - Usuario = req.Usuario?.Trim(), - Status = req.Status?.Trim(), - Skil = req.Skil?.Trim(), - Modalidade = req.Modalidade?.Trim(), - PlanoContrato = req.PlanoContrato?.Trim(), - Conta = req.Conta?.Trim(), - VencConta = req.VencConta?.Trim(), + var newLine = new MobileLine + { + Id = Guid.NewGuid(), + Item = maxItem + 1 + i, + Cliente = req.Cliente.Trim().ToUpper(), + Linha = i == 0 ? linhaLimpa : null, + Chip = i == 0 && !string.IsNullOrWhiteSpace(chipLimpo) ? chipLimpo : null, + Usuario = req.Usuario?.Trim(), + Status = req.Status?.Trim(), + Skil = req.Skil?.Trim(), + Modalidade = req.Modalidade?.Trim(), + PlanoContrato = req.PlanoContrato?.Trim(), + Conta = req.Conta?.Trim(), + VencConta = req.VencConta?.Trim(), - DataBloqueio = ToUtc(req.DataBloqueio), - DataEntregaOpera = ToUtc(req.DataEntregaOpera), - DataEntregaCliente = ToUtc(req.DataEntregaCliente), + DataBloqueio = ToUtc(req.DataBloqueio), + DataEntregaOpera = ToUtc(req.DataEntregaOpera), + DataEntregaCliente = ToUtc(req.DataEntregaCliente), - Cedente = req.Cedente?.Trim(), - Solicitante = req.Solicitante?.Trim(), + Cedente = req.Cedente?.Trim(), + Solicitante = req.Solicitante?.Trim(), - FranquiaVivo = req.FranquiaVivo, - ValorPlanoVivo = req.ValorPlanoVivo, - GestaoVozDados = req.GestaoVozDados, - Skeelo = req.Skeelo, - VivoNewsPlus = req.VivoNewsPlus, - VivoTravelMundo = req.VivoTravelMundo, - VivoGestaoDispositivo = req.VivoGestaoDispositivo, - ValorContratoVivo = req.ValorContratoVivo, - FranquiaLine = req.FranquiaLine, - FranquiaGestao = req.FranquiaGestao, - LocacaoAp = req.LocacaoAp, - ValorContratoLine = req.ValorContratoLine, - Desconto = req.Desconto, - Lucro = req.Lucro, + FranquiaVivo = req.FranquiaVivo, + ValorPlanoVivo = req.ValorPlanoVivo, + GestaoVozDados = req.GestaoVozDados, + Skeelo = req.Skeelo, + VivoNewsPlus = req.VivoNewsPlus, + VivoTravelMundo = req.VivoTravelMundo, + VivoGestaoDispositivo = req.VivoGestaoDispositivo, + ValorContratoVivo = req.ValorContratoVivo, + FranquiaLine = req.FranquiaLine, + FranquiaGestao = req.FranquiaGestao, + LocacaoAp = req.LocacaoAp, + ValorContratoLine = req.ValorContratoLine, + Desconto = req.Desconto, + Lucro = req.Lucro, - CreatedAt = now, - UpdatedAt = now - }; + CreatedAt = now, + UpdatedAt = now + }; - ApplyReservaRule(newLine); + ApplyReservaRule(newLine); + created.Add(newLine); + } - _db.MobileLines.Add(newLine); + _db.MobileLines.AddRange(created); try { @@ -422,7 +432,13 @@ namespace line_gestao_api.Controllers return StatusCode(500, new { message = "Erro ao salvar no banco de dados." }); } - return CreatedAtAction(nameof(GetById), new { id = newLine.Id }, ToDetailDto(newLine)); + if (created.Count == 1) + { + var newLine = created[0]; + return CreatedAtAction(nameof(GetById), new { id = newLine.Id }, ToDetailDto(newLine)); + } + + return StatusCode(StatusCodes.Status201Created, created.Select(ToDetailDto).ToList()); } // ========================================================== diff --git a/Dtos/CreateMobileLineDto.cs b/Dtos/CreateMobileLineDto.cs index ee9ce98..0780655 100644 --- a/Dtos/CreateMobileLineDto.cs +++ b/Dtos/CreateMobileLineDto.cs @@ -12,6 +12,7 @@ namespace line_gestao_api.Dtos public string? Chip { get; set; } // ICCID public string? Cliente { get; set; } // Obrigatório na validação do Controller public string? Usuario { get; set; } + public int? QtdLinhas { get; set; } // Quantidade de linhas a serem criadas // ========================== // Classificação e Status @@ -66,4 +67,4 @@ namespace line_gestao_api.Dtos public decimal? Desconto { get; set; } public decimal? Lucro { get; set; } } -} \ No newline at end of file +}