|
@@ -7,7 +7,9 @@ use App\Models\KnowledgePoint;
|
|
|
use App\Models\MistakeRecord;
|
|
use App\Models\MistakeRecord;
|
|
|
use App\Models\PaperQuestion;
|
|
use App\Models\PaperQuestion;
|
|
|
use App\Models\Question;
|
|
use App\Models\Question;
|
|
|
|
|
+use Illuminate\Database\QueryException;
|
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class KnowledgeExplanationService
|
|
class KnowledgeExplanationService
|
|
|
{
|
|
{
|
|
@@ -77,9 +79,7 @@ class KnowledgeExplanationService
|
|
|
'case_payload' => $casePayload,
|
|
'case_payload' => $casePayload,
|
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
|
|
|
|
|
|
|
- $record = KnowledgeExplanation::updateOrCreate([
|
|
|
|
|
- 'knowledge_id' => $knowledgeId,
|
|
|
|
|
- ], [
|
|
|
|
|
|
|
+ $recordPayload = [
|
|
|
'teacher_id' => $teacherId,
|
|
'teacher_id' => $teacherId,
|
|
|
'student_id' => $studentId,
|
|
'student_id' => $studentId,
|
|
|
'assemble_type' => 22,
|
|
'assemble_type' => 22,
|
|
@@ -89,7 +89,22 @@ class KnowledgeExplanationService
|
|
|
'content_hash' => $contentHash,
|
|
'content_hash' => $contentHash,
|
|
|
'pdf_url' => null,
|
|
'pdf_url' => null,
|
|
|
'generated_at' => null,
|
|
'generated_at' => null,
|
|
|
- ]);
|
|
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $record = KnowledgeExplanation::updateOrCreate([
|
|
|
|
|
+ 'knowledge_id' => $knowledgeId,
|
|
|
|
|
+ ], $recordPayload);
|
|
|
|
|
+ } catch (QueryException $e) {
|
|
|
|
|
+ if (! $this->isDuplicatePrimaryKeyError($e)) {
|
|
|
|
|
+ throw $e;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 兼容线上历史表主键异常(id 非正常自增):
|
|
|
|
|
+ // 1) 若 knowledge_id 已存在则直接更新;
|
|
|
|
|
+ // 2) 否则手动分配一个递增 id 再插入,避免任务失败。
|
|
|
|
|
+ $record = $this->persistWithManualIdFallback($knowledgeId, $recordPayload);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return [
|
|
return [
|
|
|
'knowledge_id' => $knowledgeId,
|
|
'knowledge_id' => $knowledgeId,
|
|
@@ -232,6 +247,45 @@ class KnowledgeExplanationService
|
|
|
return array_keys($codes);
|
|
return array_keys($codes);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private function isDuplicatePrimaryKeyError(QueryException $e): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ $message = (string) $e->getMessage();
|
|
|
|
|
+
|
|
|
|
|
+ return str_contains($message, 'Integrity constraint violation: 1062')
|
|
|
|
|
+ && str_contains($message, 'knowledge_explanations.PRIMARY');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function persistWithManualIdFallback(string $knowledgeId, array $recordPayload): KnowledgeExplanation
|
|
|
|
|
+ {
|
|
|
|
|
+ $existing = KnowledgeExplanation::query()
|
|
|
|
|
+ ->where('knowledge_id', $knowledgeId)
|
|
|
|
|
+ ->first();
|
|
|
|
|
+ if ($existing) {
|
|
|
|
|
+ $existing->fill($recordPayload);
|
|
|
|
|
+ $existing->save();
|
|
|
|
|
+
|
|
|
|
|
+ return $existing;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return DB::transaction(function () use ($knowledgeId, $recordPayload): KnowledgeExplanation {
|
|
|
|
|
+ $table = (new KnowledgeExplanation())->getTable();
|
|
|
|
|
+ $maxId = (int) DB::table($table)->lockForUpdate()->max('id');
|
|
|
|
|
+ $nextId = $maxId + 1;
|
|
|
|
|
+ $now = now();
|
|
|
|
|
+
|
|
|
|
|
+ DB::table($table)->insert(array_merge($recordPayload, [
|
|
|
|
|
+ 'id' => $nextId,
|
|
|
|
|
+ 'knowledge_id' => $knowledgeId,
|
|
|
|
|
+ 'created_at' => $now,
|
|
|
|
|
+ 'updated_at' => $now,
|
|
|
|
|
+ ]));
|
|
|
|
|
+
|
|
|
|
|
+ return KnowledgeExplanation::query()
|
|
|
|
|
+ ->where('knowledge_id', $knowledgeId)
|
|
|
|
|
+ ->firstOrFail();
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private function loadStudentQuestionHistory(string $studentId): array
|
|
private function loadStudentQuestionHistory(string $studentId): array
|
|
|
{
|
|
{
|
|
|
$done = PaperQuestion::query()
|
|
$done = PaperQuestion::query()
|