<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Redis;
use App\Modules\Book\Models\BookConfig;
use DB;
use Log;
class UpdateBookClickCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book:clicknumupdate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->update();
    }

    public function update(){

        $field = date('Y-m-d',time()-86400);
        $redis_key = 'book_click_num_bid_%s';
        BookConfig::select('bid')->get()->map(function ($item, $key)use ($redis_key,$field){
            $redis_bid_key = sprintf($redis_key,$item->bid);
            $num = Redis::hget($redis_bid_key,$field);
            if(!$num)$num = 0;
            try{
                DB::table('books_click_rates')->insert(['bid'=>$item->bid,'count'=>$num,'day'=>$field,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]);
                Redis::hdel($redis_bid_key,$field);
            }catch (\Exception $e){

            }


        });
    }
}
