ChannelRecommendBooksService.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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,$bid,$priority,$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','channel_recommend_books.priority')
  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($priority) && $priority != 0){
  15. $query->where('channel_recommend_books.priority','<=',$priority)->where('channel_recommend_books.bid','!=',$bid);
  16. }
  17. $list = $query->limit($limit)->get()->toArray();
  18. $lists = array_merge($list,$data);
  19. if(count($lists) < 3){
  20. $count = 3 - count($lists);
  21. self::getRecommendBooks($channel_id,$bid,0,$count,$lists);
  22. }else{
  23. return $lists;
  24. }
  25. }
  26. static function incrRecommendNum($channel_id,$bids)
  27. {
  28. return ChannelRecommendBooks::where('channel_id',$channel_id)->whereIn('bid',$bids)->increment('recommend_num');
  29. }
  30. }