<?php
require_once("../../config/config_dir_raiz.php");
$config = new Config();
require_once($config->diretorio_raiz()."/src/controller/Funcoes_bd.php");
require_once($config->diretorio_raiz()."/src/model/Analise.php");
class AnaliseDAO
{
	private $_funcoes_bd;
	private $_banco_atual;
	private $conexao;
	function __construct()
	{
		$this->conexao = Conexao::obterConexao();
		$this->_banco_atual = Conexao::getUtilizarBanco();
		$this->_funcoes_bd = new Funcoes_bd();
	}
	public function salvar($analise)
	{
		$inputs = array();
		if($analise->getCodigo() != "")
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>$analise->getCodigo()));
		}
		else
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>null));
		}
		if($analise->getNome() != "")
		{
			array_push($inputs, array('campo'=>':nome', 'valor'=>$analise->getNome()));
		}
		else
		{
			array_push($inputs, array('campo'=>':nome', 'valor'=>null));
		}
		if($analise->getDescricao() != "")
		{
			array_push($inputs, array('campo'=>':descricao', 'valor'=>$analise->getDescricao()));
		}
		else
		{
			array_push($inputs, array('campo'=>':descricao', 'valor'=>null));
		}
		if($analise->getQuery() != "")
		{
			array_push($inputs, array('campo'=>':query', 'valor'=>$analise->getQuery()));
		}
		else
		{
			array_push($inputs, array('campo'=>':query', 'valor'=>null));
		}
		if($analise->getData_inclusao() instanceof DateTime)
		{
			array_push($inputs, array('campo'=>':data_inclusao', 'valor'=>$analise->getData_inclusao()->format('Y-m-d H:i:s')));
		}
		else
		{
			array_push($inputs, array('campo'=>':data_inclusao', 'valor'=>null));
		}
		if($analise->getLog() != "")
		{
			array_push($inputs, array('campo'=>':log', 'valor'=>$analise->getLog()));
		}
		else
		{
			array_push($inputs, array('campo'=>':log', 'valor'=>null));
		}
		try
		{
			$sql = "INSERT INTO analise (codigo, nome, descricao, query, data_inclusao, log) VALUES (:codigo, :nome, :descricao, :query, :data_inclusao, :log)  ";
			return $this->_funcoes_bd->executarSql($sql,$inputs);
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage());
		}
	}
	public function buscarAnalise($codigo)
	{
		$inputs = array();
		array_push($inputs, array('campo'=>':codigo', 'valor'=>$codigo));
		$sql = "SELECT * FROM analise where codigo = :codigo "; 
		$analise = new Analise();
		$rows_analise = $this->_funcoes_bd->sqlArrayInputs($sql, $inputs);
		foreach ($rows_analise as $row)
		{
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
		}
		return $analise;
	}
	public function buscarFiltro($where)
	{
		$inputs = array();
		$sql = "SELECT * FROM analise  ".$where;
		$lista_analise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($lista_analise, $analise);
		}
		return $lista_analise;
	}
	public function atualizar($analise)
	{
		$inputs = array();
		if($analise->getCodigo() != "")
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>$analise->getCodigo()));
		}
		else
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>null));
		}
		if($analise->getNome() != "")
		{
			array_push($inputs, array('campo'=>':nome', 'valor'=>$analise->getNome()));
		}
		else
		{
			array_push($inputs, array('campo'=>':nome', 'valor'=>null));
		}
		if($analise->getDescricao() != "")
		{
			array_push($inputs, array('campo'=>':descricao', 'valor'=>$analise->getDescricao()));
		}
		else
		{
			array_push($inputs, array('campo'=>':descricao', 'valor'=>null));
		}
		if($analise->getQuery() != "")
		{
			array_push($inputs, array('campo'=>':query', 'valor'=>$analise->getQuery()));
		}
		else
		{
			array_push($inputs, array('campo'=>':query', 'valor'=>null));
		}
		if($analise->getLog() != "")
		{
			array_push($inputs, array('campo'=>':log', 'valor'=>$analise->getLog()));
		}
		else
		{
			array_push($inputs, array('campo'=>':log', 'valor'=>null));
		}
		try
		{
			$sql = "UPDATE  analise  SET codigo = :codigo, nome = :nome, descricao = :descricao, query = :query, log = :log WHERE codigo = :codigo  ";
			return $this->_funcoes_bd->executarSql($sql,$inputs);
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage());
		}
	}
	public function excluir($analise)
	{
		try
		{
			$sql = "DELETE FROM analise  WHERE codigo =  :codigo ";
			$inputs = array();
			array_push($inputs, array('campo'=>':codigo', 'valor'=>$analise->getCodigo()));
			return $this->_funcoes_bd->executarSql($sql,$inputs);
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage());
		}
	}
	public function listarTodosRegistros()
	{
		$inputs = array();
		$sql = "SELECT * FROM analise";
		$listaAnalise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($listaAnalise,$analise );
		}
		return $listaAnalise;
	}
	public function buscarAnalisePorNome($nome)
	{
		$inputs = array();
		$sql = "SELECT * FROM analise where nome like '%".$nome."%'";
		$lista_analise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($lista_analise, $analise);
		}
		return $lista_analise;
	}
	public function buscarAnalisePorDescricao($descricao)
	{
		$inputs = array();
		$sql = "SELECT * FROM analise where descricao like '%".$descricao."%'";
		$lista_analise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($lista_analise, $analise);
		}
		return $lista_analise;
	}
	public function buscarAnalisePorQuery($query)
	{
		$inputs = array();
		$sql = "SELECT * FROM analise where query like '%".$query."%'";
		$lista_analise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($lista_analise, $analise);
		}
		return $lista_analise;
	}
	public function buscarAnalisePorData_inclusao($data_inclusao)
	{
		$inputs = array();
		$sql = "SELECT * FROM analise where data_inclusao like '%".$data_inclusao."%'";
		$lista_analise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($lista_analise, $analise);
		}
		return $lista_analise;
	}
	public function buscarAnalisePorLog($log)
	{
		$inputs = array();
		$sql = "SELECT * FROM analise where log like '%".$log."%'";
		$lista_analise = array();
		$rows_analise = $this->_funcoes_bd->sql($sql);
		foreach ($rows_analise as $row)
		{
			$analise = new Analise();
			$analise->setCodigo($row['codigo']);
			$analise->setNome($row['nome']);
			$analise->setDescricao($row['descricao']);
			$analise->setQuery($row['query']);
			if($row['data_inclusao'] != "")
			{
				$dateData_inclusao = DateTime::createFromFormat('Y-m-d H:i:s', $row['data_inclusao']);
				$analise->setData_inclusao($dateData_inclusao);
			}
			$analise->setLog($row['log']);
			array_push($lista_analise, $analise);
		}
		return $lista_analise;
	}
}