Add bulk line creation quantity
This commit is contained in:
parent
40a94f0e4e
commit
bb6f1fc044
|
|
@ -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<MobileLine>(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());
|
||||
}
|
||||
|
||||
// ==========================================================
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue