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

namespace App\Console\Commands;

use App\Modules\Book\Models\BookConfig;
use App\Modules\Statistic\Services\SendStatsEmailService;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Log;

class CheckBookCover extends Command
{
    /**
     * 执行命令 检查图书封面
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check_book_cover_status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查图书封面';

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

    function loadBookCoverInfo()
    {
        $pageCount = 0; //当前的页数(默认从0开始)
        $pageSize = 200;//每页的条数
        $totalCount = BookConfig::getBooksCount(); //总条数
        $totalPageCount = Ceil($totalCount / $pageSize); ////总页数
        Log::info('CheckBookCover totalCount is  ' . $totalCount);
        Log::info('CheckBookCover totalPageCount is  ' . $totalPageCount);
        $errorBookCoverArray = [];
        $client = new Client(['timeout' => 3.0,]);
        while ($pageCount < $totalPageCount) {
            Log::info('CheckBookCover currentPageCount is: ' . $pageCount);
            $books = BookConfig::getBookCoverInfos(false, $pageSize, $pageCount);
            foreach ($books as $bookItem) {
                $coverUrl = $bookItem->cover;
                try {
                    $response = $client->request('get', $coverUrl);
                    $resultCode = $response->getStatusCode();
                    //成功
                    if ($resultCode == 200 || $resultCode == 302) {

                    } else {
                        Log::info('CheckBookCover load error');
                        $errorBookCoverArray[] = $bookItem;
                    }
                } catch (\Exception $e) {
                    Log::info('CheckBookCover load error');
                    $errorBookCoverArray[] = $bookItem;
                }
            }
            $pageCount++;
        }

        if ($errorBookCoverArray) {
            Log::info('CheckBookCover need sendEmail');
            $this->sendEmail($errorBookCoverArray);
        } else {
            Log::info('CheckBookCover not need sendEmail');
        }
    }

    function sendEmail($errorBookCoverArray)
    {
        Log::info('CheckBookCover start sendEmail');
        $to_user = array(
            ['address' => 'zhangzg@iqiyoo.com', 'name' => '张总'],
            ['address' => 'songdb@iqiyoo.com', 'name' => 'songdb'],
            ['address' => 'liur@iqiyoo.com', 'name' => '刘嵘'],
            ['address' => 'sijj@yqsd.net', 'name' => 'sijj'],
            ['address' => 'qincp@iqiyoo.com', 'name' => '阿才'],
        );

        $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>id</td><td align='center'>书名</td><td align='center'>作者</td></tr>";
        foreach ($errorBookCoverArray as $item) {
            $content .= "<tr><td align='center'>{$item->bid}</td><td align='center'>{$item->book_name}</td><td align='center'>{$item->author}</td></tr>";
        }
        $content .= "</table>";
        SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "追书云错误封面的书籍信息", 'body' => $content]);
    }
}