2
0

SentencesInChapterController.php 3.0 KB

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