<?php

namespace App\Console\Commands\Wechat;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use App\Http\Controllers\WechatController;
use Exception;
use Log;
use DB;
use Redis;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    private $user_action_set_id = [];

    private $assess_token = [];

    private $error_msg;

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->start();
    }
    private function start(){
        $data = $this->getDataFromDB();
        if($data){
            $this->getDataAndReport($data);
        }
    }

    private function getDataFromDB(){
        $data = [];
        $start = date('Y-m-d');
        if(date('H:i:s') < '02:00:00'){
            $start = date('Y-m-d H:i:s',time()-3*3600);
        }
        $result = DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')
            ->where('report_status',0)
            ->where('created_at','>=',$start)
            ->where('type','WECHAT')
            ->where('action_type','ALL')
            ->select('order_id','appid','openid','price','report_status','created_at','report_type')
            ->get();
        if($result){
            foreach ($result as $item){
                $order_status = DB::connection('api_mysql')->table('orders')->where('id',$item->order_id)->select('status')->first();
                if($order_status->status == 'UNPAID')continue;
                $report_info = DB::table('wechat_advertise_report_record')->where('product_id',$item->order_id)->where('result_status','SUCCESS')->count();
                if($report_info) {
                    try{
                        DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id',$item->order_id)->update([
                            'report_status'=>1,
                            'updated_at'=>date('Y-m-d H:i:s')
                        ]);
                    }catch (\Exception $e){
                        \Log::info($e);
                    }

                    continue;
                }
                $data[] = ['created_at' => $item->created_at, 'openid' => $item->openid,'report_type'=>$item->report_type,
                    'id' => $item->order_id, 'appid' => $item->appid, 'price' => $item->price];
            }
        }
        return $data;
    }

    private function getDataAndReport($order_result){
        $result = $order_result;
        $client = new Client();
        foreach ($result as $item){
            $report_type = $this->transferReportType($item['report_type'],$item['created_at'],$item['id']);
            $item_array = $item;
            $user_action_set_id = $this->getDataSurce($item_array['appid']);
            $data = [
                'user_action_set_id'=>$user_action_set_id,
                'url'=>'',
                'action_time'=>strtotime($item_array['created_at']),
                'action_type'=>'COMPLETE_ORDER',
                'openid'=>$item_array['openid'],
                'appid'=>$item_array['appid'],
                'product_id'=>$item_array['id'],
                'value'=>$item_array['price']*100,
                'source'=>'Biz',
                'claim_type'=>0,
                'object'=>$report_type
            ];
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['result_status'] = 'FAIL';

            $book_info = $this->getBookName($item_array['id']);
            $product_id = '';
            $product_name = '';
            $data['bid'] = $book_info['en_bid'];
            if($book_info['en_bid'] && $book_info['book_name']){
                $product_id = $book_info['en_bid'];
                $data['bid'] = $book_info['en_bid'];
                $product_name = $book_info['book_name'].'-'.env('PROJECT_NAME');
                $data['product_name'] = $product_name;
                $data['url'] = env('GDT_REPORT_URL').'/detail?id='.$book_info['en_bid'];
            }

            $data['result'] = '';
            if(!$user_action_set_id) {
                $data['result_msg'] = $this->error_msg;
                DB::table('wechat_advertise_report_record')->insert($data);
                DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id',$item_array['id'])->update([
                    'report_status'=>5,
                    'updated_at'=>date('Y-m-d H:i:s')
                ]);
                continue;
            };
            $request_data = [
                'actions'=>[
                    [
                        'user_action_set_id'=>$data['user_action_set_id'],
                        'url'=>$data['url'],
                        'action_time'=>$data['action_time'],
                        'action_type'=>$data['action_type'],
                        'user_id'=>[
                            'wechat_app_id'=>$data['appid'],
                            'wechat_openid'=>$data['openid']
                        ],
                        'action_param'=>[
                            'object'=>$report_type,
                            'product_name'=>$product_name,
                            'product_id'=>$product_id,
                            'source'=>$data['source'],
                            'wechat_app_id'=>'',
                            'claim_type'=>$data['claim_type'],
                            'value'=>$item_array['price']*100
                        ]
                    ]
                ]
            ];

            $token = $this->getAccessToken($item_array['appid']);
            if(!$token) continue;
            $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='.$token;
            try{
                $result = $client->request('post',$url,[
                    'headers'=>['Content-Type'=>'application/json'],
                    'body'=>\GuzzleHttp\json_encode($request_data)
                ])->getBody()->getContents();
                Log::info('user_actions/add result is: '.$result);
                $result_array = \GuzzleHttp\json_decode($result,1);
                if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
                    $data['result_status'] = 'SUCCESS';
                    $data['result_msg'] = $result_array['errcode'];
                    $data['result'] = $result;
                }else{
                    $data['result_status'] = 'FAIL';
                    $data['result_msg'] = isset($result_array['errcode'])?$result_array['errcode']:'unknown';
                    $data['result'] = $result;
                }

                DB::table('wechat_advertise_report_record')->insert($data);
                try{
                    if($data['result_status'] == 'SUCCESS'){
                        DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id',$item_array['id'])->update([
                            'report_status'=>1,
                            'updated_at'=>date('Y-m-d H:i:s')
                        ]);
                    }
                }catch (\Exception $e){\Log::info($e);}

            }catch (Exception $e){
                Log::error($e);
            }

            try{
                 $this->secondReport($client,$item,$user_action_set_id,$book_info['en_bid'],$book_info['book_name'],$token,$report_type);

            }catch (\Exception $e){
                \Log::info($e);
            }
        }

    }

    private function secondReport(Client $client,$data,$user_action_set_id,$bid='',$book_name='',$token='',$report_type){
        $item_array = $data;
        $data = [
            'user_action_set_id'=>$user_action_set_id,
            'url'=>'',
            'action_time'=>strtotime($item_array['created_at']),
            'action_type'=>'PURCHASE',
            'openid'=>$item_array['openid'],
            'appid'=>$item_array['appid'],
            'product_id'=>$item_array['id'],
            'value'=>$item_array['price']*100,
            'source'=>'Biz',
            'claim_type'=>0,
            'object'=>$report_type
        ];
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');
        $data['result_status'] = 'FAIL';
        $product_id = '';
        $product_name = '';
        $data['bid'] = '';
        if($bid && $book_name){
            $product_id = $bid;
            $data['bid'] = $bid;
            $product_name = $book_name.'-'.env('PROJECT_NAME');
            $data['product_name'] = $product_name;
            $data['url'] = env('GDT_REPORT_URL').'/detail?id='.$bid;
        }
        $data['result'] = '';
        if(!$user_action_set_id) {
            $data['result_msg'] = $this->error_msg;
            DB::table('wechat_advertise_report_record')->insert($data);
        };

        $request_data = [
            'actions'=>[
                [
                    'user_action_set_id'=>$data['user_action_set_id'],
                    'url'=>$data['url'],
                    'action_time'=>$data['action_time'],
                    'action_type'=>$data['action_type'],
                    'user_id'=>[
                        'wechat_app_id'=>$data['appid'],
                        'wechat_openid'=>$data['openid']
                    ],
                    'action_param'=>[
                        'object'=>$report_type,
                        'product_name'=>$product_name,
                        'product_id'=>$product_id,
                        'source'=>$data['source'],
                        'wechat_app_id'=>'',
                        'claim_type'=>$data['claim_type'],
                        'value'=>$item_array['price']*100
                    ]
                ]
            ]
        ];

        if(!$token) return '';
        $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='.$token;
        try{
            $result = $client->request('post',$url,[
                'headers'=>['Content-Type'=>'application/json'],
                'body'=>\GuzzleHttp\json_encode($request_data)
            ])->getBody()->getContents();
            Log::info('user_actions/add result is: '.$result);
            $result_array = \GuzzleHttp\json_decode($result,1);
            if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
                $data['result_status'] = 'SUCCESS';
                $data['result_msg'] = $result_array['errcode'];
                $data['result'] = $result;
            }else{
                $data['result_status'] = 'FAIL';
                $data['result_msg'] = isset($result_array['errcode'])?$result_array['errcode']:'unknown';
                $data['result'] = $result;
            }

            DB::table('wechat_advertise_report_record')->insert($data);
            return $data['result_status'];    
        }catch (Exception $e){
            Log::error($e);
        }
        return '';
    }

    private function getDataSurce($appid,$type='WECHAT'){
        if(!isset($this->user_action_set_id[$appid])){
            $result = $this->getDataSourceFromDB($appid,$type);
            if($result){
                $this->user_action_set_id[$appid] = $result;
                return $result;
            }
            $result = $this->createDataSource($appid,$type);
            if($result){
                $this->user_action_set_id[$appid] = $result;
                return $result;
            }
            Log::info('appid is :'.$appid.', createDataSource fail');
            return '';
        }
        return  $this->user_action_set_id[$appid];

    }
    private function getAccessToken($appid){
        if(isset($this->assess_token[$appid])){
            return $this->assess_token[$appid];
        }
        /*$WechatController = new WechatController($appid);
        $accessToken = $WechatController->app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(); // token 字符串
        if($token){
            $this->assess_token[$appid] = $token;
            return $token;
        }
        Log::info('appid is :'.$appid.',request access_token fail');
        return '';*/
        $access_token = Redis::get('Wechat:access_token:appid:'.$appid);
        if($access_token){
            $this->assess_token[$appid] = $access_token;
            return $this->assess_token[$appid];
        }
        /*$appsecret = 'f73d3be068d86417bf1a3fafefe4596a';
        $url = sprintf(
            'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s',
            $appid,
            $appsecret
        );
        $client = new Client();
        $result = $client->request('get',$url)->getBody()->getContents();
        Log::info('request access token ,result is:'.$result);
        $result = \GuzzleHttp\json_decode($result,1);
        if($result && isset($result['access_token'])){
            Redis::setex('Wechat:access_token:appid:'.$appid,7000,$result['access_token']);
            $this->assess_token[$appid] = $result['access_token'];
            return $this->assess_token[$appid];
        }*/
        $this->error_msg = $appid.',access_token fail';
        return '';
    }

    private function createDataSource(string $appid,string $type){
        $token = $this->getAccessToken($appid);
        if(!$token) return 0;
        $url = 'https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0&access_token='.$token;
        $client = new Client();
        $name = '支付下单';
        $description = '支付下单,充值统计';
        $body = json_encode([
            'type'=>$type,
            'wechat_app_id'=>$appid,
            'name'=>$name,
            'description'=>$description
        ]);
        try{
            $result = $client->request('post',$url,[
                'headers'=>['Content-Type'=>'application/json'],
                'body'=>$body
            ])->getBody()->getContents();
            $result = \GuzzleHttp\json_decode($result,1);
            if($result['errcode'] == 0 && isset($result['data']) && isset($result['data']['user_action_set_id'])){
                $user_action_set_id = $result['data']['user_action_set_id'];
                $this->saveDataSource($appid,$name,$type,$description,$user_action_set_id);
                return $user_action_set_id;
            }
            if($result['errcode'] == 900351000){
                $msg_array = explode(':',$result['errmsg']);
                if(isset($msg_array[1])){
                    $user_action_set_id = trim($msg_array[1]);
                    $this->saveDataSource($appid,$name,$type,$description,$user_action_set_id);
                    return $user_action_set_id;
                }
            }
        }catch (Exception $e){
            Log::error($e);
        }
        $this->error_msg = $appid.',createDataSource fail';
        return 0;
    }

    private function getDataSourceFromDB($appid,$type):int
    {
        $result = DB::table('wechat_advertise_data_source')->where('appid', $appid)->where('type', $type)->select('user_action_set_id')->first();
        if($result){
            return $result->user_action_set_id;
        }
        return 0;
    }

    private function saveDataSource(string $appid,string $name,string $type,string $description,int $user_action_set_id){
        DB::table('wechat_advertise_data_source')->insert([
            'appid'=>$appid,
            'name'=>$name,
            'type'=>$type,
            'description'=>$description,
            'user_action_set_id'=>$user_action_set_id,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s')
        ]);
    }


    private function getBookName($order_id){
        $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
        try{
            $order_info = DB::connection('api_mysql')->table('orders')->select('from_bid')->where('id',$order_id)->first();
            if($order_info && $order_info->from_bid){
                $result['bid'] = $order_info->from_bid;
                $result['en_bid'] = \Hashids::encode($order_info->from_bid);
                $book_info = DB::connection('api_mysql')->table('book_configs')
                    ->select('book_name','is_on_shelf')
                    ->where('bid',$order_info->from_bid)->first();
                if($book_info){
                    if($book_info->is_on_shelf == 2){
                        $result['book_name'] = $book_info->book_name;
                    }
                }
                return $result;
            }
        }catch (\Exception $e){

        }
        return $result;
    }
    private function transferReportType($report_type,$order_time,$order_id){
        $map = [
            'one_day_first_pay'=>'read_1',
            'one_day_all_pay'=>'read_2',
            'current_day_register_first_pay'=>'read_3',
            'current_day_register'=>'read_4',
            'three_day_first_pay'=>'read_5',
            'three_day_all_pay'=>'read_6'
        ];
        if(isset($map[$report_type])){
            return $map[$report_type];
        }
        $order_info = DB::connection('api_mysql')->table('orders')->where('id',$order_id)->select('uid')->first();
        if(!$order_info){
            return '';
        }
        $uid = $order_info->uid;
        $user_info = DB::connection('api_mysql')->table('users')->where('id',$uid)->select('created_at')->first();
        if(!$user_info) return '';
        $register_time = $user_info->created_at;
        $register_timestamp = strtotime($register_time);
        $order_timestamp = strtotime($order_time);
        if(date('Y-m-d',$register_timestamp) == date('Y-m-d',$order_timestamp)){
            return 'read_4';
        }
        if(strtotime($order_time) -  strtotime($register_time)  < 86400){
            return 'read_2';
        }
        if(strtotime($order_time) -  strtotime($register_time)  < 3*86400){
            return 'read_6';
        }
        return '';
    }
}
