MarkdownImportResource.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\MarkdownImportResource\Pages;
  4. use App\Models\MarkdownImport;
  5. use BackedEnum;
  6. use Filament\Actions\BulkActionGroup;
  7. use Filament\Actions\DeleteBulkAction;
  8. use Filament\Actions\EditAction;
  9. use Filament\Actions\Action;
  10. use Filament\Facades\Filament;
  11. use Filament\Notifications\Notification;
  12. use Filament\Forms\Components\FileUpload;
  13. use Filament\Forms\Components\Hidden;
  14. use Filament\Forms\Components\MarkdownEditor;
  15. use Filament\Schemas\Components\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Components\Toggle;
  18. use Filament\Forms\Components\TextInput;
  19. use Filament\Schemas\Components\Utilities\Get;
  20. use Filament\Schemas\Components\Utilities\Set;
  21. use Filament\Resources\Resource;
  22. use Filament\Schemas\Schema;
  23. use Filament\Tables;
  24. use Filament\Tables\Table;
  25. use Illuminate\Database\Eloquent\Builder;
  26. use Illuminate\Database\Eloquent\Model;
  27. use Illuminate\Support\Facades\Storage;
  28. use UnitEnum;
  29. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  30. use App\Support\TextEncoding;
  31. use App\Rules\MarkdownFileExtension;
  32. use Filament\Tables\Columns\TextColumn;
  33. use Filament\Tables\Enums\FiltersLayout;
  34. class MarkdownImportResource extends Resource
  35. {
  36. protected static ?string $model = MarkdownImport::class;
  37. protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-arrow-up-tray';
  38. protected static ?string $navigationLabel = 'Markdown 导入';
  39. protected static ?string $modelLabel = 'Markdown 导入';
  40. protected static ?string $pluralModelLabel = 'Markdown 导入';
  41. protected static UnitEnum|string|null $navigationGroup = '卷子导入流程';
  42. protected static ?int $navigationSort = 1;
  43. protected static ?string $title = 'Markdown 试卷导入管理';
  44. protected static ?string $description = '导入 Markdown 格式的数学试卷,AI 智能识别题目,人工校对后入库';
  45. public static function mutateFormDataBeforeCreate(array $data): array
  46. {
  47. // 支持上传 markdown 文件:读取内容写入 original_markdown
  48. if (!empty($data['markdown_file']) && empty($data['original_markdown'])) {
  49. $path = $data['markdown_file'];
  50. if (is_string($path) && Storage::disk('local')->exists($path)) {
  51. $data['original_markdown'] = TextEncoding::toUtf8(Storage::disk('local')->get($path));
  52. }
  53. }
  54. // 文件名默认取上传文件名(优先原始文件名,其次取存储路径 basename)
  55. if (empty($data['file_name']) && !empty($data['markdown_file'])) {
  56. $storedNames = $data['uploaded_file_names'] ?? null;
  57. if (is_array($storedNames) && !empty($storedNames)) {
  58. $data['file_name'] = (string) array_values($storedNames)[0];
  59. } else {
  60. $path = is_array($data['markdown_file']) ? ($data['markdown_file'][0] ?? '') : (string) $data['markdown_file'];
  61. $data['file_name'] = $path !== '' ? basename($path) : null;
  62. }
  63. }
  64. // 文件名作为来源名称
  65. if (!empty($data['file_name'])) {
  66. $data['source_name'] = $data['file_name'];
  67. $data['source_type'] = 'other';
  68. }
  69. unset($data['markdown_file']);
  70. unset($data['uploaded_file_names']);
  71. return $data;
  72. }
  73. /**
  74. * 允许创建新的 Markdown 导入记录
  75. */
  76. public static function canCreate(): bool
  77. {
  78. return true;
  79. }
  80. public static function form(Schema $schema): Schema
  81. {
  82. return $schema
  83. ->schema([
  84. Section::make('上传与来源信息')
  85. ->schema([
  86. \Filament\Forms\Components\TextInput::make('file_name')
  87. ->label('文件名(来源名称)')
  88. ->required(fn (Get $get): bool => empty($get('markdown_file')))
  89. ->maxLength(255),
  90. FileUpload::make('markdown_file')
  91. ->label('Markdown 文件(可选)')
  92. ->disk('local')
  93. ->directory('imports/markdown')
  94. ->helperText('仅支持 .md / .markdown / .txt;上传后会自动读取内容并填充编辑器')
  95. ->maxSize(10 * 1024)
  96. ->storeFileNamesIn('uploaded_file_names')
  97. ->dehydrated(true)
  98. ->preserveFilenames()
  99. ->rules([new MarkdownFileExtension()])
  100. ->afterStateUpdated(function ($state, Set $set, Get $get): void {
  101. // 在提交表单前,FileUpload 的 state 可能还是 TemporaryUploadedFile(尚未保存到 disk)
  102. $first = is_array($state) ? ($state[0] ?? null) : $state;
  103. if ($first instanceof TemporaryUploadedFile) {
  104. $set('original_markdown', TextEncoding::toUtf8((string) @file_get_contents($first->getRealPath())));
  105. if (empty($get('file_name'))) {
  106. $set('file_name', $first->getClientOriginalName());
  107. }
  108. return;
  109. }
  110. $paths = is_array($state) ? $state : (empty($state) ? [] : [$state]);
  111. $path = (string) ($paths[0] ?? '');
  112. if ($path === '') {
  113. return;
  114. }
  115. // 已保存到 disk 后:读取文件内容填充编辑器
  116. if (Storage::disk('local')->exists($path)) {
  117. $set('original_markdown', TextEncoding::toUtf8(Storage::disk('local')->get($path)));
  118. }
  119. // 上传后的真实文件名:BaseFileUpload 会在保存时 storeFileName($storedFile, originalName)
  120. $storedNames = $get('uploaded_file_names');
  121. if (is_string($storedNames) && $storedNames !== '') {
  122. $set('file_name', $storedNames);
  123. } elseif (empty($get('file_name'))) {
  124. $set('file_name', basename($path));
  125. }
  126. }),
  127. Hidden::make('uploaded_file_names')
  128. ->dehydrated(true),
  129. ])
  130. ->columns(2),
  131. Section::make('解析规则(可选)')
  132. ->schema([
  133. Select::make('parse_mode')
  134. ->label('解析模式')
  135. ->options([
  136. 'strict' => '严格模式',
  137. 'relaxed' => '宽松模式',
  138. ])
  139. ->default('strict')
  140. ->dehydrated(false),
  141. TextInput::make('split_marker')
  142. ->label('分题符号')
  143. ->placeholder('如:---')
  144. ->dehydrated(false),
  145. TextInput::make('type_marker')
  146. ->label('题型标记')
  147. ->placeholder('如:#选择题')
  148. ->dehydrated(false),
  149. Toggle::make('auto_detect_images')
  150. ->label('自动识别图片')
  151. ->default(true)
  152. ->dehydrated(false),
  153. ])
  154. ->columns(2)
  155. ->collapsed(),
  156. Section::make('Markdown 内容')
  157. ->schema([
  158. MarkdownEditor::make('original_markdown')
  159. ->label('Markdown 内容(编辑器)')
  160. ->required(fn (Get $get): bool => empty($get('markdown_file')))
  161. ->columnSpanFull()
  162. // 固定编辑器高度,避免内容过长把页面撑开
  163. ->minHeight('45vh')
  164. ->maxHeight('45vh')
  165. ->toolbarButtons([
  166. 'bold',
  167. 'italic',
  168. 'strike',
  169. 'blockquote',
  170. 'bulletList',
  171. 'orderedList',
  172. 'link',
  173. 'codeBlock',
  174. 'table',
  175. 'undo',
  176. 'redo',
  177. ]),
  178. ]),
  179. ]);
  180. }
  181. public static function table(Table $table): Table
  182. {
  183. return $table
  184. ->columns([
  185. TextColumn::make('file_name')
  186. ->label('文件名')
  187. ->searchable()
  188. ->sortable(),
  189. TextColumn::make('source_name')
  190. ->label('来源')
  191. ->toggleable(isToggledHiddenByDefault: true),
  192. TextColumn::make('status')
  193. ->label('状态')
  194. ->badge()
  195. ->color(fn (string $state): string => match ($state) {
  196. 'pending' => 'gray',
  197. 'processing' => 'warning',
  198. 'parsed' => 'info',
  199. 'reviewed' => 'primary',
  200. 'completed' => 'success',
  201. 'failed' => 'danger',
  202. default => 'gray',
  203. })
  204. ->getStateUsing(function (?Model $record): string {
  205. if (!$record) {
  206. return '—';
  207. }
  208. return match ($record->status) {
  209. 'pending' => '待处理',
  210. 'processing' => $record->progress_label ?: '处理中',
  211. 'parsed' => '已解析(待校对)',
  212. 'reviewed' => '已校对(待入库)',
  213. 'completed' => '已完成(已入库)',
  214. 'failed' => '失败' . ($record->progress_message ? "({$record->progress_message})" : ''),
  215. default => (string) $record->status,
  216. };
  217. }),
  218. TextColumn::make('progress_message')
  219. ->label('当前步骤')
  220. ->getStateUsing(fn (?Model $record) => $record?->progress_message ?: '—')
  221. ->wrap()
  222. ->limit(60),
  223. TextColumn::make('progress_label')
  224. ->label('进度')
  225. ->getStateUsing(fn (?Model $record) => $record?->progress_label ?: '—')
  226. ->color('gray'),
  227. TextColumn::make('parsed_count')
  228. ->label('候选题数')
  229. ->getStateUsing(fn (?Model $record) => $record?->parsed_count ?? 0)
  230. ->sortable(),
  231. TextColumn::make('accepted_count')
  232. ->label('已接受')
  233. ->getStateUsing(fn (?Model $record) => $record?->accepted_count ?? 0)
  234. ->sortable(),
  235. TextColumn::make('created_at')
  236. ->label('导入时间')
  237. ->dateTime()
  238. ->sortable(),
  239. TextColumn::make('processing_started_at')
  240. ->label('开始')
  241. ->dateTime('m-d H:i')
  242. ->toggleable(isToggledHiddenByDefault: true),
  243. TextColumn::make('processing_finished_at')
  244. ->label('结束')
  245. ->dateTime('m-d H:i')
  246. ->toggleable(isToggledHiddenByDefault: true),
  247. TextColumn::make('error_message')
  248. ->label('错误')
  249. ->visible(fn (?Model $record): bool => $record?->status === 'failed')
  250. ->wrap()
  251. ->limit(80),
  252. ])
  253. ->filters([
  254. Tables\Filters\SelectFilter::make('status')
  255. ->label('状态')
  256. ->options([
  257. 'pending' => '待处理',
  258. 'processing' => '处理中',
  259. 'parsed' => '已解析',
  260. 'reviewed' => '已校对',
  261. 'completed' => '已完成',
  262. 'failed' => '处理失败',
  263. ]),
  264. Tables\Filters\SelectFilter::make('source_type')
  265. ->label('来源类型')
  266. ->options([
  267. 'textbook' => '教材',
  268. 'exam' => '考试',
  269. 'other' => '其他',
  270. ]),
  271. ], layout: FiltersLayout::AboveContentCollapsible)
  272. ->actions([
  273. EditAction::make()
  274. ->label('编辑'),
  275. Action::make('run_pipeline')
  276. ->label('触发全流程')
  277. ->icon('heroicon-o-play-circle')
  278. ->color('success')
  279. ->requiresConfirmation()
  280. ->modalHeading('触发 Markdown 拆分 + AI 结构化')
  281. ->modalDescription('立即提交队列,按 source_file → source_paper → paper_part → candidate → AI 结构化 执行。')
  282. ->action(function (?Model $record) {
  283. if (!$record) {
  284. return;
  285. }
  286. dispatch(new \App\Jobs\ProcessMarkdownSplit($record->id));
  287. $record->update([
  288. 'status' => MarkdownImport::STATUS_PROCESSING,
  289. 'progress_stage' => MarkdownImport::STAGE_QUEUED,
  290. 'progress_message' => '已进入队列…',
  291. 'processing_started_at' => now(),
  292. 'processing_finished_at' => null,
  293. 'error_message' => null,
  294. ]);
  295. Notification::make()
  296. ->title('已提交解析队列')
  297. ->success()
  298. ->send();
  299. }),
  300. Action::make('parse')
  301. ->label('解析 Markdown')
  302. ->icon('heroicon-o-cog-6-tooth')
  303. ->color('info')
  304. ->visible(fn (?Model $record): bool => in_array($record?->status, ['pending', 'failed']))
  305. ->requiresConfirmation()
  306. ->modalHeading('解析 Markdown')
  307. ->modalDescription('将解析 Markdown 中的题目候选,并使用 AI 进行初步筛选。')
  308. ->action(function (?Model $record) {
  309. if ($record) {
  310. static::parseMarkdown($record);
  311. }
  312. }),
  313. Action::make('review')
  314. ->label('进入校对')
  315. ->icon('heroicon-o-clipboard-document-list')
  316. ->color('success')
  317. ->visible(fn (?Model $record): bool => in_array($record?->status, ['parsed', 'reviewed', 'completed']))
  318. ->url(function (?Model $record): string {
  319. // 根据状态跳转到不同页面
  320. $importId = $record?->id;
  321. $status = $record?->status;
  322. // 兼容 PHP 7.4 的写法
  323. if ($status === 'parsed') {
  324. return route('filament.admin.resources.pre-question-candidates.index', [
  325. 'import_id' => $importId
  326. ]);
  327. } elseif (in_array($status, ['reviewed', 'completed'])) {
  328. return route('filament.admin.resources.pre-question-candidates.index', [
  329. 'import_id' => $importId,
  330. 'tab' => 'reviewed' // 显示已校对标签页
  331. ]);
  332. }
  333. return route('filament.admin.resources.pre-question-candidates.index', [
  334. 'import_id' => $importId
  335. ]);
  336. }),
  337. Action::make('delete')
  338. ->label('删除')
  339. ->icon('heroicon-o-trash')
  340. ->color('danger')
  341. ->requiresConfirmation()
  342. ->modalHeading('删除导入记录')
  343. ->modalDescription('确定要删除这条导入记录吗?此操作不可撤销。')
  344. ->action(function (?Model $record) {
  345. if ($record) {
  346. $record->delete();
  347. Notification::make()
  348. ->title('删除成功')
  349. ->success()
  350. ->send();
  351. }
  352. }),
  353. ])
  354. ->bulkActions([
  355. BulkActionGroup::make([
  356. DeleteBulkAction::make(),
  357. ]),
  358. ])
  359. ->recordClasses(fn (Model $record) => $record->status === 'failed' ? 'bg-rose-50/60' : null)
  360. ->defaultSort('created_at', 'desc')
  361. ->paginated([10, 25, 50, 100]);
  362. }
  363. public static function getEloquentQuery(): Builder
  364. {
  365. // 让 parsed_count / accepted_count 成为可排序的 SQL 字段(避免 order by accessor 报错)
  366. return parent::getEloquentQuery()
  367. ->withCount([
  368. 'candidates as parsed_count',
  369. 'candidates as accepted_count' => fn (Builder $query) => $query->where('is_question_candidate', true),
  370. ]);
  371. }
  372. public static function getPages(): array
  373. {
  374. return [
  375. 'index' => Pages\ListMarkdownImports::route('/'),
  376. 'create' => Pages\CreateMarkdownImport::route('/create'),
  377. 'edit' => Pages\EditMarkdownImport::route('/{record}/edit'),
  378. ];
  379. }
  380. /**
  381. * 解析 Markdown
  382. */
  383. public static function parseMarkdown(Model $record): void
  384. {
  385. try {
  386. // 验证状态
  387. if (!in_array($record->status, ['pending', 'failed'], true)) {
  388. Notification::make()
  389. ->title('只能解析待处理或失败状态的记录')
  390. ->warning()
  391. ->send();
  392. return;
  393. }
  394. // 验证 markdown 内容
  395. if (empty($record->original_markdown)) {
  396. Notification::make()
  397. ->title('Markdown 内容不能为空')
  398. ->warning()
  399. ->send();
  400. return;
  401. }
  402. // 失败状态重试:清空错误信息并重新进入待处理
  403. if ($record->status === 'failed') {
  404. $record->update([
  405. 'status' => 'pending',
  406. 'error_message' => null,
  407. ]);
  408. }
  409. // 先更新状态,确保列表页可见变化(避免“点了没反应”的体验)
  410. $record->update([
  411. 'status' => 'processing',
  412. 'progress_stage' => \App\Models\MarkdownImport::STAGE_QUEUED,
  413. 'progress_message' => '已提交解析任务,等待处理…',
  414. 'progress_current' => 0,
  415. 'progress_total' => 0,
  416. 'progress_updated_at' => now(),
  417. 'processing_started_at' => now(),
  418. 'processing_finished_at' => null,
  419. 'error_message' => null,
  420. ]);
  421. \Log::info('Markdown import parse queued', [
  422. 'import_id' => $record->id,
  423. 'status' => $record->status,
  424. 'stage' => $record->progress_stage,
  425. ]);
  426. // 派发异步任务
  427. \App\Jobs\ProcessMarkdownSplit::dispatch($record->id);
  428. Notification::make()
  429. ->title('已提交解析任务,正在后台处理...')
  430. ->body('列表页将自动刷新显示进度;若长期无进度,请确认 queue worker 正在运行。')
  431. ->success()
  432. ->send();
  433. } catch (\Exception $e) {
  434. Notification::make()
  435. ->title('解析失败:' . $e->getMessage())
  436. ->danger()
  437. ->send();
  438. }
  439. }
  440. }