api_monitor/vendor/model/StationDisk.class.php

373 lines
9.6 KiB
PHP

<?php
/**
* Classe StationDisk.class.php
* @filesource
* @autor Kenio de Souza
* @copyright Copyright 2024 - Souza Consultoria Tecnológica
* @package vendor
* @subpackage model
* @version 1.0
* @date 10/08/2024
*/
/** Defino o local onde esta a classe */
namespace vendor\model;
class StationDisk
{
/** 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 $stationDiskId = null;
private $stationId = null;
private $description = null;
private $capacity = null;
private $used = null;
private $available = null;
private $availablePercentage = null;
private $registrationDate = null;
private $field = null;
private $lastId = null;
private $errors = [];
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_disk";
/** 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 $stationDiskId)
{
/** Parametros de entrada */
$this->stationDiskId = $stationDiskId;
/** Consulta SQL */
$this->sql = 'select * from station_disk
where station_disk_id = :station_disk_id';
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam(':station_disk_id', $this->stationDiskId);
/** Executo o SQL */
$this->stmt->execute();
/** Retorno o resultado */
return $this->stmt->fetchObject();
}
/** Consulta uma unidade de disco */
public function Search(int $stationId, string $description)
{
/** Parametros de entrada */
$this->stationId = $stationId;
$this->description = $description;
/** Consulta SQL */
$this->sql = 'select * from station_disk
where description = :description
and station_id = :station_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(':station_id', $this->stationId);
/** 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 $stationId)
{
/** Parametros de entrada */
$this->stationId = $stationId;
/** Consulta SQL */
$this->sql = 'select * from station_disk
where station_id = :station_id';
/** Preparo o SQL para execução */
$this->stmt = $this->connection->connect()->prepare($this->sql);
/** Preencho os parâmetros do SQL */
$this->stmt->bindParam(':station_id', $this->stationId);
/** 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(station_disk_id) as qtde
from station_disk ';
/** 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 $stationDiskId,
? int $clientId,
? int $stationId,
? string $description,
? string $capacity,
? string $used,
? string $available,
? string $availablePercentage)
{
/** Parametros */
$this->clientId = $clientId;
$this->stationDiskId = $stationDiskId;
$this->stationId = $stationId;
$this->description = $description;
$this->capacity = $capacity;
$this->used = $used;
$this->available = $available;
$this->availablePercentage = $availablePercentage;
/** Verifica se o ID do registro foi informado */
if($this->stationDiskId > 0){
/** Consulta SQL */
$this->sql = 'update station_disk set description = :description,
capacity = :capacity,
used = :used,
available = :available,
available_percentage = :available_percentage
where station_disk_id = :station_disk_id';
/** 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_disk_id', $this->stationDiskId);
$this->stmt->bindParam('description', $this->description);
$this->stmt->bindParam('capacity', $this->capacity);
$this->stmt->bindParam('used', $this->used);
$this->stmt->bindParam('available', $this->available);
$this->stmt->bindParam('available_percentage', $this->availablePercentage);
/** 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;
}
}else{//Se o ID não foi informado, grava-se um novo registro
/** Consulta SQL */
$this->sql = 'insert into station_disk(station_disk_id,
client_id,
station_id,
description,
capacity,
used,
available,
available_percentage
) values (:station_disk_id,
:client_id,
:station_id,
:description,
:capacity,
:used,
:available,
:available_percentage)';
/** 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_disk_id', $this->stationDiskId);
$this->stmt->bindParam('client_id', $this->clientId);
$this->stmt->bindParam('station_id', $this->stationId);
$this->stmt->bindParam('description', $this->description);
$this->stmt->bindParam('capacity', $this->capacity);
$this->stmt->bindParam('used', $this->used);
$this->stmt->bindParam('available', $this->available);
$this->stmt->bindParam('available_percentage', $this->availablePercentage);
/** 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;
}
}
}
/** 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;
}
/** 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_disk
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();
}
/** 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;
}
}