This commit is contained in:
167
api/app/Services/Cadastro/Grupo/GrupoService.php
Normal file
167
api/app/Services/Cadastro/Grupo/GrupoService.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cadastro\Grupo;
|
||||
|
||||
use App\Models\Cadastro\Grupo\Grupo;
|
||||
use App\Models\Cadastro\Subgrupo\Subgrupo;
|
||||
use App\Services\Service;
|
||||
use DB;
|
||||
|
||||
class GrupoService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
$grupos = DB::table('cadastro_grupo')
|
||||
->select(
|
||||
'cadastro_grupo.id',
|
||||
'cadastro_grupo.nome',
|
||||
'cadastro_grupo_tipo.nome AS grupo_tipo'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_grupo_tipo',
|
||||
'cadastro_grupo_tipo.id',
|
||||
'cadastro_grupo.grupo_tipo_id'
|
||||
)
|
||||
->whereNull('cadastro_grupo.deleted_at');
|
||||
|
||||
return $grupos;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'nome' => null,
|
||||
'grupo_tipo_id' => null,
|
||||
'impressao_mapa_producao' => false,
|
||||
'is_regulado' => false,
|
||||
'regulacao_tipo_id' => null,
|
||||
'subgrupos' => []
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
$grupo = Grupo::create(request()->all());
|
||||
|
||||
self::saveSubgrupos(request()->subgrupos, $grupo->id);
|
||||
|
||||
return $grupo->fresh();
|
||||
}
|
||||
|
||||
private static function saveSubgrupos($params, $grupoId)
|
||||
{
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($params as $param_grupo) {
|
||||
|
||||
if ($param_grupo['id'] && is_numeric($param_grupo['id'])) {
|
||||
|
||||
$grupo = SubGrupo::where('id', $param_grupo['id'])->first();
|
||||
} else {
|
||||
|
||||
$grupo = new SubGrupo();
|
||||
}
|
||||
|
||||
$grupo->grupo_id = $grupoId;
|
||||
$grupo->nome = $param_grupo['nome'];
|
||||
|
||||
$grupo->save();
|
||||
$ids[] = $grupo->id;
|
||||
}
|
||||
|
||||
SubGrupo::destroy(SubGrupo::where('grupo_id', $grupoId)
|
||||
->whereNotIn('id', $ids)->pluck('id'));
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$grupo = Grupo::findOrFail($id);
|
||||
$grupo['subgrupos'] = $grupo->subgrupos;
|
||||
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => $grupo->makeHidden(['id'])
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'grupo_tipos' => self::getGrupoTipos(),
|
||||
'regulacao_tipos' => self::getRegulacaoTipos(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getGrupoTipos()
|
||||
{
|
||||
return DB::table('cadastro_grupo_tipo')
|
||||
->select(
|
||||
'id',
|
||||
'nome'
|
||||
)
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getRegulacaoTipos()
|
||||
{
|
||||
return DB::table('regulacao_tipo')
|
||||
->select(
|
||||
'id',
|
||||
'nome'
|
||||
)
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validate();
|
||||
|
||||
$grupo = tap(Grupo::findOrFail($id))->update(request()->all());
|
||||
|
||||
self::saveSubgrupos(request()->subgrupos, $grupo->id);
|
||||
|
||||
return $grupo->fresh();
|
||||
}
|
||||
|
||||
public static function validate()
|
||||
{
|
||||
$rule = [
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
'unique:\App\Models\Cadastro\Grupo\Grupo,nome,'
|
||||
. (request()->route('grupo') ?? 'NULL') . ',id,deleted_at,NULL',
|
||||
],
|
||||
'grupo_tipo_id' => [
|
||||
'nullable',
|
||||
'integer'
|
||||
],
|
||||
'regulacao_tipo_id' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
]
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'nome.unique' => 'O nome do Grupo já foi inserido anteriormente.',
|
||||
];
|
||||
|
||||
request()->validate($rule, $messages);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
Grupo::findOrFail($id)->delete();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user