PreQuestionCandidateResource.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\PreQuestionCandidateResource\Pages;
  4. use App\Models\PreQuestionCandidate;
  5. use BackedEnum;
  6. use Filament\Actions\Action;
  7. use Filament\Actions\BulkActionGroup;
  8. use Filament\Forms\Components\TagsInput;
  9. use Filament\Forms\Components\Textarea;
  10. use Filament\Forms\Components\TextInput;
  11. use Filament\Forms\Components\Toggle;
  12. use Filament\Resources\Resource;
  13. use Filament\Schemas\Components\Section;
  14. use Filament\Tables;
  15. use Filament\Tables\Columns\TextColumn;
  16. use Filament\Tables\Filters\TernaryFilter;
  17. use Illuminate\Database\Eloquent\Model;
  18. use UnitEnum;
  19. class PreQuestionCandidateResource extends Resource
  20. {
  21. protected static ?string $model = PreQuestionCandidate::class;
  22. protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-check-circle';
  23. protected static ?string $navigationLabel = '题目校对';
  24. protected static ?string $modelLabel = '题目候选';
  25. protected static ?string $pluralModelLabel = '题目候选';
  26. protected static UnitEnum|string|null $navigationGroup = '题库管理';
  27. protected static ?int $navigationSort = 2;
  28. protected static ?string $title = '候选题目校对';
  29. protected static ?string $description = '从 Markdown 导入解析出的候选题目,支持 AI 辅助校对和批量操作';
  30. public static function canViewAny(): bool
  31. {
  32. // 允许访问的条件:
  33. // 1. URL 中有 import_id 参数(从 MarkdownImport 跳转进入)
  34. $hasImportId = request()->has('import_id')
  35. && !empty(request()->input('import_id'));
  36. // 2. 或者是管理员角色(直接检查 role 字段)
  37. $user = auth()->user();
  38. $isAdmin = $user && in_array($user->role, ['super_admin', 'admin']);
  39. return $hasImportId || $isAdmin;
  40. }
  41. public static function canCreate(): bool
  42. {
  43. // 不允许手动创建候选题目,只能从 Markdown 解析生成
  44. return false;
  45. }
  46. public static function canEdit(Model $record): bool
  47. {
  48. // 允许在“人工校对”阶段对候选题进行编辑(题干/选项/标记)
  49. return true;
  50. }
  51. public static function table(Tables\Table $table): Tables\Table
  52. {
  53. return $table
  54. ->columns([
  55. TextColumn::make('sequence')
  56. ->label('序')
  57. ->sortable()
  58. ->width('60px'),
  59. TextColumn::make('index')
  60. ->label('题号')
  61. ->sortable()
  62. ->width('70px'),
  63. TextColumn::make('question_number')
  64. ->label('原题号')
  65. ->sortable()
  66. ->toggleable()
  67. ->width('80px'),
  68. TextColumn::make('part.title')
  69. ->label('区块')
  70. ->toggleable()
  71. ->limit(16),
  72. TextColumn::make('sourcePaper.title')
  73. ->label('卷子')
  74. ->toggleable()
  75. ->limit(16),
  76. TextColumn::make('raw_markdown')
  77. ->label('题目预览')
  78. ->wrap()
  79. ->limit(140)
  80. ->toggleable(),
  81. Tables\Columns\ImageColumn::make('first_image')
  82. ->label('图片')
  83. ->height(60)
  84. ->width(60)
  85. ->circular(),
  86. TextColumn::make('ai_confidence')
  87. ->label('AI 置信度')
  88. ->badge()
  89. ->color(fn (Model $record): string => $record->confidence_badge)
  90. ->formatStateUsing(function (?float $state, Model $record): string {
  91. $val = $state ?? $record->confidence;
  92. return $val !== null ? number_format((float)$val * 100, 1) . '%' : 'N/A';
  93. }),
  94. TextColumn::make('structured_json')
  95. ->label('结构化')
  96. ->getStateUsing(fn (?Model $record) => $record?->structured_json ? '已生成' : '未生成')
  97. ->badge()
  98. ->color(fn (?Model $record) => $record?->structured_json ? 'success' : 'gray')
  99. ->toggleable(isToggledHiddenByDefault: true),
  100. Tables\Columns\ToggleColumn::make('is_question_candidate')
  101. ->label('是题目'),
  102. TextColumn::make('status')
  103. ->label('状态')
  104. ->badge()
  105. ->color(fn (Model $record): string => $record->status_badge),
  106. ])
  107. ->filters([
  108. Tables\Filters\SelectFilter::make('import_id')
  109. ->label('导入记录')
  110. ->options(function () {
  111. return \App\Models\MarkdownImport::query()
  112. ->pluck('file_name', 'id')
  113. ->toArray();
  114. })
  115. ->query(function ($query, $data) {
  116. if (!empty($data['value'])) {
  117. $query->where('import_id', $data['value']);
  118. }
  119. }),
  120. TernaryFilter::make('is_question_candidate')
  121. ->label('是否为题目'),
  122. Tables\Filters\SelectFilter::make('status')
  123. ->label('审核状态')
  124. ->options([
  125. 'ai_pending' => 'AI 解析中',
  126. 'pending' => '待审核',
  127. 'reviewed' => '已审核',
  128. 'accepted' => '已接受',
  129. 'rejected' => '已拒绝',
  130. 'superseded' => '已被新解析覆盖',
  131. ]),
  132. ])
  133. ->actions([
  134. Action::make('review_edit')
  135. ->label('校对/编辑')
  136. ->icon('heroicon-o-pencil-square')
  137. ->color('primary')
  138. ->modalHeading(fn (Model $record): string => "校对候选题 #{$record->index}")
  139. ->form([
  140. Section::make('审核标记')
  141. ->schema([
  142. Toggle::make('is_question_candidate')
  143. ->label('是题目')
  144. ->default(fn (Model $record) => (bool) $record->is_question_candidate),
  145. TextInput::make('ai_confidence')
  146. ->label('AI 置信度')
  147. ->disabled(),
  148. ])->columns(2),
  149. Section::make('原始 Markdown(可编辑)')
  150. ->schema([
  151. Textarea::make('raw_markdown')
  152. ->label('raw_markdown')
  153. ->rows(10)
  154. ->required(),
  155. ])->columnSpanFull(),
  156. Section::make('结构化字段(可编辑)')
  157. ->schema([
  158. Textarea::make('stem')
  159. ->label('题干(stem)')
  160. ->rows(6),
  161. Textarea::make('options')
  162. ->label('选项(JSON)')
  163. ->rows(6)
  164. ->helperText('示例:{"A":"...","B":"..."};没有选项填空留空'),
  165. TagsInput::make('images')
  166. ->label('图片 URLs')
  167. ->placeholder('https://...'),
  168. Textarea::make('tables')
  169. ->label('表格(JSON 数组或 HTML)')
  170. ->rows(6)
  171. ->helperText('支持填写 JSON 数组(推荐)或直接粘贴 <table>...</table>'),
  172. ])->columns(2),
  173. ])
  174. ->fillForm(function (Model $record): array {
  175. return [
  176. 'is_question_candidate' => (bool) $record->is_question_candidate,
  177. 'ai_confidence' => $record->ai_confidence ? number_format($record->ai_confidence * 100, 1) : null,
  178. 'raw_markdown' => (string) $record->raw_markdown,
  179. 'stem' => $record->stem,
  180. 'options' => $record->options ? json_encode($record->options, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : null,
  181. 'images' => $record->images ?? [],
  182. 'tables' => $record->tables ? json_encode($record->tables, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : null,
  183. ];
  184. })
  185. ->action(function (array $data, Model $record): void {
  186. $options = null;
  187. if (!empty($data['options'])) {
  188. $decoded = json_decode((string) $data['options'], true);
  189. if (json_last_error() === JSON_ERROR_NONE) {
  190. $options = $decoded;
  191. }
  192. }
  193. $tables = [];
  194. if (!empty($data['tables'])) {
  195. $decoded = json_decode((string) $data['tables'], true);
  196. if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
  197. $tables = $decoded;
  198. } else {
  199. $tables = [(string) $data['tables']];
  200. }
  201. }
  202. $record->update([
  203. 'raw_markdown' => (string) $data['raw_markdown'],
  204. 'stem' => $data['stem'] ?? null,
  205. 'options' => $options,
  206. 'images' => $data['images'] ?? [],
  207. 'tables' => $tables,
  208. 'is_question_candidate' => (bool) ($data['is_question_candidate'] ?? false),
  209. 'status' => 'reviewed',
  210. ]);
  211. }),
  212. ])
  213. ->bulkActions([
  214. BulkActionGroup::make([
  215. \App\Filament\Resources\PreQuestionCandidateResource\Actions\MarkAsQuestionsBulkAction::make()
  216. ->label('标记为题目'),
  217. \App\Filament\Resources\PreQuestionCandidateResource\Actions\MarkAsNonQuestionsBulkAction::make()
  218. ->label('标记为非题目'),
  219. \App\Filament\Resources\PreQuestionCandidateResource\Actions\ConvertToPreQuestionsBulkAction::make()
  220. ->label('入库到筛选库'),
  221. ]),
  222. ])
  223. ->defaultSort('sequence', 'asc')
  224. ->paginated([20, 50, 100])
  225. ->poll('10s');
  226. }
  227. public static function getPages(): array
  228. {
  229. return [
  230. 'index' => Pages\ListPreQuestionCandidates::route('/'),
  231. ];
  232. }
  233. }