Proyectos de Subversion LeadersLinked - Services

Rev

Rev 529 | Rev 531 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 529 Rev 530
Línea 322... Línea 322...
322
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // variante RFC 4122
322
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // variante RFC 4122
Línea 323... Línea 323...
323
 
323
 
324
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
324
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
Línea 325... Línea -...
325
    }
-
 
326
 
325
    }
327
 
326
 
328
    public function insertOrUpdateCompetenciesFromAIResponse($jobDescriptionId, array $aiCompetencies, $competencyTypeId = 1)
327
    public function insertOrUpdateCompetenciesFromAIResponse($jobDescriptionId, array $aiCompetencies, $competencyTypeId = 1)
-
 
328
    {
-
 
329
        if (!is_numeric($jobDescriptionId) || $jobDescriptionId <= 0) {
329
    {
330
            return [
-
 
331
                'success' => false,
-
 
332
                'message' => 'ID de descripción inválido',
330
        if (!is_numeric($jobDescriptionId) || $jobDescriptionId <= 0) {
333
                'data' => []
Línea 331... Línea 334...
331
            return [false, 'ID de descripción inválido'];
334
            ];
332
        }
335
        }
333
 
336
 
Línea 334... Línea 337...
334
        $adapter = $this->adapter;
337
        $adapter = $this->adapter;
-
 
338
        $sql = new Sql($adapter);
335
        $sql = new Sql($adapter);
339
        $results = [];
336
        $results = [];
340
 
337
 
341
        foreach ($aiCompetencies as $comp) {
338
        foreach ($aiCompetencies as $comp) {
-
 
339
            $name = trim($comp['name'] ?? '');
342
            // 🔹 Preparar datos
Línea 340... Línea 343...
340
            $description = trim($comp['description'] ?? '');
343
            $name = trim($comp['name'] ?? '');
341
            $hasIdRaw = $comp['has_id'] ?? "{'bool': false, 'nro': 0}";
344
            $description = trim($comp['description'] ?? '');
342
 
345
            $hasIdRaw = str_replace("'", '"', $comp['has_id'] ?? '{"bool": false, "nro": 0}');
Línea 343... Línea 346...
343
            $hasIdData = json_decode(str_replace("'", '"', $hasIdRaw), true);
346
            $hasIdData = json_decode($hasIdRaw, true);
344
 
347
 
Línea 345... Línea 348...
345
            if (!$name || !$description || !is_array($hasIdData)) {
348
            if (!$name || !$description || !is_array($hasIdData)) {
346
                continue;
349
                continue; // Datos incompletos, lo ignoramos
347
            }
350
            }
-
 
351
 
348
 
352
            $competencyId = null;
349
            $competencyId = null;
353
            $status = '';
350
            $status = '';
354
 
351
 
355
            // 🔹 Crear nueva competencia si nro == 0
352
            // 🔹 Si no tiene ID (nro = 0) → Crear
356
            if ((int) ($hasIdData['nro'] ?? 0) === 0) {
Línea 364... Línea 368...
364
                    ]);
368
                    ]);
365
                $sql->prepareStatementForSqlObject($insert)->execute();
369
                $sql->prepareStatementForSqlObject($insert)->execute();
366
                $competencyId = $adapter->getDriver()->getLastGeneratedValue();
370
                $competencyId = $adapter->getDriver()->getLastGeneratedValue();
367
                $status = 'created';
371
                $status = 'created';
368
            }
372
            }
369
            // 🔹 Si tiene ID existente → Buscar y actualizar nombre + descripción
373
            // 🔹 Actualizar competencia existente si nro > 0
