<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Modules\Channel\Services\ChannelService;
use App\Libs\Pay\WechatPay;
use DB;
use App\Libs\SMS;
use App\Libs\Pay\Merchants\AllinPay;
use App\Libs\PayClient;
class PayChannelCheck extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pay:check';

    /**
     * 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()
    {
        $this->start();
    }

    public function start(){
        $res = $this->getPayChannel();
        print_r($res);
        if(empty($res)){
            //$this->sendMsg();
            //$this->changechannel();
        }
    }

    private function getPayChannel(){
        $channel = ChannelService::getById(5);
        $pay_merchant = DB::table('pay_merchants')->select('appid','source','config_info')->where('id',$channel->pay_merchant_id)->where('is_enabled',1)->first();
        $config = json_decode($pay_merchant->config_info,true);
        $wechatPay = WechatPay::instance($pay_merchant->source,$config);
        $data = [
            'body'=>'novel buy',
            'detail'=>'novel buy',
            'trade_no'=>date("YmdHis").'check',
            'openid'=>'oEteU1USo_yaT0NzA4QttI6Tl7gU',
            'price'=>1,
            'create_ip'=>'47.96.174.172',
            'remark'=>json_encode(
                [
                    'uid'=>13,
                    'dcd'=>5,
                    'bid'=>0,
                    'cp'=>'47.96.174.172',
                    'pms'=>$pay_merchant->source,
                    'pmi'=>$channel->pay_merchant_id,
                    'pd'=>1,
                    'soi'=>1,
                ]
            )
        ];
        $pay_info = $wechatPay->send($data);
        return $pay_info;
    }

    private function changechannel(){
        DB::table('distribution_channels')->where('phone','!=','')->update(['pay_merchant_id'=>3]);
    }

    private function sendMsg(){
        //$phone_arr = ['15868100210','18072713392','15088790066','13706510321','13858057394','18668029091'];
        $phone_arr = ['18668029091'];
        foreach ($phone_arr as $phone){
            SMS::send($phone,'支付通道已经切换到官方支付了');
        }

    }

    public function payChannelSend($type,$config){
        switch ($type){
            case 'ALLINPAY':
                $wechatPay = new AllinPay($config);

            break;
        }
    }
}
