<?php

namespace App\Console\Commands;

use App\Models\Amount;
use App\Models\Store;
use App\Handlers\SignHandler;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

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

    /**
     * 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()
    {
        Log::info('提现修正8任务正在运行 ing');
        $date=date("Y-m-d",strtotime('-8 days'));
        $handle=new SignHandler();
        $refunds=Amount::where('type',3)->where('status',1)->whereDate('created_at',$date)->orderBy('id')->get();
        foreach($refunds as $key=>$val){
            $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$val->store_id)->value('sub_mchid');
            $withdraw_id=$val->transaction_id;
            $out_request_no=$val->order_no;
            $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
//        $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$out_request_no.'?sub_mchid='.$sub_mchid;
            $merchant_id=config('wechat.payment.default.mch_id');//商户号
            $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
            $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
            $timestamp=time();//时间戳
            $nonce=$handle->nonce_str();//随机字符串
            $body="";
            $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
            $header=[
                'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
                'Accept:application/json',
                'User-Agent:' . $merchant_id,
                'Content-Type:application/json',
                'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
            ];
            $result=$handle->curl($url,'',$header,'GET');
            $result=json_decode($result,true);
//            log::info('ssss---ssss---ssss---ssss');
            if($val->status_code<>$result['status']){
                Log::info('提现8状态有误：'.$val->store_id.'---'.$val->order_no.'---'.$result['status']);
                Store::withTrashed()->where('is_failure',0)->where('id',$val->store_id)->decrement('withdrawal',$val->money);
                if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
                    $reason=$result['reason'];
                }else{
                    $reason=null;
                }
                Amount::where('id',$val->id)
                    ->update([
                        'status'=>0,
                        'status_code'=>$result['status'],
                        'reason'=>$reason
                    ]);

            }
        }
    }
}
