SentencesInChapterController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\PaliText;
  4. use App\Models\PaliSentence;
  5. use Illuminate\Http\Request;
  6. class SentencesInChapterController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index(Request $request)
  14. {
  15. //
  16. $book = $request->get('book');
  17. $para = $request->get('para');
  18. $chapter = PaliText::where('book',$book)
  19. ->where('paragraph',$para)
  20. ->first();
  21. if(!$chapter){
  22. return $this->error("no chapter data");
  23. }
  24. $paraFrom = $para;
  25. $paraTo = $para+$chapter->chapter_len-1;
  26. //1. 计算 标题和下一级第一个标题之间 是否有间隔
  27. $nextChapter = PaliText::where('book',$book)
  28. ->where('paragraph',">",$para)
  29. ->where('level','<',8)
  30. ->orderBy('paragraph')
  31. ->value('paragraph');
  32. $between = $nextChapter - $para;
  33. //查找子目录
  34. $chapterLen = $chapter->chapter_len;
  35. $toc = PaliText::where('book',$book)
  36. ->whereBetween('paragraph',[$paraFrom+1,$paraFrom+$chapterLen-1])
  37. ->where('level','<',8)
  38. ->orderBy('paragraph')
  39. ->select(['book','paragraph','level','toc'])
  40. ->get();
  41. if($between > 1){
  42. //有间隔
  43. $paraTo = $nextChapter - 1;
  44. }else{
  45. if($chapter->chapter_strlen>2000){
  46. if(count($toc)>0){
  47. //有子目录只输出标题和目录
  48. $paraTo = $paraFrom;
  49. }else{
  50. //没有子目录 全部输出
  51. }
  52. }else{
  53. //章节小。全部输出 不输出子目录
  54. $toc = [];
  55. }
  56. }
  57. $sent = PaliSentence::where('book',$book)
  58. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  59. ->select(['book','paragraph','word_begin','word_end'])
  60. ->orderBy('paragraph')
  61. ->orderBy('word_begin')
  62. ->get();
  63. return $this->ok(['rows'=>$sent,'count'=>count($sent)]);
  64. }
  65. /**
  66. * Store a newly created resource in storage.
  67. *
  68. * @param \Illuminate\Http\Request $request
  69. * @return \Illuminate\Http\Response
  70. */
  71. public function store(Request $request)
  72. {
  73. //
  74. }
  75. /**
  76. * Display the specified resource.
  77. *
  78. * @param \App\Models\PaliText $paliText
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function show(PaliText $paliText)
  82. {
  83. //
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param \Illuminate\Http\Request $request
  89. * @param \App\Models\PaliText $paliText
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function update(Request $request, PaliText $paliText)
  93. {
  94. //
  95. }
  96. /**
  97. * Remove the specified resource from storage.
  98. *
  99. * @param \App\Models\PaliText $paliText
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function destroy(PaliText $paliText)
  103. {
  104. //
  105. }
  106. }