<?php
namespace App\Controller\Componentes;
use App\Controller\Customs\Custom2IMController;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/nota-corte")
*/
class NotaCorteController extends Custom2IMController
{
protected const TABELA_EVS = 'evs';
protected const CAMPO_EVS = 'valor';
protected const DEFAULT_EVS = 3.32;
protected const TABELA_GPS = 'gps';
protected const DEFAULT_GPS = 70;
protected const CAMPO_GPS = 'performance';
private function __processaWhere(
float $notaCorte,
string $direcao
): string
{
$campoPerformance = $this->__getCampo();
return " AND {$campoPerformance} {$direcao} {$notaCorte}";
}
/**
* @Route("/", name="nota-corte_componente")
*/
public function index(): Response
{
if (!$this->getSession()->has('nota_corte')) {
$this->setOnSession($this->__getMediaDefault());
}
return $this->render('componentes/nota_corte/componente.html.twig', [
'evs' => $this->getSession()->get('modelagem')->getPrograma()->getEvs(),
'nota_corte' => $this->getSession()->get('nota_corte')['valor']
]);
}
/**
* @Route("/processa", name="nota-corte_processa")
*/
public function processa(Request $request): JsonResponse
{
$notaCorte = (float) $request->request->get('nota_corte', $this->__getMediaDefault());
try {
$this->setOnSession($notaCorte);
$return = [
'sucesso' => true,
'mensagem' => 'Nota de Corte configurada!'
];
} catch (\Throwable $th) {
$return = [
'sucesso' => false,
'mensagem' => 'Erro no sistema de Nota de Corte!'
];
}
return new JsonResponse($return);
}
private function setOnSession(float $notaCorte)
{
$this->getSession()->set('nota_corte', [
'valor' => $notaCorte,
'where' => [
'abaixo' => $this->__processaWhere($notaCorte, '<'),
'acima' => $this->__processaWhere($notaCorte, '>=')
],
'tabela' => $this->__getTabela(),
'campo' =>$this->__getCampo()
]);
}
private function __getTabela()
{
return $this->getSession()->get('modelagem')->getPrograma()->getEvs() ? self::TABELA_EVS : self::TABELA_GPS;
}
private function __getCampo()
{
return $this->getSession()->get('modelagem')->getPrograma()->getEvs() ? self::CAMPO_EVS : self::CAMPO_GPS;
}
private function __getMediaDefault()
{
return $this->getSession()->get('modelagem')->getPrograma()->getEvs() ? self::DEFAULT_EVS : self::DEFAULT_GPS;
}
}