questionId); if (!$question) { return; } $matches = $service->matchKnowledgePoints($question->stem ?? ''); $hasValidKp = $service->isValidKnowledgePoint($question->kp_code); if (empty($matches) || !$hasValidKp) { $matches = $service->matchKnowledgePointsByAi($question->stem ?? ''); } if (!empty($matches)) { $question->kp_code = $matches[0]['kp_code'] ?? $question->kp_code; $question->save(); } foreach ($matches as $match) { QuestionKpRelation::updateOrCreate([ 'question_id' => $question->id, 'kp_code' => $match['kp_code'] ?? '', ], [ 'weight' => $match['weight'] ?? 1, ]); } } }