150 lines
4.0 KiB
PHP
150 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Regulacao;
|
|
|
|
use App\Models\Regulacao\Status;
|
|
use App\Models\Regulacao\StatusTipo;
|
|
use App\Services\Service;
|
|
use App\Exceptions\Regulacao\StatusVinculadoException;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StatusService extends Service
|
|
{
|
|
public static function datatable()
|
|
{
|
|
return DB::table('regulacao_status as status')
|
|
->join('regulacao_status_tipo as status_tipo', 'status_tipo.id', 'status.tipo_id')
|
|
->leftJoin('regulacao_status_status as statu_status', 'statu_status.status_de_id', 'status.id')
|
|
->leftJoin('regulacao_status as status_para', 'status_para.id', 'statu_status.status_para_id')
|
|
->selectRaw("
|
|
status.id,
|
|
status.nome,
|
|
status_tipo.nome as tipo_nome,
|
|
GROUP_CONCAT(status_para.nome separator ', ') as status_para
|
|
")
|
|
->groupByRaw("
|
|
status.id,
|
|
status.nome,
|
|
status_tipo.nome
|
|
");
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'form' => self::getFormData(),
|
|
'model' => self::getModelData(),
|
|
];
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
$status = Status::findOrFail($id);
|
|
|
|
return [
|
|
'form' => self::getFormData($status),
|
|
'model' => self::getModelData($status),
|
|
];
|
|
}
|
|
|
|
private static function getFormData($status = null)
|
|
{
|
|
return [
|
|
'tipos' => StatusTipo::orderBy('nome')->get(['id', 'nome']),
|
|
'status' => Status::when($status, fn ($q) => $q->where('id', '<>', $status->id))
|
|
->orderBy('nome')
|
|
->get(['id', 'nome']),
|
|
];
|
|
}
|
|
|
|
private static function getModelData($status = null)
|
|
{
|
|
return [
|
|
'tipo_id' => $status->tipo_id ?? null,
|
|
'nome' => $status->nome ?? null,
|
|
'ativo' => $status->ativo ?? true,
|
|
'status_para' => is_null($status) ? collect() : $status->statusPara()->get(['id', 'nome']),
|
|
];
|
|
}
|
|
|
|
public static function store()
|
|
{
|
|
self::validate();
|
|
|
|
$status = new Status();
|
|
|
|
DB::transaction(function () use ($status) {
|
|
|
|
$status->fill(request()->except('status_para'));
|
|
$status->save();
|
|
|
|
$status->statusPara()->sync(request()->status_para);
|
|
});
|
|
|
|
return $status;
|
|
}
|
|
|
|
public static function update($id)
|
|
{
|
|
$status = Status::findOrFail($id);
|
|
|
|
self::validate();
|
|
|
|
DB::transaction(function () use ($status) {
|
|
|
|
$status->update(request()->except('status_para'));
|
|
$status->refresh();
|
|
|
|
$status->statusPara()->sync(request()->status_para);
|
|
});
|
|
|
|
return $status;
|
|
}
|
|
|
|
public static function validate()
|
|
{
|
|
return request()->validate([
|
|
'tipo_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(StatusTipo::class, 'id'),
|
|
],
|
|
'nome' => [
|
|
'required',
|
|
'string',
|
|
'between:3,100',
|
|
Rule::unique(Status::class, 'nome')->ignore(request()->route('status')),
|
|
],
|
|
'ativo' => [
|
|
'required',
|
|
'boolean',
|
|
],
|
|
'status_para' => [
|
|
'present',
|
|
'array',
|
|
],
|
|
'status_para.*' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Status::class, 'id'),
|
|
]
|
|
], [
|
|
'nome.unique' => 'O nome do Status já foi inserido anteriormente.'
|
|
]);
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
$status = Status::findOrFail($id);
|
|
|
|
$statusStatus = DB::table('regulacao_status_status')->where('status_para_id', $id)->exists();
|
|
|
|
if($statusStatus){
|
|
throw new StatusVinculadoException();
|
|
};
|
|
|
|
$status->delete();
|
|
}
|
|
}
|