SentencesInChapterController.php 3.4 KB

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