PaliTextController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Models\PaliText;
  5. use App\Models\BookTitle;
  6. use App\Models\Tag;
  7. use App\Models\TagMap;
  8. use Illuminate\Http\Request;
  9. use App\Tools\RedisClusters;
  10. class PaliTextController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. //
  20. $all_count = 0;
  21. switch ($request->get('view')) {
  22. case 'chapter-tag':
  23. $tm = (new TagMap)->getTable();
  24. $tg = (new Tag)->getTable();
  25. $pt = (new PaliText)->getTable();
  26. if ($request->get('tags') && $request->get('tags') !== '') {
  27. $tags = explode(',', $request->get('tags'));
  28. foreach ($tags as $tag) {
  29. # code...
  30. if (!empty($tag)) {
  31. $tagNames[] = $tag;
  32. }
  33. }
  34. }
  35. if (isset($tagNames)) {
  36. $where1 = " where co = " . count($tagNames);
  37. $a = implode(",", array_fill(0, count($tagNames), '?'));
  38. $in1 = "and t.name in ({$a})";
  39. $param = $tagNames;
  40. } else {
  41. $where1 = " ";
  42. $in1 = " ";
  43. }
  44. $query = "
  45. select tags.id,tags.name,co as count
  46. from (
  47. select tm.tag_id,count(*) as co from (
  48. select anchor_id as id from (
  49. select tm.anchor_id , count(*) as co
  50. from $tm as tm
  51. left join $tg as t on tm.tag_id = t.id
  52. left join $pt as pc on tm.anchor_id = pc.uid
  53. where tm.table_name = 'pali_texts'
  54. $in1
  55. group by tm.anchor_id
  56. ) T
  57. $where1
  58. ) CID
  59. left join $tm as tm on tm.anchor_id = CID.id
  60. group by tm.tag_id
  61. ) tid
  62. left join $tg on $tg.id = tid.tag_id
  63. order by count desc
  64. ";
  65. if (isset($param)) {
  66. $chapters = DB::select($query, $param);
  67. } else {
  68. $chapters = DB::select($query);
  69. }
  70. $all_count = count($chapters);
  71. break;
  72. case 'chapter':
  73. if ($request->get('tags') && $request->get('tags') !== '') {
  74. $tags = explode(',', $request->get('tags'));
  75. foreach ($tags as $tag) {
  76. # code...
  77. if (!empty($tag)) {
  78. $tagNames[] = $tag;
  79. }
  80. }
  81. }
  82. $tm = (new TagMap)->getTable();
  83. $tg = (new Tag)->getTable();
  84. $pt = (new PaliText)->getTable();
  85. if (isset($tagNames)) {
  86. $where1 = " where co = " . count($tagNames);
  87. $a = implode(",", array_fill(0, count($tagNames), '?'));
  88. $in1 = "and t.name in ({$a})";
  89. $param = $tagNames;
  90. $where2 = "where level < 3";
  91. } else {
  92. $where1 = " ";
  93. $in1 = " ";
  94. $where2 = "where level = 1";
  95. }
  96. $query = "
  97. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  98. select anchor_id as cid from (
  99. select tm.anchor_id , count(*) as co
  100. from $tm as tm
  101. left join $tg as t on tm.tag_id = t.id
  102. where tm.table_name = 'pali_texts'
  103. $in1
  104. group by tm.anchor_id
  105. ) T
  106. $where1
  107. ) CID
  108. left join $pt as pt on CID.cid = pt.uid
  109. $where2
  110. order by book,paragraph";
  111. if (isset($param)) {
  112. $chapters = DB::select($query, $param);
  113. } else {
  114. $chapters = DB::select($query);
  115. }
  116. $all_count = count($chapters);
  117. break;
  118. case 'chapter_children':
  119. $table = PaliText::where('book', $request->get('book'))
  120. ->where('parent', $request->get('para'))
  121. ->where('level', '<', 8);
  122. $all_count = $table->count();
  123. $chapters = $table->orderBy('paragraph')->get();
  124. break;
  125. case 'paragraph':
  126. $result = PaliText::where('book', $request->get('book'))
  127. ->where('paragraph', $request->get('para'))
  128. ->first();
  129. if ($result) {
  130. return $this->ok($result);
  131. } else {
  132. return $this->error("no data");
  133. }
  134. break;
  135. case 'book-toc':
  136. /**
  137. * 获取全书目录
  138. * 2023-1-25 改进算法
  139. * 需求:目录显示丛书以及此丛书下面的所有书。比如,选择清净道论的一个章节。显示清净道论两本书的目录
  140. * 算法:
  141. * 1. 查询这个目录的顶级目录
  142. * 2. 查询book-title 获取丛书名
  143. * 3. 根据从书名找到全部的书
  144. * 4. 获取全部书的目录
  145. */
  146. if ($request->has('series')) {
  147. $book_title = $request->get('series');
  148. //获取丛书书目列表
  149. $books = BookTitle::where('title', $request->get('series'))->get();
  150. } else {
  151. //查询这个目录的顶级目录
  152. $path = PaliText::where('book', $request->get('book'))
  153. ->where('paragraph', $request->get('para'))
  154. ->select('path')->first();
  155. if (!$path) {
  156. return $this->error("no data");
  157. }
  158. $json = \json_decode($path->path);
  159. $root = null;
  160. foreach ($json as $key => $value) {
  161. # code...
  162. if ($value->level == 1) {
  163. $root = $value;
  164. break;
  165. }
  166. }
  167. if ($root === null) {
  168. return $this->error("no data");
  169. }
  170. //查询书起始段落
  171. $rootPara = PaliText::where('book', $root->book)
  172. ->where('paragraph', $root->paragraph)
  173. ->first();
  174. //获取丛书书名
  175. $book_title = BookTitle::where('book', $rootPara->book)
  176. ->where('paragraph', $rootPara->paragraph)
  177. ->value('title');
  178. //获取丛书书目列表
  179. $books = BookTitle::where('title', $book_title)->get();
  180. }
  181. $chapters = [];
  182. $chapters[] = ['book' => 0, 'paragraph' => 0, 'toc' => $book_title, 'level' => 1];
  183. foreach ($books as $book) {
  184. # code...
  185. $rootPara = PaliText::where('book', $book->book)
  186. ->where('paragraph', $book->paragraph)
  187. ->first();
  188. $table = PaliText::where('book', $rootPara->book)
  189. ->whereBetween('paragraph', [$rootPara->paragraph, ($rootPara->paragraph + $rootPara->chapter_len - 1)])
  190. ->where('level', '<', 8);
  191. $all_count = $table->count();
  192. $curr_chapters = $table->select(['book', 'paragraph', 'toc', 'level'])->orderBy('paragraph')->get();
  193. foreach ($curr_chapters as $chapter) {
  194. # code...
  195. $chapters[] = ['book' => $chapter->book, 'paragraph' => $chapter->paragraph, 'toc' => $chapter->toc, 'level' => ($chapter->level + 1)];
  196. }
  197. }
  198. break;
  199. }
  200. if ($chapters) {
  201. if ($request->get('view') !== 'book-toc') {
  202. foreach ($chapters as $key => $value) {
  203. if (is_object($value)) {
  204. //TODO $value->book 可能不存在
  205. $progress_key = "/chapter_dynamic/{$value->book}/{$value->paragraph}/global";
  206. $chapters[$key]->progress_line = RedisClusters::get($progress_key);
  207. }
  208. }
  209. }
  210. return $this->ok(["rows" => $chapters, "count" => $all_count]);
  211. } else {
  212. return $this->error("no data");
  213. }
  214. }
  215. /**
  216. * Store a newly created resource in storage.
  217. *
  218. * @param \Illuminate\Http\Request $request
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function store(Request $request)
  222. {
  223. //
  224. }
  225. /**
  226. * Display the specified resource.
  227. *
  228. * @param \Illuminate\Http\Request $request
  229. * @return \Illuminate\Http\Response
  230. */
  231. public function show(Request $request)
  232. {
  233. //
  234. }
  235. /**
  236. * Update the specified resource in storage.
  237. *
  238. * @param \Illuminate\Http\Request $request
  239. * @param \App\Models\PaliText $paliText
  240. * @return \Illuminate\Http\Response
  241. */
  242. public function update(Request $request, PaliText $paliText)
  243. {
  244. //
  245. }
  246. /**
  247. * Remove the specified resource from storage.
  248. *
  249. * @param \App\Models\PaliText $paliText
  250. * @return \Illuminate\Http\Response
  251. */
  252. public function destroy(PaliText $paliText)
  253. {
  254. //
  255. }
  256. }