NavCSParaController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\NavCSParaResource;
  4. use App\Models\PaliText;
  5. use App\Models\WbwTemplate;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. class NavCSParaController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return Response
  14. */
  15. public function index()
  16. {
  17. //
  18. }
  19. /**
  20. * Store a newly created resource in storage.
  21. *
  22. * @return Response
  23. */
  24. public function store(Request $request)
  25. {
  26. //
  27. }
  28. /**
  29. * Display the specified resource.
  30. *
  31. * @param WbwTemplate $wbwTemplate
  32. * @return Response
  33. */
  34. public function show(string $paraNumber)
  35. {
  36. // 99_5_37-38
  37. $id = explode('_', $paraNumber);
  38. if (count($id) !== 3) {
  39. return $this->error('参数错误。参数应为3 实际得到'.count($id), 400, 400);
  40. }
  41. // 查询段落起始
  42. $para = PaliText::where('book', $id[0])
  43. ->where('paragraph', $id[1])
  44. ->first();
  45. if (! $para) {
  46. return $this->error('没有找到段落起始'.$id, 404, 404);
  47. }
  48. // cs 段落号列表
  49. $csPara = explode('-', $id[2]);
  50. $csList = [];
  51. for ($i = (int) $csPara[0]; $i <= (int) end($csPara); $i++) {
  52. $csList[] = $i;
  53. }
  54. // 段落区间
  55. $begin = $id[1];
  56. $end = (int) $id[1] + $para->chapter_len;
  57. $curr = WbwTemplate::where('book', $id[0])
  58. ->where('style', 'paranum')
  59. ->whereIn('word', $csList)
  60. ->whereBetween('paragraph', [$begin, $end])
  61. ->orderBy('paragraph')
  62. ->select('book', 'paragraph')->get();
  63. if (! $curr) {
  64. return $this->error('没有找到段落'.$id, 404, 404);
  65. }
  66. $data = [];
  67. $data['curr'] = new NavCSParaResource($curr[0]);
  68. $next = WbwTemplate::where('book', $id[0])
  69. ->where('style', 'paranum')
  70. ->where('word', (int) end($csPara) + 1)
  71. ->whereBetween('paragraph', [$begin, $end])
  72. ->select('book', 'paragraph')->first();
  73. if ($next) {
  74. $data['next'] = new NavCSParaResource($next);
  75. $data['end'] = $next->paragraph - 1;
  76. } else {
  77. $data['end'] = $end;
  78. }
  79. $prev = WbwTemplate::where('book', $id[0])
  80. ->where('style', 'paranum')
  81. ->where('word', (int) $csPara[0] - 1)
  82. ->whereBetween('paragraph', [$begin, $end])
  83. ->select('book', 'paragraph')->first();
  84. if ($prev) {
  85. $data['prev'] = new NavCSParaResource($prev);
  86. }
  87. return $this->ok($data);
  88. }
  89. /**
  90. * Update the specified resource in storage.
  91. *
  92. * @return Response
  93. */
  94. public function update(Request $request, WbwTemplate $wbwTemplate)
  95. {
  96. //
  97. }
  98. /**
  99. * Remove the specified resource from storage.
  100. *
  101. * @return Response
  102. */
  103. public function destroy(WbwTemplate $wbwTemplate)
  104. {
  105. //
  106. }
  107. }