<?php
/**
 * Created by sublime.
 * User: wosinC
 * Date: 2017/12/2
 * Time: 上午11:39
 */

namespace App\Modules\OfficialAccount\Services;

use App\Libs\SMS;
use App\Libs\AliSMS;
use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\OfficialAccount\Models\OfficialAuthUrl;
use App\Modules\User\Services\UserService;
use App\Modules\Channel\Models\Channel;
use DB;
use GuzzleHttp\Client;
use Redis;

class OfficialAccountService
{

    public $secret_key = 'zhuishuyun#_2017';

    //-----------公众号---------------

    /**
     * 根据appid获取公众号
     */
    static function officialAccountByAppid($officialAccountPrams)
    {
        try {
            \Log::info('==========================通过appid获取公众号' . $officialAccountPrams['appid']);

            $officialMsg = Redis::hget('official_account_info:app_id:' . $officialAccountPrams['appid'], 'official_account_info');
            if (!$officialMsg) {

                $official_accounts = OfficialAccount::officialAccountByAppid($officialAccountPrams['appid']);

                if (!empty($official_accounts)) {
                    $officialAccountArray = $official_accounts->toArray();
                    Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
                }

                $official_accounts['todayForceSubscribeUsers'] = 0;
                $official_accounts['allForceSubscribeUsers'] = 0;
                return $official_accounts;

            } else {
                $official_object = json_decode($officialMsg);


                return $official_object;

            }


        } catch (\Exception $e) {

            \Log::info('==========================通过appid获取公众号报错');
            \Log::info($e->getMessage());
        }

    }


    /**
     * 根据id获取公众号
     */
    static function officialAccountById($officialAccountPrams)
    {

        $official_accounts = OfficialAccount::officialAccountById($officialAccountPrams['id']);

        return $official_accounts;
    }

    /**
     * 获取全部公众号
     */
    static function officialAuthAccounts($auth, $is_enabled)
    {

        $official_accounts = OfficialAccount::officialAuthAccounts($auth, $is_enabled);

        return $official_accounts;
    }
    
    /**
     * 获取1个公众号
     */
    static function getOneOfficialAuthAccounts($distribution_channel_id,$auth, $is_enabled)
    {
    	$official_accounts = OfficialAccount::getOneOfficialAuthAccounts($distribution_channel_id,$auth, $is_enabled);
    	return $official_accounts;
    }
    
    
    
    /**
     * 获取全部公众号
     */
    static function officialAuthSubscribeAccounts($auth, $is_enabled)
    {
    
    	$official_accounts = OfficialAccount::officialAuthSubscribeAccounts($auth, $is_enabled);
    
    	return $official_accounts;
    }
    

    /**
     * 根据distribution_channel_id获取一个可用的公众号
     */
    static function canUseOfficialAccountByChannelId($officialAccountPrams)
    {

        $is_auth = 1;

        $is_enabled = 1;

        $official_accounts = OfficialAccount::officialAuthAccountBydistributionChannelId($officialAccountPrams['distribution_channel_id'], $is_auth, $is_enabled);
        // if(isset($official_accounts[0])) {
        //     return $official_accounts[0];
        // }
        // return null;
        //\Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----'.$official_accounts);
        \Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----official_accounts');
        for ($i = 0; $i < count($official_accounts); $i++) {
            # code...

            //公众号强关用户总数
            $total_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], 'total');
            $total_fans_cancel_nums = (int)Redis::hget('day_appid_fans_nums_appid:cancel:' . $official_accounts[$i]['appid'], 'total');
            $allForceSubscribeUsers = $total_fans_add_nums - $total_fans_cancel_nums;
            // $allForceSubscribeUsers = ForceSubscribeUsers::allForceSubscribeUsers($official_accounts[$i]['appid']);
            \Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----allForceSubscribeUsers' . $allForceSubscribeUsers);

