BookAuditService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if ($sex == 'male') {
  27. $channel = 1;
  28. } else {
  29. $channel = 2;
  30. }
  31. // 模块
  32. [$hot, $live, $recom, $new] = [$home['hot'], $home['zhibo'], $home['recom'], $home['new_recom']];
  33. [$hotBids, $liveBids, $recomBids, $newBids] = [BookConfigService::getCheckBooks($hot[$sex],$channel,$package,$is_auth),BookConfigService::getCheckBooks($live[$sex],$channel,$package,$is_auth) , BookConfigService::getCheckBooks($recom[$sex],$channel,$package,$is_auth), BookConfigService::getCheckBooks($new[$sex],$channel,$package,$is_auth)];
  34. // 一次性获取书籍列表
  35. $bids = array_merge($hotBids, $liveBids, $recomBids, $newBids);
  36. $channel_id = is_public_package($package) ? get_default_public_channel_id() : 0;
  37. $books = BookConfigService::getBookLists(compact('bids','channel_id'));
  38. return [
  39. [
  40. 'type' => 'reco_banner',
  41. 'lable' => $banner['label'],
  42. 'books' => $bannerBooks
  43. ],
  44. [
  45. 'type' => 'hot',
  46. 'lable' => $hot['label'],
  47. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $hotBids)->all())
  48. ],
  49. [
  50. 'type' => 'zhibo',
  51. 'lable' => $live['label'],
  52. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $liveBids)->all())
  53. ],
  54. [
  55. 'type' => 'recom',
  56. 'lable' => $recom['label'],
  57. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $recomBids)->all())
  58. ],
  59. [
  60. 'type' => 'new_recom',
  61. 'lable' => $new['label'],
  62. 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $newBids)->all())
  63. ],
  64. ];
  65. }
  66. public static function getYueaiHomeBooksData($sex, $package,$is_auth,$channel): array
  67. {
  68. $home = config('home.yueai');
  69. if (!$is_auth){
  70. $home = config('home.ycsd');
  71. }
  72. $banner = $home['reco_banner'];
  73. $bannerBooks = $banner[$sex];
  74. // 模块
  75. [$hot, $live, $recom, $new] = [$home['hot'], $home['zhibo'], $home['recom'], $home['new_recom']];
  76. [$hotBids, $liveBids, $recomBids, $newBids] = [$hot[$sex], $live[$sex], $recom[$sex], $new[$sex]];
  77. return [
  78. [
  79. 'type' => 'reco_banner',
  80. 'lable' => $banner['label'],
  81. 'books' => $bannerBooks
  82. ],
  83. [
  84. 'type' => 'hot',
  85. 'lable' => $hot['label'],
  86. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($hotBids,$channel,$package,$is_auth),[],false)),
  87. ],
  88. [
  89. 'type' => 'zhibo',
  90. 'lable' => $live['label'],
  91. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($liveBids,$channel,$package,$is_auth),[],false)),
  92. ],
  93. [
  94. 'type' => 'recom',
  95. 'lable' => $recom['label'],
  96. // 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $recomBids)->all())
  97. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($recomBids,$channel,$package,$is_auth),[],false)),
  98. ],
  99. [
  100. 'type' => 'new_recom',
  101. 'lable' => $new['label'],
  102. // 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $newBids)->all())
  103. 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($newBids,$channel,$package,$is_auth),[],false)),
  104. ],
  105. ];
  106. }
  107. private static function getCheckBooks($bid_list,$channel,$package,$is_author)
  108. {
  109. $hidden_cp = getHiddenCp($package);
  110. // if(!is_public_package($package)){
  111. // $hidden_cp = array_merge($hidden_cp,['lianshang']);
  112. // }
  113. //获取书本数量
  114. $count = count($bid_list);
  115. $where = [
  116. ['book_configs.charge_type','!=','BOOK'],
  117. ];
  118. //获取当前有效书本数量
  119. $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  120. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  121. ->whereIn('book_configs.bid',$bid_list)
  122. ->whereIn('book_configs.is_on_shelf',[1,2])
  123. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  124. ->where($where)
  125. ->where('book_categories.pid',$channel)
  126. ->count();
  127. if($count == $book_count){
  128. return $bid_list;
  129. }
  130. //获取需要补充的书籍数量
  131. $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  132. if($supplement_count <= 0){
  133. return $bid_list;
  134. }
  135. //获取书籍交集bid,过滤掉不符合要求的书
  136. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  137. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  138. ->whereIn('book_configs.bid',$bid_list)
  139. ->whereIn('book_configs.is_on_shelf',[1,2])
  140. ->where($where)
  141. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  142. ->where('book_categories.pid',$channel)
  143. ->pluck('book_configs.bid')->all();
  144. $bid_list = array_intersect($bid_list,$bids);
  145. //获取随机的有效的书籍bid
  146. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  147. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  148. ->where('book_configs.is_on_shelf',2)
  149. // ->where('book_configs.charge_type','!=','BOOK')
  150. ->where($where)
  151. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  152. ->where('book_categories.pid',$channel)
  153. ->orderBy('book_configs.recommend_index', 'desc')
  154. ->limit($supplement_count)
  155. ->get()->pluck('bid')->toArray();
  156. return array_filter(array_merge($bid_list,$rand_bid));
  157. }
  158. }