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

namespace App\Console\Commands\OfficialAccountStat;

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

class OfficialAccountForbiddenStatus extends Command
{
    /**
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check_official_account_forbidden_status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '公众号每日被封号的情况';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('check_official_account_forbidden_status start command');
        $date = date('Y-m-d', strtotime('-1 day'));
        $table_name = 'forbidden_official_account_records';
        $result = DB::table($table_name)->where('date', $date)->orderBy('created_at')->get();
        if ($result && count($result) > 0) {
            Log::info('check_official_account_forbidden_status need sendEmail');
            $this->sendEmail($result);
        } else {
            Log::info('check_official_account_forbidden_status not need sendEmail');
        }
    }

    function sendEmail($result)
    {
        Log::info('check_official_account_forbidden_status start sendEmail');
        $to_user = array(
            ['address' => 'songdb@iqiyoo.com', 'name' => '宋栋波'],
            ['address' => 'zhaojp@yqsd.net', 'name' => '赵君平'],
            ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰'],
        );

        $content = "<table border='1' cellpadding='10' cellspacing='0'>
        <tr>
            <td align='center'>日期</td>
            <td align='center'>站点id</td>
            <td align='center'>appid</td>
            <td align='center'>服务号名称</td>
            <td align='center'>公司名称</td>
            <td align='center'>封号时间</td>
       </tr>";

        $index = 0;
        foreach ($result as $item) {
            $index++;
            $content .= "<tr>
                            <td align='center' nowrap='nowrap'>{$item->date}</td>
                            <td align='center' nowrap='nowrap'>{$item->distribution_channel_id}</td>
                            <td align='center' nowrap='nowrap'>{$item->appid}</td>
                            <td align='center' nowrap='nowrap'>{$item->nickname}</td>
                            <td align='center' nowrap='nowrap'>{$item->company_name}</td>
                            <td align='center' nowrap='nowrap'>{$item->forbbidden_time}</td>
                        </tr>";
        }
        $content .= "</table>";
        SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "网读昨天封号" . $index . "个", 'body' => $content]);
    }
}