<?

/**
 * TRIBUNAL REGIONAL FEDERAL DA 4 REGIO
 *
 * 15/01/2024 - criado por sabino.colab
 *
 * Verso do Gerador de Cdigo: 1.43.3
 */

require_once dirname(__FILE__) . '../../../../SEI.php';

class MdIaHistClassRN extends InfraRN
{
    public static $OPERACAO_INSERT                 = "I";
    public static $OPERACAO_INSERT_DESC            = "Adicionado";

    public static $OPERACAO_DELETE                 = "D";
    public static $OPERACAO_DELETE_DESC            = "Excludo";

    public static $OPERACAO_CONFIRMACAO            = "C";
    public static $OPERACAO_CONFIRMACAO_DESC       = "Sugesto Confirmada";

    public static $OPERACAO_NAO_CONFIRMACAO        = "N";
    public static $OPERACAO_NAO_CONFIRMACAO_DESC   = "Sugesto No Confirmada";

    public static $OPERACAO_SOBRESCRITA            = "S";
    public static $OPERACAO_SOBRESCRITA_DESC       = "Sobrescreveu sugesto dada por IA";

    public static $OPERACAO_ATUALIZACAO            = "U";
    public static $OPERACAO_ATUALIZACAO_DESC       = "Atualizao";

    public function __construct()
    {
        parent::__construct();
    }

    protected function inicializarObjInfraIBanco()
    {
        return BancoSEI::getInstance();
    }

    private function validarNumIdMdIaAdmMetaOds(MdIaHistClassDTO $objMdIaHistClassDTO, InfraException $objInfraException)
    {
        if (InfraString::isBolVazia($objMdIaHistClassDTO->getNumIdMdIaAdmMetaOds())) {
            $objInfraException->adicionarValidacao(' no informad.');
        }
    }

    private function validarStrOperacao(MdIaHistClassDTO $objMdIaHistClassDTO, InfraException $objInfraException)
    {
        if (InfraString::isBolVazia($objMdIaHistClassDTO->getStrOperacao())) {
            $objInfraException->adicionarValidacao(' no informad.');
        } else {
            $objMdIaHistClassDTO->setStrOperacao(trim($objMdIaHistClassDTO->getStrOperacao()));

            if (strlen($objMdIaHistClassDTO->getStrOperacao()) > 1) {
                $objInfraException->adicionarValidacao(' possui tamanho superior a 1 caracteres.');
            }
        }
    }

    private function validarNumIdUsuario(MdIaHistClassDTO $objMdIaHistClassDTO, InfraException $objInfraException)
    {
        if (InfraString::isBolVazia($objMdIaHistClassDTO->getNumIdUsuario())) {
            $objInfraException->adicionarValidacao(' no informad.');
        }
    }

    private function validarNumIdUnidade(MdIaHistClassDTO $objMdIaHistClassDTO, InfraException $objInfraException)
    {
        if (InfraString::isBolVazia($objMdIaHistClassDTO->getNumIdUnidade())) {
            $objInfraException->adicionarValidacao(' no informad.');
        }
    }

    private function validarDthCadastro(MdIaHistClassDTO $objMdIaHistClassDTO, InfraException $objInfraException)
    {
        if (InfraString::isBolVazia($objMdIaHistClassDTO->getDthCadastro())) {
            $objMdIaHistClassDTO->setDthCadastro(null);
        } else {
            if (!InfraData::validarDataHora($objMdIaHistClassDTO->getDthCadastro())) {
                $objInfraException->adicionarValidacao(' invlid.');
            }
        }
    }

    protected function cadastrarControlado(MdIaHistClassDTO $objMdIaHistClassDTO)
    {
        try {

            SessaoSEI::getInstance()->validarAuditarPermissao('md_ia_hist_class_cadastrar', __METHOD__, $objMdIaHistClassDTO);

            //Regras de Negocio
            $objInfraException = new InfraException();

            $this->validarNumIdMdIaAdmMetaOds($objMdIaHistClassDTO, $objInfraException);
            $this->validarStrOperacao($objMdIaHistClassDTO, $objInfraException);
            $this->validarNumIdUsuario($objMdIaHistClassDTO, $objInfraException);
            $this->validarDthCadastro($objMdIaHistClassDTO, $objInfraException);

            $objInfraException->lancarValidacoes();

            $objMdIaHistClassBD = new MdIaHistClassBD($this->getObjInfraIBanco());
            $ret = $objMdIaHistClassBD->cadastrar($objMdIaHistClassDTO);

            return $ret;
        } catch (Exception $e) {
            throw new InfraException('Erro cadastrando Histrico de Classificao.', $e);
        }
    }

