This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Mobile\ConectaCidadao\ExameImagem;
|
||||
|
||||
use App\Enums\ExameImagem\Fechamento\Status;
|
||||
use App\Models\ExameImagem\Parametrizacao\IntegracaoAplicativo;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class LaudoService extends Service
|
||||
{
|
||||
public static function laudos(): array
|
||||
{
|
||||
self::validarVisualizacaoHabilitada();
|
||||
|
||||
$paciente = auth('mobile')->user();
|
||||
|
||||
return DB::table('exame_imagem_agendamento as agendamento')
|
||||
->join('exame_imagem_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->leftJoin('cadastro_infraestrutura as infra_sol', 'infra_sol.id', 'agendamento.infraestrutura_solicitante_id')
|
||||
->leftJoin('cadastro_unidade as unidade_sol', 'unidade_sol.id', 'infra_sol.unidade_id')
|
||||
->leftJoin('cadastro_usuario as medico_usuario', 'medico_usuario.id', 'agendamento.profissional_id')
|
||||
->leftJoin('cadastro_cidadao as medico_cidadao', 'medico_cidadao.id', 'medico_usuario.cidadao_id')
|
||||
->where('agendamento.paciente_id', $paciente->id)
|
||||
->whereExists(function ($sub) {
|
||||
$sub->select(DB::raw(1))
|
||||
->from('exame_imagem_agendamento_exame as exame')
|
||||
->whereColumn('exame.agendamento_id', 'agendamento.id')
|
||||
->where('exame.status_id', Status::REALIZADO);
|
||||
})
|
||||
->orderByDesc('jornada.data')
|
||||
->get([
|
||||
'agendamento.id',
|
||||
'jornada.data',
|
||||
'agendamento.hora_exame',
|
||||
'unidade.nome_fantasia as unidade_nome',
|
||||
'unidade_sol.nome_fantasia as unidade_solicitante',
|
||||
'medico_cidadao.nome as medico_nome',
|
||||
])
|
||||
->map(
|
||||
fn($item) =>
|
||||
[
|
||||
'id' => $item->id,
|
||||
'data' => $item->data,
|
||||
'hora_exame' => $item->hora_exame,
|
||||
'unidade' => $item->unidade_nome,
|
||||
'unidade_solicitante' => $item->unidade_solicitante,
|
||||
'medico' => $item->medico_nome,
|
||||
'exames' => self::getExamesComResultado($item->id),
|
||||
]
|
||||
)
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private static function getExamesComResultado(int $agendamentoId): array
|
||||
{
|
||||
return DB::table('exame_imagem_agendamento_exame as exame')
|
||||
->join('parametrizacao_exame as param_exame', 'param_exame.id', 'exame.exame_id')
|
||||
->leftJoin('cadastro_usuario as usuario', 'usuario.id', 'exame.usuario_id')
|
||||
->leftJoin('cadastro_cidadao as cidadao_usuario', 'cidadao_usuario.id', 'usuario.cidadao_id')
|
||||
->where('exame.agendamento_id', $agendamentoId)
|
||||
->where('exame.status_id', Status::REALIZADO)
|
||||
->get(
|
||||
[
|
||||
'exame.id',
|
||||
'param_exame.nome',
|
||||
'param_exame.codigo',
|
||||
'exame.arquivo',
|
||||
'exame.resultado',
|
||||
'exame.laudo_nome',
|
||||
'cidadao_usuario.nome as lancado_por',
|
||||
]
|
||||
)
|
||||
->map(
|
||||
fn($exame) =>
|
||||
[
|
||||
'id' => $exame->id,
|
||||
'nome' => $exame->nome,
|
||||
'codigo' => $exame->codigo,
|
||||
'resultado' => $exame->resultado,
|
||||
'laudo_nome' => $exame->laudo_nome,
|
||||
'lancado_por' => $exame->lancado_por,
|
||||
'url_download' => $exame->arquivo
|
||||
? url("api/mobile/conecta-cidadao/exame-imagem/laudos/{$exame->id}/download")
|
||||
: null,
|
||||
]
|
||||
)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public static function download(int $exameId): array
|
||||
{
|
||||
$paciente = auth('mobile')->user();
|
||||
|
||||
$exame = DB::table('exame_imagem_agendamento_exame as exame')
|
||||
->join('exame_imagem_agendamento as agendamento', 'agendamento.id', 'exame.agendamento_id')
|
||||
->where('exame.id', $exameId)
|
||||
->where('agendamento.paciente_id', $paciente->id)
|
||||
->whereNotNull('exame.arquivo')
|
||||
->first(['exame.arquivo']);
|
||||
|
||||
abort_if(is_null($exame), Response::HTTP_NOT_FOUND, 'Laudo não encontrado.');
|
||||
|
||||
$caminho = 'exame-imagem' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $exame->arquivo;
|
||||
|
||||
abort_if(!Storage::exists($caminho), Response::HTTP_NOT_FOUND, 'Arquivo do laudo não encontrado.');
|
||||
|
||||
return [
|
||||
'nome' => $exame->arquivo,
|
||||
'caminho' => Storage::path($caminho),
|
||||
];
|
||||
}
|
||||
|
||||
private static function validarVisualizacaoHabilitada(): void
|
||||
{
|
||||
$integracao = IntegracaoAplicativo::first();
|
||||
|
||||
if (!($integracao->habilita_consulta_imagem_conectasus_cidadao ?? false)) {
|
||||
|
||||
abort(Response::HTTP_FORBIDDEN, 'A consulta de exames de imagem pelo ConectaSUS Cidadão não está habilitada.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user