ChannelRecommendBooksService.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. $lists = array_merge($list,$data);
  19. \Log::info('merge:list');
  20. \Log::info($lists);
  21. if(count($lists) < 3){
  22. $count = 3 - count($lists);
  23. \Log::info('count:'.$count);
  24. self::getRecommendBooks($channel_id,0,$count,$lists);
  25. }
  26. \Log::info('return:lists');
  27. \Log::info($lists);
  28. return $lists;
  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. }