BookAuditService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 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. if (!$is_author){
  111. $where = [
  112. ['book_configs.charge_type','!=','BOOK'],
  113. ['book_configs.cp_source','=','ycsd'],
  114. ];
  115. }else{
  116. $where = [
  117. ['book_configs.charge_type','!=','BOOK'],
  118. ];
  119. }
  120. //获取当前有效书本数量
  121. $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  122. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  123. ->whereIn('book_configs.bid',$bid_list)
  124. ->where('book_configs.is_on_shelf','in',[1,2])
  125. ->where('book_configs.charge_type','!=','BOOK')
  126. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  127. ->where($where)
  128. ->where('book_categories.pid',$channel)
  129. ->count();
  130. if($count == $book_count){
  131. return $bid_list;
  132. }
  133. //获取需要补充的书籍数量
  134. $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  135. if($supplement_count <= 0){
  136. return $bid_list;
  137. }
  138. //获取书籍交集bid,过滤掉不符合要求的书
  139. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  140. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  141. ->whereIn('book_configs.bid',$bid_list)
  142. ->where('book_configs.is_on_shelf','in',[1,2])
  143. ->where($where)
  144. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  145. ->where('book_categories.pid',$channel)
  146. ->pluck('book_configs.bid')->all();
  147. $bid_list = array_intersect($bid_list,$bids);
  148. //获取随机的有效的书籍bid
  149. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  150. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  151. ->where('book_configs.is_on_shelf',2)
  152. // ->where('book_configs.charge_type','!=','BOOK')
  153. ->where($where)
  154. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  155. ->where('book_categories.pid',$channel)
  156. ->inRandomOrder()
  157. ->limit($supplement_count)
  158. ->get()->pluck('bid')->toArray();
  159. return array_filter(array_merge($bid_list,$rand_bid));
  160. }
  161. }