2
0

BookTitleController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\BookTitleResource;
  4. use App\Models\BookTitle;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Response;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. class BookTitleController extends Controller
  11. {
  12. /**
  13. * 书目清单基本不变,而每次都要连 pali_texts / tag_maps / tags 三张表,
  14. * 所以整份结果缓存 24 小时。
  15. */
  16. private const CACHE_KEY = 'book-titles/with-tags';
  17. private const CACHE_TTL = 60 * 60 * 24;
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Response
  22. */
  23. public function index()
  24. {
  25. //
  26. $data = Cache::remember(self::CACHE_KEY, self::CACHE_TTL, function () {
  27. $result = BookTitle::orderBy('sn')->get();
  28. $meta = $this->paliTextMeta($result);
  29. $relatedNames = $this->relatedNames($result);
  30. foreach ($result as $row) {
  31. $key = "{$row->book}-{$row->paragraph}";
  32. $row->toc = $meta[$key]['toc'] ?? null;
  33. $row->tags = $meta[$key]['tags'] ?? [];
  34. $row->related_name = $relatedNames[$key] ?? null;
  35. }
  36. return [
  37. 'rows' => BookTitleResource::collection($result)->resolve(),
  38. 'count' => count($result),
  39. ];
  40. });
  41. return $this->ok($data);
  42. }
  43. /**
  44. * 取每条书目在 pali_texts 里对应的 toc 与 tag 名。
  45. *
  46. * book_titles 的 (book, paragraph) 指向 pali_texts 的同名字段;toc 直接取自
  47. * pali_texts,tag 则再经 pali_texts.uid → tag_maps.anchor_id → tags 一跳。
  48. *
  49. * 两条查询都按 (book, paragraph) 的取值范围收窄,一次查完在内存里归组,
  50. * 避免 281 条书目各查一次。whereIn 取的是两个维度的笛卡尔超集,最后按精确的
  51. * "{book}-{paragraph}" 键取用,多出来的行不会被匹配到。
  52. *
  53. * @param Collection $bookTitles
  54. * @return array<string, array{toc: ?string, tags: string[]}> 键是 "{book}-{paragraph}"
  55. */
  56. private function paliTextMeta($bookTitles): array
  57. {
  58. if ($bookTitles->isEmpty()) {
  59. return [];
  60. }
  61. $books = $bookTitles->pluck('book')->unique()->all();
  62. $paragraphs = $bookTitles->pluck('paragraph')->unique()->all();
  63. $texts = DB::table('pali_texts')
  64. ->whereIn('book', $books)
  65. ->whereIn('paragraph', $paragraphs)
  66. ->select('uid', 'book', 'paragraph', 'toc')
  67. ->get();
  68. $meta = [];
  69. $keyByUid = [];
  70. foreach ($texts as $text) {
  71. $key = "{$text->book}-{$text->paragraph}";
  72. $meta[$key] = ['toc' => $text->toc, 'tags' => []];
  73. $keyByUid[$text->uid] = $key;
  74. }
  75. if (empty($keyByUid)) {
  76. return $meta;
  77. }
  78. $tags = DB::table('tag_maps')
  79. ->join('tags', 'tags.id', '=', 'tag_maps.tag_id')
  80. ->where('tag_maps.table_name', 'pali_texts')
  81. ->whereIn('tag_maps.anchor_id', array_keys($keyByUid))
  82. ->select('tag_maps.anchor_id', 'tags.name')
  83. ->get();
  84. foreach ($tags as $tag) {
  85. $key = $keyByUid[$tag->anchor_id] ?? null;
  86. if ($key === null || in_array($tag->name, $meta[$key]['tags'], true)) {
  87. continue;
  88. }
  89. $meta[$key]['tags'][] = $tag->name;
  90. }
  91. foreach ($meta as &$item) {
  92. sort($item['tags']);
  93. }
  94. return $meta;
  95. }
  96. /**
  97. * 取每条书目对应的 CST 书名(related_paragraphs.book_name)。
  98. *
  99. * 按 (book, para) 配对,而不是 related_paragraphs.book_id = book_titles.sn。
  100. * 两者实测差异:book_id 给的是「这本书横跨的全部 CST 书」(pācityādiyojanā →
  101. * vin2..vin5),(book, para) 给的是「起始段所在的那一本」(→ vin2),且后者能查到
  102. * book_id 归错地方的 samantapāsādikā(sn=280) 与 Bhikkhunīvibhaṅga(sn=281)。
  103. *
  104. * 实测每个 (book, para) 至多对应一个非空 book_name,故返回标量。
  105. *
  106. * @param Collection $bookTitles
  107. * @return array<string, string> 键是 "{book}-{paragraph}"
  108. */
  109. private function relatedNames($bookTitles): array
  110. {
  111. if ($bookTitles->isEmpty()) {
  112. return [];
  113. }
  114. $rows = DB::table('related_paragraphs')
  115. ->whereIn('book', $bookTitles->pluck('book')->unique()->all())
  116. ->whereIn('para', $bookTitles->pluck('paragraph')->unique()->all())
  117. ->whereNotNull('book_name')
  118. ->where('book_name', '<>', '')
  119. ->select('book', 'para', 'book_name')
  120. ->distinct()
  121. ->get();
  122. $map = [];
  123. foreach ($rows as $row) {
  124. $map["{$row->book}-{$row->para}"] = $row->book_name;
  125. }
  126. return $map;
  127. }
  128. /**
  129. * Store a newly created resource in storage.
  130. *
  131. * @return Response
  132. */
  133. public function store(Request $request)
  134. {
  135. //
  136. }
  137. /**
  138. * Display the specified resource.
  139. *
  140. * @return Response
  141. */
  142. public function show(BookTitle $bookTitle)
  143. {
  144. //
  145. }
  146. /**
  147. * Update the specified resource in storage.
  148. *
  149. * @return Response
  150. */
  151. public function update(Request $request, BookTitle $bookTitle)
  152. {
  153. //
  154. }
  155. /**
  156. * Remove the specified resource from storage.
  157. *
  158. * @return Response
  159. */
  160. public function destroy(BookTitle $bookTitle)
  161. {
  162. //
  163. }
  164. }