<?php

namespace App\Console\Commands\Temp;
use Illuminate\Console\Command;
use App\Modules\Message\MessageNotify;
use DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查cid';

    /**
     * Create a new command instance.
     *
     * @return void
     */

    public function __construct()
    {
        parent::__construct();
    }


    private $emails = ['lisy@zw88.net','songdb@iqiyoo.com','zhoulj@iqiyoo.com','zhaoy@zw88.net'];
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(){
        \Log::info('check_cid_error_start'.date('Y-m-d'));
        $this->Check();
        \Log::info('check_cid_error_end'.date('Y-m-d'));

    }
    public function Check()
    {
        $msg = '['.env('PROJECT_NAME').']';
        $wrong_next_cid = DB::select('select bid,next_cid,count(1) as num
from chapters c
where next_cid =0 and bid > 0
group by c.bid 
having num > 1');
        $msg .=  !empty($wrong_next_cid) ? 'wrong_next_cid: '.\GuzzleHttp\json_encode($wrong_next_cid) : '';
        $wrong_prev_cid =  DB::select('select bid,prev_cid,count(1) as num
from chapters c
where prev_cid =0 and bid > 0
group by c.bid 
having num > 1;');
        $msg .=  !empty($wrong_prev_cid) ? 'wrong_prev_cid: '.\GuzzleHttp\json_encode($wrong_prev_cid) : '';

        \Log::info('check_cid_error'.date('Y-m-d').$msg);
        if($msg){
            $notify = MessageNotify::mail([
                'to_emails' => implode(",", $this->emails),
                'subject' => $msg,
                'body' =>$msg,
                'delay_times' => 0,
            ]);
            $notify->notify();
            exit();

        }
    }

}
