107 lines
4.4 KiB
C#
107 lines
4.4 KiB
C#
using line_gestao_api.Data;
|
|
using line_gestao_api.Dtos;
|
|
using line_gestao_api.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace line_gestao_api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/mureg")]
|
|
public class MuregController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _db;
|
|
|
|
public MuregController(AppDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public class ImportExcelForm
|
|
{
|
|
public IFormFile File { get; set; } = default!;
|
|
}
|
|
|
|
// ==========================================================
|
|
// ✅ GET: /api/mureg (com paginação, busca e ordenação)
|
|
// ==========================================================
|
|
[HttpGet]
|
|
public async Task<ActionResult<PagedResult<MuregListDto>>> GetAll(
|
|
[FromQuery] string? search,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 10,
|
|
[FromQuery] string? sortBy = "item",
|
|
[FromQuery] string? sortDir = "asc")
|
|
{
|
|
page = page < 1 ? 1 : page;
|
|
pageSize = pageSize < 1 ? 10 : pageSize;
|
|
|
|
var q = _db.MuregLines.AsNoTracking();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.Trim();
|
|
q = q.Where(x =>
|
|
EF.Functions.ILike((x.LinhaAntiga ?? ""), $"%{s}%") ||
|
|
EF.Functions.ILike((x.LinhaNova ?? ""), $"%{s}%") ||
|
|
EF.Functions.ILike((x.ICCID ?? ""), $"%{s}%") ||
|
|
EF.Functions.ILike((x.Cliente ?? ""), $"%{s}%") ||
|
|
EF.Functions.ILike(x.Item.ToString(), $"%{s}%"));
|
|
}
|
|
|
|
var total = await q.CountAsync();
|
|
|
|
var sb = (sortBy ?? "item").Trim().ToLowerInvariant();
|
|
var desc = string.Equals((sortDir ?? "asc").Trim(), "desc", StringComparison.OrdinalIgnoreCase);
|
|
|
|
q = sb switch
|
|
{
|
|
"item" => desc ? q.OrderByDescending(x => x.Item) : q.OrderBy(x => x.Item),
|
|
"linhaantiga" => desc ? q.OrderByDescending(x => x.LinhaAntiga ?? "").ThenBy(x => x.Item) : q.OrderBy(x => x.LinhaAntiga ?? "").ThenBy(x => x.Item),
|
|
"linhanova" => desc ? q.OrderByDescending(x => x.LinhaNova ?? "").ThenBy(x => x.Item) : q.OrderBy(x => x.LinhaNova ?? "").ThenBy(x => x.Item),
|
|
"iccid" => desc ? q.OrderByDescending(x => x.ICCID ?? "").ThenBy(x => x.Item) : q.OrderBy(x => x.ICCID ?? "").ThenBy(x => x.Item),
|
|
"datadamureg" => desc ? q.OrderByDescending(x => x.DataDaMureg).ThenBy(x => x.Item) : q.OrderBy(x => x.DataDaMureg).ThenBy(x => x.Item),
|
|
"cliente" => desc ? q.OrderByDescending(x => x.Cliente ?? "").ThenBy(x => x.Item) : q.OrderBy(x => x.Cliente ?? "").ThenBy(x => x.Item),
|
|
_ => desc ? q.OrderByDescending(x => x.Item) : q.OrderBy(x => x.Item)
|
|
};
|
|
|
|
var items = await q
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(x => new MuregListDto
|
|
{
|
|
Id = x.Id,
|
|
Item = x.Item,
|
|
LinhaAntiga = x.LinhaAntiga,
|
|
LinhaNova = x.LinhaNova,
|
|
ICCID = x.ICCID,
|
|
DataDaMureg = x.DataDaMureg,
|
|
Cliente = x.Cliente
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(new PagedResult<MuregListDto>
|
|
{
|
|
Page = page,
|
|
PageSize = pageSize,
|
|
Total = total,
|
|
Items = items
|
|
});
|
|
}
|
|
|
|
// ==========================================================
|
|
// ✅ POST: /api/mureg/import-excel (opcional)
|
|
// Se você usar o botão "Importar" da tela MUREG, ele vai bater aqui
|
|
// ==========================================================
|
|
[HttpPost("import-excel")]
|
|
[Consumes("multipart/form-data")]
|
|
[RequestSizeLimit(50_000_000)]
|
|
public async Task<IActionResult> ImportExcel([FromForm] ImportExcelForm form)
|
|
{
|
|
// Se você quiser manter "importa só no GERAL", pode remover este endpoint.
|
|
// Eu deixei para não quebrar o botão do seu front (que chama /api/mureg/import-excel).
|
|
return BadRequest(new { message = "Importe a planilha pela página GERAL. O MUREG será carregado automaticamente." });
|
|
}
|
|
}
|
|
}
|