PaliTextController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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. $tm = (new TagMap)->getTable();
  74. $tg = (new Tag)->getTable();
  75. $pt = (new PaliText)->getTable();
  76. if($request->get('tags') && $request->get('tags')!==''){
  77. $tags = explode(',',$request->get('tags'));
  78. foreach ($tags as $tag) {
  79. # code...
  80. if(!empty($tag)){
  81. $tagNames[] = $tag;
  82. }
  83. }
  84. }
  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'))->where('paragraph',$request->get('para'))->first();
  127. if($result){
  128. return $this->ok($result);
  129. }else{
  130. return $this->error("no data");
  131. }
  132. break;
  133. case 'book-toc':
  134. /**
  135. * 获取全书目录
  136. * 2023-1-25 改进算法
  137. * 需求:目录显示丛书以及此丛书下面的所有书。比如,选择清净道论的一个章节。显示清净道论两本书的目录
  138. * 算法:
  139. * 1. 查询这个目录的顶级目录
  140. * 2. 查询book-title 获取丛书名
  141. * 3. 根据从书名找到全部的书
  142. * 4. 获取全部书的目录
  143. */
  144. $path = PaliText::where('book',$request->get('book'))
  145. ->where('paragraph',$request->get('para'))
  146. ->select('path')->first();
  147. if(!$path){
  148. return $this->error("no data");
  149. }
  150. $json = \json_decode($path->path);
  151. $root = null;
  152. foreach ($json as $key => $value) {
  153. # code...
  154. if( $value->level == 1 ){
  155. $root = $value;
  156. break;
  157. }
  158. }
  159. if($root===null){
  160. return $this->error("no data");
  161. }
  162. //查询书起始段落
  163. $rootPara = PaliText::where('book',$root->book)
  164. ->where('paragraph',$root->paragraph)
  165. ->first();
  166. $book_title = BookTitle::where('book',$rootPara->book)->where('paragraph',$rootPara->paragraph)->value('title');
  167. $books = BookTitle::where('title',$book_title)->get();
  168. $chapters = [];
  169. $chapters[] = ['book'=>0,'paragraph'=>0,'toc'=>$book_title,'level'=>1];
  170. foreach ($books as $book) {
  171. # code...
  172. $rootPara = PaliText::where('book',$book->book)
  173. ->where('paragraph',$book->paragraph)
  174. ->first();
  175. $table = PaliText::where('book',$rootPara->book)
  176. ->whereBetween('paragraph',[$rootPara->paragraph,($rootPara->paragraph+$rootPara->chapter_len-1)])
  177. ->where('level','<',8);
  178. $all_count = $table->count();
  179. $curr_chapters = $table->select(['book','paragraph','toc','level'])->orderBy('paragraph')->get();
  180. foreach ($curr_chapters as $chapter) {
  181. # code...
  182. $chapters[] = ['book'=>$chapter->book,'paragraph'=>$chapter->paragraph,'toc'=>$chapter->toc,'level'=>($chapter->level+1)];
  183. }
  184. }
  185. break;
  186. }
  187. if($chapters){
  188. foreach ($chapters as $key => $value) {
  189. $chapters[$key]->progress_line = Cache::get("/chapter_dynamic/{$value->book}/{$value->paragraph}/global");
  190. }
  191. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  192. }else{
  193. return $this->error("no data");
  194. }
  195. }
  196. /**
  197. * Store a newly created resource in storage.
  198. *
  199. * @param \Illuminate\Http\Request $request
  200. * @return \Illuminate\Http\Response
  201. */
  202. public function store(Request $request)
  203. {
  204. //
  205. }
  206. /**
  207. * Display the specified resource.
  208. *
  209. * @param \Illuminate\Http\Request $request
  210. * @return \Illuminate\Http\Response
  211. */
  212. public function show(Request $request)
  213. {
  214. //
  215. }
  216. /**
  217. * Update the specified resource in storage.
  218. *
  219. * @param \Illuminate\Http\Request $request
  220. * @param \App\Models\PaliText $paliText
  221. * @return \Illuminate\Http\Response
  222. */
  223. public function update(Request $request, PaliText $paliText)
  224. {
  225. //
  226. }
  227. /**
  228. * Remove the specified resource from storage.
  229. *
  230. * @param \App\Models\PaliText $paliText
  231. * @return \Illuminate\Http\Response
  232. */
  233. public function destroy(PaliText $paliText)
  234. {
  235. //
  236. }
  237. }