api_monitor/vendor/model/Station.class.php

418 lines
11 KiB
PHP

<?php
/**
* Classe Station.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 Station
{
/** 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 $stationId = null;
private $clientId = null;
private $userId = null;
private $description = null;
private $ip = null;
private $port = null;
private $networkPath = null;
private $connectionName = null;
private $connectionId = null;
private $connectionUser = null;
private $connectionPassword = null;
private $observation = null;
private $errors = [];
private $field = null;
private $lastId = null;
private $type = null;
private $operatingSystem = null;
private $cpu = null;
private $memory = 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 station";
/** 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 station
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();
}
/** Pesquisa uma estação pelo nome */
public function Search(int $clientId, string $description)
{
/** Parametros de entrada */
$this->clientId = $clientId;
$this->description = $description;
/** Consulta SQL */
$this->sql = 'select * from station
where description = :description
and 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(':description', $this->description);
$this->stmt->bindParam(':client_id', $this->clientId);
/** 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(int $clientId)
{
/** Parametros de entrada */
$this->clientId = $clientId;
/** Consulta SQL */
$this->sql = 'select * from station
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->fetchAll(\PDO::FETCH_OBJ);
}
/** Conta a quantidades de registros */
public function Count(int $clientId)
{
/** Parametros de entrada */
$this->clientId = $clientId;
/** Consulta SQL */
$this->sql = 'select count(client_id) as qtde
from station
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()->qtde;
}
/** Insere um novo registro no banco */
public function Save(? int $stationId,
? int $clientId,
? int $userId,
? string $description,
? string $ip,
? string $port,
? string $networkPath,
? string $connectionName,
? string $connectionId,
? string $connectionUser,
? string $connectionPassword,
? string $observation,
? string $type,
? string $operatingSystem,
? string $cpu,
? string $memory)
{
/** Parametros */
$this->stationId = $stationId;
$this->clientId = $clientId;
$this->userId = $userId;
$this->description = $description;
$this->ip = $ip;
$this->port = $port;
$this->networkPath = $networkPath;
$this->connectionName = $connectionName;
$this->connectionId = $connectionId;
$this->connectionUser = $connectionUser;
$this->connectionPassword = $connectionPassword;
$this->observation = $observation;
$this->type = $type;
$this->operatingSystem = $operatingSystem;
$this->cpu = $cpu;
$this->memory = $memory;
/** Verifica se o ID do registro foi informado */
if($this->stationId > 0){
/** Consulta SQL */
$this->sql = 'update station set description = :description,
ip = :ip,
port = :port,
network_path = :network_path,
connection_name = :connection_name,
connection_id = :connection_id,
connection_user = :connection_user,
connection_password = :connection_password,
observation = :observation
where station_id = :station_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('station_id', $this->stationId);
$this->stmt->bindParam('description', $this->description);
$this->stmt->bindParam('ip', $this->ip);
$this->stmt->bindParam('port', $this->port);
$this->stmt->bindParam('network_path', $this->networkPath);
$this->stmt->bindParam('connection_name', $this->connectionName);
$this->stmt->bindParam('connection_id', $this->connectionId);
$this->stmt->bindParam('connection_user', $this->connectionUser);
$this->stmt->bindParam('connection_password', $this->connectionPassword);
$this->stmt->bindParam('observation', $this->observation);
/** 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 station(station_id,
client_id,
user_id,
description,
ip,
port,
network_path,
connection_name,
connection_id,
connection_user,
connection_password,
observation,
type,
operating_system,
cpu,
memory
) values (:station_id,
:client_id,
:user_id,
:description,
:ip,
:port,
:network_path,
:connection_name,
:connection_id,
:connection_user,
:connection_password,
:observation,
:type,
:operating_system,
:cpu,
:memory)';
/** 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('station_id', $this->stationId);
$this->stmt->bindParam('client_id', $this->clientId);
$this->stmt->bindParam('user_id', $this->userId);
$this->stmt->bindParam('description', $this->description);
$this->stmt->bindParam('ip', $this->ip);
$this->stmt->bindParam('port', $this->port);
$this->stmt->bindParam('network_path', $this->networkPath);
$this->stmt->bindParam('connection_name', $this->connectionName);
$this->stmt->bindParam('connection_id', $this->connectionId);
$this->stmt->bindParam('connection_user', $this->connectionUser);
$this->stmt->bindParam('connection_password', $this->connectionPassword);
$this->stmt->bindParam('observation', $this->observation);
$this->stmt->bindParam('type', $this->type);
$this->stmt->bindParam('operating_system', $this->operatingSystem);
$this->stmt->bindParam('cpu', $this->cpu);
$this->stmt->bindParam('memory', $this->memory);
/** 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 station
where station_id = :station_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;
}
}