370
            else {
374
            else {
371
                $competencyId = (int) $hasIdData['nro'];
375
                $competencyId = (int) $hasIdData['nro'];
Línea 372... Línea 376...
372
 
376
 
373
                $select = $sql->select(CompetencyMapper::_TABLE)
377
                $select = $sql->select(CompetencyMapper::_TABLE)
374
                    ->where(['id' => $competencyId]);
378
                    ->where(['id' => $competencyId]);
Línea 375... Línea 379...
375
                $existing = $sql->prepareStatementForSqlObject($select)->execute()->current();
379
                $existing = $sql->prepareStatementForSqlObject($select)->execute()->current();
376
 
-
 
377
                if ($existing) {
-
 
378
                    // Actualizar nombre y descripción si cambiaron
-
 
379
                    if ($existing['name'] !== $name || $existing['description'] !== $description) {
380
 
380
                        $update = $sql->update(CompetencyMapper::_TABLE)
381
                if (!$existing) {
381
                            ->set([
382
                    return [
382
                                'name' => $name,
-
 
383
                                'description' => $description,
383
                        'success' => false,
384
                                'updated_on' => date('Y-m-d H:i:s')
-
 
385
                            ])
-
 
386
                            ->where(['id' => $competencyId]);
384
                        'message' => "ID de competencia {$competencyId} no encontrado",
387
                        $sql->prepareStatementForSqlObject($update)->execute();
-
 
388
                    }
-
 
389
 
-
 
390
                    $status = 'updated_existing';
-
 
391
                } else {
-
 
392
                    // ⚠️ Si no encuentra el ID, podrías lanzar error o tratarlo como nueva
385
                        'data' => []
-
 
386
                    ];
-
 
387
                }
-
 
388
 
-
 
389
                // Solo actualizamos si realmente cambia el nombre o la descripción
-
 
390
                if ($existing['name'] !== $name || $existing['description'] !== $description) {
-
 
391
                    $update = $sql->update(CompetencyMapper::_TABLE)
-
 
392
                        ->set([
-
 
393
                            'name' => $name,
-
 
394
                            'description' => $description,
-
 
395
                            'updated_on' => date('Y-m-d H:i:s')
-
 
396
                        ])
-
 
397
                        ->where(['id' => $competencyId]);
-
 
398
                    $sql->prepareStatementForSqlObject($update)->execute();
-
 
399
                }
393
                    return [false, "ID de competencia {$competencyId} no encontrado"];
400
 
Línea 394... Línea 401...
394
                }
401
                $status = 'updated';
395
            }
402
            }
396
 
403
 
397
            // 🔹 Vincular competencia con descripción de trabajo
404
            // 🔹 Vincular competencia con job description si no está vinculada
398
            $checkLink = $sql->select(JobDescriptionCompetencyMapper::_TABLE)
405
            $linkCheck = $sql->select(JobDescriptionCompetencyMapper::_TABLE)
399
                ->where([
406
                ->where([
400
                    'job_description_id' => $jobDescriptionId,
407
                    'job_description_id' => $jobDescriptionId,
Línea 401... Línea 408...
401
                    'competency_id' => $competencyId
408
                    'competency_id' => $competencyId
402
                ]);
409
                ]);
403
            $existsLink = $sql->prepareStatementForSqlObject($checkLink)->execute()->current();
410
            $existsLink = $sql->prepareStatementForSqlObject($linkCheck)->execute()->current();
404
 
411
 
405
            if (!$existsLink) {
412
            if (!$existsLink) {
406
                $linkInsert = $sql->insert(JobDescriptionCompetencyMapper::_TABLE)
413
                $linkInsert = $sql->insert(JobDescriptionCompetencyMapper::_TABLE)
407
                    ->values([
414
                    ->values([
408
                        'job_description_id' => $jobDescriptionId,
415
                        'job_description_id' => $jobDescriptionId,
Línea 409... Línea 416...
409
                        'competency_id' => $competencyId
416
                        'competency_id' => $competencyId
410
                    ]);
417
                    ]);
411
                $sql->prepareStatementForSqlObject($linkInsert)->execute();
418
                $sql->prepareStatementForSqlObject($linkInsert)->execute();
412
            }
419
            }
413
 
420
 
414
            // 🔹 Registrar resultado
421
            // 🔹 Guardar resultado
415
            $results[] = [
422
            $results[] = [
Línea 416... Línea 423...
416
                'name' => $name,
423
                'name' => $name,
-
 
424
                'competency_id' => $competencyId,
-
 
425
                'status' => $status
-
 
426
            ];
-
 
427
        }
417
                'competency_id' => $competencyId,
428
 
Línea -... Línea 429...
-
 
429
        return [
418
                'status' => $status
430
            'success' => true,
419
            ];
431
            'message' => 'Procesamiento completado',
420
        }
432
            'data' => $results
421
 
433
        ];
422
        return $results;
434
    }