PaliTextController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Models\PaliText;
  5. use App\Models\Tag;
  6. use App\Models\TagMap;
  7. use Illuminate\Http\Request;
  8. class PaliTextController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. //
  18. $all_count = 0;
  19. switch ($request->get('view')) {
  20. case 'chapter-tag':
  21. $tm = (new TagMap)->getTable();
  22. $tg = (new Tag)->getTable();
  23. $pt = (new PaliText)->getTable();
  24. if($request->get('tags') && $request->get('tags')!==''){
  25. $tags = explode(',',$request->get('tags'));
  26. foreach ($tags as $tag) {
  27. # code...
  28. if(!empty($tag)){
  29. $tagNames[] = $tag;
  30. }
  31. }
  32. }
  33. if(isset($tagNames)){
  34. $where1 = " where co = ".count($tagNames);
  35. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  36. $in1 = "and t.name in ({$a})";
  37. $param = $tagNames;
  38. }else{
  39. $where1 = " ";
  40. $in1 = " ";
  41. }
  42. $query = "
  43. select tags.id,tags.name,co as count
  44. from (
  45. select tm.tag_id,count(*) as co from (
  46. select anchor_id as id from (
  47. select tm.anchor_id , count(*) as co
  48. from $tm as tm
  49. left join $tg as t on tm.tag_id = t.id
  50. left join $pt as pc on tm.anchor_id = pc.uid
  51. where tm.table_name = 'pali_texts'
  52. $in1
  53. group by tm.anchor_id
  54. ) T
  55. $where1
  56. ) CID
  57. left join $tm as tm on tm.anchor_id = CID.id
  58. group by tm.tag_id
  59. ) tid
  60. left join $tg on $tg.id = tid.tag_id
  61. order by count desc
  62. ";
  63. if(isset($param)){
  64. $chapters = DB::select($query,$param);
  65. }else{
  66. $chapters = DB::select($query);
  67. }
  68. $all_count = count($chapters);
  69. break;
  70. case 'chapter':
  71. $tm = (new TagMap)->getTable();
  72. $tg = (new Tag)->getTable();
  73. $pt = (new PaliText)->getTable();
  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. if(isset($tagNames)){
  84. $where1 = " where co = ".count($tagNames);
  85. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  86. $in1 = "and t.name in ({$a})";
  87. $param = $tagNames;
  88. $where2 = "where level < 3";
  89. }else{
  90. $where1 = " ";
  91. $in1 = " ";
  92. $where2 = "where level = 1";
  93. }
  94. $query = "
  95. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  96. select anchor_id as cid from (
  97. select tm.anchor_id , count(*) as co
  98. from $tm as tm
  99. left join $tg as t on tm.tag_id = t.id
  100. where tm.table_name = 'pali_texts'
  101. $in1
  102. group by tm.anchor_id
  103. ) T
  104. $where1
  105. ) CID
  106. left join $pt as pt on CID.cid = pt.uid
  107. $where2
  108. order by book,paragraph";
  109. if(isset($param)){
  110. $chapters = DB::select($query,$param);
  111. }else{
  112. $chapters = DB::select($query);
  113. }
  114. $all_count = count($chapters);
  115. break;
  116. case 'chapter_children':
  117. $table = PaliText::where('book',$request->get('book'))
  118. ->where('parent',$request->get('para'))
  119. ->where('level','<',8);
  120. $all_count = $table->count();
  121. $chapters = $table->orderBy('paragraph')->get();
  122. break;
  123. case 'paragraph':
  124. $result = PaliText::where('book',$request->get('book'))->where('paragraph',$request->get('para'))->first();
  125. if($result){
  126. return $this->ok($result);
  127. }else{
  128. return $this->error("no data");
  129. }
  130. break;
  131. case 'book-toc':
  132. //获取全书目录
  133. $path = PaliText::where('book',$request->get('book'))
  134. ->where('paragraph',$request->get('para'))
  135. ->select('path')->first();
  136. if(!$path){
  137. return $this->error("no data");
  138. }
  139. $json = \json_decode($path->path);
  140. $root = null;
  141. foreach ($json as $key => $value) {
  142. # code...
  143. if( $value->level == 1 ){
  144. $root = $value;
  145. break;
  146. }
  147. }
  148. if($root===null){
  149. return $this->error("no data");
  150. }
  151. //查询书起始段落
  152. $rootPara = PaliText::where('book',$root->book)
  153. ->where('paragraph',$root->paragraph)
  154. ->first();
  155. $table = PaliText::where('book',$rootPara->book)
  156. ->whereBetween('paragraph',[$rootPara->paragraph,($rootPara->paragraph+$rootPara->chapter_len-1)])
  157. ->where('level','<',8);
  158. $all_count = $table->count();
  159. $chapters = $table->select(['book','paragraph','toc','level'])->orderBy('paragraph')->get();
  160. break;
  161. }
  162. if($chapters){
  163. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  164. }else{
  165. return $this->error("no data");
  166. }
  167. }
  168. /**
  169. * Store a newly created resource in storage.
  170. *
  171. * @param \Illuminate\Http\Request $request
  172. * @return \Illuminate\Http\Response
  173. */
  174. public function store(Request $request)
  175. {
  176. //
  177. }
  178. /**
  179. * Display the specified resource.
  180. *
  181. * @param \Illuminate\Http\Request $request
  182. * @return \Illuminate\Http\Response
  183. */
  184. public function show(Request $request)
  185. {
  186. //
  187. }
  188. /**
  189. * Update the specified resource in storage.
  190. *
  191. * @param \Illuminate\Http\Request $request
  192. * @param \App\Models\PaliText $paliText
  193. * @return \Illuminate\Http\Response
  194. */
  195. public function update(Request $request, PaliText $paliText)
  196. {
  197. //
  198. }
  199. /**
  200. * Remove the specified resource from storage.
  201. *
  202. * @param \App\Models\PaliText $paliText
  203. * @return \Illuminate\Http\Response
  204. */
  205. public function destroy(PaliText $paliText)
  206. {
  207. //
  208. }
  209. }