96 lines
3.5 KiB
PHP
96 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\PainelSenha;
|
|
|
|
use App\Enums\Administracao\Modulo;
|
|
use App\Models\PainelSenha\Fila;
|
|
use App\Models\PainelSenha\OpcaoEntrada;
|
|
use App\Services\Administracao\ModuloService;
|
|
use App\Services\Telegram\TelegramService;
|
|
use Exception;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
class LimparFilaAtendimentoCommand extends Command
|
|
{
|
|
protected $signature = 'painel-senha:limpar-fila-atendimento';
|
|
protected $description = 'Comando para limpesa da fila de atendimento no módulo painel de senha.';
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
try {
|
|
$currentDatabase = Config::get('database.default');
|
|
|
|
$this->getClientes()->each(function($cliente) {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
Config::set('database.default', $cliente);
|
|
|
|
Fila::where('finalizado', false)
|
|
->where('evadido', false)
|
|
->whereIn('opcao_entrada_id', $this->getOpcoesEntrada())
|
|
->update([
|
|
'finalizado' => true,
|
|
'evadido' => true,
|
|
]);
|
|
|
|
Log::channel('painel-senha-fila-atendimento')->info('Comando executado com sucesso.', [
|
|
'cliente' => $cliente,
|
|
'origem' => 'LimparFilaAtendimentoCommand',
|
|
]);
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
|
|
Log::channel('painel-senha-fila-atendimento')->error('Erro ao executar o comando', [
|
|
'cliente' => $cliente,
|
|
'origem' => 'LimparFilaAtendimentoCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
if (env('APP_ENV') === 'production') {
|
|
TelegramService::send('<strong>Farmácia (' . ($cliente) . '):</strong> (Erro ao executar o comando: LimparFilaAtendimentoCommand).');
|
|
}
|
|
|
|
return;
|
|
}
|
|
});
|
|
|
|
Config::set('database.default', $currentDatabase);
|
|
} catch (Exception $error) {
|
|
Log::channel('painel-senha-fila-atendimento')->error('Erro ao executar o comando', [
|
|
'origem' => 'LimparFilaAtendimentoCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
if (env('APP_ENV') === 'production') {
|
|
TelegramService::send('<strong>Painel de senha:</strong> (Erro ao executar o comando: LimparFilaAtendimentoCommand).');
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getClientes()
|
|
{
|
|
return collect(json_decode(file_get_contents(base_path('connections.json')), true))
|
|
->filter(fn ($config) => isset($config['database']) && Str::startsWith($config['database'], 'conectasus'))
|
|
->reject(fn ($config) => empty(DB::connection('schema')->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", [$config['database']])))
|
|
->map(fn ($config, $customer) => $customer)
|
|
->sort()
|
|
->values();
|
|
}
|
|
|
|
private function getOpcoesEntrada()
|
|
{
|
|
return OpcaoEntrada::where('modulo_id', ModuloService::getId(Modulo::PAINEL_SENHA))
|
|
->pluck('id');
|
|
}
|
|
}
|