            //公众号当日强关用户数
            $day_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], date("Y-m-d"));
            // $day_fans_cancel_nums = (int)Redis::hget('day_appid_fans_nums_appid:cancel:'.$official_accounts[$i]['appid'],date("Y-m-d"));
            $todayForceSubscribeUsers = $day_fans_add_nums;
            // $todayForceSubscribeUsers = ForceSubscribeUsers::todayForceSubscribeUsers($official_accounts[$i]['appid']);
            \Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----todayForceSubscribeUsers' . $todayForceSubscribeUsers);

            if (($allForceSubscribeUsers >= $official_accounts[$i]['subscribe_top_num']) || ($todayForceSubscribeUsers >= $official_accounts[$i]['subscribe_day_maximum'])) {
                # code...
                continue;
            } else {
                $official_accounts[$i]['todayForceSubscribeUsers'] = $todayForceSubscribeUsers;
                $official_accounts[$i]['allForceSubscribeUsers'] = $allForceSubscribeUsers;
                return $official_accounts[$i];
            }


        }

    }


    /**
     * 根据distribution_channel_id获取已授权可用的所有公众号
     */
    static function officialAuthAccountBydistributionChannelId($officialAccountPrams)
    {

        $is_auth = 1;

        $is_enabled = 1;

        $official_accounts = OfficialAccount::officialAuthAccountBydistributionChannelId($officialAccountPrams['distribution_channel_id'], $is_auth, $is_enabled);
        // dd($official_accounts);
        for ($i = 0; $i < count($official_accounts); $i++) {
            # code...
            $official_accounts[$i]['todayForceSubscribeUsers'] = 0;
            $official_accounts[$i]['allForceSubscribeUsers'] = 0;
        }

        return $official_accounts;
    }


    /**
     * 根据distribution_channel_id获取可用的所有公众号
     */
    static function allOfficialAccountBydistributionChannelId($officialAccountPrams)
    {

        $is_enabled = 1;

        //$official_accounts = OfficialAccount::allOfficialAccountBydistributionChannelId($officialAccountPrams['distribution_channel_id'],$is_enabled);
        $official_accounts = OfficialAccount::allOfficialAccountBydistributionChannelIdAndGetForceType($officialAccountPrams['distribution_channel_id'], $is_enabled);
        //return $official_accounts;
        for ($i = 0; $i < count($official_accounts); $i++) {
            # code...
            $day_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], date("Y-m-d"));
            // $day_fans_cancel_nums = (int)Redis::hget('day_appid_fans_nums_appid:cancel:'.$official_accounts[$i]['appid'],date("Y-m-d"));
            $todayForceSubscribeUsers = $day_fans_add_nums;
            $official_accounts[$i]['todayForceSubscribeUsers'] = $todayForceSubscribeUsers;
            // $official_accounts[$i]['todayForceSubscribeUsers'] = ForceSubscribeUsers::todayForceSubscribeUsers($official_accounts[$i]['appid']);

            $total_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], 'total');
            $total_fans_cancel_nums = (int)Redis::hget('day_appid_fans_nums_appid:cancel:' . $official_accounts[$i]['appid'], 'total');
            $allForceSubscribeUsers = $total_fans_add_nums - $total_fans_cancel_nums;
            $official_accounts[$i]['allForceSubscribeUsers'] = $allForceSubscribeUsers;
            // $official_accounts[$i]['allForceSubscribeUsers'] = ForceSubscribeUsers::allForceSubscribeUsers($official_accounts[$i]['appid']);
        }
        return $official_accounts;
    }


    /**
     * 根据distribution_channel_id获取公众号数量
     */
    static function officialAccountCountByChannelId($officialAccountPrams)
    {


        $official_accounts = OfficialAccount::officialAccountCountByChannelId($officialAccountPrams['distribution_channel_id']);

        return $official_accounts;
    }


    /**
     * 授权公众号信息
     */
    static function authOfficialAccount($officialAccountPrams)
    {

        try {

            $official_accounts = OfficialAccount::officialAccountByAppid($officialAccountPrams['appid']);

            if (empty($official_accounts)) {

                $officialAccount = OfficialAccount::create($officialAccountPrams);
                $officialAccountArray = $officialAccount->toArray();
//                Redis::hset('official_account_info:app_id:'.$officialAccountPrams['appid'], 'official_account_info', '[{appid:'.$officialAccountPrams['appid'].'},{distribution_channel_id:'.$officialAccountPrams['distribution_channel_id'].'},{appsecret:'.$officialAccountPrams['appsecret'].'},{alias:'.$officialAccountPrams['alias'].'},{name:'.$officialAccountPrams['name'].'},{nickname:'.$officialAccountPrams['nickname'].'},{head_img:'.$officialAccountPrams['head_img'].'},{qrcode_url:'.$officialAccountPrams['qrcode_url'].'},{principal_name:'.$officialAccountPrams['principal_name'].'},{service_type_info:'.$officialAccountPrams['service_type_info'].'},{func_info:'.$officialAccountPrams['func_info'].'},{authorizer_refresh_token:'.$officialAccountPrams['authorizer_refresh_token'].'},{is_auth:'.$officialAccountPrams['is_auth'].'},{official_account_type:'.$officialAccountPrams['official_account_type'].'},{is_enabled:'.$officialAccountPrams['is_enabled'].'},{verify_txt:'.$officialAccountPrams['verify_txt'].'}]');
                Redis::hset('official_account_info:app_id:' . $officialAccountPrams['appid'], 'official_account_info', json_encode($officialAccountArray));


                return 1;

            } else {

                if (!empty($officialAccountPrams['distribution_channel_id'])) {
                    $official_accounts['distribution_channel_id'] = $officialAccountPrams['distribution_channel_id'];
                }
                if (!empty($officialAccountPrams['appsecret'])) {
                    $official_accounts['appsecret'] = $officialAccountPrams['appsecret'];
                }
                if (!empty($officialAccountPrams['alias'])) {
                    $official_accounts['alias'] = $officialAccountPrams['alias'];
                }
                if (!empty($officialAccountPrams['name'])) {
                    $official_accounts['name'] = $officialAccountPrams['name'];
                }
                if (!empty($officialAccountPrams['nickname'])) {
                    $official_accounts['nickname'] = $officialAccountPrams['nickname'];
                }
                if (!empty($officialAccountPrams['head_img'])) {
                    $official_accounts['head_img'] = $officialAccountPrams['head_img'];
                }
                if (!empty($officialAccountPrams['qrcode_url'])) {
                    $official_accounts['qrcode_url'] = $officialAccountPrams['qrcode_url'];
                }
                if (!empty($officialAccountPrams['principal_name'])) {
                    $official_accounts['principal_name'] = $officialAccountPrams['principal_name'];
                }
                if (!empty($officialAccountPrams['service_type_info'])) {
                    $official_accounts['service_type_info'] = $officialAccountPrams['service_type_info'];
                }
                if (!empty($officialAccountPrams['func_info'])) {
                    $official_accounts['func_info'] = $officialAccountPrams['func_info'];
                }
                if (!empty($officialAccountPrams['authorizer_refresh_token'])) {
                    $official_accounts['authorizer_refresh_token'] = $officialAccountPrams['authorizer_refresh_token'];
                }
                if (!empty($officialAccountPrams['official_account_type'])) {
                    $official_accounts['official_account_type'] = $officialAccountPrams['official_account_type'];
                }
                if (!empty($officialAccountPrams['verify_txt'])) {
                    $official_accounts['verify_txt'] = $officialAccountPrams['verify_txt'];
                }
                $official_accounts['is_auth'] = 1;

                $official_accounts->save();

                $officialAccountArray = $official_accounts->toArray();
                Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
//                Redis::hset('official_account_info:app_id:'.$official_accounts['appid'], 'official_account_info', '[{appid:'.$official_accounts['appid'].'},{distribution_channel_id:'.$official_accounts['distribution_channel_id'].'},{appsecret:'.$official_accounts['appsecret'].'},{alias:'.$official_accounts['alias'].'},{name:'.$official_accounts['name'].'},{nickname:'.$official_accounts['nickname'].'},{head_img:'.$official_accounts['head_img'].'},{qrcode_url:'.$official_accounts['qrcode_url'].'},{principal_name:'.$official_accounts['principal_name'].'},{service_type_info:'.$official_accounts['service_type_info'].'},{func_info:'.$official_accounts['func_info'].'},{authorizer_refresh_token:'.$official_accounts['authorizer_refresh_token'].'},{is_auth:'.$official_accounts['is_auth'].'},{official_account_type:'.$official_accounts['official_account_type'].'},{is_enabled:'.$official_accounts['is_enabled'].'},{verify_txt:'.$official_accounts['verify_txt'].'}]');


                return 1;

            }
        } catch (\Exception $e) {
            // \Log("授权公众号日志：".$e);
            return 0;

        }


    }


    /**
     * 更新公众号信息
     */
    static function updateOfficialAccount($officialAccountPrams)
    {

        try {

            $official_accounts = OfficialAccount::officialAccountByAppidAndChannelId($officialAccountPrams['appid'], $officialAccountPrams['distribution_channel_id']);

            // dd($official_accounts);

            if (empty($official_accounts)) {
                return 2;
            } else {
                $official_accounts['appsecret'] = $officialAccountPrams['appsecret'];
                $official_accounts['subscribe_top_num'] = $officialAccountPrams['subscribe_top_num'];
                $official_accounts['subscribe_day_maximum'] = $officialAccountPrams['subscribe_day_maximum'];
                $official_accounts->save();

                $officialAccountArray = $official_accounts->toArray();
                Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
//                Redis::hset('official_account_info:app_id:'.$official_accounts['appid'], 'official_account_info', '[{appid:'.$official_accounts['appid'].'},{distribution_channel_id:'.$official_accounts['distribution_channel_id'].'},{appsecret:'.$official_accounts['appsecret'].'},{alias:'.$official_accounts['alias'].'},{name:'.$official_accounts['name'].'},{nickname:'.$official_accounts['nickname'].'},{head_img:'.$official_accounts['head_img'].'},{qrcode_url:'.$official_accounts['qrcode_url'].'},{principal_name:'.$official_accounts['principal_name'].'},{service_type_info:'.$official_accounts['service_type_info'].'},{func_info:'.$official_accounts['func_info'].'},{authorizer_refresh_token:'.$official_accounts['authorizer_refresh_token'].'},{is_auth:'.$official_accounts['is_auth'].'},{official_account_type:'.$official_accounts['official_account_type'].'},{is_enabled:'.$official_accounts['is_enabled'].'},{verify_txt:'.$official_accounts['verify_txt'].'}]');

                return 1;
            }

        } catch (\Exception $e) {
            // \Log("更新公众号日志：".$e);

            return 0;

        }

    }

    /**
     * 更新公众号信息
     */
    static function updateOfficialAccountTopNum($officialAccountPrams)
    {

        try {

            $official_accounts = OfficialAccount::officialAccountById($officialAccountPrams['id']);

            // dd($official_accounts);

            if (empty($official_accounts)) {
                return 2;
            } else {
                //$official_accounts['appsecret'] = $officialAccountPrams['appsecret'];
                $official_accounts['subscribe_top_num'] = $officialAccountPrams['subscribe_top_num'];
                $official_accounts['subscribe_day_maximum'] = $officialAccountPrams['subscribe_day_maximum'];
                $official_accounts['sort_no'] = $officialAccountPrams['sort_no'];
                $official_accounts->save();

                $officialAccountArray = $official_accounts->toArray();
                Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
//                Redis::hset('official_account_info:app_id:'.$official_accounts['appid'], 'official_account_info', '[{appid:'.$official_accounts['appid'].'},{distribution_channel_id:'.$official_accounts['distribution_channel_id'].'},{appsecret:'.$official_accounts['appsecret'].'},{alias:'.$official_accounts['alias'].'},{name:'.$official_accounts['name'].'},{nickname:'.$official_accounts['nickname'].'},{head_img:'.$official_accounts['head_img'].'},{qrcode_url:'.$official_accounts['qrcode_url'].'},{principal_name:'.$official_accounts['principal_name'].'},{service_type_info:'.$official_accounts['service_type_info'].'},{func_info:'.$official_accounts['func_info'].'},{authorizer_refresh_token:'.$official_accounts['authorizer_refresh_token'].'},{is_auth:'.$official_accounts['is_auth'].'},{official_account_type:'.$official_accounts['official_account_type'].'},{is_enabled:'.$official_accounts['is_enabled'].'},{verify_txt:'.$official_accounts['verify_txt'].'}]');

                return 1;
            }

        } catch (\Exception $e) {

            \Log("updateOfficialAccountTopNum_ept：" . $e->getMessage());
            return 0;

        }

    }

    /**
     * 取消公众号授权
     */
    static function cancelAuthOfficialAccount($officialAccountPrams)
    {

        try {

            // $official_accounts = OfficialAccount::officialAccountByAppidAndChannelId($officialAccountPrams['appid'],$officialAccountPrams['distribution_channel_id']);
            $official_accounts = OfficialAccount::officialAccountByAppid($officialAccountPrams['appid']);

            if (empty($official_accounts)) {


                return 2;

            } else {

                $official_accounts['is_auth'] = 0;
                $official_accounts['cancel_auth_time'] = date("Y-m-d H:i:s");
                $official_accounts->save();

                $officialAccountArray = $official_accounts->toArray();
                Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
//                Redis::hset('official_account_info:app_id:'.$official_accounts['appid'], 'official_account_info', '[{appid:'.$official_accounts['appid'].'},{distribution_channel_id:'.$official_accounts['distribution_channel_id'].'},{appsecret:'.$official_accounts['appsecret'].'},{alias:'.$official_accounts['alias'].'},{name:'.$official_accounts['name'].'},{nickname:'.$official_accounts['nickname'].'},{head_img:'.$official_accounts['head_img'].'},{qrcode_url:'.$official_accounts['qrcode_url'].'},{principal_name:'.$official_accounts['principal_name'].'},{service_type_info:'.$official_accounts['service_type_info'].'},{func_info:'.$official_accounts['func_info'].'},{authorizer_refresh_token:'.$official_accounts['authorizer_refresh_token'].'},{is_auth:'.$official_accounts['is_auth'].'},{official_account_type:'.$official_accounts['official_account_type'].'},{is_enabled:'.$official_accounts['is_enabled'].'},{verify_txt:'.$official_accounts['verify_txt'].'}]');

                return 1;

            }

        } catch (\Exception $e) {

            // \Log("取关公众号日志：".$e);
            return 0;

        }

    }

    /**
     * 获取服务号授权URL
     */
    static function officialAccountAuthUrl($officialAccountPrams)
    {

        try {
            $officialAuthUrl = OfficialAuthUrl::officialAuthUrl();

            $base_url = $officialAuthUrl['base_url'];

            $official_account_type = $officialAuthUrl['official_account_type'];
            $group_nick = $officialAuthUrl['group_nick'];
            $redirect_url = $officialAuthUrl['redirect_url'];
            $timestamp = time();
            $distribution_channel_id = $officialAccountPrams['distribution_channel_id'];

            $params = compact('official_account_type', 'group_nick', 'redirect_url', 'timestamp', 'distribution_channel_id');

            $params['sign'] = get_sign($params);
            return $base_url . http_build_query($params);

        } catch (\Exception $e) {
            // \Log("获取公众号授权URL日志：".$e);
        }


    }


    //-----------用户---------------

    /**
     * 添加用户
     */
    static function saveOfficialWechatUser($officialWechatUserPrams)
    {

        try {

            UserService::addUser($officialWechatUserPrams);
            return 1;

        } catch (\Exception $e) {
            // \Log("增加用户日志：".$e);
            return 0;
        }

    }


    /**
     * 获取用户
     */
    static function getOfficialWechatUser($officialWechatUserPrams)
    {

        return UserService::getById($officialWechatUserPrams['id']);

    }

    /**
     * 获取托管公众号列表
     */
    static function getTrusteeshipOfficialAccounts()
    {
        return OfficialAccount::getTrusteeshipOfficialAccounts();

    }


    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/check_gzh_ban 检查公众号是否被封
     * @apiGroup OfficialAccount
     * @apiName check_gzh_ban
     */
    static function check_gzh_ban()
    {

        \Log::info('check_gzh_ban_start');

        $officail_accounts = OfficialAccountService::officialAuthAccounts(1, 1);
        //\Log::info($officail_accounts);
        foreach ($officail_accounts as $key=>$officail_account) {
            try {
                //         		if($officail_account->appid != 'wxdbc486f1b4f6a8c3') {
                //         			\Log::info('not_in:'.$officail_account->appid);
                //         			continue;
                //         		}

                $timestamp = time();
                $params = array('gzh_app_id' => $officail_account->appid, "timestamp" => $timestamp);
                $sign = get_sign($params);

                \Log::info('check_gzh_ban_start,appid:' . $officail_account->appid);

                //\Log::info('check_gzh_ban_end,appid:'.$officail_account->appid);
                //\Log::info($resJson);
                
                $phone_arr = [
	                '15858178353',// 君平
	                '13858057394',// 张总
	                '15868100210',// 栋波
	                '18457087995',// 刘杰
	                '15088790066',// 陈帅军
	                '18072713392',// 斯嘉杰
	                '18668029091',// 赵娃
	                '15268462210',// 池佳美
	                '13575592290',// 金碧莹
	                '18657562939',// 裘俊杰
	                '13758229484',// 潘超俊
	                '18668420256' // 灵杰
                ];
                
                $test = false;
                $resJson = [];
                if($test){
                	\Log::info('test_phone_send:'.$key);
                	$phone_arr = ['18668420256'];
                	$resJson = ['code'=>1,'data'=>0];
                	if($key == 1){
                		\Log::info('test_phone_send_end:'.$key);
                		break;
                	}
                }else{
                	$resJson = self::getUserClient()->request("GET", "check_gzh_ban",
                			['query' => ['gzh_app_id' => $officail_account->appid, 'timestamp' => $timestamp, 'sign' => $sign], 'connect_timeout' => 10]
                	)->getBody()->getContents();
                	$resJson = object_to_array(json_decode($resJson));
                }

                $redis_key = 'check_gzh_ban_altert:appid:' . $officail_account->appid;
                //\Log::info('check_gzh_ban_redis_key:'.$redis_key);

                // TODO发送短信
                if ($resJson['code'] == 1 && $resJson['data'] == 0) {

                    $redis_val = Redis::get($redis_key);
                    \Log::info('check_gzh_ban_fail:' . $officail_account->appid . ' redis_val:' . $redis_val);
                    if ($redis_val) {
                        \Log::info('check_gzh_ban_already_notice_continue:' . $officail_account->appid . ' redis_val:' . $redis_val);
                        continue;
                    }
                    
                    // 获取公司和商务信息,短信限制20个字符的变量
                    $distribution_channel_id = $officail_account->distribution_channel_id;
                    $distribution_channel_info = Channel::get_distribution_channel_info($distribution_channel_id);
                    $company_name = isset($distribution_channel_info->company_name)?$distribution_channel_info->company_name:'';
                    $business_name = isset($distribution_channel_info->person_in_charge_name)?$distribution_channel_info->person_in_charge_name:'';
                    
                    \Log::info('check_gzh_ban_distribution_channel_id:'.$distribution_channel_id.' business_name:'.$business_name.' company_name:'.$company_name);
                    
                    foreach ($phone_arr as $phone) {
//                         $res = SMS::send($phone, $content);
                        
                        $param = array('official_account_name'=> $officail_account->nickname,'business_name'=>$business_name,'company_name'=>$company_name);  
                        $res = AliSMS::send($phone,'forbidden_remind',$param);
                        	
                        Redis::set($redis_key, date('Y-m-d H:i:s'));
                        \Log::info('send_phone:' . $phone . ' appid:' . $officail_account->appid . ' res:' . json_encode($res));
                    }
                    

                    // 日上限设置为0
                    DB::table('official_accounts')->where(['appid' => $officail_account->appid])->update(['subscribe_day_maximum' => 0, 'updated_at' => date('Y-m-d H:i:s')]);

                } else {
                    \Log::info('check_gzh_ban_suc:' . $officail_account->appid);
                    // 解封了就删掉
                    Redis::del($redis_key);
                }

            } catch (\Exception $e) {
                \Log::info($e);
            }
        }
        \Log::info('check_gzh_ban_end');
        return 1;
        //return response()->success(['status'=>'1']);

    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/get_short_url 获取短连接
     * @apiGroup OfficialAccount
     * @apiName get_short_url
     */
    static function get_short_url($distribution_channel_id,$url)
    {
    
    	$result = ['code'=>0,'url'=>'','msg'=>''];
    
    	$officail_account = OfficialAccountService::getOneOfficialAuthAccounts($distribution_channel_id,1, 1);
    	$gzh_app_id = isset($officail_account->appid)?$officail_account->appid:env('WECHAT_AUTH_APPID');
    	\Log::info('get_short_url_start:'.$distribution_channel_id.' $gzh_app_id:'.$gzh_app_id.' url:'.$url);
    	 
    	$short_url = '';
    	try {
    		    $url = urlencode($url);
    			$timestamp = time();
    			$params = array('gzh_app_id' => $gzh_app_id,'url'=>$url, "timestamp" => $timestamp);
    			$sign = get_sign($params);
    			
    			$resJson = self::getUserClient()->request("GET", "get_short_url",
    					['query' => ['gzh_app_id' => $gzh_app_id,'url'=>$url, 'timestamp' => $timestamp, 'sign' => $sign], 'connect_timeout' => 10]
    			)->getBody()->getContents();
    			$resJson = object_to_array(json_decode($resJson));
    			\Log::info('get_short_url_end,appid:'.$gzh_app_id);
    			\Log::info($resJson);
    			$short_url = isset($resJson['data'])?$resJson['data']:'';
    			$msg = isset($resJson['msg'])?$resJson['msg']:'';
    			if(!empty($short_url)){
    				$result['code'] = 1;
    				$result['url'] = $short_url;
    			}else{
    				$result['msg'] = $msg;
    			}
    
    	} catch (\Exception $e) {
    			\Log::info($e->getMessage());
    			$result['msg'] = '短链接转化异常，请稍后再试！';
    	}
    	\Log::info('get_short_url_end:'.json_encode($result));
    	return $result;
    }


    static public function getUserClient()
    {
        return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
    }

    /**
     * 获取渠道粉丝阈值信息
     */
    static public function getBusinessChannelData($channels, $distribution_channel_name = '', $official_account_name='',$isAll = false)
    {
        return OfficialAccount::getBusinessChannelData($channels, $distribution_channel_name, $official_account_name,$isAll);
    }
}