<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use Redis;
use Log;
class ChapterOrderTotal extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book:cot {--type=}';

    /**
     * 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()
    {
        $option = $this->option('type');
        if(!$option){
            $this->sub1();
        }
        if($option == 'cp'){
            $this->cpChargeAndReward();
            //$this->cpData();
        }
        if($option == 'order'){
            $this->orderDayStat();
            //$this->orderDayStatAll();
        }
    }


    public function sub1(){
        $start = date('Y-m-d',time()-86400);
        $end = date('Y-m-d');
        $sql = 'call cp_data_temp("'.$start.'","'.$end.'");';
        DB::connection('chapter_order_mysql')->select($sql);
        $bids = DB::table('book_configs')->select('bid')->get();
        $data = [];
        $day = date('Y--m-d',time()-86400);
        foreach ($bids as $book){
            $fee = Redis::hget('wap:chapterandbookorder:bid:'.$book->bid,$day);
            if(!$fee) $fee = 0;
            $data[] = ['bid'=>$book->bid,'day'=>$day,'fee'=>$fee,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
            Redis::hdel('wap:chapterandbookorder:bid:'.$book->bid,$day);
        }
        DB::table('book_order_statistical')->insert($data);
        $end2 = $start.' 23:59:59';

        $sql2 = 'call chapter_orders("'.$start.'","'.$end2.'");';
        DB::connection('chapter_order_mysql')->select($sql2);
    }

    public function cpData(){
        $sql = "select bid,date,sum(sum_charge_balance) as sum_charge_balance, sum(sum_reward_balance) as sum_reward_balance from sub_bak5 GROUP by bid,date";
        $chapter = DB::connection('chapter_order_mysql')->select($sql);
        Log::info($chapter);
        foreach ($chapter as $v){
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->update(['charge_balance'=>(int)$v->sum_charge_balance,'reward_balance'=>(int)$v->sum_reward_balance]);
            $v = null;
        }
        $sql = "select bid,date(created_at) as date,sum(charge_balance) as sum_charge_balance ,sum(reward_balance) as sum_reward_balance from book_orders GROUP by bid,date(created_at)";
        $book = DB::select($sql);
        foreach ($book as $v){
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
        }
    }

    public function cpChargeAndReward(){
        $date = date('Y-m-d',time()-86400);
        $sql_format = "select bid,date(created_at) as date,sum(charge_balance) as sum_charge_balance ,sum(reward_balance) as sum_reward_balance from book_orders where date(created_at)='%s' GROUP by bid,date(created_at)";
        echo sprintf($sql_format,$date);
        $book = DB::select(sprintf($sql_format,$date));
        foreach ($book as $v){
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
        }

        $sql = "select bid,date,sum(sum_charge_balance) as sum_charge_balance, sum(sum_reward_balance) as sum_reward_balance from sub_bak5 where date='%s'  GROUP by bid,date";
        $chapter = DB::connection('chapter_order_mysql')->select(sprintf($sql,$date));
        foreach ($chapter as $v){
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
            DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
        }
    }

    public function orderDayStatAll(){
        DB::table('order_day_stats')->select('id', 'distribution_channel_id', 'date')->orderBy('id')->chunk(1000, function ($res) {
            foreach ($res as $v) {
                $chapter_sql = "select sum(sum_fee) as sum_fee,sum(sum_charge_balance) as sum_charge_balance,sum(sum_reward_balance) as sum_reward_balance from sub_bak5 where distribution_channel_id=%s and date='%s'";
                $chapter_data = DB::connection('chapter_order_mysql')->select(sprintf($chapter_sql,$v->distribution_channel_id,$v->date));
                DB::table('send_orders_stats')->where('id',$v->id)->update([
                    'fee'=>(int)$chapter_data[0]->sum_fee,
                    'charge_balance'=>(int)$chapter_data[0]->sum_charge_balance,
                    'reward_balance'=>(int)$chapter_data[0]->sum_reward_balance,
                    ]);

                $end_date = date('Y-m-d',strtotime($v->date)+86400);
                $book_sql = "select sum(fee) as sum_fee,sum(charge_balance) as sum_charge_balance,sum(reward_balance) as sum_reward_balance from book_orders where distribution_channel_id=%s and created_at >='{$v->date}' and created_at < '{$end_date}'";
                $chapter_data = DB::select($book_sql);

                $update_sql = "update send_orders_stats set sum_fee=sum_fee+".(int)$chapter_data[0]->sum_fee.',sum_charge_balance=sum_charge_balance+'.(int)$chapter_data[0]->sum_charge_balance.',sum_reward_balance=sum_reward_balance+'.(int)$chapter_data[0]->sum_reward_balance.' where id='.$v->id;
                DB::update($update_sql);
            }
        });
    }

    public function orderDayStat(){
        DB::table('order_day_stats')->select('id', 'distribution_channel_id', 'date')->where('date',date('Y-m-d',time()-86400))->orderBy('id')->chunk(1000, function ($res) {
            foreach ($res as $v) {
                $chapter_sql = "select sum(sum_fee) as sum_fee,sum(sum_charge_balance) as sum_charge_balance,sum(sum_reward_balance) as sum_reward_balance from sub_bak5 where distribution_channel_id=%s and date='%s'";
                $chapter_data = DB::connection('chapter_order_mysql')->select(sprintf($chapter_sql,$v->distribution_channel_id,$v->date));

                $update_sql = "update send_orders_stats set sum_fee=sum_fee+".(int)$chapter_data[0]->sum_fee.',sum_charge_balance=sum_charge_balance+'.(int)$chapter_data[0]->sum_charge_balance.',sum_reward_balance=sum_reward_balance+'.(int)$chapter_data[0]->sum_reward_balance.' where id='.$v->id;
                DB::update($update_sql);

                $end_date = date('Y-m-d',strtotime($v->date)+86400);
                $book_sql = "select sum(fee) as sum_fee,sum(charge_balance) as sum_charge_balance,sum(reward_balance) as sum_reward_balance from book_orders where distribution_channel_id={$v->distribution_channel_id} and created_at >='{$v->date}' and created_at < '{$end_date}'";
                $chapter_data = DB::select($book_sql);

                $update_sql = "update send_orders_stats set sum_fee=sum_fee+".(int)$chapter_data[0]->sum_fee.',sum_charge_balance=sum_charge_balance+'.(int)$chapter_data[0]->sum_charge_balance.',sum_reward_balance=sum_reward_balance+'.(int)$chapter_data[0]->sum_reward_balance.' where id='.$v->id;
                DB::update($update_sql);
            }
        });
    }

}
