BookAuditService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Modules\Book\Services;
  3. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  4. use App\Modules\Book\Models\BookConfig;
  5. class BookAuditService
  6. {
  7. /**
  8. * 审核数据
  9. * @param $sex
  10. * @param $package
  11. * @return array[]
  12. */
  13. public static function getHomeBooksData($sex, $package,$is_auth): array
  14. {
  15. // 基本配置数据
  16. $home = config('home.default');
  17. if ($package === 'com.app.kyy.jdqyy') {
  18. $home = config('home.new');
  19. }
  20. if (!$is_auth){
  21. $home = config('home.ycsd');
  22. }
  23. // banner
  24. $banner = $home['reco_banner'];
  25. $bannerBooks = $banner[$sex];
  26. // 模块
  27. [$hot, $live, $recom, $new] = [$home['hot'], $home['zhibo'], $home['recom'], $home['new_recom']];
  28. [$hotBids, $liveBids, $recomBids, $newBids] = [$hot[$sex], $live[$sex], $recom[$sex], $new[$sex]];
  29. // 一次性获取书籍列表
  30. $bids = array_merge($hotBids, $liveBids, $recomBids, $newBids);
  31. $channel_id = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  32. $books = BookConfigService::getBookLists(compact('bids','channel_id'));
  33. return [
  34. [
  35. 'type' => 'reco_banner',
  36. 'lable' => $banner['label'],
  37. 'books' => $bannerBooks
  38. ],
  39. [
  40. 'type' => 'hot',
  41. 'lable' => $hot['label'],
  42. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $hotBids)->all())
  43. ],
  44. [
  45. 'type' => 'zhibo',
  46. 'lable' => $live['label'],
  47. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $liveBids)->all())
  48. ],
  49. [
  50. 'type' => 'recom',
  51. 'lable' => $recom['label'],
  52. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $recomBids)->all())
  53. ],
  54. [
  55. 'type' => 'new_recom',
  56. 'lable' => $new['label'],
  57. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $newBids)->all())
  58. ],
  59. ];
  60. }
  61. public static function getYueaiHomeBooksData($sex, $package,$is_auth,$channel): array
  62. {
  63. $home = config('home.yueai');
  64. if (!$is_auth){
  65. $home = config('home.ycsd');
  66. }
  67. $banner = $home['reco_banner'];
  68. $bannerBooks = $banner[$sex];
  69. // 模块
  70. [$hot, $live, $recom, $new] = [$home['hot'], $home['zhibo'], $home['recom'], $home['new_recom']];
  71. [$hotBids, $liveBids, $recomBids, $newBids] = [$hot[$sex], $live[$sex], $recom[$sex], $new[$sex]];
  72. return [
  73. [
  74. 'type' => 'reco_banner',
  75. 'lable' => $banner['label'],
  76. 'books' => $bannerBooks
  77. ],
  78. [
  79. 'type' => 'hot',
  80. 'lable' => $hot['label'],
  81. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($hotBids,$channel,$package,$is_auth),[],false)),
  82. ],
  83. [
  84. 'type' => 'zhibo',
  85. 'lable' => $live['label'],
  86. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($liveBids,$channel,$package,$is_auth),[],false)),
  87. ],
  88. [
  89. 'type' => 'recom',
  90. 'lable' => $recom['label'],
  91. // 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $recomBids)->all())
  92. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($recomBids,$channel,$package,$is_auth),[],false)),
  93. ],
  94. [
  95. 'type' => 'new_recom',
  96. 'lable' => $new['label'],
  97. // 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $newBids)->all())
  98. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($newBids,$channel,$package,$is_auth),[],false)),
  99. ],
  100. ];
  101. }
  102. private static function getCheckBooks($bid_list,$channel,$package,$is_author)
  103. {
  104. $hidden_cp = getHiddenCp();
  105. if($package !== 'com.beidao.kuaiying.zsy'){
  106. $hidden_cp = array_merge($hidden_cp,['lianshang']);
  107. }
  108. //获取书本数量
  109. $count = count($bid_list);
  110. $where = [
  111. ['book_configs.charge_type','!=','BOOK'],
  112. ];
  113. //获取当前有效书本数量
  114. $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  115. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  116. ->whereIn('book_configs.bid',$bid_list)
  117. ->whereIn('book_configs.is_on_shelf',[1,2])
  118. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  119. ->where($where)
  120. ->where('book_categories.pid',$channel)
  121. ->count();
  122. if($count == $book_count){
  123. return $bid_list;
  124. }
  125. //获取需要补充的书籍数量
  126. $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  127. if($supplement_count <= 0){
  128. return $bid_list;
  129. }
  130. //获取书籍交集bid,过滤掉不符合要求的书
  131. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  132. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  133. ->whereIn('book_configs.bid',$bid_list)
  134. ->whereIn('book_configs.is_on_shelf',[1,2])
  135. ->where($where)
  136. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  137. ->where('book_categories.pid',$channel)
  138. ->pluck('book_configs.bid')->all();
  139. $bid_list = array_intersect($bid_list,$bids);
  140. //获取随机的有效的书籍bid
  141. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  142. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  143. ->where('book_configs.is_on_shelf',2)
  144. // ->where('book_configs.charge_type','!=','BOOK')
  145. ->where($where)
  146. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  147. ->where('book_categories.pid',$channel)
  148. ->inRandomOrder()
  149. ->limit($supplement_count)
  150. ->get()->pluck('bid')->toArray();
  151. return array_filter(array_merge($bid_list,$rand_bid));
  152. }
  153. }