PaliTextController.php 10.0 KB

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