Pārlūkot izejas kodu

'首页随机补充已下架的书籍'

zhuchengjie 3 gadi atpakaļ
vecāks
revīzija
fa48911d71
1 mainītis faili ar 26 papildinājumiem un 4 dzēšanām
  1. 26 4
      app/Http/Controllers/QuickApp/Book/BookController.php

+ 26 - 4
app/Http/Controllers/QuickApp/Book/BookController.php

@@ -236,10 +236,10 @@ class BookController extends BaseController
      */
     private function getCheckBids($channel,$books,$package_id)
     {
-        $hotBids   = QappRecommendService::getRecommendByPacketId($channel, 'hot',$package_id);
-        $liveBids  = QappRecommendService::getRecommendByPacketId($channel, 'live',$package_id);
-        $recomBids = QappRecommendService::getRecommendByPacketId($channel, 'recom',$package_id);
-        $newBids   = QappRecommendService::getRecommendByPacketId($channel, 'new_recom',$package_id);
+        $hotBids   = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'hot',$package_id));
+        $liveBids  = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'live',$package_id));
+        $recomBids = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'recom',$package_id));
+        $newBids   = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'new_recom',$package_id));
 
         return array_filter([
             ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
@@ -251,6 +251,28 @@ class BookController extends BaseController
 
     }
 
+    /**
+     * 补充不满足条件的书籍id
+     * @param $bid_list
+     * @return array
+     */
+    private function getCheckBooks($bid_list)
+    {
+        //获取书本数量
+        $count = count($bid_list);
+        //获取当前有效书本数量
+        $book_count = BookConfig::whereIn('bid',$bid_list)->whereNotIn('cp_source',getHiddenCp())->count();
+        if($count == $book_count){
+            return $bid_list;
+        }
+        //获取需要补充的书籍数量
+        $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
+
+        //获取随机的有效的书籍bid
+        $rand_bid = BookConfig::where('is_on_shelf',1)->whereNotIn('cp_source',getHiddenCp())->inRandomOrder()->limit($supplement_count)->get()->pluck('bid')->toArray();
+
+        return array_merge($bid_list,$rand_bid);
+    }
 
     private function getBidCidFromUrl(string $url)
     {