PaliTextController.php 12 KB

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