This commit is contained in:
138
api/app/Services/TransportePaciente/Grupo/GrupoService.php
Normal file
138
api/app/Services/TransportePaciente/Grupo/GrupoService.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente\Grupo;
|
||||
|
||||
use App\Exceptions\Model\DeletingModelException;
|
||||
use App\Models\TransportePaciente\Grupo\Grupo;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class GrupoService extends Service
|
||||
{
|
||||
|
||||
private static function getModelData($grupo = null)
|
||||
{
|
||||
|
||||
$grupo = [
|
||||
'id' => $grupo->id ?? null,
|
||||
'descricao' => $grupo->descricao ?? null,
|
||||
'bloqueado' => $grupo->bloqueado ?? null,
|
||||
];
|
||||
|
||||
return $grupo;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'model' => self::getModelData(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return Grupo::find($id);
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Grupo::where('id', $id)->update(request()->all());
|
||||
}
|
||||
|
||||
public static function bloquear($id)
|
||||
{
|
||||
$grupo = self::show($id);
|
||||
|
||||
return Grupo::where('id', $id)->update(['bloqueado' => $grupo->bloqueado == 1 ? 0 : 1]);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
return [
|
||||
'model' => self::getModelData(self::show($id)),
|
||||
];
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Grupo::create(request()->all())->fresh();
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
try {
|
||||
return self::show($id)->delete();
|
||||
} catch (\Throwable $th) {
|
||||
throw new DeletingModelException();
|
||||
}
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
|
||||
$grupos = DB::table('transporte_paciente_grupo')
|
||||
->select(
|
||||
'transporte_paciente_grupo.id',
|
||||
'transporte_paciente_grupo.descricao',
|
||||
'transporte_paciente_grupo.bloqueado',
|
||||
DB::raw('
|
||||
(
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.grupo_id IS NULL THEN
|
||||
true
|
||||
ELSE
|
||||
false
|
||||
END
|
||||
) AS delete_grupo'
|
||||
),
|
||||
DB::raw("
|
||||
CASE WHEN transporte_paciente_grupo.bloqueado THEN
|
||||
'INATIVO'
|
||||
ELSE
|
||||
'ATIVO'
|
||||
END AS status
|
||||
"),
|
||||
)
|
||||
->leftJoin(
|
||||
'transporte_paciente_motivo_viagem_procedimento',
|
||||
'transporte_paciente_motivo_viagem_procedimento.grupo_id',
|
||||
'transporte_paciente_grupo.id'
|
||||
)
|
||||
->distinct();
|
||||
|
||||
return datatables()
|
||||
->of($grupos)
|
||||
->filterColumn('transporte_paciente_grupo.bloqueado', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("
|
||||
CASE WHEN transporte_paciente_grupo.bloqueado THEN
|
||||
'INATIVO'
|
||||
ELSE
|
||||
'ATIVO'
|
||||
END LIKE ?", ["%{$keyword}%"]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private static function validate()
|
||||
{
|
||||
return request()->validate(
|
||||
[
|
||||
'descricao' => [
|
||||
'required',
|
||||
'min:5',
|
||||
'max:100',
|
||||
Rule::unique('transporte_paciente_grupo')
|
||||
->ignore(request()->grupo)
|
||||
],
|
||||
],
|
||||
[
|
||||
'descricao.unique' => 'O grupo já foi inserido anteriormente.',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user