<?php

namespace app\business;

use Illuminate\Support\Arr;
use support\Db;
use support\Redis;

class BankCardBusiness
{
    /** 用户卡号信息
     * @param array $param
     * @return void
     */
    static public function data(array $param)
    {
        $data=Db::table('wa_bank_card')->where(function ($query)use ($param){
            if(Arr::get($param,'uid')){
                $query->where('uid',$param['uid']);
            }
        })->first();
        return $data;
    }

    /**绑定和修改实名信息
     * @param array $param
     * @return void
     */
    static public function UpData(array $param)
    {
        try {

            $data=Db::table('wa_bank_card')->where('uid',$param['user_data']['id'])->first();
            $user_identity_name=Db::table('wa_user_identity')->where('uid',$param['user_data']['id'])->value('name');
            if(empty($user_identity_name)){
                throw new \Exception('请先实名!');
            }
            if($user_identity_name != $param['account_holder']){
                throw new \Exception('实名人和开户人不一致,请绑定实名人的银行卡!');
            }
            if(empty($data)){

                /** 绑定 */
                Db::table('wa_bank_card')->insert([
                    'affiliated_bank'       =>$param['affiliated_bank'],
                    'account_holder'        =>$param['account_holder'],
                    'card_number'           =>$param['card_number'],
                    'uid'                   =>$param['user_data']['id'],
                    'created_at'            =>date('Y-m-d H:i:s'),
                    'updated_at'            =>date('Y-m-d H:i:s'),
                ]);
            }else{
//                throw new \Exception('请联系客服进行修改!');
                /** 修改 */
                Db::table('wa_bank_card')->where('uid',$param['user_data']['id'])->update([
                    'affiliated_bank'       =>$param['affiliated_bank'],
                    'account_holder'        =>$param['account_holder'],
                    'card_number'           =>$param['card_number'],
                    'o_affiliated_bank'     =>$data->affiliated_bank,
                    'o_account_holder'      =>$data->account_holder,
                    'o_card_number'         =>$data->o_card_number,
                    'created_at'            =>date('Y-m-d H:i:s'),
                    'updated_at'            =>date('Y-m-d H:i:s'),
                ]);
            }
        }catch (\Throwable $exception){
            throw new \Exception($exception->getMessage());
        }
        return true;
    }
}