select([ DB::RAW(" CASE WHEN cadastro_usuario.id IS NOT NULL AND cadastro_paciente.id IS NOT NULL THEN 'AMBOS' WHEN cadastro_usuario.id IS NOT NULL THEN 'USUÁRIO' WHEN cadastro_paciente.id IS NOT NULL THEN 'PACIENTE' ELSE 'NENHUM' END as tipo_cidadao "), 'cadastro_cidadao.id', 'cadastro_paciente.id AS paciente_id', 'cadastro_usuario.id AS usuario_id', 'cadastro_cidadao.nome', 'cadastro_cidadao.data_nascimento', 'cadastro_cidadao.nome_mae', 'cadastro_cidadao.cns', 'cadastro_cidadao.cpf', 'cadastro_paciente.prontuario', ]) ->leftJoin('cadastro_paciente', 'cadastro_paciente.cidadao_id', 'cadastro_cidadao.id') ->leftJoin('cadastro_usuario', 'cadastro_usuario.cidadao_id', 'cadastro_cidadao.id') ->whereNull('cadastro_paciente.deleted_at') ->whereNull('cadastro_usuario.deleted_at'); } public static function store($request) { DB::transaction(function() use ($request) { $cliente = app('config')->get('database.default'); $cidadaoDe = $request['cidadao_de']; $cidadaoPara = $request['cidadao_para']; if (!is_null($cidadaoDe['paciente_id']) && !is_null($cidadaoPara['paciente_id'])) { DB::table('INFORMATION_SCHEMA.KEY_COLUMN_USAGE') ->select([ 'TABLE_SCHEMA', 'TABLE_NAME', 'COLUMN_NAME', ]) ->where('REFERENCED_TABLE_SCHEMA', "conectasus_$cliente") ->where('REFERENCED_TABLE_NAME', 'cadastro_paciente') ->where('REFERENCED_COLUMN_NAME', 'id') ->get() ->each(function ($query) use ($cidadaoDe, $cidadaoPara) { DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['paciente_id']) ->update([$query->COLUMN_NAME => $cidadaoPara['paciente_id']]); }); } if (!is_null($cidadaoDe['usuario_id']) && !is_null($cidadaoPara['usuario_id'])) { DB::table('INFORMATION_SCHEMA.KEY_COLUMN_USAGE') ->select([ 'TABLE_SCHEMA', 'TABLE_NAME', 'COLUMN_NAME', ]) ->where('REFERENCED_TABLE_SCHEMA', "conectasus_$cliente") ->where('REFERENCED_TABLE_NAME', 'cadastro_usuario') ->where('REFERENCED_COLUMN_NAME', 'id') ->get() ->each(function ($query) use ($cidadaoDe, $cidadaoPara) { DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['usuario_id']) ->update([$query->COLUMN_NAME => $cidadaoPara['usuario_id']]); }); } DB::table('INFORMATION_SCHEMA.KEY_COLUMN_USAGE') ->select([ 'TABLE_SCHEMA', 'TABLE_NAME', 'COLUMN_NAME', ]) ->where('REFERENCED_TABLE_SCHEMA', "conectasus_$cliente") ->where('REFERENCED_TABLE_NAME', 'cadastro_cidadao') ->where('REFERENCED_COLUMN_NAME', 'id') ->get() ->each(function ($query) use ($cidadaoDe, $cidadaoPara) { if ($query->TABLE_NAME === 'cadastro_paciente') { $hasPacientePara = DB::table('cadastro_paciente') ->where('cidadao_id', $cidadaoPara['id']) ->exists(); if (!$hasPacientePara) { DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['id']) ->update([$query->COLUMN_NAME => $cidadaoPara['id']]); } } else if ($query->TABLE_NAME === 'cadastro_usuario') { $hasUsuarioPara = DB::table('cadastro_usuario') ->where('cidadao_id', $cidadaoPara['id']) ->exists(); if (!$hasUsuarioPara) { DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['id']) ->update([$query->COLUMN_NAME => $cidadaoPara['id']]); } } if($query->TABLE_NAME === 'cadastro_paciente_informacao_social'){ DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['id']) ->update([$query->COLUMN_NAME=> $cidadaoPara['id']]); } if($query->TABLE_NAME === 'atencao_basica_cadastro_individual'){ DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['id']) ->update([$query->COLUMN_NAME=> $cidadaoPara['id']]); } DB::table("$query->TABLE_SCHEMA.$query->TABLE_NAME") ->where($query->COLUMN_NAME, $cidadaoDe['id']) ->delete(); }); DB::table("conectasus_$cliente.cadastro_cidadao") ->where('id', $cidadaoDe['id']) ->delete(); LogCadastroUnificacaoUsuarioPaciente::create([ 'dados' => json_encode([ 'de' => [ 'cidadao_id' => $cidadaoDe['id'], 'paciente_id' => $cidadaoDe['paciente_id'], 'usuario_id' => $cidadaoDe['usuario_id'], 'nome' => $cidadaoDe['nome'], 'data_nascimento' => $cidadaoDe['data_nascimento'], 'nome_mae' => $cidadaoDe['nome_mae'], 'cns' => isset($cidadaoDe['cns']) ? str_replace(' ', '', $cidadaoDe['cns']) : null, 'cpf' => isset($cidadaoDe['cpf']) ? str_replace(['.', '-'], '', $cidadaoDe['cpf']) : null, 'prontuario' => $cidadaoDe['prontuario'], ], 'para' => [ 'cidadao_id' => $cidadaoPara['id'], 'paciente_id' => $cidadaoPara['paciente_id'], 'usuario_id' => $cidadaoPara['usuario_id'], 'nome' => $cidadaoPara['nome'], 'data_nascimento' => $cidadaoPara['data_nascimento'], 'nome_mae' => $cidadaoPara['nome_mae'], 'cns' => isset($cidadaoPara['cns']) ? str_replace(' ', '', $cidadaoPara['cns']) : null, 'cpf' => isset($cidadaoPara['cpf']) ? str_replace(['.', '-'], '', $cidadaoPara['cpf']) : null, 'prontuario' => $cidadaoPara['prontuario'], ], ]) ]); }); } }