ListPreQuestionCandidates.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Filament\Resources\PreQuestionCandidateResource\Pages;
  3. use App\Filament\Resources\PreQuestionCandidateResource;
  4. use App\Models\PreQuestionCandidate;
  5. use Filament\Resources\Pages\ListRecords;
  6. use Illuminate\Database\Eloquent\Builder;
  7. class ListPreQuestionCandidates extends ListRecords
  8. {
  9. protected static string $resource = PreQuestionCandidateResource::class;
  10. protected string $view = 'filament.resources.pre-question-candidate-resource.pages.list-pre-question-candidates';
  11. public array $paperTree = [];
  12. public array $summaryStats = [];
  13. protected function canCreate(): bool
  14. {
  15. return false;
  16. }
  17. protected function getTableQuery(): Builder
  18. {
  19. $query = PreQuestionCandidate::query();
  20. $importId = request()->input('import_id');
  21. $user = auth()->user();
  22. $isAdmin = $user && in_array($user->role, ['super_admin', 'admin'], true);
  23. if (!empty($importId)) {
  24. $query->where('import_id', (int) $importId);
  25. } elseif (!$isAdmin) {
  26. // 非管理员必须通过 import_id 进入,否则不展示任何数据
  27. $query->whereRaw('1=0');
  28. }
  29. // 默认隐藏被新解析覆盖的记录(可通过筛选器查看)
  30. if (!request()->has('tableFilters.status.value')) {
  31. $query->where('status', '!=', PreQuestionCandidate::STATUS_SUPERSEDED);
  32. }
  33. return $query;
  34. }
  35. public function mount(): void
  36. {
  37. parent::mount();
  38. $importId = request()->input('import_id');
  39. $query = PreQuestionCandidate::query()
  40. ->with(['sourcePaper', 'part'])
  41. ->when($importId, fn ($q) => $q->where('import_id', (int) $importId));
  42. $records = $query->get();
  43. $this->summaryStats = [
  44. 'total' => $records->count(),
  45. 'accepted' => $records->where('status', PreQuestionCandidate::STATUS_ACCEPTED)->count(),
  46. 'reviewed' => $records->where('status', PreQuestionCandidate::STATUS_REVIEWED)->count(),
  47. 'pending' => $records->where('status', PreQuestionCandidate::STATUS_PENDING)->count(),
  48. 'rejected' => $records->where('status', PreQuestionCandidate::STATUS_REJECTED)->count(),
  49. ];
  50. $tree = [];
  51. foreach ($records as $record) {
  52. $paperId = $record->sourcePaper?->id ?? 0;
  53. $partId = $record->part?->id ?? 0;
  54. $paperTitle = $record->sourcePaper?->title ?? '未命名卷子';
  55. $partTitle = $record->part?->title ?? '未标注区块';
  56. $tree[$paperId]['title'] = $paperTitle;
  57. $tree[$paperId]['parts'][$partId]['title'] = $partTitle;
  58. $tree[$paperId]['parts'][$partId]['count'] = ($tree[$paperId]['parts'][$partId]['count'] ?? 0) + 1;
  59. }
  60. $this->paperTree = array_values(array_map(function ($paper) {
  61. $parts = $paper['parts'] ?? [];
  62. $paper['parts'] = array_values($parts);
  63. return $paper;
  64. }, $tree));
  65. }
  66. }