<?php

namespace plugin\admin\app\repositories;

use app\api\repositories\MoneyLogRepositories;
use Illuminate\Support\Arr;
use plugin\admin\app\model\BankCard;
use plugin\admin\app\model\System;
use plugin\admin\app\model\User;
use plugin\admin\app\model\Withdraw;
use support\Log;
use yzh52521\EasyHttp\Http;

class WithdrawladingRepositories
{

    /** @var string 商户号 */
    private $mchid = '200064';
    /** @var string 秘钥 */
    private $mch_key = '1a55fd7bc23fed9030b7ed593ba12866';

    /** 申请提现
     * @param $order 订单号
     * @param $money 下发金额
     * @param $bankname 归属银行
     * @param $accountname 开户人
     * @param $accountno  卡号
     * @return array|mixed
     * @throws \Exception
     */
    public function jiujbatchdelivery($order, $money, $bankname, $accountname, $accountno)
    {
        try {
            $arr              = [
                'mchid'       => $this->mchid,
                'send'        => '1',
                'channel'     => '903',
                'tradeid'     => $order,
                'type'        => 'cny',
                'bankname'    => $bankname,
                'subbranch'   => '123',
                'cardnumber'  => $accountno,
                'accountname' => $accountname,
                'money'       => bcmul($money, 1, 2),
                'paydate'     => time(),

            ];
            $arr['sign']      = $this->jthsign($arr);
            $arr['notifyurl'] = getenv('API_HOST') . '/api/issue_three.html';


            $rst = Http::post('https://hongyun.heimao.uk/api/order', $arr)->array();

            Log::channel('issue')->info('调下发', [
                '请求数据' => $arr,
                '回调数据' => $rst
            ]);
            if (Arr::get($rst, 'code') != 1) {
                throw new \Exception(Arr::get($rst, 'msg', '请求失败'));
            }
        } catch (\Throwable $exception) {
            throw new \Exception($exception->getMessage());
        }
        return $rst;
    }

    /** 救急签名
     * @param $array
     * @return void
     */
    public function jthsign($array = [])
    {
        ksort($array);                               //ASCII码排序
        $md5str = "";
        foreach ($array as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }

        $sign = md5($md5str . "key=" . $this->mch_key);
        return $sign;
    }

}