SearchPaliWbwController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\WbwTemplate;
  6. use App\Http\Resources\SearchPaliWbwResource;
  7. use App\Http\Resources\SearchBookResource;
  8. class SearchPaliWbwController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. //获取书的范围
  18. $bookId = [];
  19. $search = new SearchController;
  20. if($request->has('book')){
  21. foreach (explode(',',$request->get('book')) as $key => $id) {
  22. $bookId[] = (int)$id;
  23. }
  24. }else if($request->has('tags')){
  25. //查询搜索范围
  26. //查询搜索范围
  27. $tagItems = explode(';',$request->get('tags'));
  28. foreach ($tagItems as $tagItem) {
  29. $bookId = array_merge($bookId,$search->getBookIdByTags(explode(',',$tagItem)));
  30. }
  31. }
  32. $keyWords = explode(',',$request->get('key'));
  33. $table = WbwTemplate::whereIn('real',$keyWords)
  34. ->groupBy(['book','paragraph'])
  35. ->selectRaw('book,paragraph,sum(weight) as rank');
  36. $whereBold = '';
  37. if($request->get('bold')==='on'){
  38. $table = $table->where('style','bld');
  39. $whereBold = " and style='bld'";
  40. }else if($request->get('bold')==='off'){
  41. $table = $table->where('style','<>','bld');
  42. $whereBold = " and style <> 'bld'";
  43. }
  44. $placeholderWord = implode(",",array_fill(0, count($keyWords), '?')) ;
  45. $whereWord = "real in ({$placeholderWord})";
  46. $whereBookId = '';
  47. if(count($bookId)>0){
  48. $table = $table->whereIn('pcd_book_id',$bookId);
  49. $placeholderBookId = implode(",",array_fill(0, count($bookId), '?')) ;
  50. $whereBookId = " and pcd_book_id in ({$placeholderBookId}) ";
  51. }
  52. $queryCount = "SELECT count(*) FROM ( SELECT book,paragraph FROM wbw_templates WHERE $whereWord $whereBookId $whereBold GROUP BY book,paragraph) T;";
  53. $count = DB::select($queryCount,array_merge($keyWords,$bookId));
  54. $table = $table->orderBy('rank','desc');
  55. $table = $table->skip($request->get("offset",0))
  56. ->take($request->get('limit',10));
  57. $result = $table->get();
  58. return $this->ok([
  59. "rows"=>SearchPaliWbwResource::collection($result),
  60. "count"=>$count[0]->count,
  61. ]);
  62. }
  63. public function book_list(Request $request){
  64. //获取书的范围
  65. $bookId = [];
  66. $search = new SearchController;
  67. if($request->has('tags')){
  68. //查询搜索范围
  69. //查询搜索范围
  70. $tagItems = explode(';',$request->get('tags'));
  71. foreach ($tagItems as $tagItem) {
  72. $bookId = array_merge($bookId,$search->getBookIdByTags(explode(',',$tagItem)));
  73. }
  74. }
  75. $keyWords = explode(',',$request->get('key'));
  76. $table = WbwTemplate::whereIn('real',$keyWords);
  77. if(count($bookId)>0){
  78. $table = $table->whereIn('pcd_book_id',$bookId);
  79. }
  80. $table = $table->groupBy('pcd_book_id')
  81. ->selectRaw('pcd_book_id,count(*) as co')
  82. ->orderBy('co','desc');
  83. $result = $table->get();
  84. return $this->ok(["rows"=>SearchBookResource::collection($result),"count"=>count($result)]);
  85. }
  86. /**
  87. * Store a newly created resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function store(Request $request)
  93. {
  94. return $this->index($request);
  95. }
  96. /**
  97. * Display the specified resource.
  98. *
  99. * @param \App\Models\WbwTemplate $wbwTemplate
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function show(WbwTemplate $wbwTemplate)
  103. {
  104. //
  105. }
  106. /**
  107. * Update the specified resource in storage.
  108. *
  109. * @param \Illuminate\Http\Request $request
  110. * @param \App\Models\WbwTemplate $wbwTemplate
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function update(Request $request, WbwTemplate $wbwTemplate)
  114. {
  115. //
  116. }
  117. /**
  118. * Remove the specified resource from storage.
  119. *
  120. * @param \App\Models\WbwTemplate $wbwTemplate
  121. * @return \Illuminate\Http\Response
  122. */
  123. public function destroy(WbwTemplate $wbwTemplate)
  124. {
  125. //
  126. }
  127. }