ViewPaperPart.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Filament\Resources\PaperPartResource\Pages;
  3. use App\Filament\Resources\PaperPartResource;
  4. use App\Models\PaperQuestionRef;
  5. use App\Models\PreQuestionCandidate;
  6. use App\Services\QuestionExtractorService;
  7. use Filament\Actions\Action;
  8. use Filament\Notifications\Notification;
  9. use Filament\Resources\Pages\ViewRecord;
  10. use Illuminate\Support\Facades\DB;
  11. class ViewPaperPart extends ViewRecord
  12. {
  13. protected static string $resource = PaperPartResource::class;
  14. protected function getHeaderActions(): array
  15. {
  16. return [
  17. Action::make('rebuild_candidates')
  18. ->label('重新拆题')
  19. ->color('warning')
  20. ->requiresConfirmation()
  21. ->action(function (): void {
  22. $part = $this->record;
  23. $raw = trim((string) ($part->raw_markdown ?? ''));
  24. if ($raw === '') {
  25. Notification::make()
  26. ->title('无法拆题')
  27. ->body('该区块没有原始 Markdown 内容')
  28. ->danger()
  29. ->send();
  30. return;
  31. }
  32. DB::transaction(function () use ($part): void {
  33. PaperQuestionRef::where('part_id', $part->id)->delete();
  34. PreQuestionCandidate::where('part_id', $part->id)->delete();
  35. $sequence = 1;
  36. app(QuestionExtractorService::class)->extractAndPersist($part, null, $sequence);
  37. });
  38. $part->refresh();
  39. Notification::make()
  40. ->title('拆题完成')
  41. ->body('已根据最新规则重新生成题目候选')
  42. ->success()
  43. ->send();
  44. }),
  45. ];
  46. }
  47. }