This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\PainelSenha;
|
||||
|
||||
use App\Events\PainelSenha\AvisoEvent;
|
||||
use App\Models\PainelSenha\AvisoAgendamento;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class EnvioAvisoAgendadoCommand extends Command
|
||||
{
|
||||
protected $signature = 'painel-senha:envio-aviso-agendado';
|
||||
protected $description = 'Comando para enviar os avisos que estão agendados';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
$currentDatabase = Config::get('database.default');
|
||||
|
||||
self::getClientes()->each(function($cliente) {
|
||||
|
||||
Config::set('database.default', $cliente);
|
||||
|
||||
if(!Schema::hasTable('painel_senha_aviso')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$agora = Carbon::now();
|
||||
$toleranciaInicio = $agora->copy()->subSeconds(30);
|
||||
$toleranciaFim = $agora->copy()->addSeconds(30);
|
||||
|
||||
$agendamentos = DB::table('painel_senha_aviso_agendamento')
|
||||
->whereDate('data_hora_agendada', $agora->toDateString())
|
||||
->whereBetween('data_hora_agendada', [$toleranciaInicio, $toleranciaFim])
|
||||
->whereNull('data_hora_executada')
|
||||
->where('painel_senha_aviso.is_ativo', true)
|
||||
->join('painel_senha_aviso', 'painel_senha_aviso.id', 'painel_senha_aviso_agendamento.aviso_id')
|
||||
->get();
|
||||
|
||||
if($agendamentos->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$idsAgendamentos = $agendamentos->pluck("id");
|
||||
$idsAvisos = $agendamentos->pluck("aviso_id");
|
||||
|
||||
AvisoAgendamento::whereIn('id', $idsAgendamentos)
|
||||
->update(['data_hora_executada' => $agora]);
|
||||
|
||||
$avisos = DB::table('painel_senha_aviso')
|
||||
->select(
|
||||
'painel_senha_aviso.id',
|
||||
'painel_senha_aviso.mensagem',
|
||||
DB::raw('GROUP_CONCAT(cadastro_unidade.nome_fantasia) as unidades'),
|
||||
DB::raw('GROUP_CONCAT(cadastro_unidade.id) as ids_unidade'),
|
||||
'painel_senha_aviso.is_ativo',
|
||||
)
|
||||
->join('painel_senha_aviso_unidade', 'painel_senha_aviso_unidade.aviso_id', 'painel_senha_aviso.id')
|
||||
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'painel_senha_aviso_unidade.unidade_id')
|
||||
->groupBy('painel_senha_aviso.id', 'painel_senha_aviso.mensagem', 'painel_senha_aviso.is_ativo')
|
||||
->whereIn('painel_senha_aviso.id', $idsAvisos)
|
||||
->get();
|
||||
|
||||
$avisos->map(function($aviso) {
|
||||
AvisoEvent::dispatch($aviso->id, $aviso);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Config::set('database.default', $currentDatabase);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
Log::info("Não foi possível enviar aviso {$th}");
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user