CategoryController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Models\PaliText;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Tag;
  7. use App\Models\TagMap;
  8. class CategoryController extends Controller
  9. {
  10. protected static int $nextId = 1;
  11. public function index()
  12. {
  13. $categories = $this->loadCategories();
  14. // 获取一级分类和对应的书籍
  15. $categoryData = [];
  16. foreach ($categories as $category) {
  17. if ($category['level'] == 1) {
  18. $children = $this->subCategories($categories, $category['id']);
  19. $categoryData[] = [
  20. 'category' => $category,
  21. 'children' => $children,
  22. ];
  23. }
  24. }
  25. $update = $this->getUpdateBooks();
  26. return view('library.index', compact('categoryData', 'categories', 'update'));
  27. }
  28. public function show($id)
  29. {
  30. $categories = $this->loadCategories();
  31. $currentCategory = collect($categories)->firstWhere('id', $id);
  32. if (!$currentCategory) {
  33. abort(404);
  34. }
  35. // 获取子分类
  36. $subCategories = array_filter($categories, function ($cat) use ($id) {
  37. return $cat['parent_id'] == $id;
  38. });
  39. // 获取该分类下的书籍
  40. $categoryBooks = $this->getBooks($categories, $id);
  41. // 获取面包屑
  42. $breadcrumbs = $this->getBreadcrumbs($currentCategory, $categories);
  43. return view('library.category', compact('currentCategory', 'subCategories', 'categoryBooks', 'breadcrumbs'));
  44. }
  45. private function subCategories($categories, int $id)
  46. {
  47. return array_filter($categories, function ($cat) use ($id) {
  48. return $cat['parent_id'] == $id;
  49. });
  50. }
  51. private function getUpdateBooks()
  52. {
  53. $books = ProgressChapter::with('channel.owner')
  54. ->leftJoin('pali_texts', function ($join) {
  55. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  56. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  57. })
  58. ->whereHas('channel', function ($query) {
  59. $query->where('status', 30);
  60. })
  61. ->where('progress', '>', config('mint.library.list_min_progress'))
  62. ->take(10)
  63. ->get();
  64. return $this->getBooksInfo($books);
  65. }
  66. private function getBooks($categories, $id)
  67. {
  68. $currentCategory = collect($categories)->firstWhere('id', $id);
  69. if (!$currentCategory) {
  70. abort(404);
  71. }
  72. // 标签查章节
  73. $tagNames = $currentCategory['tag'];
  74. $tm = (new TagMap)->getTable();
  75. $tg = (new Tag)->getTable();
  76. $pt = (new PaliText)->getTable();
  77. $where1 = " where co = " . count($tagNames);
  78. $a = implode(",", array_fill(0, count($tagNames), '?'));
  79. $in1 = "and t.name in ({$a})";
  80. $param = $tagNames;
  81. $where2 = "where level = 1";
  82. $query = "
  83. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  84. select anchor_id as cid from (
  85. select tm.anchor_id , count(*) as co
  86. from $tm as tm
  87. left join $tg as t on tm.tag_id = t.id
  88. where tm.table_name = 'pali_texts'
  89. $in1
  90. group by tm.anchor_id
  91. ) T
  92. $where1
  93. ) CID
  94. left join $pt as pt on CID.cid = pt.uid
  95. $where2
  96. order by book,paragraph";
  97. $chapters = DB::select($query, $param);
  98. $chaptersParam = [];
  99. foreach ($chapters as $key => $chapter) {
  100. $chaptersParam[] = [$chapter->book, $chapter->paragraph];
  101. }
  102. // 获取该分类下的章节
  103. $books = ProgressChapter::with('channel.owner')
  104. ->leftJoin('pali_texts', function ($join) {
  105. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  106. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  107. })
  108. ->whereIns(['progress_chapters.book', 'progress_chapters.para'], $chaptersParam)
  109. ->whereHas('channel', function ($query) {
  110. $query->where('status', 30);
  111. })
  112. ->where('progress', '>', config('mint.library.list_min_progress'))
  113. ->get();
  114. return $this->getBooksInfo($books);
  115. }
  116. private function getBooksInfo($books,)
  117. {
  118. $pali = PaliText::where('level', 1)->get();
  119. // 获取该分类下的书籍
  120. $categoryBooks = [];
  121. $books->each(function ($book) use (&$categoryBooks, $pali) {
  122. $title = $book->title;
  123. if (empty($title)) {
  124. $title = $pali->firstWhere('book', $book->book)->toc;
  125. }
  126. $categoryBooks[] = [
  127. "id" => $book->uid,
  128. "title" => $title,
  129. "author" => $book->channel->name,
  130. "publisher" => $book->channel->owner,
  131. "type" => __('labels.' . $book->channel->type),
  132. "cover" => "/assets/images/cover/zh-hans/1/{$book->pcd_book_id}.png",
  133. "description" => $book->summary ?? "比库戒律的详细说明",
  134. "language" => __('language.' . $book->channel->lang),
  135. ];
  136. });
  137. return $categoryBooks;
  138. }
  139. private function loadCategories()
  140. {
  141. $json = file_get_contents(public_path("date/category/default.json"));
  142. $tree = json_decode($json, true);
  143. $flat = self::flattenWithIds($tree);
  144. return $flat;
  145. }
  146. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  147. {
  148. $flat = [];
  149. foreach ($tree as $node) {
  150. $currentId = self::$nextId++;
  151. $item = [
  152. 'id' => $currentId,
  153. 'parent_id' => $parentId,
  154. 'name' => $node['name'] ?? null,
  155. 'tag' => $node['tag'] ?? [],
  156. "description" => "佛教戒律经典",
  157. 'level' => $level,
  158. ];
  159. $flat[] = $item;
  160. if (isset($node['children']) && is_array($node['children'])) {
  161. $childrenLevel = $level + 1;
  162. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  163. }
  164. }
  165. return $flat;
  166. }
  167. private function getBreadcrumbs($category, $categories)
  168. {
  169. $breadcrumbs = [];
  170. $current = $category;
  171. while ($current) {
  172. array_unshift($breadcrumbs, $current);
  173. $current = collect($categories)->firstWhere('id', $current['parent_id']);
  174. }
  175. return $breadcrumbs;
  176. }
  177. }