This commit is contained in:
166
api/app/Services/Administracao/RelatorioAcessoSistemaService.php
Normal file
166
api/app/Services/Administracao/RelatorioAcessoSistemaService.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Administracao;
|
||||
|
||||
use App\Enums\Log\AcessoSistemaStatus;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioAcessoSistemaService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
$itens = DB::table('log_acesso_sistema')
|
||||
->select([
|
||||
'log_acesso_sistema.created_at AS data',
|
||||
DB::raw("DATE_FORMAT(log_acesso_sistema.created_at, '%H:%i:%s') AS hora"),
|
||||
'cadastro_usuario.id AS usuario_id',
|
||||
'cadastro_cidadao.nome AS usuario_nome',
|
||||
'log_acesso_sistema_status.id AS status_id',
|
||||
'log_acesso_sistema_status.nome AS status_nome',
|
||||
])
|
||||
->join('cadastro_usuario', 'cadastro_usuario.id', 'log_acesso_sistema.usuario_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('log_acesso_sistema_status', 'log_acesso_sistema_status.id', 'log_acesso_sistema.status_id')
|
||||
->whereBetween('log_acesso_sistema.created_at', $params->data)
|
||||
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_sistema.created_at', '>=', $params->hora['start']))
|
||||
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_sistema.created_at', '<=', $params->hora['end']))
|
||||
->when($params->usuario_id, fn ($q) => $q->where('log_acesso_sistema.usuario_id', $params->usuario_id))
|
||||
->when($params->status_id, fn ($q) => $q->where('log_acesso_sistema.status_id', $params->status_id));
|
||||
|
||||
self::setOrderBy($itens);
|
||||
|
||||
$itens = $itens->get();
|
||||
|
||||
return [
|
||||
'itens' => $itens,
|
||||
'totalizadores' => [
|
||||
'total_acessos' => $itens->count(),
|
||||
'total_usuario_acesso' => $itens->where('status_id', AcessoSistemaStatus::ACESSO_REALIZADO_COM_SUCESSO)->unique('usuario_id')->count(),
|
||||
'total_acesso_realizado_com_sucesso' => $itens->reduce(fn ($a, $b) => $a + ($b->status_id === AcessoSistemaStatus::ACESSO_REALIZADO_COM_SUCESSO ? 1 : 0)),
|
||||
'total_usuario_e_ou_senha_incorretos' => $itens->reduce(fn ($a, $b) => $a + ($b->status_id === AcessoSistemaStatus::USUARIO_E_OU_SENHA_INCORRETOS ? 1 : 0)),
|
||||
'total_usuario_bloqueado' => $itens->reduce(fn ($a, $b) => $a + ($b->status_id === AcessoSistemaStatus::USUARIO_BLOQUEADO ? 1 : 0)),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'additionals' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'form' => [
|
||||
'usuarios' => [],
|
||||
'status' => [],
|
||||
],
|
||||
'model' => [
|
||||
'data' => ['start' => null, 'end' => null],
|
||||
'hora' => ['start' => null, 'end' => null],
|
||||
'usuario_id' => null,
|
||||
'status_id' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
switch (request()->listar) {
|
||||
case 'usuarios': {
|
||||
return [
|
||||
'usuarios' => self::getUsuarios($params),
|
||||
'status' => self::getStatus($params),
|
||||
];
|
||||
}
|
||||
case 'status': {
|
||||
return [
|
||||
'status' => self::getStatus($params),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function getParams()
|
||||
{
|
||||
return (object) [
|
||||
'data' => [
|
||||
'start' => Carbon::createFromFormat('d/m/Y', request()->get('data')['start'])->setTime(0, 0, 0),
|
||||
'end' => Carbon::createFromFormat('d/m/Y', request()->get('data')['end'])->setTime(23, 59, 59)
|
||||
],
|
||||
'hora' => [
|
||||
'start' => isset(request()->get('hora')['start']) ? Carbon::createFromFormat('H:i:s', request()->get('hora')['start']) : null,
|
||||
'end' => isset(request()->get('hora')['end']) ? Carbon::createFromFormat('H:i:s', request()->get('hora')['end']) : null,
|
||||
],
|
||||
'usuario_id' => request()->get('usuario_id'),
|
||||
'status_id' => request()->get('status_id'),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getUsuarios($params)
|
||||
{
|
||||
return DB::table('cadastro_usuario')
|
||||
->select([
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome',
|
||||
])
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('log_acesso_sistema')
|
||||
->whereBetween('log_acesso_sistema.created_at', $params->data)
|
||||
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_sistema.created_at', '>=', $params->hora['start']))
|
||||
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_sistema.created_at', '<=', $params->hora['end']))
|
||||
->whereColumn('log_acesso_sistema.usuario_id', 'cadastro_usuario.id')
|
||||
)
|
||||
->orderBy('cadastro_cidadao.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getStatus($params)
|
||||
{
|
||||
return DB::table('log_acesso_sistema_status')
|
||||
->select([
|
||||
'log_acesso_sistema_status.id',
|
||||
'log_acesso_sistema_status.nome',
|
||||
])
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('log_acesso_sistema')
|
||||
->whereBetween('log_acesso_sistema.created_at', $params->data)
|
||||
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_sistema.created_at', '>=', $params->hora['start']))
|
||||
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_sistema.created_at', '<=', $params->hora['end']))
|
||||
->when($params->usuario_id, fn ($q) => $q->where('log_acesso_sistema.usuario_id', $params->usuario_id))
|
||||
->whereColumn('log_acesso_sistema.status_id', 'log_acesso_sistema_status.id')
|
||||
)
|
||||
->orderBy('log_acesso_sistema_status.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function setOrderBy($builder)
|
||||
{
|
||||
$columns = [
|
||||
'data' => 'log_acesso_sistema.created_at',
|
||||
'hora' => DB::raw("DATE_FORMAT(log_acesso_sistema.created_at, '%H:%i:%s')"),
|
||||
'usuario_nome' => 'cadastro_cidadao.nome',
|
||||
'status_nome' => 'log_acesso_sistema_status.nome',
|
||||
];
|
||||
|
||||
if (is_null(request()->sort_by)) {
|
||||
$builder->orderByRaw(join(', ', $columns));
|
||||
return;
|
||||
}
|
||||
|
||||
$column = $columns[request()->sort_by];
|
||||
$direction = DataUtil::getBooleanValueFromStringBoolean(request()->sort_desc) ? 'DESC' : 'ASC';
|
||||
|
||||
unset($columns[request()->sort_by]);
|
||||
|
||||
$builder->orderByRaw("{$column} {$direction}, " . join(', ', $columns));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user