api_monitor/vendor/model/Client.class.php

357 lines
8.5 KiB
PHP

<?php
/**
* Classe Client.class.php
* @filesource
* @autor Kenio de Souza
* @copyright Copyright 2024 - Souza Consultoria Tecnológica
* @package vendor
* @subpackage model
* @version 1.0
* @date 07/08/2024
*/
/** Defino o local onde esta a classe */
namespace vendor\model;
class Client
{
/** Declaro as vaiavéis da classe */
private $connection = null;
private $sql = null;
private $stmt = null;
private $start = null;
private $max = null;
private $limit = null;
private $clientId = null;
private $cns = null;
private $name = null;
private $dateRegister = null;
private $state = null;
private $city = null;
private $responsible = null;
private $consultant = null;
private $typeContract = null;
private $errors = [];
private $field = null;
private $lastId = null;
private $info = null;
/** Construtor da classe */
function __construct()
{
/** Cria o objeto de conexão com o banco de dados */
$this->connection = new Mysql();
}
/** Carrega os campos de uma tabela */
public function Describe()
{
/** Consulta SQL */
$this->sql = "describe client";
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Executo o SQL */
$this->stmt->execute();
/** Retorno o resultado */
$this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ);
/** Declara o objeto */
$resultDescribe = new \stdClass();
$Field = '';
/** Lista os campos da tabela para objetos */
foreach($this->field as $UsersKey => $Result){
/** Pega o nome do Field/Campo */
$Field = $Result->Field;
/** Carrega os objetos como null */
$resultDescribe->$Field = null;
}
/** Retorna os campos declarados como vazios */
return $resultDescribe;
}
/** Lista os registros do banco de dados com limitação */
public function Get(int $clientId)
{
/** Parametros de entrada */
$this->clientId = $clientId;
/** Consulta SQL */
$this->sql = 'select * from client
where client_id = :client_id';
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam(':client_id', $this->clientId);
/** Executo o SQL */
$this->stmt->execute();
/** Retorno o resultado */
return $this->stmt->fetchObject();
}
/** Lista os registros do banco de dados com limitação */
public function GetCns(string $cns)
{
/** Parametros de entrada */
$this->cns = $cns;
/** Consulta SQL */
$this->sql = 'select c.client_id,
c.cns,
c.name,
c.date_register,
c.state,
c.city,
c.responsible,
c.consultant,
c.type_contract
from client c
where c.cns = :cns';
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam(':cns', $this->cns);
/** Executo o SQL */
$this->stmt->execute();
/** Retorno o resultado */
return $this->stmt->fetchObject();
}
/** Lista todos os egistros do banco com ou sem paginação*/
public function All()
{
/** Consulta SQL */
$this->sql = 'select * from client
order by name asc ';
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Executo o SQL */
$this->stmt->execute();
/** Retorno o resultado */
return $this->stmt->fetchAll(\PDO::FETCH_OBJ);
}
/** Conta a quantidades de registros */
public function Count()
{
/** Consulta SQL */
$this->sql = 'select count(client_id) as qtde
from client ';
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Executo o SQL */
$this->stmt->execute();
/** Retorno o resultado */
return $this->stmt->fetchObject()->qtde;
}
/** Insere um novo registro no banco */
public function Save(? int $clientId,
? string $cns,
? string $name,
? string $state,
? string $city,
? string $responsible,
? string $consultant,
? string $typeContract)
{
/** Parametros */
$this->clientId = $clientId;
$this->cns = $cns;
$this->name = $name;
$this->state = $state;
$this->city = $city;
$this->responsible = $responsible;
$this->consultant = $consultant;
$this->typeContract = $typeContract;
/** Verifica se o ID do registro foi informado */
if($this->clientId > 0){
/** Consulta SQL */
$this->sql = 'update client set cns = :cns,
name = :name,
state = :state,
city = :city,
responsible = :responsible,
consultant = :consultant,
type_contract = :type_contract
where client_id = :client_id';
/** Preparo o sql para receber os valores */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam('client_id', $this->clientId);
$this->stmt->bindParam('cns', $this->cns);
$this->stmt->bindParam('name', $this->name);
$this->stmt->bindParam('state', $this->state);
$this->stmt->bindParam('city', $this->city);
$this->stmt->bindParam('responsible', $this->responsible);
$this->stmt->bindParam('consultant', $this->consultant);
$this->stmt->bindParam('type_contract', $this->typeContract);
/** Executo o SQL */
return $this->stmt->execute();
}else{//Se o ID não foi informado, grava-se um novo registro
/** Consulta SQL */
$this->sql = 'insert into client(cns,
name,
state,
city,
responsible,
consultant,
type_contract
) values (:cns,
:name,
:state,
:city,
:responsible,
:consultant,
:type_contract)';
/** Preparo o sql para receber os valores */
$this->stmt = $this->connection->connect()->prepare($this->sql);
try{
/** Inicia a transação */
$this->connection->connect()->beginTransaction();
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam('cns', $this->cns);
$this->stmt->bindParam('name', $this->name);
$this->stmt->bindParam('state', $this->state);
$this->stmt->bindParam('city', $this->city);
$this->stmt->bindParam('responsible', $this->responsible);
$this->stmt->bindParam('consultant', $this->consultant);
$this->stmt->bindParam('type_contract', $this->typeContract);
/** Executo o SQL */
$this->stmt->execute();
/** Retorna o ID do novo registro */
$this->setId($this->connection->connect()->lastInsertId());
/** Confirma a transação */
$this->connection->connect()->commit();
return true;
}catch(\Exception $exception) {
/** Desfaz a transação */
$this->connection->connect()->rollback();
/** Captura o erro */
array_push($this->errors, $exception->getMessage());
return false;
}
}
}
/** Deleta um determinado registro no banco de dados */
function Delete(int $clientId)
{
/** Parametros de entrada */
$this->clientId = $clientId;
/** Consulta SQL */
$this->sql = 'delete from client
where client_id = :client_id';
/** Preparo o sql para receber os valores */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam('client_id', $this->clientId);
/** Executo o SQL */
return $this->stmt->execute();
}
/** Define o Último ID inserido */
public function setId($lastId) : void
{
$this->lastId = $lastId;
}
/** Recupera o Último ID inserido */
public function getId(): ? int
{
return (int)$this->lastId;
}
/** Retorna os erros ocorridos */
public function getErrors(): ? string
{
/** Verifico se deve informar os erros */
if (count($this->errors)) {
/** Verifica a quantidade de erros para informar a legenda */
$this->info = count($this->errors) > 1 ? '<center>Os seguintes erros foram encontrados</center>' : '<center>O seguinte erro foi encontrado</center>';
/** Lista os erros */
foreach ($this->errors as $keyError => $error) {
/** Monto a mensagem de erro */
$this->info .= '</br>' . ($keyError + 1) . ' - ' . $error;
}
/** Retorno os erros encontrados */
return (string)$this->info;
} else {
return false;
}
}
/** Fecha uma conexão aberta anteriormente com o banco de dados */
function __destruct()
{
$this->connection = null;
}
}