<?php

namespace App\Listeners;

use App\Events\WithdrawalsCompleteEvent;
use App\Jobs\SendTemplateNotice;
use App\Models\Partner;
use App\Models\Wechat;

class SendResultOfWithdrawalsNotifyListener
{
    public function __construct()
    {
    }

    public function handle(WithdrawalsCompleteEvent $event): void
    {
        // 获取提现记录和用户信息
        $withdrawal = $event->withdrawal;
        $user_id = $withdrawal->user_id;

        $openid = Wechat::query()->where('user_id',$user_id)->where('type','official')->value('openid');

        $param = [
            'touser'=>$openid,
            'template_id'=>'',
            'url'=>config('app.url')."h5/#/withdrawalRecord",
            'data'=>[]
        ];

        if ($withdrawal->status == 2){
            //通过
            $param['template_id'] = config('wechat.tpls.withdrawal_success_notice_to_partner');
            $bank = $withdrawal->info['bank_of_deposit']."(".substr($withdrawal->info['bank_num'],-4,4).")";
            $param['data'] = [
                'keyword1'=>['value'=>$withdrawal->send_amount],
                "keyword2"=>['value'=>$bank],
                "keyword3"=>['value'=>$withdrawal->info['bank_name']],
                "keyword4"=>['value'=>$withdrawal->send_amount ."元"],
                "keyword5"=>['value'=>date('Y-m-d H:i:s')],
            ];

        }elseif ($withdrawal->status == 3){
            //驳回
            $param['template_id'] = config('wechat.tpls.withdrawal_fail_notice_to_partner');
            $param['data'] = [
                "keyword1"=>['value'=>date('Y-m-d H:i:s')],
                'keyword2'=>['value'=>$withdrawal->send_amount],
                "keyword3"=>['value'=>$withdrawal->reason],
            ];
        }

        SendTemplateNotice::dispatch($param)->onQueue('health');
    }
}
