ChannelRecommendBooksService.php 2.8 KB

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