MarkdownImportWorkbench.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Models\MarkdownImport;
  4. use App\Models\SourcePaper;
  5. use App\Models\Textbook;
  6. use App\Models\TextbookCatalog;
  7. use Filament\Pages\Page;
  8. use Illuminate\Support\Arr;
  9. class MarkdownImportWorkbench extends Page
  10. {
  11. protected static ?string $navigationLabel = '导入工作台';
  12. protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
  13. protected static string|\UnitEnum|null $navigationGroup = '卷子导入流程';
  14. protected static ?int $navigationSort = 2;
  15. protected string $view = 'filament.pages.markdown-import-workbench';
  16. public ?int $importId = null;
  17. public ?int $selectedPaperId = null;
  18. public array $selectedIds = [];
  19. public string $search = '';
  20. public string $groupBy = 'bundle';
  21. public bool $dense = false;
  22. public bool $filenameValid = true;
  23. public array $filenameParsed = [];
  24. public ?string $filenameWarning = null;
  25. public array $form = [
  26. 'title' => null,
  27. 'edition' => null,
  28. 'grade' => null,
  29. 'term' => null,
  30. 'chapter' => null,
  31. 'source_type' => null,
  32. 'source_year' => null,
  33. 'textbook_id' => null,
  34. 'textbook_series' => null,
  35. 'source_name' => null,
  36. 'source_page' => null,
  37. 'tags' => '',
  38. 'bundle_key' => null,
  39. 'expected_count' => null,
  40. 'catalog_node_id' => null,
  41. ];
  42. public array $batch = [
  43. 'edition' => null,
  44. 'grade' => null,
  45. 'term' => null,
  46. 'chapter' => null,
  47. 'source_type' => null,
  48. 'source_year' => null,
  49. 'textbook_id' => null,
  50. 'textbook_series' => null,
  51. 'source_name' => null,
  52. 'source_page' => null,
  53. 'tags' => '',
  54. 'bundle_key' => null,
  55. 'expected_count' => null,
  56. 'catalog_node_id' => null,
  57. ];
  58. public function mount(): void
  59. {
  60. $this->importId = request()->integer('import_id');
  61. $parsed = $this->parseImportFilename();
  62. if (empty($parsed)) {
  63. $this->filenameValid = false;
  64. $this->filenameWarning = '文件名不符合规范:系列_年级_学期_学科_名称(例:北师大版_7_1_数学_...)';
  65. } else {
  66. $this->filenameParsed = $parsed;
  67. $this->applyFilenameDefaults();
  68. }
  69. $first = $this->papers()->first();
  70. if ($first) {
  71. $this->selectPaper($first->id);
  72. }
  73. }
  74. public function importRecord(): ?MarkdownImport
  75. {
  76. return $this->importId ? MarkdownImport::query()->find($this->importId) : null;
  77. }
  78. public function papers()
  79. {
  80. if (!$this->importId) {
  81. return collect();
  82. }
  83. $query = SourcePaper::query()
  84. ->whereHas('candidates', fn ($q) => $q->where('import_id', $this->importId))
  85. ->withCount(['candidates', 'parts'])
  86. ->orderBy('order');
  87. if ($this->search !== '') {
  88. $query->where(function ($q) {
  89. $q->where('title', 'like', '%' . $this->search . '%')
  90. ->orWhere('full_title', 'like', '%' . $this->search . '%')
  91. ->orWhere('paper_code', 'like', '%' . $this->search . '%');
  92. });
  93. }
  94. return $query->get();
  95. }
  96. public function groupedPapers(): array
  97. {
  98. $papers = $this->papers();
  99. if ($this->groupBy === 'paper') {
  100. return $papers->groupBy(fn ($paper) => $paper->title ?: $paper->full_title ?: '未命名卷子')->toArray();
  101. }
  102. if ($this->groupBy === 'grade') {
  103. return $papers->groupBy(fn ($paper) => $paper->grade ? $paper->grade . '年级' : '未标注年级')->toArray();
  104. }
  105. return $papers->groupBy(fn ($paper) => Arr::get($paper->meta ?? [], 'bundle_key', '未归并套卷'))->toArray();
  106. }
  107. public function selectedPaper(): ?SourcePaper
  108. {
  109. return $this->selectedPaperId
  110. ? SourcePaper::query()->withCount(['candidates', 'parts'])->find($this->selectedPaperId)
  111. : null;
  112. }
  113. public function selectPaper(int $paperId): void
  114. {
  115. $paper = SourcePaper::query()->find($paperId);
  116. if (!$paper) {
  117. return;
  118. }
  119. $meta = $paper->meta ?? [];
  120. $this->selectedPaperId = $paperId;
  121. $this->form = [
  122. 'title' => $paper->title,
  123. 'edition' => $paper->edition,
  124. 'grade' => $paper->grade,
  125. 'term' => $paper->term,
  126. 'chapter' => $paper->chapter,
  127. 'source_type' => $paper->source_type,
  128. 'source_year' => $paper->source_year,
  129. 'textbook_id' => $paper->textbook_id,
  130. 'textbook_series' => $paper->textbook_series,
  131. 'source_name' => Arr::get($meta, 'source_name'),
  132. 'source_page' => Arr::get($meta, 'source_page'),
  133. 'tags' => implode(',', Arr::get($meta, 'tags', [])),
  134. 'bundle_key' => Arr::get($meta, 'bundle_key'),
  135. 'expected_count' => Arr::get($meta, 'expected_count'),
  136. 'catalog_node_id' => Arr::get($meta, 'catalog_node_id'),
  137. ];
  138. }
  139. public function seedBatchFromCurrent(): void
  140. {
  141. $this->batch = [
  142. 'edition' => $this->form['edition'] ?? null,
  143. 'grade' => $this->form['grade'] ?? null,
  144. 'term' => $this->form['term'] ?? null,
  145. 'chapter' => $this->form['chapter'] ?? null,
  146. 'source_type' => $this->form['source_type'] ?? null,
  147. 'source_year' => $this->form['source_year'] ?? null,
  148. 'textbook_id' => $this->form['textbook_id'] ?? null,
  149. 'textbook_series' => $this->form['textbook_series'] ?? null,
  150. 'source_name' => $this->form['source_name'] ?? null,
  151. 'source_page' => $this->form['source_page'] ?? null,
  152. 'tags' => $this->form['tags'] ?? '',
  153. 'bundle_key' => $this->form['bundle_key'] ?? null,
  154. 'expected_count' => $this->form['expected_count'] ?? null,
  155. 'catalog_node_id' => $this->form['catalog_node_id'] ?? null,
  156. ];
  157. }
  158. public function savePaper(): void
  159. {
  160. $paper = $this->selectedPaper();
  161. if (!$paper) {
  162. return;
  163. }
  164. $meta = $paper->meta ?? [];
  165. $meta['source_name'] = $this->form['source_name'] ?? null;
  166. $meta['source_page'] = $this->form['source_page'] ?? null;
  167. $meta['tags'] = $this->explodeTags($this->form['tags'] ?? '');
  168. $meta['bundle_key'] = $this->form['bundle_key'] ?? null;
  169. $meta['expected_count'] = $this->form['expected_count'] ?? null;
  170. $meta['catalog_node_id'] = $this->form['catalog_node_id'] ?? null;
  171. $paper->update([
  172. 'title' => $this->form['title'] ?? null,
  173. 'edition' => $this->form['edition'] ?? null,
  174. 'grade' => $this->form['grade'] ?? null,
  175. 'term' => $this->form['term'] ?? null,
  176. 'chapter' => $this->form['chapter'] ?? null,
  177. 'source_type' => $this->form['source_type'] ?? null,
  178. 'source_year' => $this->form['source_year'] ?? null,
  179. 'textbook_id' => $this->form['textbook_id'] ?? null,
  180. 'textbook_series' => $this->form['textbook_series'] ?? null,
  181. 'meta' => $meta,
  182. ]);
  183. }
  184. public function applyBatch(): void
  185. {
  186. if (empty($this->selectedIds)) {
  187. return;
  188. }
  189. $updates = array_filter([
  190. 'edition' => $this->batch['edition'] ?? null,
  191. 'grade' => $this->batch['grade'] ?? null,
  192. 'term' => $this->batch['term'] ?? null,
  193. 'chapter' => $this->batch['chapter'] ?? null,
  194. 'source_type' => $this->batch['source_type'] ?? null,
  195. 'source_year' => $this->batch['source_year'] ?? null,
  196. 'textbook_id' => $this->batch['textbook_id'] ?? null,
  197. 'textbook_series' => $this->batch['textbook_series'] ?? null,
  198. ], fn ($value) => $value !== null && $value !== '');
  199. foreach (SourcePaper::query()->whereIn('id', $this->selectedIds)->get() as $paper) {
  200. $meta = $paper->meta ?? [];
  201. if (!empty($this->batch['source_name'])) {
  202. $meta['source_name'] = $this->batch['source_name'];
  203. }
  204. if (!empty($this->batch['source_page'])) {
  205. $meta['source_page'] = $this->batch['source_page'];
  206. }
  207. if (!empty($this->batch['tags'])) {
  208. $meta['tags'] = $this->explodeTags($this->batch['tags']);
  209. }
  210. if (!empty($this->batch['bundle_key'])) {
  211. $meta['bundle_key'] = $this->batch['bundle_key'];
  212. }
  213. if (!empty($this->batch['expected_count'])) {
  214. $meta['expected_count'] = $this->batch['expected_count'];
  215. }
  216. if (!empty($this->batch['catalog_node_id'])) {
  217. $meta['catalog_node_id'] = $this->batch['catalog_node_id'];
  218. }
  219. $paper->update(array_merge($updates, ['meta' => $meta]));
  220. }
  221. }
  222. public function autoInfer(): void
  223. {
  224. $paper = $this->selectedPaper();
  225. if (!$paper) {
  226. return;
  227. }
  228. $parsed = $this->parseImportFilename();
  229. if (!empty($parsed)) {
  230. $this->form['textbook_series'] = $this->form['textbook_series'] ?: $parsed['series'];
  231. $this->form['grade'] = $this->form['grade'] ?: $parsed['grade'];
  232. $this->form['term'] = $this->form['term'] ?: $parsed['term'];
  233. $this->form['source_name'] = $this->form['source_name'] ?: $parsed['name'];
  234. }
  235. $title = (string) ($paper->title ?? $paper->full_title ?? '');
  236. $raw = (string) ($paper->raw_markdown ?? '');
  237. $context = $title . ' ' . $raw;
  238. $this->form['term'] = $this->inferTerm($context) ?? $this->form['term'];
  239. $this->form['grade'] = $this->inferGrade($context) ?? $this->form['grade'];
  240. $this->form['chapter'] = $this->inferChapter($context) ?? $this->form['chapter'];
  241. }
  242. public function autoBundleKey(): void
  243. {
  244. $paper = $this->selectedPaper();
  245. if (!$paper) {
  246. return;
  247. }
  248. $this->form['bundle_key'] = $this->buildBundleKey($paper);
  249. }
  250. public function autoBundleKeySelected(): void
  251. {
  252. if (empty($this->selectedIds)) {
  253. return;
  254. }
  255. foreach (SourcePaper::query()->whereIn('id', $this->selectedIds)->get() as $paper) {
  256. $meta = $paper->meta ?? [];
  257. $meta['bundle_key'] = $this->buildBundleKey($paper);
  258. $paper->update(['meta' => $meta]);
  259. }
  260. }
  261. public function autoInferSelected(): void
  262. {
  263. if (empty($this->selectedIds)) {
  264. return;
  265. }
  266. $parsed = $this->parseImportFilename();
  267. foreach (SourcePaper::query()->whereIn('id', $this->selectedIds)->get() as $paper) {
  268. $context = (string) ($paper->title ?? $paper->full_title ?? '') . ' ' . (string) ($paper->raw_markdown ?? '');
  269. $updates = array_filter([
  270. 'term' => $this->inferTerm($context),
  271. 'grade' => $this->inferGrade($context),
  272. 'chapter' => $this->inferChapter($context),
  273. ], fn ($value) => $value !== null && $value !== '');
  274. if (!empty($parsed)) {
  275. if (empty($paper->textbook_series) && !empty($parsed['series'])) {
  276. $updates['textbook_series'] = $parsed['series'];
  277. }
  278. if (empty($paper->grade) && !empty($parsed['grade'])) {
  279. $updates['grade'] = $parsed['grade'];
  280. }
  281. if (empty($paper->term) && !empty($parsed['term'])) {
  282. $updates['term'] = $parsed['term'];
  283. }
  284. }
  285. $meta = $paper->meta ?? [];
  286. if (!empty($parsed['name']) && empty($meta['source_name'])) {
  287. $meta['source_name'] = $parsed['name'];
  288. }
  289. if (!empty($updates)) {
  290. $updates['meta'] = $meta;
  291. $paper->update($updates);
  292. } elseif (!empty($meta)) {
  293. $paper->update(['meta' => $meta]);
  294. }
  295. }
  296. }
  297. public function selectAllVisible(): void
  298. {
  299. $this->selectedIds = $this->papers()->pluck('id')->toArray();
  300. }
  301. public function clearSelection(): void
  302. {
  303. $this->selectedIds = [];
  304. }
  305. public function gradeOptions(): array
  306. {
  307. return collect(range(1, 12))->mapWithKeys(fn ($grade) => [$grade => $grade . '年级'])->toArray();
  308. }
  309. public function termOptions(): array
  310. {
  311. return [
  312. '上册' => '上册',
  313. '下册' => '下册',
  314. '上学期' => '上学期',
  315. '下学期' => '下学期',
  316. ];
  317. }
  318. public function sourceTypeOptions(): array
  319. {
  320. return [
  321. '期中' => '期中卷',
  322. '期末' => '期末卷',
  323. '单元卷' => '单元卷',
  324. '专项卷' => '专项卷',
  325. '教材' => '教材',
  326. '其他' => '其他',
  327. ];
  328. }
  329. public function textbookOptions(): array
  330. {
  331. return Textbook::query()
  332. ->orderBy('id')
  333. ->get(['id', 'official_title'])
  334. ->mapWithKeys(function ($textbook) {
  335. $title = $textbook->official_title ?: '未命名教材';
  336. return [$textbook->id => $title];
  337. })
  338. ->toArray();
  339. }
  340. public function catalogOptions(): array
  341. {
  342. if (empty($this->form['textbook_id']) && empty($this->batch['textbook_id'])) {
  343. return [];
  344. }
  345. $textbookId = $this->form['textbook_id'] ?: $this->batch['textbook_id'];
  346. $nodes = TextbookCatalog::query()
  347. ->where('textbook_id', $textbookId)
  348. ->orderBy('sort_order')
  349. ->get(['id', 'title', 'parent_id']);
  350. $grouped = $nodes->groupBy('parent_id');
  351. $walk = function ($parent, int $depth) use (&$walk, $grouped): array {
  352. $items = [];
  353. foreach ($grouped->get($parent, collect()) as $node) {
  354. $title = $node->title ?: '未命名目录';
  355. $indent = str_repeat('—', $depth);
  356. $items[$node->id] = trim($indent . ' ' . $title);
  357. $items += $walk($node->id, $depth + 1);
  358. }
  359. return $items;
  360. };
  361. return $walk(null, 0);
  362. }
  363. public function catalogCoverageSummary(): array
  364. {
  365. $textbookId = $this->selectedTextbookId();
  366. if (!$textbookId) {
  367. return [];
  368. }
  369. $nodes = TextbookCatalog::query()
  370. ->where('textbook_id', $textbookId)
  371. ->whereDoesntHave('children')
  372. ->get(['id']);
  373. $coverage = [];
  374. SourcePaper::query()
  375. ->where('textbook_id', $textbookId)
  376. ->get(['meta'])
  377. ->each(function ($paper) use (&$coverage) {
  378. $catalogId = $paper->meta['catalog_node_id'] ?? null;
  379. if ($catalogId) {
  380. $coverage[$catalogId] = ($coverage[$catalogId] ?? 0) + 1;
  381. }
  382. });
  383. $linked = count($coverage);
  384. $missing = max(0, $nodes->count() - count($coverage));
  385. return [
  386. 'total' => $nodes->count(),
  387. 'linked' => $linked,
  388. 'missing' => $missing,
  389. ];
  390. }
  391. public function missingCatalogNodes(): array
  392. {
  393. $textbookId = $this->selectedTextbookId();
  394. if (!$textbookId) {
  395. return [];
  396. }
  397. $nodes = TextbookCatalog::query()
  398. ->where('textbook_id', $textbookId)
  399. ->whereDoesntHave('children')
  400. ->orderBy('sort_order')
  401. ->get(['id', 'title']);
  402. $coverage = [];
  403. SourcePaper::query()
  404. ->where('textbook_id', $textbookId)
  405. ->get(['meta'])
  406. ->each(function ($paper) use (&$coverage) {
  407. $catalogId = $paper->meta['catalog_node_id'] ?? null;
  408. if ($catalogId) {
  409. $coverage[$catalogId] = ($coverage[$catalogId] ?? 0) + 1;
  410. }
  411. });
  412. $missing = [];
  413. foreach ($nodes as $node) {
  414. if (!isset($coverage[$node->id])) {
  415. $missing[] = [
  416. 'id' => $node->id,
  417. 'title' => $node->title,
  418. ];
  419. }
  420. }
  421. return array_slice($missing, 0, 8);
  422. }
  423. public function textbookSuggestions(): array
  424. {
  425. $paper = $this->selectedPaper();
  426. if (!$paper) {
  427. return [];
  428. }
  429. $title = (string) ($paper->title ?? $paper->full_title ?? '');
  430. $context = mb_strtolower($title);
  431. $parsed = $this->parseImportFilename();
  432. $grade = $paper->grade ? (int) $paper->grade : ($parsed['grade'] ?? null);
  433. $semester = $this->termToSemester($paper->term) ?? $this->termToSemester($parsed['term'] ?? null);
  434. $seriesHint = $paper->textbook_series ?: ($parsed['series'] ?? null);
  435. $subjectHint = $parsed['subject'] ?? null;
  436. $suggestions = [];
  437. $textbooks = Textbook::query()->with('series')->get();
  438. foreach ($textbooks as $textbook) {
  439. $score = 0;
  440. if ($grade && (int) $textbook->grade === $grade) {
  441. $score += 3;
  442. }
  443. if ($semester && (int) $textbook->semester === $semester) {
  444. $score += 3;
  445. }
  446. $official = mb_strtolower((string) $textbook->official_title);
  447. if ($official !== '' && str_contains($context, $official)) {
  448. $score += 4;
  449. }
  450. $aliases = $textbook->aliases ?? [];
  451. foreach ($aliases as $alias) {
  452. $alias = mb_strtolower((string) $alias);
  453. if ($alias !== '' && str_contains($context, $alias)) {
  454. $score += 2;
  455. }
  456. }
  457. $seriesName = $textbook->series?->name ?? null;
  458. if ($seriesHint && $seriesName && str_contains((string) $seriesHint, (string) $seriesName)) {
  459. $score += 5;
  460. }
  461. if ($subjectHint) {
  462. $subjectHint = mb_strtolower((string) $subjectHint);
  463. $official = mb_strtolower((string) $textbook->official_title);
  464. if ($official !== '' && str_contains($official, $subjectHint)) {
  465. $score += 1;
  466. }
  467. }
  468. if ($score > 0) {
  469. $suggestions[] = [
  470. 'id' => $textbook->id,
  471. 'title' => $textbook->official_title,
  472. 'series' => $textbook->series?->name ?? '未归类系列',
  473. 'grade' => $textbook->grade,
  474. 'semester' => $textbook->semester,
  475. 'score' => $score,
  476. ];
  477. }
  478. }
  479. usort($suggestions, fn ($a, $b) => $b['score'] <=> $a['score']);
  480. return array_slice($suggestions, 0, 5);
  481. }
  482. public function catalogSuggestions(): array
  483. {
  484. $paper = $this->selectedPaper();
  485. $textbookId = $paper?->textbook_id ?? $this->form['textbook_id'];
  486. if (!$paper || !$textbookId) {
  487. return [];
  488. }
  489. $needle = trim((string) ($paper->chapter ?? $paper->title ?? ''));
  490. if ($needle === '') {
  491. return [];
  492. }
  493. $nodes = TextbookCatalog::query()
  494. ->where('textbook_id', $textbookId)
  495. ->orderBy('sort_order')
  496. ->get(['id', 'title']);
  497. $matches = [];
  498. foreach ($nodes as $node) {
  499. $title = (string) $node->title;
  500. if ($title !== '' && str_contains($needle, $title)) {
  501. $matches[] = ['id' => $node->id, 'title' => $title];
  502. }
  503. }
  504. return array_slice($matches, 0, 5);
  505. }
  506. public function applyTextbookSuggestion(int $textbookId): void
  507. {
  508. $textbook = Textbook::query()->with('series')->find($textbookId);
  509. if (!$textbook) {
  510. return;
  511. }
  512. $this->form['textbook_id'] = $textbook->id;
  513. $this->form['textbook_series'] = $textbook->series?->name ?? $this->form['textbook_series'];
  514. }
  515. public function applyCatalogSuggestion(int $catalogId): void
  516. {
  517. $this->form['catalog_node_id'] = $catalogId;
  518. }
  519. public function candidateCountFor(SourcePaper $paper): int
  520. {
  521. return (int) ($paper->candidates_count ?? 0);
  522. }
  523. private function inferTerm(string $context): ?string
  524. {
  525. if (str_contains($context, '上册') || str_contains($context, '上学期')) {
  526. return '上册';
  527. }
  528. if (str_contains($context, '下册') || str_contains($context, '下学期')) {
  529. return '下册';
  530. }
  531. return null;
  532. }
  533. private function inferGrade(string $context): ?string
  534. {
  535. foreach (['七年级' => '7', '八年级' => '8', '九年级' => '9', '高一' => '10', '高二' => '11', '高三' => '12'] as $label => $value) {
  536. if (str_contains($context, $label)) {
  537. return $value;
  538. }
  539. }
  540. return null;
  541. }
  542. private function inferChapter(string $context): ?string
  543. {
  544. if (preg_match('/第[一二三四五六七八九十]+章[^\\n]*/u', $context, $match)) {
  545. return $match[0];
  546. }
  547. return null;
  548. }
  549. private function explodeTags(string $tags): array
  550. {
  551. return array_values(array_filter(array_map('trim', explode(',', $tags))));
  552. }
  553. private function termToSemester(?string $term): ?int
  554. {
  555. if (!$term) {
  556. return null;
  557. }
  558. if (str_contains($term, '上')) {
  559. return 1;
  560. }
  561. if (str_contains($term, '下')) {
  562. return 2;
  563. }
  564. return null;
  565. }
  566. private function buildBundleKey(SourcePaper $paper): string
  567. {
  568. $import = $this->importRecord();
  569. $base = $import?->file_name
  570. ? pathinfo($import->file_name, PATHINFO_FILENAME)
  571. : ($paper->title ?: $paper->full_title ?: '卷子');
  572. $grade = $paper->grade ? $this->gradeToLabel((int) $paper->grade) : null;
  573. $term = $paper->term ? $paper->term : null;
  574. $sourceType = $paper->source_type ?: null;
  575. $parts = array_filter([$grade, $term, $sourceType, $base]);
  576. return implode('·', $parts);
  577. }
  578. private function gradeToLabel(int $grade): string
  579. {
  580. $map = [
  581. 7 => '七年级',
  582. 8 => '八年级',
  583. 9 => '九年级',
  584. 10 => '高一',
  585. 11 => '高二',
  586. 12 => '高三',
  587. ];
  588. return $map[$grade] ?? $grade . '年级';
  589. }
  590. private function selectedTextbookId(): ?int
  591. {
  592. return $this->form['textbook_id'] ?: $this->batch['textbook_id'];
  593. }
  594. private function applyFilenameDefaults(): void
  595. {
  596. if (!$this->importId || empty($this->filenameParsed)) {
  597. return;
  598. }
  599. $parsed = $this->filenameParsed;
  600. $papers = SourcePaper::query()
  601. ->whereHas('candidates', fn ($q) => $q->where('import_id', $this->importId))
  602. ->get();
  603. foreach ($papers as $paper) {
  604. $meta = $paper->meta ?? [];
  605. if (!empty($meta['filename_defaults_applied'])) {
  606. continue;
  607. }
  608. $updates = [];
  609. if (empty($paper->textbook_series) && !empty($parsed['series'])) {
  610. $updates['textbook_series'] = $parsed['series'];
  611. }
  612. if (empty($paper->grade) && !empty($parsed['grade'])) {
  613. $updates['grade'] = $parsed['grade'];
  614. }
  615. if (empty($paper->term) && !empty($parsed['term'])) {
  616. $updates['term'] = $parsed['term'];
  617. }
  618. if (empty($meta['source_name']) && !empty($parsed['name'])) {
  619. $meta['source_name'] = $parsed['name'];
  620. }
  621. if (!empty($updates)) {
  622. $meta['filename_defaults_applied'] = true;
  623. $updates['meta'] = $meta;
  624. $paper->update($updates);
  625. } elseif (!empty($meta)) {
  626. $meta['filename_defaults_applied'] = true;
  627. $paper->update(['meta' => $meta]);
  628. }
  629. }
  630. }
  631. private function parseImportFilename(): array
  632. {
  633. $import = $this->importRecord();
  634. return $import?->parseFilename() ?? [];
  635. }
  636. }