<?php

namespace App\Console\Commands\SuperiorBooks;

use App\Modules\Book\Models\BookConfig;
use App\Modules\Book\Services\SuperiorNewBookService;
use DB;
use Illuminate\Console\Command;

class UpdateHighQualityBooks extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_high_quality_books';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新优质书库到Redis';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $high_quality_books = BookConfig::join('books','books.id','=','book_configs.bid')
            ->join('book_categories','book_categories.id','=','books.category_id')
            ->where('is_high_quality',1)
            ->whereIn('is_on_shelf',[1,2])
            ->select('book_configs.bid','book_categories.pid')
            ->get();
            //->pluck('bid');
        $male = $female = [];
        foreach ($high_quality_books as $high_quality_book) {
            if ($high_quality_book->pid == 1) {
                $male[] = $high_quality_book->bid;
            }

            if ($high_quality_book->pid == 2) {
                $female[] = $high_quality_book->bid;
            }
        }
        \Redis::del('male_high_reco_bids','female_high_reco_bids');
        \Redis::sadd('male_high_reco_bids',$male);
        \Redis::sadd('female_high_reco_bids',$female);

    }
}
