NavCSParaController.php 3.2 KB

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