CategoryController.php 7.6 KB

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