SearchPaliWbwController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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->input('book')) as $key => $id) {
  22. $bookId[] = (int)$id;
  23. }
  24. } else if ($request->has('tags')) {
  25. //查询搜索范围
  26. //查询搜索范围
  27. $tagItems = explode(';', $request->input('tags'));
  28. foreach ($tagItems as $tagItem) {
  29. $bookId = array_merge($bookId, $search->getBookIdByTags(explode(',', $tagItem)));
  30. }
  31. }
  32. $keyWords = explode(',', $request->input('key'));
  33. $table = WbwTemplate::whereIn('real', $keyWords)
  34. ->groupBy(['book', 'paragraph'])
  35. ->selectRaw('book,paragraph,sum(weight) as rank');
  36. $whereBold = '';
  37. if ($request->input('bold') === 'on') {
  38. $table = $table->where('style', 'bld');
  39. $whereBold = " and style='bld'";
  40. } else if ($request->input('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->input("offset", 0))
  56. ->take($request->input('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. //获取书的范围
  66. $bookId = [];
  67. $search = new SearchController;
  68. if ($request->has('tags')) {
  69. //查询搜索范围
  70. //查询搜索范围
  71. $tagItems = explode(';', $request->input('tags'));
  72. foreach ($tagItems as $tagItem) {
  73. $bookId = array_merge($bookId, $search->getBookIdByTags(explode(',', $tagItem)));
  74. }
  75. }
  76. $keyWords = explode(',', $request->input('key'));
  77. $table = WbwTemplate::whereIn('real', $keyWords);
  78. if (count($bookId) > 0) {
  79. $table = $table->whereIn('pcd_book_id', $bookId);
  80. }
  81. $table = $table->groupBy('pcd_book_id')
  82. ->selectRaw('pcd_book_id,count(*) as co')
  83. ->orderBy('co', 'desc');
  84. $result = $table->get();
  85. return $this->ok(["rows" => SearchBookResource::collection($result), "count" => count($result)]);
  86. }
  87. /**
  88. * Store a newly created resource in storage.
  89. *
  90. * @param \Illuminate\Http\Request $request
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function store(Request $request)
  94. {
  95. return $this->index($request);
  96. }
  97. /**
  98. * Display the specified resource.
  99. *
  100. * @param \App\Models\WbwTemplate $wbwTemplate
  101. * @return \Illuminate\Http\Response
  102. */
  103. public function show(WbwTemplate $wbwTemplate)
  104. {
  105. //
  106. }
  107. /**
  108. * Update the specified resource in storage.
  109. *
  110. * @param \Illuminate\Http\Request $request
  111. * @param \App\Models\WbwTemplate $wbwTemplate
  112. * @return \Illuminate\Http\Response
  113. */
  114. public function update(Request $request, WbwTemplate $wbwTemplate)
  115. {
  116. //
  117. }
  118. /**
  119. * Remove the specified resource from storage.
  120. *
  121. * @param \App\Models\WbwTemplate $wbwTemplate
  122. * @return \Illuminate\Http\Response
  123. */
  124. public function destroy(WbwTemplate $wbwTemplate)
  125. {
  126. //
  127. }
  128. }