<?php
/**
 * Created by PhpStorm.
 * User: qincp
 * Date: 2019/1/7
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Models\ChannelUser;
use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\Statistic\Services\SendStatsEmailService;
use Illuminate\Console\Command;
use Log;

class CheckZsySiteStatus extends Command
{
    /**
     * 执行命令 检查站点信息
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check_zsy_site_status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查网读云站点信息';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('CheckZsySiteStatus start command');
        $start_date = date("Y-m-d", strtotime("-1 day"));
        $end_date = date("Y-m-d");
        $params = compact('start_date', 'end_date');
        //站点信息
        $result = Channel::getChannelInfoList($params, true);
        //服务号信息
        $officialAccounts = OfficialAccount::getInfoList($params);
        //账号数
        $accountCount = ChannelUser::getCount($params);

        if ($result) {
            $this->sendEmail($result, $officialAccounts, $accountCount);
        } else {
            Log::info('CheckZsySiteStatus not need sendEmail');
        }
    }

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

        $content = "<table border='1' cellpadding='10' cellspacing='0' style='float: left'><tr><td align='center'>序号</td><td align='center'>站点id</td><td align='center'>公司名称</td></tr>";
        $siteIndex = 0;
        foreach ($result as $item) {
            $siteIndex++;
            $content .= "<tr><td align='center'>{$siteIndex}</td><td align='center'>{$item->id}</td><td align='center'>{$item->company_name}</td></tr>";
        }
        $content .= "</table>";

        $content .= "<table border='1'  style='float: left;margin-left: 35px' cellpadding='10' cellspacing='0'><tr><td align='center'>序号</td><td align='center'>站点id</td><td align='center'>服务号名称</td><td align='center'>主体</td></tr>";
        $officialAccountIndex = 0;
        foreach ($officialAccounts as $item) {
            $officialAccountIndex++;
            $content .= "<tr><td align='center'>{$officialAccountIndex}</td><td align='center'>{$item->distribution_channel_id}</td><td align='center'>{$item->nickname}</td><td align='center'>{$item->principal_name}</td></tr>";
        }
        $content .= "</table>";

        SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "网读云昨天新增站点" . $siteIndex . "个,账号" . $accountCount . "个,服务号" . $officialAccountIndex . "个", 'body' => $content]);
    }
}