<?php

namespace App\Console\Commands;

use App\Modules\Book\Models\Chapter;
use Illuminate\Console\Command;
use DB;
use Redis;

class BookAttr extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book:attr {--start=} {--end=}';

    /**
     * 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->tempReadRecord();
        //print_r($res);
        $start = $this->option('start');
        $end = $this->option('end');
        if($start && $end){
            while (strtotime($start)<=strtotime($end)){
                //echo $start.PHP_EOL;
                $this->sendOrderDayAttr($start);
                $start = date('Y-m-d',strtotime($start)+86400);
            }
        }else{
            $this->sendOrderDayAttr(date('Y-m-d',time()-86400));
            $this->sendOrderSevenDayAttr();
        }
    }

    private function sendOrderDayAttr($day){
        $res = DB::table('send_orders')
            ->where('created_at','>=',$day)
            ->where('created_at','<=',$day.' 23:59:59')
            ->select('book_id','distribution_channel_id',DB::raw('count(*) as counts'))
            ->groupBy('book_id')
            ->groupBy('distribution_channel_id')
            ->get();
        $data = [];
        foreach ($res as $v){
            if($v->book_id){
                $book_total_send_order_stats = DB::table('book_total_send_order_stats')->where('bid',$v->book_id)->first();
                if($book_total_send_order_stats){
                    DB::table('book_total_send_order_stats')->where('bid',$v->book_id)->increment('num',$v->counts,['updated_at'=>date('Y-m-d H:i:s')]);
                }else{
                    DB::table('book_total_send_order_stats')->insert([
                        'bid'=>$v->book_id,
                        'num'=>$v->counts,
                        'created_at'=>date('Y-m-d H:i:s'),
                        'updated_at'=>date('Y-m-d H:i:s')
                        ]);
                }
                $channel_book_total_send_order_stats =DB::table('channel_book_total_send_order_stats')
                ->where('bid',$v->book_id)
                ->where('distribution_channel_id',$v->distribution_channel_id)
                ->first();
                if($channel_book_total_send_order_stats){
                    DB::table('channel_book_total_send_order_stats')
                    ->where('bid',$v->book_id)
                    ->where('distribution_channel_id',$v->distribution_channel_id)
                    ->increment('num',$v->counts,['updated_at'=>date('Y-m-d H:i:s')]);
                }else{
                    DB::table('channel_book_total_send_order_stats')->insert([
                        'bid'=>$v->book_id,
                        'num'=>$v->counts,
                        'distribution_channel_id'=>$v->distribution_channel_id,
                        'created_at'=>date('Y-m-d H:i:s'),
                        'updated_at'=>date('Y-m-d H:i:s')
                        ]);
                }
                
                $data[] = [
                    'bid'=>$v->book_id,
                    'num'=>$v->counts,
                    'distribution_channel_id'=>$v->distribution_channel_id,
                    'day'=>$day,
                    'created_at'=>date('Y-m-d H:i:s'),
                    'updated_at'=>date('Y-m-d H:i:s')
                ] ;
            }
        }
        DB::table('book_send_order_stats')->insert($data);
    }

    private function sendOrderSevenDayAttr(){
        $result = DB::table('book_total_send_order_stats')->select('id','bid')->get();
        foreach ($result as  $value) {
            $num = DB::table('book_send_order_stats')->where('bid',$value->bid)->where('day','>=',date('Y-m-d', strtotime('-7 day')))->sum('num');
            DB::table('book_total_send_order_stats')->where('id',$value->id)->update(['seven_day_num'=>$num]); 
        }
    }

    private function tempReadRecord(){
        $data = [];
        $book_chapter_seq = [];
        $j = 1;
        for ($i = 10000;$i<=24139688;$i++){
            $read_bids = Redis::hgetall('book_read:' . $i);
            if(!$read_bids)
                continue;

            foreach ($read_bids as $key=>$v){
                if($key == 'last_read' || $key == 'send_order_id'){
                    continue;
                }
                if(!isset($book_chapter_seq[$key])){
                    $book_chapter_seq[$key] = $this->chapters($key);
                }
                $record = explode('_', $v);
                $cid = 0;
                if(isset($record[0])){
                    $cid = $record[0];
                }
                $seq = 0;
                if(isset($book_chapter_seq[$key][$cid])){
                    $seq = $book_chapter_seq[$key][$cid];
                }
                $data[]  = [
                    'bid'=>$key,
                    'uid'=>$i,
                    'cid'=>$cid,
                    'seq'=>$seq,
                    'created_at'=>date('Y-m-d H:i:s'),
                    'updated_at'=>date('Y-m-d H:i:s')
                ];
                $j++;
                if($j%1000 == 0){
                    DB::table('read_record_temp')->insert($data);
                    $data = [];
                }
            }
        }
        DB::table('read_record_temp')->insert($data);
    }

    private function chapters($bid){
        return Chapter::where('bid',$bid)->select('id','sequence')->get()->pluck('sequence','id')->all();
    }

}