<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2020/10/21
 * Time: 11:05
 */

namespace App\Console\Commands\Book;

use DB;
use Redis;
use Illuminate\Console\Command;

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

    /**
     * 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(){
        $day = date('Y-m-d',time()-86400);
        $this->start($day);
    }


    private function start($day){
        $max_bid_info = DB::table('books')->select('id')->orderBy('id','desc')->first();
        $max_bid = $max_bid_info->id;
        $data = [];
        for($i = 1;$i <= $max_bid;$i++){
            $amount = Redis::hget('book:charge:stats:'.$day,$i);
            if(!$amount) continue;
            $data[] = ['bid'=>$i,'day'=>$day,'amount'=>$amount,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
            if(count($data) % 100 == 0){
                DB::table('book_charge_statistical')->insert($data);
                $data = [];
            }
        }
        if($data){
            DB::table('book_charge_statistical')->insert($data);
        }
        Redis::del('book:charge:stats:'.$day);
    }
}