<?php

namespace App\Console\Commands\SuperiorBooks;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '扫描新上架优质书，加入榜单';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $month_ago = date('Y-m-d',strtotime(' -30 day'));
        $new_books = DB::table('book_configs as bc')
                ->leftjoin('books as b','b.id','=','bc.bid')
                ->leftjoin('book_categories as bcs','bcs.id','=','b.category_id')
                ->select('bc.bid','bcs.pid','b.category_id','bc.recommend_index')
                ->where([
                    ['bc.created_at','>=',$month_ago.' 00:00:00'],
                    ['bc.created_at','<=',date('Y-m-d')],
                    ['bc.recommend_index','>=',90]
                ])
                ->orderBy('bc.created_at','asc')
                ->get();
        \Log::info('new_books:'.json_encode($new_books));
        foreach ($new_books as $new_book){
            try{
                SuperiorNewBookService::addSuperiorItem($new_book);
            }catch (\Exception $e){
                \Log::error('优质新书库加入失败！'.($e->getMessage()));
            }
        }
    }
}
