281 lines
14 KiB
PHP
281 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Administracao;
|
|
|
|
use App\Services\Service;
|
|
use App\Utils\DataUtil;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RelatorioAcessoFuncionalidadeService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
$params = self::getParams();
|
|
|
|
$itens = DB::table('log_acesso_funcionalidade')
|
|
->select([
|
|
'log_acesso_funcionalidade.id',
|
|
DB::raw("DATE_FORMAT(log_acesso_funcionalidade.created_at, '%d/%m/%Y') AS data"),
|
|
DB::raw("DATE_FORMAT(log_acesso_funcionalidade.created_at, '%H:%i:%s') AS hora"),
|
|
'cadastro_cidadao.nome AS usuario_nome',
|
|
'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia',
|
|
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome"),
|
|
'administracao_modulo.nome AS modulo_nome',
|
|
'administracao_funcionalidade.nome AS funcionalidade_nome',
|
|
])
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'log_acesso_funcionalidade.created_lotacao_id')
|
|
->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
|
->join('administracao_funcionalidade', 'administracao_funcionalidade.id', 'log_acesso_funcionalidade.funcionalidade_id')
|
|
->join('administracao_modulo', 'administracao_modulo.id', 'administracao_funcionalidade.modulo_id')
|
|
->whereBetween('log_acesso_funcionalidade.created_at', $params->data)
|
|
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '>=', $params->hora['start']))
|
|
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '<=', $params->hora['end']))
|
|
->when($params->usuario_id, fn ($q) => $q->where('cadastro_usuario.id', $params->usuario_id))
|
|
->when($params->unidade_id, fn ($q) => $q->where('cadastro_unidade.id', $params->unidade_id))
|
|
->when($params->infraestrutura_id, fn ($q) => $q->where('cadastro_infraestrutura.id', $params->infraestrutura_id))
|
|
->when($params->modulo_id, fn ($q) => $q->where('administracao_modulo.id', $params->modulo_id))
|
|
->when($params->funcionalidade_id, fn ($q) => $q->where('administracao_funcionalidade.id', $params->funcionalidade_id));
|
|
|
|
self::setOrderBy($itens);
|
|
|
|
$itens = $itens->get();
|
|
|
|
return [
|
|
'itens' => $itens,
|
|
'totalizadores' => [
|
|
'total_acessos' => $itens->count(),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'additionals' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
],
|
|
'form' => [
|
|
'usuarios' => [],
|
|
'unidades' => [],
|
|
'modulos' => [],
|
|
'funcionalidades' => [],
|
|
],
|
|
'model' => [
|
|
'data' => ['start' => null, 'end' => null],
|
|
'hora' => ['start' => null, 'end' => null],
|
|
'usuario_id' => null,
|
|
'unidade_id' => null,
|
|
'modulo_id' => null,
|
|
'funcionalidade_id' => null,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$params = self::getParams();
|
|
|
|
switch (request()->listar) {
|
|
case 'usuarios': {
|
|
return [
|
|
'usuarios' => self::getUsuarios($params),
|
|
'unidades' => self::getUnidades($params),
|
|
'infraestruturas' => self::getInfraestruturas($params),
|
|
'modulos' => self::getModulos($params),
|
|
'funcionalidades' => self::getFuncionalidades($params),
|
|
];
|
|
}
|
|
case 'unidades': {
|
|
return [
|
|
'unidades' => self::getUnidades($params),
|
|
'infraestruturas' => self::getInfraestruturas($params),
|
|
'modulos' => self::getModulos($params),
|
|
'funcionalidades' => self::getFuncionalidades($params),
|
|
];
|
|
}
|
|
case 'infraestruturas': {
|
|
return [
|
|
'infraestruturas' => self::getInfraestruturas($params),
|
|
'modulos' => self::getModulos($params),
|
|
'funcionalidades' => self::getFuncionalidades($params),
|
|
];
|
|
}
|
|
case 'modulos': {
|
|
return [
|
|
'modulos' => self::getModulos($params),
|
|
'funcionalidades' => self::getFuncionalidades($params),
|
|
];
|
|
}
|
|
case 'funcionalidades': {
|
|
return [
|
|
'funcionalidades' => self::getFuncionalidades($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'),
|
|
'unidade_id' => request()->get('unidade_id'),
|
|
'infraestrutura_id' => request()->get('infraestrutura_id'),
|
|
'modulo_id' => request()->get('modulo_id'),
|
|
'funcionalidade_id' => request()->get('funcionalidade_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_funcionalidade')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'log_acesso_funcionalidade.created_lotacao_id')
|
|
->whereBetween('log_acesso_funcionalidade.created_at', $params->data)
|
|
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '>=', $params->hora['start']))
|
|
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '<=', $params->hora['end']))
|
|
->whereColumn('cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
|
)
|
|
->orderBy('cadastro_cidadao.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getUnidades($params)
|
|
{
|
|
return DB::table('cadastro_unidade')
|
|
->select([
|
|
'cadastro_unidade.id',
|
|
'cadastro_unidade.nome_fantasia',
|
|
])
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('log_acesso_funcionalidade')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'log_acesso_funcionalidade.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->whereBetween('log_acesso_funcionalidade.created_at', $params->data)
|
|
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '>=', $params->hora['start']))
|
|
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '<=', $params->hora['end']))
|
|
->when($params->usuario_id, fn ($q) => $q->where('cadastro_lotacao.usuario_id', $params->usuario_id))
|
|
->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
|
)
|
|
->orderBy('cadastro_unidade.nome_fantasia')
|
|
->get();
|
|
}
|
|
|
|
private static function getInfraestruturas($params)
|
|
{
|
|
return DB::table('cadastro_infraestrutura')
|
|
->select([
|
|
'cadastro_infraestrutura.id',
|
|
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS nome"),
|
|
])
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
|
->when($params->unidade_id, fn ($q) => $q->where('cadastro_infraestrutura.unidade_id', $params->unidade_id))
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('log_acesso_funcionalidade')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'log_acesso_funcionalidade.created_lotacao_id')
|
|
->whereBetween('log_acesso_funcionalidade.created_at', $params->data)
|
|
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '>=', $params->hora['start']))
|
|
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '<=', $params->hora['end']))
|
|
->when($params->usuario_id, fn ($q) => $q->where('cadastro_lotacao.usuario_id', $params->usuario_id))
|
|
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
|
|
)
|
|
->orderByRaw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)')
|
|
->get();
|
|
}
|
|
|
|
private static function getModulos($params)
|
|
{
|
|
return DB::table('administracao_modulo')
|
|
->select([
|
|
'administracao_modulo.id',
|
|
'administracao_modulo.nome',
|
|
])
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('log_acesso_funcionalidade')
|
|
->join('administracao_funcionalidade', 'administracao_funcionalidade.id', 'log_acesso_funcionalidade.funcionalidade_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'log_acesso_funcionalidade.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->whereBetween('log_acesso_funcionalidade.created_at', $params->data)
|
|
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '>=', $params->hora['start']))
|
|
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '<=', $params->hora['end']))
|
|
->when($params->usuario_id, fn ($q) => $q->where('cadastro_lotacao.usuario_id', $params->usuario_id))
|
|
->when($params->unidade_id, fn ($q) => $q->where('cadastro_infraestrutura.unidade_id', $params->unidade_id))
|
|
->when($params->infraestrutura_id, fn ($q) => $q->where('cadastro_lotacao.infraestrutura_id', $params->infraestrutura_id))
|
|
->whereColumn('administracao_funcionalidade.modulo_id', 'administracao_modulo.id')
|
|
)
|
|
->orderBy('administracao_modulo.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getFuncionalidades($params)
|
|
{
|
|
return DB::table('administracao_funcionalidade')
|
|
->select([
|
|
'administracao_funcionalidade.id',
|
|
'administracao_funcionalidade.nome',
|
|
])
|
|
->when($params->modulo_id, fn ($q) => $q->where('administracao_funcionalidade.modulo_id', $params->modulo_id))
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('log_acesso_funcionalidade')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'log_acesso_funcionalidade.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->whereBetween('log_acesso_funcionalidade.created_at', $params->data)
|
|
->when($params->hora['start'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '>=', $params->hora['start']))
|
|
->when($params->hora['end'], fn ($q) => $q->whereTime('log_acesso_funcionalidade.created_at', '<=', $params->hora['end']))
|
|
->when($params->usuario_id, fn ($q) => $q->where('cadastro_lotacao.usuario_id', $params->usuario_id))
|
|
->when($params->unidade_id, fn ($q) => $q->where('cadastro_infraestrutura.unidade_id', $params->unidade_id))
|
|
->when($params->infraestrutura_id, fn ($q) => $q->where('cadastro_lotacao.infraestrutura_id', $params->infraestrutura_id))
|
|
->whereColumn('log_acesso_funcionalidade.funcionalidade_id', 'administracao_funcionalidade.id')
|
|
)
|
|
->orderBy('administracao_funcionalidade.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function setOrderBy($builder)
|
|
{
|
|
$columns = [
|
|
'data' => DB::raw("DATE_FORMAT(log_acesso_funcionalidade.created_at, '%d/%m/%Y')"),
|
|
'hora' => DB::raw("DATE_FORMAT(log_acesso_funcionalidade.created_at, '%H:%i:%s')"),
|
|
'usuario_nome' => 'cadastro_cidadao.nome',
|
|
'unidade_nome_fantasia' => 'cadastro_unidade.nome_fantasia',
|
|
'infraestrutura_nome' => DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"),
|
|
'modulo_nome' => 'administracao_modulo.nome',
|
|
'funcionalidade_nome' => 'administracao_funcionalidade.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));
|
|
}
|
|
}
|