ChannelRecommendBooksService.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Modules\Channel\Services;
  3. use App\Modules\Channel\Models\ChannelRecommendBooks;
  4. class ChannelRecommendBooksService
  5. {
  6. static function getRecommendBooks($channel_id,$id,$limit = 3,$data = [])
  7. {
  8. if(!isset($channel_id)) return [];
  9. $query = ChannelRecommendBooks::leftjoin('book_configs','book_configs.bid','channel_recommend_books.bid')
  10. ->leftjoin('books','books.id','channel_recommend_books.bid')
  11. ->select('books.intro','books.category_name','book_configs.book_name','book_configs.cover','book_configs.bid')
  12. ->orderBy('channel_recommend_books.priority','DESC')->orderBy('channel_recommend_books.created_at','DESC');
  13. $query->where('channel_recommend_books.channel_id',$channel_id);
  14. if(!empty($id) && $id != 0){
  15. $query->where('channel_recommend_books.id','<',$id);
  16. }
  17. $list = $query->limit($limit)->get()->toArray();
  18. \Log::info('count:$limit:'.$limit);
  19. \Log::info($list);
  20. \Log::info($data);
  21. $data = array_merge($list,$data);
  22. if(count($data) < 3){
  23. $count = 3 - count($data);
  24. \Log::info('count:'.$count);
  25. self::getRecommendBooks($channel_id,0,$count,$data);
  26. }
  27. \Log::info($data);
  28. return $data;
  29. }
  30. static function incrRecommendNum($channel_id,$bids)
  31. {
  32. return ChannelRecommendBooks::where('channel_id',$channel_id)->whereIn('bid',$bids)->increment('recommend_num');
  33. }
  34. }