QuestionReviewWorkbench.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Models\PreQuestionCandidate;
  4. use App\Services\QuestionReviewService;
  5. use App\Services\QuestionGenerationService;
  6. use App\Services\AiKnowledgeService;
  7. use App\Services\AiSolutionService;
  8. use Filament\Pages\Page;
  9. use UnitEnum;
  10. class QuestionReviewWorkbench extends Page
  11. {
  12. protected static bool $shouldRegisterNavigation = true;
  13. protected static ?string $navigationLabel = '题目审核工作台';
  14. protected static UnitEnum|string|null $navigationGroup = '卷子导入流程';
  15. protected static ?int $navigationSort = 5;
  16. protected string $view = 'filament.pages.question-review-workbench';
  17. public ?int $selectedId = null;
  18. public array $selectedIds = [];
  19. public ?string $groupBy = 'paper';
  20. public function candidates()
  21. {
  22. return PreQuestionCandidate::query()
  23. ->with(['sourcePaper', 'part'])
  24. ->orderBy('sequence')
  25. ->limit(200)
  26. ->get();
  27. }
  28. public function groupedCandidates()
  29. {
  30. $candidates = $this->candidates();
  31. if ($this->groupBy === 'part') {
  32. return $candidates->groupBy(fn ($candidate) => $candidate->part?->title ?? '未分区块');
  33. }
  34. if ($this->groupBy === 'type') {
  35. return $candidates->groupBy(fn ($candidate) => $candidate->meta['question_type'] ?? '未标注题型');
  36. }
  37. return $candidates->groupBy(fn ($candidate) => $candidate->sourcePaper?->title ?? '未分卷');
  38. }
  39. public function stats(): array
  40. {
  41. $all = PreQuestionCandidate::query()->count();
  42. $pending = PreQuestionCandidate::query()->where('status', PreQuestionCandidate::STATUS_PENDING)->count();
  43. $reviewed = PreQuestionCandidate::query()->where('status', PreQuestionCandidate::STATUS_REVIEWED)->count();
  44. $accepted = PreQuestionCandidate::query()->where('status', PreQuestionCandidate::STATUS_ACCEPTED)->count();
  45. return compact('all', 'pending', 'reviewed', 'accepted');
  46. }
  47. public function selectCandidate(int $candidateId): void
  48. {
  49. $this->selectedId = $candidateId;
  50. }
  51. public function selectAllVisible(): void
  52. {
  53. $this->selectedIds = $this->candidates()->pluck('id')->toArray();
  54. }
  55. public function clearSelection(): void
  56. {
  57. $this->selectedIds = [];
  58. }
  59. public function approve(?int $candidateId = null, QuestionReviewService $service): void
  60. {
  61. if (!$candidateId) {
  62. return;
  63. }
  64. $service->promoteCandidateToQuestion($candidateId);
  65. }
  66. public function reject(?int $candidateId = null): void
  67. {
  68. if (!$candidateId) {
  69. return;
  70. }
  71. PreQuestionCandidate::where('id', $candidateId)->update([
  72. 'status' => PreQuestionCandidate::STATUS_REJECTED,
  73. ]);
  74. }
  75. public function bulkApprove(QuestionReviewService $service): void
  76. {
  77. foreach ($this->selectedIds as $candidateId) {
  78. $service->promoteCandidateToQuestion((int) $candidateId);
  79. }
  80. }
  81. public function bulkReject(): void
  82. {
  83. PreQuestionCandidate::query()
  84. ->whereIn('id', $this->selectedIds)
  85. ->update(['status' => PreQuestionCandidate::STATUS_REJECTED]);
  86. }
  87. public function aiAssist(): void
  88. {
  89. $candidate = $this->currentCandidate();
  90. if (!$candidate) {
  91. return;
  92. }
  93. $sourceText = $candidate->stem ?: (string) $candidate->raw_markdown;
  94. $result = app(QuestionGenerationService::class)->generateFromSource($sourceText);
  95. if (!($result['success'] ?? false)) {
  96. return;
  97. }
  98. $question = $result['question'] ?? [];
  99. $meta = $candidate->meta ?? [];
  100. $meta['question_type'] = $question['question_type'] ?? $meta['question_type'] ?? null;
  101. $meta['difficulty'] = $question['difficulty'] ?? $meta['difficulty'] ?? null;
  102. $meta['kp_codes'] = $question['knowledge_points'] ?? $meta['kp_codes'] ?? [];
  103. $meta['solution'] = $question['solution'] ?? $meta['solution'] ?? null;
  104. $meta['solution_steps'] = $question['solution_steps'] ?? $meta['solution_steps'] ?? [];
  105. $candidate->update([
  106. 'stem' => $question['stem'] ?? $candidate->stem,
  107. 'options' => $question['options'] ?? $candidate->options,
  108. 'meta' => $meta,
  109. ]);
  110. }
  111. public function aiMatchKp(): void
  112. {
  113. $candidate = $this->currentCandidate();
  114. if (!$candidate) {
  115. return;
  116. }
  117. $text = $candidate->stem ?: (string) $candidate->raw_markdown;
  118. $matches = app(AiKnowledgeService::class)->matchKnowledgePointsByAi($text);
  119. $kpCodes = array_values(array_filter(array_map(fn ($item) => $item['kp_code'] ?? null, $matches)));
  120. $meta = $candidate->meta ?? [];
  121. $meta['kp_codes'] = $kpCodes;
  122. $candidate->update(['meta' => $meta]);
  123. }
  124. public function aiGenerateSolution(): void
  125. {
  126. $candidate = $this->currentCandidate();
  127. if (!$candidate) {
  128. return;
  129. }
  130. $result = app(AiSolutionService::class)->generateSolution($candidate->stem ?? '');
  131. $meta = $candidate->meta ?? [];
  132. $meta['solution'] = $result['solution'] ?? '';
  133. $meta['solution_steps'] = $result['steps'] ?? [];
  134. $candidate->update(['meta' => $meta]);
  135. }
  136. public function currentCandidate(): ?PreQuestionCandidate
  137. {
  138. return $this->selectedId ? PreQuestionCandidate::query()->find($this->selectedId) : null;
  139. }
  140. public function jumpToNextIssue(): void
  141. {
  142. $candidates = $this->candidates();
  143. $currentIndex = $candidates->search(fn ($item) => $item->id === $this->selectedId);
  144. $find = function ($start) use ($candidates) {
  145. for ($i = $start; $i < $candidates->count(); $i++) {
  146. $candidate = $candidates[$i];
  147. $meta = $candidate->meta ?? [];
  148. if (empty($meta['difficulty']) || empty($meta['kp_codes']) || empty($meta['answer']) || empty($meta['solution'])) {
  149. return $candidate->id;
  150. }
  151. }
  152. return null;
  153. };
  154. $nextId = $find(($currentIndex !== false ? $currentIndex + 1 : 0)) ?? $find(0);
  155. if ($nextId) {
  156. $this->selectedId = $nextId;
  157. }
  158. }
  159. }