<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\User\Services\UserSubscribeBehaviorStatsService;
use DB;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Log;
use Redis;


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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新强关号实际新增粉丝';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $lists = UserSubscribeBehaviorStatsService::getList(date('Y-m-d',strtotime('-1 day')));
        foreach ($lists as $list){
            $new_user_num_raw = self::getOfficialAccountFansNum($list->appid,$list->date,$list->date);
            $new_user_num = 0;
            $cancel_user_num = 0;
            if(isset($new_user_num_raw['user_summary']['list']) && count($new_user_num_raw['user_summary']['list'])>0){
                $new_user_num =  $new_user_num_raw['user_summary']['list'][0]['new_user'];
                $cancel_user_num =  $new_user_num_raw['user_summary']['list'][0]['cancel_user'];
            }
            $list->yesterday_actual_new_user = $new_user_num;
            $list->yesterday_actual_sub_num = $new_user_num-$cancel_user_num;
            //\Log::info(json_encode($new_user_num_raw));
            //\Log::info($new_user_num);
            $list->save();
        }
    }

    private static function getOfficialAccountFansNum($appid,$start_date,$end_date){
        $client = New Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
        $response = $client->request('GET','get_gzh_statistics',['query'=>['gzh_app_id'=>$appid,'from_date'=>$start_date,'to_date'=>$end_date]]);
        $res = json_decode($response->getBody(),true);
        //if(isset($res['']))
        return $res['data'];
    }
}


