BookTitleController.php 5.6 KB

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