PaliTextController.php 10 KB

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