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

namespace App\Console\Commands;

use App\Modules\Statistic\Services\SendStatsEmailService;
use DB;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Log;

/**
 * 检测掌读的站点信息
 * Class CheckZhangDuSiteStatus
 * @package App\Console\Commands
 */
class CheckZhangDuSiteStatus extends Command
{
    /**
     * 执行命令 检测掌读的站点信息
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check_zhang_du_site_status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检测掌读的站点信息';

    public $tag = 'check_zhang_du_site_status';
    public $table_name = 'novel_platform_sites_zhangdu';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info($this->tag . 'start command');
        $this->loadSiteIndexInfo();
        Log::info($this->tag . 'end command');
    }

    function loadSiteIndexInfo()
    {
        $client = new Client(['timeout' => 3.0,]);
        $top_site_arr = ['.com', '.cn', '.net'];
        $results = [];
        $count = 0;
        $header = ['User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A404 MicroMessenger/6.7.4(0x1607042c) NetType/WIFI Language/zh_CN'];

        $lastIndex = DB::table($this->table_name)->max('site');
        $index = $lastIndex + 1;
        while ($index <= $lastIndex + 1000) {
            foreach ($top_site_arr as $item) {
                try {
                    $request_url = 'https://s' . $index . '.zhangdu520' . $item;
                    $response = $client->request('get', $request_url, [
                        'headers' => $header,
                        'curl' => [
                            CURLOPT_SSL_VERIFYPEER => false
                        ]
                    ]);
                    $resultCode = $response->getStatusCode();
                    if ($resultCode == 200 || $resultCode == 302) {
                        $count++;
                        $results[] = ['url' => $request_url, 'site' => $index];
                        $this->saveToDB($index, $request_url);
                        break;
                    }
                } catch (\Exception $e) {
                    Log::info($e->getMessage());
                }
            }
            if ($index % 10000 == 0) print_r($index);
            $index++;
        }

        if ($results && count($results) > 0) {
            $this->sendEmail($results);
        }
    }

    function saveToDB($site, $url)
    {
        $params = [
            'url' => $url,
            'site' => $site,
            'platform_name' => '掌读',
            'created_at' => date("Y-m-d H:i:s"),
            'updated_at' => date("Y-m-d H:i:s")];
        DB::table($this->table_name)->insert($params);
    }

    function sendEmail($results)
    {
        Log::info($this->tag . ' start sendEmail');
        $to_user = array(
            ['address' => 'zhangzg@iqiyoo.com', 'name' => '张总'],
            ['address' => 'zhaojp@yqsd.net', 'name' => '赵君平'],
            ['address' => 'songdb@iqiyoo.com', 'name' => 'songdb'],
            ['address' => 'sijj@yqsd.net', 'name' => 'sijj'],
            ['address' => 'qincp@iqiyoo.com', 'name' => '阿才'],
        );

        $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>序号</td><td align='center'>站点id</td><td align='center'>访问的url</td></tr>";
        $index = 0;
        foreach ($results as $item) {
            $index++;
            $content .= "<tr><td align='center'>{$index}</td><td align='center'>{$item['site']}</td><td align='center'>{$item['url']}</td></tr>";
        }
        $content .= "</table>";
        SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "掌读昨天新增站点" . $index . "个", 'body' => $content]);
    }
}
