PaliTextController.php 9.4 KB

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