<?php
/**
 * Created by PhpStorm.
 * User: sogndb
 * Date: 2018/02/05
 * Time: 下午5:26
 */

namespace App\Console\Commands\SendOrder;

use Log;
use Illuminate\Console\Command;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Statistic\Services\WapVisitStatService;
use DB;
use Redis;

class SetExtraStats extends Command
{
    /**
     * 执行命令   php artisan send_order:set_extra_stats
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send_order:set_extra_stats';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '派单额外数据uv24小时设置';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '1024M');
        print_r('memory_used:'.memory_get_usage()."\n");
        print_r("======派单额外数据uv24小时设置 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        Log::info("======派单额外数据uv24小时设置 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        $start = date('Y-m-d H:i:s',strtotime('-1 day') - 120);
        $end = date('Y-m-d H:i:s',strtotime($start)+240);
        collect(DB::select("select send_orders.id,send_orders.send_time,uv_one_day from send_orders left join send_order_extra_stats on send_order_extra_stats.send_order_id = send_orders.id where send_orders.send_time >= '{$start}' and send_orders.send_time <= '{$end}'"))->each(function ($send_order){
            if($send_order->uv_one_day === null)
            {
                $uv_one_day = $this->getOneDayUv($send_order->id);
                $pv_one_day = $this->getOneDayPv($send_order->id);
                $time = date('Y-m-d H:i:s');
                $data = [
                    'uv_one_day'=>$uv_one_day,
                    'pv_one_day'=>$pv_one_day,
                    'send_order_id'=>$send_order->id,
                    'send_time'=>$send_order->send_time,
                    'created_at'=>$time,
                    'updated_at'=>$time
                ];
                DB::table('send_order_extra_stats')->insert($data);
            }
        });

        $start = date('Y-m-d H:i:s',strtotime('-3 days') - 120);
        $end = date('Y-m-d H:i:s',strtotime($start)+240);
        collect(DB::select("select send_orders.id,send_orders.send_time,send_order_extra_stats.id as extra_id,uv_three_day from send_orders left join send_order_extra_stats on send_order_extra_stats.send_order_id = send_orders.id where send_orders.send_time >= '{$start}' and send_orders.send_time <= '{$end}'"))->each(function ($send_order){
            if($send_order->uv_three_day === null)
            {
                $uv_three_day = $this->getThreeDayUv($send_order->id);
                $pv_three_day = $this->getThreeDayPv($send_order->id);
                $time = date('Y-m-d H:i:s');
                $data = [
                    'uv_three_day'=>$uv_three_day,
                    'pv_three_day'=>$pv_three_day,
                    'updated_at'=>$time
                ];
                DB::table('send_order_extra_stats')->where('id',$send_order->extra_id)->update($data);
            }
        });

        Log::info("======派单额外数据uv24小时设置 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r("======派单额外数据uv24小时设置 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r('memory_used:'.memory_get_usage()."\n");
    }

    //采集24小时uv
    private function getOneDayUv($id)
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d',strtotime('-1 day'));

        $one_day_uv = 0;
        $redis_uv = Redis::hgetall('send_order_uv_'. $id);
        $one_day_uv += isset($redis_uv[$today]) ? (int)$redis_uv[$today] : 0;//当天从redis获取

        $yesterday_uv = isset($redis_uv[$yesterday]) ? (int)$redis_uv[$yesterday] : 0;
        if(!$yesterday_uv)
        {
            //从数据库中获取v
            $yesterday_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $yesterday);
            $yesterday_uv = $yesterday_uv_pv['uv'];
        }
        $one_day_uv += $yesterday_uv;
        return $one_day_uv;
    }

    //采集24小时pv
    private function getOneDayPv($id)
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d',strtotime('-1 day'));

        $one_day_pv = 0;
        $redis_pv = Redis::hgetall('send_order_pv_'. $id);
        $one_day_pv += isset($redis_pv[$today]) ? (int)$redis_pv[$today] : 0;//当天从redis获取

        $yesterday_pv = isset($redis_pv[$yesterday]) ? (int)$redis_pv[$yesterday] : 0;
        if(!$yesterday_pv)
        {
            //从数据库中获取
            $yesterday_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $yesterday);
            $yesterday_pv = $yesterday_uv_pv['pv'];
        }
        $one_day_pv += $yesterday_pv;
        return $one_day_pv;
    }

    //采集72小时uv
    private function getThreeDayUv($id)
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d',strtotime('-1 day'));
        $the_day_before_yesterday = date('Y-m-d',strtotime('-2 day'));
        $three_days_ago = date('Y-m-d',strtotime('-3 day'));

        $one_day_uv = 0;
        $redis_uv = Redis::hgetall('send_order_uv_'. $id);
        $one_day_uv += isset($redis_uv[$today]) ? (int)$redis_uv[$today] : 0;//当天从redis获取

        $yesterday_uv = isset($redis_uv[$yesterday]) ? (int)$redis_uv[$yesterday] : 0;
        if(!$yesterday_uv)
        {
            //从数据库中获取v
            $yesterday_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $yesterday);
            $yesterday_uv = $yesterday_uv_pv['uv'];
        }
        $one_day_uv += $yesterday_uv;

        $the_day_before_yesterday_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $the_day_before_yesterday);
        $one_day_uv += $the_day_before_yesterday_uv_pv['uv'];
        $three_days_ago_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $three_days_ago);
        $one_day_uv += $three_days_ago_uv_pv['uv'];
        return $one_day_uv;
    }

    //采集72小时pv
    private function getThreeDayPv($id)
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d',strtotime('-1 day'));
        $the_day_before_yesterday = date('Y-m-d',strtotime('-2 days'));
        $three_days_ago = date('Y-m-d',strtotime('-3 days'));

        $one_day_pv = 0;
        $redis_pv = Redis::hgetall('send_order_pv_'. $id);
        $one_day_pv += isset($redis_pv[$today]) ? (int)$redis_pv[$today] : 0;//当天从redis获取

        $yesterday_pv = isset($redis_pv[$yesterday]) ? (int)$redis_pv[$yesterday] : 0;
        if(!$yesterday_pv)
        {
            //从数据库中获取
            $yesterday_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $yesterday);
            $yesterday_pv = $yesterday_uv_pv['pv'];
        }
        $one_day_pv += $yesterday_pv;

        $the_day_before_yesterday_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $the_day_before_yesterday);
        $one_day_pv += $the_day_before_yesterday_uv_pv['pv'];
        $three_days_ago_uv_pv = WapVisitStatService::getSendOrderDayUvPvFromStat($id, $three_days_ago);
        $one_day_pv += $three_days_ago_uv_pv['pv'];
        return $one_day_pv;
    }

}