    protected function alterarControlado(MdIaHistClassDTO $objMdIaHistClassDTO)
    {
        try {

            SessaoSEI::getInstance()->validarAuditarPermissao('md_ia_hist_class_alterar', __METHOD__, $objMdIaHistClassDTO);

            //Regras de Negocio
            $objInfraException = new InfraException();

            if ($objMdIaHistClassDTO->isSetNumIdMdIaAdmMetaOds()) {
                $this->validarNumIdMdIaAdmMetaOds($objMdIaHistClassDTO, $objInfraException);
            }
            if ($objMdIaHistClassDTO->isSetStrOperacao()) {
                $this->validarStrOperacao($objMdIaHistClassDTO, $objInfraException);
            }
            if ($objMdIaHistClassDTO->isSetNumIdUsuario()) {
                $this->validarNumIdUsuario($objMdIaHistClassDTO, $objInfraException);
            }
            if ($objMdIaHistClassDTO->isSetNumIdUnidade()) {
                $this->validarNumIdUnidade($objMdIaHistClassDTO, $objInfraException);
            }
            if ($objMdIaHistClassDTO->isSetDthCadastro()) {
                $this->validarDthCadastro($objMdIaHistClassDTO, $objInfraException);
            }

            $objInfraException->lancarValidacoes();

            $objMdIaHistClassBD = new MdIaHistClassBD($this->getObjInfraIBanco());
            $objMdIaHistClassBD->alterar($objMdIaHistClassDTO);
        } catch (Exception $e) {
            throw new InfraException('Erro alterando Histrico de Classificao.', $e);
        }
    }

    protected function excluirControlado($arrObjMdIaHistClassDTO)
    {
        try {

            //SessaoSEI::getInstance()->validarAuditarPermissao('md_ia_hist_class_excluir', __METHOD__, $arrObjMdIaHistClassDTO);

            //Regras de Negocio
            //$objInfraException = new InfraException();

            //$objInfraException->lancarValidacoes();

            $objMdIaHistClassBD = new MdIaHistClassBD($this->getObjInfraIBanco());
            for ($i = 0; $i < count($arrObjMdIaHistClassDTO); $i++) {
                $objMdIaHistClassBD->excluir($arrObjMdIaHistClassDTO[$i]);
            }
        } catch (Exception $e) {
            throw new InfraException('Erro excluindo Histrico de Classificao.', $e);
        }
    }

    protected function consultarConectado(MdIaHistClassDTO $objMdIaHistClassDTO)
    {
        try {

            SessaoSEI::getInstance()->validarAuditarPermissao('md_ia_hist_class_consultar', __METHOD__, $objMdIaHistClassDTO);

            //Regras de Negocio
            //$objInfraException = new InfraException();

            //$objInfraException->lancarValidacoes();

            $objMdIaHistClassBD = new MdIaHistClassBD($this->getObjInfraIBanco());

            /** @var MdIaHistClassDTO $ret */
            $ret = $objMdIaHistClassBD->consultar($objMdIaHistClassDTO);

            return $ret;
        } catch (Exception $e) {
            throw new InfraException('Erro consultando Histrico de Classificao.', $e);
        }
    }

    protected function listarConectado(MdIaHistClassDTO $objMdIaHistClassDTO)
    {
        try {

            SessaoSEI::getInstance()->validarAuditarPermissao('md_ia_hist_class_listar', __METHOD__, $objMdIaHistClassDTO);

            //Regras de Negocio
            //$objInfraException = new InfraException();

            //$objInfraException->lancarValidacoes();

            $objMdIaHistClassBD = new MdIaHistClassBD($this->getObjInfraIBanco());

            /** @var MdIaHistClassDTO[] $ret */
            $ret = $objMdIaHistClassBD->listar($objMdIaHistClassDTO);

            return $ret;
        } catch (Exception $e) {
            throw new InfraException('Erro listando Histrico de Classificaes.', $e);
        }
    }

    protected function contarConectado(MdIaHistClassDTO $objMdIaHistClassDTO)
    {
        try {

            SessaoSEI::getInstance()->validarAuditarPermissao('md_ia_hist_class_listar', __METHOD__, $objMdIaHistClassDTO);

            //Regras de Negocio
            //$objInfraException = new InfraException();

            //$objInfraException->lancarValidacoes();

            $objMdIaHistClassBD = new MdIaHistClassBD($this->getObjInfraIBanco());
            $ret = $objMdIaHistClassBD->contar($objMdIaHistClassDTO);

            return $ret;
        } catch (Exception $e) {
            throw new InfraException('Erro contando Histrico de Classificaes.', $e);
        }
    }
}
