ChannelRecommendBooksService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Modules\Channel\Services;
  3. use App\Modules\Channel\Models\ChannelRecommendBooks;
  4. class ChannelRecommendBooksService
  5. {
  6. static function getChannelBid($channel_id)
  7. {
  8. return ChannelRecommendBooks::leftjoin('book_configs','book_configs.bid','channel_recommend_books.bid')
  9. ->where('channel_recommend_books.channel_id',$channel_id)
  10. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  11. ->whereIn('book_configs.is_on_shelf',[1,2])
  12. ->orderBy('channel_recommend_books.priority','DESC')
  13. ->orderBy('channel_recommend_books.created_at','DESC')
  14. ->pluck('channel_recommend_books.bid')->all();
  15. }
  16. static function getMergerBids($channel_id,$bid)
  17. {
  18. $bids = self::getChannelBid($channel_id);
  19. if(isset($bid) && $bid != 0){
  20. $key = array_keys($bids,$bid);
  21. if(!empty($key)){
  22. $value = 1;
  23. if(isset($bids[$key[0]+1])){
  24. $bidArr[] = $bids[$key[0]+1];
  25. }else{
  26. $bidArr[] = $bids[$value];
  27. $value += 1;
  28. }
  29. if(isset($bids[$key[0]+2])){
  30. $bidArr[] = $bids[$key[0]+2];
  31. }else{
  32. $bidArr[] = $bids[$value-1];
  33. $value += 1;
  34. }
  35. if(isset($bids[$key[0]+3])){
  36. $bidArr[] = $bids[$key[0]+3];
  37. }else{
  38. $bidArr[] = $bids[$value-1];
  39. }
  40. }else{
  41. $bidArr = [$bids[0],$bids[1],$bids[2]];
  42. }
  43. }else{
  44. $bidArr = [$bids[0],$bids[1],$bids[2]];
  45. }
  46. return $bidArr;
  47. }
  48. static function getRecommendBooks($channel_id,$bid = 0)
  49. {
  50. $bids = self::getMergerBids($channel_id,$bid);
  51. if(!isset($channel_id)) return [];
  52. $query = ChannelRecommendBooks::leftjoin('book_configs','book_configs.bid','channel_recommend_books.bid')
  53. ->leftjoin('books','books.id','channel_recommend_books.bid')
  54. ->select('books.intro','books.category_name','book_configs.book_name','book_configs.cover','book_configs.bid','channel_recommend_books.priority')
  55. ->orderBy('channel_recommend_books.priority','DESC')->orderBy('channel_recommend_books.created_at','DESC');
  56. $query->where('channel_recommend_books.channel_id',$channel_id)
  57. ->whereIn('channel_recommend_books.bid',$bids);
  58. return $query->get()->toArray();
  59. }
  60. static function incrRecommendNum($channel_id,$bids)
  61. {
  62. return ChannelRecommendBooks::where('channel_id',$channel_id)->whereIn('bid',$bids)->increment('recommend_num');
  63. }
  64. }