NavCSParaController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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
  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. //cs 段落号列表
  50. $csPara = explode('-',$id[2]);
  51. $csList = [];
  52. for ($i=(int)$csPara[0]; $i <=(int)end($csPara) ; $i++) {
  53. $csList[] = $i;
  54. }
  55. //段落区间
  56. $begin = $id[1];
  57. $end = (int)$id[1] + $para->chapter_len;
  58. $curr = WbwTemplate::where('book',$id[0])
  59. ->where('style','paranum')
  60. ->whereIn('word',$csList)
  61. ->whereBetween('paragraph',[$begin,$end])
  62. ->orderBy('paragraph')
  63. ->select('book','paragraph')->get();
  64. if(!$curr){
  65. return $this->error('没有找到段落'.$id,404,404);
  66. }
  67. $data = [];
  68. $data['curr'] = new NavCSParaResource($curr[0]);
  69. $next = WbwTemplate::where('book',$id[0])
  70. ->where('style','paranum')
  71. ->where('word',(int)end($csPara)+1)
  72. ->whereBetween('paragraph',[$begin,$end])
  73. ->select('book','paragraph')->first();
  74. if($next){
  75. $data['next'] = new NavCSParaResource($next);
  76. $data['end'] = $next->paragraph -1;
  77. }else{
  78. $data['end'] = $end;
  79. }
  80. $prev = WbwTemplate::where('book',$id[0])
  81. ->where('style','paranum')
  82. ->where('word',(int)$csPara[0]-1)
  83. ->whereBetween('paragraph',[$begin,$end])
  84. ->select('book','paragraph')->first();
  85. if($prev){
  86. $data['prev'] = new NavCSParaResource($prev);
  87. }
  88. return $this->ok($data);
  89. }
  90. /**
  91. * Update the specified resource in storage.
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @param \App\Models\WbwTemplate $wbwTemplate
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function update(Request $request, WbwTemplate $wbwTemplate)
  98. {
  99. //
  100. }
  101. /**
  102. * Remove the specified resource from storage.
  103. *
  104. * @param \App\Models\WbwTemplate $wbwTemplate
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function destroy(WbwTemplate $wbwTemplate)
  108. {
  109. //
  110. }
  111. }