This commit is contained in:
140
api/app/Services/Sigtap/CidService.php
Normal file
140
api/app/Services/Sigtap/CidService.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Sigtap;
|
||||
|
||||
use App\Models\Geral\Ciap;
|
||||
use App\Models\Sigtap\Cid;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CidService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
$cids = DB::table('sigtap.cid')
|
||||
->select(
|
||||
'cid.id',
|
||||
'cid.codigo',
|
||||
'cid.nome',
|
||||
'cid.is_ativo',
|
||||
);
|
||||
|
||||
$procedimentoId = request()->query('procedimento_id');
|
||||
|
||||
if (is_numeric($procedimentoId)) {
|
||||
|
||||
$cids->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('sigtap.procedimento_cid')
|
||||
->whereColumn('procedimento_cid.cid_id', 'cid.id')
|
||||
->where('procedimento_cid.procedimento_id', request()->query('procedimento_id'));
|
||||
});
|
||||
}
|
||||
|
||||
if (DataUtil::isStringBoolean(request()->query('principal'))) {
|
||||
$cids->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('sigtap.procedimento_cid')
|
||||
->whereColumn('procedimento_cid.cid_id', 'cid.id')
|
||||
->where('procedimento_cid.principal', DataUtil::getBooleanValueFromStringBoolean(request()->query('principal')) ? 'S' : 'N');
|
||||
});
|
||||
}
|
||||
|
||||
return datatables()
|
||||
->of($cids)
|
||||
->addColumn('status', function ($cid) {
|
||||
return $cid->is_ativo ? 'Ativo' : 'Inativo';
|
||||
})
|
||||
->skipTotalRecords()
|
||||
->make(true);
|
||||
}
|
||||
|
||||
public static function autocomplete()
|
||||
{
|
||||
$cids = DB::table('sigtap.cid');
|
||||
|
||||
$procedimentoId = request()->query('procedimento_id');
|
||||
|
||||
if (is_numeric($procedimentoId)) {
|
||||
|
||||
$cids->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('sigtap.procedimento_cid')
|
||||
->whereColumn('procedimento_cid.cid_id', 'cid.id')
|
||||
->where('procedimento_cid.procedimento_id', request()->query('procedimento_id'));
|
||||
});
|
||||
}
|
||||
|
||||
if (DataUtil::isStringBoolean(request()->query('principal'))) {
|
||||
$cids->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('sigtap.procedimento_cid')
|
||||
->whereColumn('procedimento_cid.cid_id', 'cid.id')
|
||||
->when(request()->query('competencia_id'), fn ($q, $id) => $q->where('procedimento_cid.competencia_id', $id))
|
||||
->when(request()->query('procedimento_id'), fn ($q, $id) => $q->where('procedimento_cid.procedimento_id', $id))
|
||||
->where('procedimento_cid.principal', DataUtil::getBooleanValueFromStringBoolean(request()->query('principal')) ? 'S' : 'N');
|
||||
});
|
||||
}
|
||||
|
||||
if (request()->query('ciap_id')) {
|
||||
|
||||
$cids->where(function ($query) {
|
||||
|
||||
$ciapCodigo = Ciap::findOrFail(request()->query('ciap_id'))->codigo;
|
||||
|
||||
$query->whereExists(function($query) use ($ciapCodigo) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('geral_ciap_cid')
|
||||
->whereColumn('geral_ciap_cid.codigo_cid', 'sigtap.cid.codigo')
|
||||
->where('codigo_ciap', $ciapCodigo);
|
||||
})
|
||||
->orWhereNotExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('geral_ciap_cid')
|
||||
->whereColumn('geral_ciap_cid.codigo_cid', 'sigtap.cid.codigo');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$cids->when(request()->cid_id, fn ($q, $id) => $q->whereNotIn('sigtap.cid.id', [$id]));
|
||||
$cids->when(request()->sexo, fn ($q, $string) => $q->whereIn('sigtap.cid.sexo', [$string, 'I']));
|
||||
|
||||
$cids->where('sigtap.cid.is_ativo', true);
|
||||
|
||||
return $cids;
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
return Cid::find($id)->update([
|
||||
'is_ativo' => DataUtil::getBooleanValueFromStringBoolean(request()->is_ativo),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('sigtap.cid as cid')
|
||||
->when(request()->codigo, fn ($q, $codigo) => $q->where('cid.codigo', $codigo))
|
||||
->select(
|
||||
'cid.id',
|
||||
'cid.codigo',
|
||||
'cid.nome',
|
||||
'cid.agravo',
|
||||
'cid.sexo',
|
||||
'cid.estado',
|
||||
'cid.campos_irradiados',
|
||||
)
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getCidId($codigo)
|
||||
{
|
||||
return Cid::where('codigo', $codigo)->first('id')->id ?? null;
|
||||
}
|
||||
|
||||
public static function getCid($codigo)
|
||||
{
|
||||
return Cid::where('codigo', $codigo)->first();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user