<?php


namespace App\Modules\Book\Services;


use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
use App\Modules\Book\Models\BookConfig;

class BookAuditService
{
    /**
     * 审核数据
     * @param $sex
     * @param $package
     * @return array[]
     */
    public static function getHomeBooksData($sex, $package,$is_auth): array
    {
        // 基本配置数据
        $home = config('home.default');
        if ($package === 'com.app.kyy.jdqyy') {
            $home = config('home.new');
        }

        if (!$is_auth){
            $home = config('home.ycsd');
        }

        // banner
        $banner      = $home['reco_banner'];
        $bannerBooks = $banner[$sex];
        if ($sex == 'male') {
            $channel          = 1;
        } else {
            $channel          = 2;
        }

        // 模块
        [$hot, $live, $recom, $new] = [$home['hot'], $home['zhibo'], $home['recom'], $home['new_recom']];
        [$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)];

        // 一次性获取书籍列表
        $bids  = array_merge($hotBids, $liveBids, $recomBids, $newBids);
        $channel_id = is_public_package($package) ? get_default_public_channel_id() : 0;
        $books = BookConfigService::getBookLists(compact('bids','channel_id'));

        return [
            [
                'type'  => 'reco_banner',
                'lable' => $banner['label'],
                'books' => $bannerBooks
            ],
            [
                'type'  => 'hot',
                'lable' => $hot['label'],
                'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $hotBids)->all())
            ],
            [
                'type'  => 'zhibo',
                'lable' => $live['label'],
                'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $liveBids)->all())
            ],
            [
                'type'  => 'recom',
                'lable' => $recom['label'],
                'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $recomBids)->all())
            ],
            [
                'type'  => 'new_recom',
                'lable' => $new['label'],
                'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $newBids)->all())
            ],
        ];
    }

    public static function getYueaiHomeBooksData($sex, $package,$is_auth,$channel): array
    {
        $home = config('home.yueai');
        if (!$is_auth){
            $home = config('home.ycsd');
        }
        $banner      = $home['reco_banner'];
        $bannerBooks = $banner[$sex];

        // 模块
        [$hot, $live, $recom, $new] = [$home['hot'], $home['zhibo'], $home['recom'], $home['new_recom']];
        [$hotBids, $liveBids, $recomBids, $newBids] = [$hot[$sex], $live[$sex], $recom[$sex], $new[$sex]];

        return [
            [
                'type'  => 'reco_banner',
                'lable' => $banner['label'],
                'books' => $bannerBooks
            ],
            [
                'type'  => 'hot',
                'lable' => $hot['label'],
                'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($hotBids,$channel,$package,$is_auth),[],false)),
            ],
            [
                'type'  => 'zhibo',
                'lable' => $live['label'],
                'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($liveBids,$channel,$package,$is_auth),[],false)),
            ],
            [
                'type'  => 'recom',
                'lable' => $recom['label'],
                // 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $recomBids)->all())
                 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($recomBids,$channel,$package,$is_auth),[],false)),

            ],
            [
                'type'  => 'new_recom',
                'lable' => $new['label'],
                // 'books' => collectionTransform(new BookTransformer, collect($books)->whereIn('bid', $newBids)->all())
                'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(self::getCheckBooks($newBids,$channel,$package,$is_auth),[],false)),
            ],
        ];
    }

    private static function getCheckBooks($bid_list,$channel,$package,$is_author)
    {
        $hidden_cp = getHiddenCp();
        if(!is_public_package($package)){
            $hidden_cp = array_merge($hidden_cp,['lianshang']);
        }
        //获取书本数量
        $count = count($bid_list);
        $where = [
            ['book_configs.charge_type','!=','BOOK'],
        ];
        //获取当前有效书本数量
        $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
            ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
            ->whereIn('book_configs.bid',$bid_list)
            ->whereIn('book_configs.is_on_shelf',[1,2])
            ->whereNotIn('book_configs.cp_source',$hidden_cp)
            ->where($where)
            ->where('book_categories.pid',$channel)
            ->count();
        if($count == $book_count){
            return $bid_list;
        }
        //获取需要补充的书籍数量
        $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
        if($supplement_count <= 0){
            return $bid_list;
        }
        //获取书籍交集bid，过滤掉不符合要求的书
        $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
            ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
            ->whereIn('book_configs.bid',$bid_list)
            ->whereIn('book_configs.is_on_shelf',[1,2])
            ->where($where)
            ->whereNotIn('book_configs.cp_source',$hidden_cp)
            ->where('book_categories.pid',$channel)
            ->pluck('book_configs.bid')->all();
        $bid_list = array_intersect($bid_list,$bids);

        //获取随机的有效的书籍bid
        $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
            ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
            ->where('book_configs.is_on_shelf',2)
            // ->where('book_configs.charge_type','!=','BOOK')
            ->where($where)
            ->whereNotIn('book_configs.cp_source',$hidden_cp)

            ->where('book_categories.pid',$channel)
            ->inRandomOrder()
            ->limit($supplement_count)
            ->get()->pluck('bid')->toArray();

        return array_filter(array_merge($bid_list,$rand_bid));
    }
}