PaliTextController.php 12 KB

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