TipitakaPageNumberService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Http\Api\BookTitle as EditionBookTitles;
  4. use App\Models\PageNumber;
  5. use App\Models\WbwTemplate;
  6. /**
  7. * 按印本页码找三藏段落:PTS、缅甸第六次结集版、VRI、泰国版、其他,以及 CS 版印的段落号。
  8. *
  9. * 印本页码在 `page_numbers`(每条是正文里一个页码标记的位置);CS 段落号在逐词表里
  10. * (`real = para{n}` 的词元)。一个页码在各版本里加起来最多约一千处,不分页。
  11. */
  12. class TipitakaPageNumberService
  13. {
  14. /** page_numbers.type 代号 → 对外的版本名(与 tipitaka-word-paragraphs 的 refs.type 一致) */
  15. public const EDITIONS = ['P' => 'PTS', 'M' => 'My', 'V' => 'VRI', 'T' => 'Thai', 'O' => 'Other'];
  16. /** CS 版印的段落号 */
  17. public const CS = 'CS';
  18. /** 版本 → 静态书名表里该版本的书名字段(v6 印本页阅读页按它定位,同 QuoteLinkLoader) */
  19. private const TITLE_FIELDS = ['PTS' => 'p_title', 'My' => 'm_title', 'VRI' => 'v_title'];
  20. public function __construct(private readonly TipitakaBookService $books) {}
  21. /**
  22. * @param array{edition?: string, volume?: int} $filters
  23. * @return list<array{
  24. * edition: string, volume: int|null, page: int, book: int, para: int,
  25. * book_id: int, book_title: string, book_para: int, edition_title: string|null, layer: string|null, tags: list<string>
  26. * }>
  27. */
  28. public function find(int $page, array $filters): array
  29. {
  30. $edition = $filters['edition'] ?? null;
  31. $volume = $filters['volume'] ?? null;
  32. $rows = [];
  33. if ($edition !== self::CS) {
  34. $types = $edition === null ? array_keys(self::EDITIONS) : [array_search($edition, self::EDITIONS, true)];
  35. $pages = PageNumber::query()
  36. ->where('page', $page)
  37. ->whereIn('type', $types)
  38. ->when($volume !== null, fn ($q) => $q->where('volume', $volume))
  39. ->get(['type', 'volume', 'page', 'book', 'paragraph', 'pcd_book_id']);
  40. foreach ($pages as $p) {
  41. $rows[] = [
  42. 'edition' => self::EDITIONS[$p->type],
  43. // 0 是「卷号未知」
  44. 'volume' => (int) $p->volume ?: null,
  45. 'page' => (int) $p->page,
  46. 'book' => (int) $p->book,
  47. 'para' => (int) $p->paragraph,
  48. 'book_id' => (int) $p->pcd_book_id,
  49. ];
  50. }
  51. }
  52. // CS 段落号不分卷:给了卷号就不查
  53. if (($edition === null || $edition === self::CS) && $volume === null) {
  54. $paras = WbwTemplate::where('real', "para{$page}")->get(['book', 'paragraph', 'pcd_book_id']);
  55. foreach ($paras as $p) {
  56. $rows[] = [
  57. 'edition' => self::CS,
  58. 'volume' => null,
  59. 'page' => $page,
  60. 'book' => (int) $p->book,
  61. 'para' => (int) $p->paragraph,
  62. 'book_id' => (int) $p->pcd_book_id,
  63. ];
  64. }
  65. }
  66. $books = $this->books->find(array_column($rows, 'book_id'));
  67. $editionTitles = collect(EditionBookTitles::get())->keyBy('id');
  68. $rows = array_map(function (array $row) use ($books, $editionTitles) {
  69. $book = $books->get($row['book_id']);
  70. $titles = $editionTitles->get($row['book_id']);
  71. $field = self::TITLE_FIELDS[$row['edition']] ?? 'term';
  72. return $row + [
  73. 'book_title' => $book['title'] ?? '',
  74. 'book_para' => $book['para'] ?? 0,
  75. 'edition_title' => $row['edition'] === self::CS ? null : ($titles[$field] ?? null),
  76. 'layer' => $book['layer'] ?? null,
  77. 'tags' => $book['tags'] ?? [],
  78. ];
  79. }, $rows);
  80. // 按版本(PTS、缅、VRI、泰、其他、CS)、书、卷、段排
  81. $order = array_flip([...array_values(self::EDITIONS), self::CS]);
  82. usort($rows, fn (array $a, array $b) => [$order[$a['edition']], $a['book_id'], $a['volume'] ?? 0, $a['book'], $a['para']]
  83. <=> [$order[$b['edition']], $b['book_id'], $b['volume'] ?? 0, $b['book'], $b['para']]);
  84. return $rows;
  85. }
  86. }