<?php

namespace App\Console\Commands\Book;

use Illuminate\Console\Command;

use App\Modules\Statistic\Services\WapVisitStatService;
use DB;



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

    /**
     * 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()
    {
        $this->oneDayDataFill();
        $this->threeDayDataFill();
        $this->sixDayDataFill();
        $this->sevenDayDataFill();


    }
    function oneDayDataFill(){
        $one_day_before = date('Y-m-d H:i:s',strtotime('-1 day'));

        $new_book_tests = DB::table('new_book_tests')
            ->where('test_end_time','<=',$one_day_before)
//            ->where('status',2)
            ->whereNull('uv_in_one_day')
            ->whereNull('sub_amount_in_one_day')
            ->get();
        if(!$new_book_tests){return;}

        foreach ($new_book_tests as $item) {
            $data = array();
            $book_statistics = WapVisitStatService::smartPushTestBookStats($item->bid);
            $data['uv_in_one_day'] = $book_statistics['uv'];
            $data['sub_amount_in_one_day'] = $book_statistics['book_amount'];
            DB::table('new_book_tests')
                ->where('id',$item->id)
                ->update($data);

        }
    }
    function threeDayDataFill(){
        $three_day_before = date('Y-m-d H:i:s',strtotime('-3 day'));
        $new_book_tests = DB::table('new_book_tests')
            ->where('test_end_time','<=',$three_day_before)
//            ->where('status',2)
            ->whereNull('uv_in_three_day')
            ->whereNull('sub_amount_in_three_day')
            ->get();
        if(!$new_book_tests){return;}

        foreach ($new_book_tests as $item) {
            $data = array();
            $book_statistics = WapVisitStatService::smartPushTestBookStats($item->bid);
            $data['uv_in_three_day'] = $book_statistics['uv'];
            $data['sub_amount_in_three_day'] = $book_statistics['book_amount'];
            DB::table('new_book_tests')
                ->where('id',$item->id)
                ->update($data);

        }
    }

    function sevenDayDataFill(){
        $seven_day_before = date('Y-m-d H:i:s',strtotime('-7 day'));
        $seven_day_before_limit = date('Y-m-d H:i:s',strtotime('-7 day -1hour'));

        $new_book_tests = DB::table('new_book_tests')
            ->where('test_end_time','<=',$seven_day_before)
            ->where('test_end_time','>=',$seven_day_before_limit)
            ->whereNull('uv_in_seven_day')
            ->whereNull('sub_amount_in_seven_day')
            ->get();

        if(!$new_book_tests){return;}

        foreach ($new_book_tests as $item) {
            $data = array();
            $book_statistics = WapVisitStatService::smartPushTestBookStats($item->bid);
            $data['uv_in_seven_day'] = $book_statistics['uv'];
            $data['sub_amount_in_seven_day'] = $book_statistics['book_amount'];
            DB::table('new_book_tests')
                ->where('id',$item->id)
                ->update($data);

        }
    }

    function sixDayDataFill(){
        $six_day_before = date('Y-m-d H:i:s',strtotime('-6 day'));
        $six_day_before_limit = date('Y-m-d H:i:s',strtotime('-6 day -1hour'));
        $new_book_tests = DB::table('new_book_tests')
            ->where('test_end_time','<=',$six_day_before)
            ->where('test_end_time','>=',$six_day_before_limit)
            ->whereNull('uv_in_six_day')
            ->whereNull('sub_amount_in_six_day')
            ->get();
        if(!$new_book_tests){return;}

        foreach ($new_book_tests as $item) {
            $data = array();
            $book_statistics = WapVisitStatService::smartPushTestBookStats($item->bid);
            $data['uv_in_six_day'] = $book_statistics['uv'];
            $data['sub_amount_in_six_day'] = $book_statistics['book_amount'];
            DB::table('new_book_tests')
                ->where('id',$item->id)
                ->update($data);

        }
    }
}