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))
|
if (string.IsNullOrWhiteSpace(req.Cliente))
|
||||||
return BadRequest(new { message = "O nome do Cliente é obrigatório." });
|
return BadRequest(new { message = "O nome do Cliente é obrigatório." });
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(req.Linha))
|
var quantidade = req.QtdLinhas.GetValueOrDefault(1);
|
||||||
return BadRequest(new { message = "O número da Linha é obrigatório." });
|
if (quantidade < 1)
|
||||||
|
return BadRequest(new { message = "A quantidade de linhas deve ser maior que zero." });
|
||||||
|
|
||||||
var linhaLimpa = OnlyDigits(req.Linha);
|
var linhaLimpa = OnlyDigits(req.Linha);
|
||||||
var chipLimpo = OnlyDigits(req.Chip);
|
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." });
|
return BadRequest(new { message = "Número de linha inválido." });
|
||||||
|
|
||||||
var exists = await _db.MobileLines.AsNoTracking().AnyAsync(x => x.Linha == linhaLimpa);
|
if (!string.IsNullOrWhiteSpace(linhaLimpa))
|
||||||
if (exists)
|
{
|
||||||
return Conflict(new { message = $"A linha {req.Linha} já está cadastrada no sistema." });
|
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 maxItem = await _db.MobileLines.MaxAsync(x => (int?)x.Item) ?? 0;
|
||||||
var nextItem = maxItem + 1;
|
|
||||||
|
|
||||||
var now = DateTime.UtcNow;
|
var now = DateTime.UtcNow;
|
||||||
|
var created = new List<MobileLine>(quantidade);
|
||||||
|
|
||||||
var newLine = new MobileLine
|
for (var i = 0; i < quantidade; i++)
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
var newLine = new MobileLine
|
||||||
Item = nextItem,
|
{
|
||||||
Cliente = req.Cliente.Trim().ToUpper(),
|
Id = Guid.NewGuid(),
|
||||||
Linha = linhaLimpa,
|
Item = maxItem + 1 + i,
|
||||||
Chip = string.IsNullOrWhiteSpace(chipLimpo) ? null : chipLimpo,
|
Cliente = req.Cliente.Trim().ToUpper(),
|
||||||
Usuario = req.Usuario?.Trim(),
|
Linha = i == 0 ? linhaLimpa : null,
|
||||||
Status = req.Status?.Trim(),
|
Chip = i == 0 && !string.IsNullOrWhiteSpace(chipLimpo) ? chipLimpo : null,
|
||||||
Skil = req.Skil?.Trim(),
|
Usuario = req.Usuario?.Trim(),
|
||||||
Modalidade = req.Modalidade?.Trim(),
|
Status = req.Status?.Trim(),
|
||||||
PlanoContrato = req.PlanoContrato?.Trim(),
|
Skil = req.Skil?.Trim(),
|
||||||
Conta = req.Conta?.Trim(),
|
Modalidade = req.Modalidade?.Trim(),
|
||||||
VencConta = req.VencConta?.Trim(),
|
PlanoContrato = req.PlanoContrato?.Trim(),
|
||||||
|
Conta = req.Conta?.Trim(),
|
||||||
|
VencConta = req.VencConta?.Trim(),
|
||||||
|
|
||||||
DataBloqueio = ToUtc(req.DataBloqueio),
|
DataBloqueio = ToUtc(req.DataBloqueio),
|
||||||
DataEntregaOpera = ToUtc(req.DataEntregaOpera),
|
DataEntregaOpera = ToUtc(req.DataEntregaOpera),
|
||||||
DataEntregaCliente = ToUtc(req.DataEntregaCliente),
|
DataEntregaCliente = ToUtc(req.DataEntregaCliente),
|
||||||
|
|
||||||
Cedente = req.Cedente?.Trim(),
|
Cedente = req.Cedente?.Trim(),
|
||||||
Solicitante = req.Solicitante?.Trim(),
|
Solicitante = req.Solicitante?.Trim(),
|
||||||
|
|
||||||
FranquiaVivo = req.FranquiaVivo,
|
FranquiaVivo = req.FranquiaVivo,
|
||||||
ValorPlanoVivo = req.ValorPlanoVivo,
|
ValorPlanoVivo = req.ValorPlanoVivo,
|
||||||
GestaoVozDados = req.GestaoVozDados,
|
GestaoVozDados = req.GestaoVozDados,
|
||||||
Skeelo = req.Skeelo,
|
Skeelo = req.Skeelo,
|
||||||
VivoNewsPlus = req.VivoNewsPlus,
|
VivoNewsPlus = req.VivoNewsPlus,
|
||||||
VivoTravelMundo = req.VivoTravelMundo,
|
VivoTravelMundo = req.VivoTravelMundo,
|
||||||
VivoGestaoDispositivo = req.VivoGestaoDispositivo,
|
VivoGestaoDispositivo = req.VivoGestaoDispositivo,
|
||||||
ValorContratoVivo = req.ValorContratoVivo,
|
ValorContratoVivo = req.ValorContratoVivo,
|
||||||
FranquiaLine = req.FranquiaLine,
|
FranquiaLine = req.FranquiaLine,
|
||||||
FranquiaGestao = req.FranquiaGestao,
|
FranquiaGestao = req.FranquiaGestao,
|
||||||
LocacaoAp = req.LocacaoAp,
|
LocacaoAp = req.LocacaoAp,
|
||||||
ValorContratoLine = req.ValorContratoLine,
|
ValorContratoLine = req.ValorContratoLine,
|
||||||
Desconto = req.Desconto,
|
Desconto = req.Desconto,
|
||||||
Lucro = req.Lucro,
|
Lucro = req.Lucro,
|
||||||
|
|
||||||
CreatedAt = now,
|
CreatedAt = now,
|
||||||
UpdatedAt = now
|
UpdatedAt = now
|
||||||
};
|
};
|
||||||
|
|
||||||
ApplyReservaRule(newLine);
|
ApplyReservaRule(newLine);
|
||||||
|
created.Add(newLine);
|
||||||
|
}
|
||||||
|
|
||||||
_db.MobileLines.Add(newLine);
|
_db.MobileLines.AddRange(created);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -422,7 +432,13 @@ namespace line_gestao_api.Controllers
|
||||||
return StatusCode(500, new { message = "Erro ao salvar no banco de dados." });
|
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? Chip { get; set; } // ICCID
|
||||||
public string? Cliente { get; set; } // Obrigatório na validação do Controller
|
public string? Cliente { get; set; } // Obrigatório na validação do Controller
|
||||||
public string? Usuario { get; set; }
|
public string? Usuario { get; set; }
|
||||||
|
public int? QtdLinhas { get; set; } // Quantidade de linhas a serem criadas
|
||||||
|
|
||||||
// ==========================
|
// ==========================
|
||||||
// Classificação e Status
|
// Classificação e Status
|
||||||
|
|
@ -66,4 +67,4 @@ namespace line_gestao_api.Dtos
|
||||||
public decimal? Desconto { get; set; }
|
public decimal? Desconto { get; set; }
|
||||||
public decimal? Lucro { get; set; }
|
public decimal? Lucro { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue