PaliTextController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. if($request->has('series')){
  149. $book_title = $request->get('series');
  150. //获取丛书书目列表
  151. $books = BookTitle::where('title',$request->get('series'))->get();
  152. }else{
  153. //查询这个目录的顶级目录
  154. $path = PaliText::where('book',$request->get('book'))
  155. ->where('paragraph',$request->get('para'))
  156. ->select('path')->first();
  157. if(!$path){
  158. return $this->error("no data");
  159. }
  160. $json = \json_decode($path->path);
  161. $root = null;
  162. foreach ($json as $key => $value) {
  163. # code...
  164. if( $value->level == 1 ){
  165. $root = $value;
  166. break;
  167. }
  168. }
  169. if($root===null){
  170. return $this->error("no data");
  171. }
  172. //查询书起始段落
  173. $rootPara = PaliText::where('book',$root->book)
  174. ->where('paragraph',$root->paragraph)
  175. ->first();
  176. //获取丛书书名
  177. $book_title = BookTitle::where('book',$rootPara->book)
  178. ->where('paragraph',$rootPara->paragraph)
  179. ->value('title');
  180. //获取丛书书目列表
  181. $books = BookTitle::where('title',$book_title)->get();
  182. }
  183. $chapters = [];
  184. $chapters[] = ['book'=>0,'paragraph'=>0,'toc'=>$book_title,'level'=>1];
  185. foreach ($books as $book) {
  186. # code...
  187. $rootPara = PaliText::where('book',$book->book)
  188. ->where('paragraph',$book->paragraph)
  189. ->first();
  190. $table = PaliText::where('book',$rootPara->book)
  191. ->whereBetween('paragraph',[$rootPara->paragraph,($rootPara->paragraph+$rootPara->chapter_len-1)])
  192. ->where('level','<',8);
  193. $all_count = $table->count();
  194. $curr_chapters = $table->select(['book','paragraph','toc','level'])->orderBy('paragraph')->get();
  195. foreach ($curr_chapters as $chapter) {
  196. # code...
  197. $chapters[] = ['book'=>$chapter->book,'paragraph'=>$chapter->paragraph,'toc'=>$chapter->toc,'level'=>($chapter->level+1)];
  198. }
  199. }
  200. break;
  201. }
  202. if($chapters){
  203. if($request->get('view') !== 'book-toc'){
  204. foreach ($chapters as $key => $value) {
  205. if(is_object($value)){
  206. //TODO $value->book 可能不存在
  207. $progress_key="/chapter_dynamic/{$value->book}/{$value->paragraph}/global";
  208. $chapters[$key]->progress_line = RedisClusters::get($progress_key);
  209. }
  210. }
  211. }
  212. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  213. }else{
  214. return $this->error("no data");
  215. }
  216. }
  217. /**
  218. * Store a newly created resource in storage.
  219. *
  220. * @param \Illuminate\Http\Request $request
  221. * @return \Illuminate\Http\Response
  222. */
  223. public function store(Request $request)
  224. {
  225. //
  226. }
  227. /**
  228. * Display the specified resource.
  229. *
  230. * @param \Illuminate\Http\Request $request
  231. * @return \Illuminate\Http\Response
  232. */
  233. public function show(Request $request)
  234. {
  235. //
  236. }
  237. /**
  238. * Update the specified resource in storage.
  239. *
  240. * @param \Illuminate\Http\Request $request
  241. * @param \App\Models\PaliText $paliText
  242. * @return \Illuminate\Http\Response
  243. */
  244. public function update(Request $request, PaliText $paliText)
  245. {
  246. //
  247. }
  248. /**
  249. * Remove the specified resource from storage.
  250. *
  251. * @param \App\Models\PaliText $paliText
  252. * @return \Illuminate\Http\Response
  253. */
  254. public function destroy(PaliText $paliText)
  255. {
  256. //
  257. }
  258. }