<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Libs\SMS;
use Illuminate\Support\Facades\Cache;
use DB;
class SubscribeAlert extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'subscribealert {--type=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '粉丝和强关预警';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $type = $this->option('type');
        if($type == 'getTopAlert'){
            $this->getTopAlert();
        }
        if($type == 'subscribeRateAlert'){
            $this->subscribeRateAlert();
        }
    }

    /**
     * 商户平台日粉丝到达上限90%、达到总上限90%
     */
    private function getTopAlert(){
        $sql = "SELECT distribution_channel_id,GROUP_CONCAT(nickname)as nickname,GROUP_CONCAT(appid) as appid,SUM(subscribe_day_maximum) as `max` FROM official_accounts WHERE is_auth = 1 and is_enabled = 1 GROUP BY distribution_channel_id";
        $res = DB::select($sql);
        foreach ($res as $v){
            $count = DB::table('force_subscribe_users')
                ->whereIn('appid',explode(',',$v->appid))
                ->where('is_subscribed',1)
                ->where('created_at','>=',date('Y-m-d'))
                ->where('created_at','<',date('Y-m-d',time()+86400))
                ->count();
            if($count>= $v->max*0.9){
                $key = date('Y-m-d').'-distribution_channel_id-'.$v->distribution_channel_id;
                $old = Cache::store('file')->get($key);

                if($old){
                    continue;
                }else{
                    Cache::store('file')->put($key,time(),24*60);
                    $content = '强关峰值预警,渠道'.$v->distribution_channel_id.'日粉丝到达上限90%,公众号:'.$v->nickname;
                    $this->send($content);
                }
            }
            return;
        }
    }

    private function send($content){
        //(君平15858178353  张总 13858057394      陈帅军15088790066  zw 186.. 栋波15868100210 )
        $phones = ['15858178353','18668420256','15868100210'];
        //$phones = ['18668029091'];
        foreach ($phones as $phone){
            SMS::send($phone,$content);
        }
    }

    public function subscribeRateAlert(){

        $key = 'getSubscribeRate';
        $old = Cache::store('file')->get($key);
        if($old){
            return;
        }
        $now_hour = date('G');
        //echo '$now_hour--'.$now_hour.PHP_EOL;
        $rate = $this->getSubscribeRate();
        //echo '$tate is:'.$rate.PHP_EOL;
        if($now_hour>=4 &&  $now_hour<=7){
            if($rate < 0.2){
                $content = '平台强关率预警,当前强关率'.$rate;
                $this->send($content);
                Cache::store('file')->put($key,time(),60*3);
            }
        }else{
            if($rate < 0.25){
                $content = '平台强关率预警,当前强关率'.$rate;
                $this->send($content);
                Cache::store('file')->put($key,time(),60*3);
            }
        }
    }

    private function getSubscribeRate(){
        $register_user_num = DB::table('users')->where('created_at','>=',date('Y-m-d H',time()-3600))->where('created_at','<',date('Y-m-d H'))->count();
        $force_subscribe_users_num = DB::table('force_subscribe_users')->where('created_at','>=',date('Y-m-d H',time()-3600))->where('created_at','<',date('Y-m-d H'))->count();
        if($register_user_num == 0){
            return 0;
        }
        return round($force_subscribe_users_num/$register_user_num,2);
    }
}