<?php

    namespace app\controller;


    use app\business\GoodsBusiness;
    use app\business\LoginBusiness;
    use app\business\PayorderBusiness;
    use app\business\StreamBusiness;
    use app\business\WithdrawBusiness;
    use Illuminate\Support\Arr;
    use Respect\Validation\Validator;
    use support\Db;
    use support\Redis;
    use support\Request;
    use hg\apidoc\annotation as Apidoc;
    use Webman\Captcha\CaptchaBuilder;
    use Webman\Captcha\PhraseBuilder;

    #[Apidoc\Title("提现")]
    #[Apidoc\Group("Stream")]
    #[Apidoc\Sort(5)]
    class WithdrawController
    {
        #[Apidoc\Title("提现申请")]
        #[Apidoc\Url("api/withdraw.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("money", type: "int", require: true, desc: "提现金额", mock: 10)]
        #[Apidoc\Param("mold", type: "int", require: true, desc: "钱包类型 ", mock: 1)]
        #[Apidoc\Param(name: "pay_characteristic", type: "int", require: true, desc: '支付通道标识', default: '1')]
        public function applyfor(Request $request)
        {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            Db::beginTransaction();
            try {
                Validator::input($param, [
                    'money' => Validator::notEmpty()->Number()->setName('提现金额'),
                    'mold'  => Validator::notEmpty()->intType()->setName('钱包类型'),
                ]);
                if (!empty(Redis::get($param['mold'] . $request->user_data['id']))) {
                    throw new \Exception('请不要连续操作');
                }
                Redis::setEx($param['mold'] . $request->user_data['id'], 10, $request->user_data['id']);

                $name = Db::table('wa_user_identity')->where('uid', $request->user_data['id'])->value('name');
                if (empty($name)) {
                    throw new \Exception('请实名后再进行提现!');
                }

//                $is_buy   = Db::table('wa_payorder')
//                    ->where('user_id',  $param['user_data']['id'])
//                    ->where('is_pay', 2)
//                    ->where('goods_type', 1)
//                    ->first();
//                if(!$is_buy){
//                    Db::rollBack();
//                    return error('功能暂未开放!',[],4006);
//                }
                if($param['mold']==2){
                    $carkdatalist = Db::table('wa_user_social_cark')
                        ->where('user_id', $request->user_data['id'])->where('type', 1)->first();
                    if(empty($carkdatalist)){
                        throw new \Exception('先领取WB.U卡在兑换!');
                    }
                    $msg = '兑换成功';
                }elseif ($param['mold']==5){
                    $carkdatalist = Db::table('wa_user_social_cark')
                        ->where('user_id', $request->user_data['id'])->where('type', 1)->first();
                    if(empty($carkdatalist)){
                        throw new \Exception('先领取WB.U卡在兑换!');
                    }
                    $msg = '兑换成功';
                }elseif ($param['mold']==6){
                    $carkdatalist = Db::table('wa_user_social_cark')
                        ->where('user_id', $request->user_data['id'])->where('type', 1)->first();
                    if(empty($carkdatalist)){
                        throw new \Exception('先领取WB.U卡在来提现!');
                    }
                    $msg = '提现成功';
                }elseif ($param['mold']==10){
                    $msg = '解冻成功';
                }elseif ($param['mold']==1){
                    $msg = '提现成功';
                }elseif ($param['mold']==55){
                    $msg = '提现成功';
                } else{
                    $account_holder = Db::table('wa_bank_card')->where('uid', $request->user_data['id'])->value('account_holder');
                    if (empty($account_holder)) {
                        throw new \Exception('请绑定银行卡!');
                    }
                    if ($account_holder != $name) {
                        throw new \Exception('实名人和开户人不一致,请绑定实名人的银行卡!');
                    }
                    $msg = '提现成功';
                }
                $arr = WithdrawBusiness::applyfor($param);
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success($arr, $msg);

        }

        #[Apidoc\Title("提现记录")]
        #[Apidoc\Url("api/withdraw/stream.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("page", type: "int", require: true, desc: "页面", mock: 1)]
        #[Apidoc\Param("limit", type: "int", require: true, desc: "输入条数", mock: 10)]
        #[Apidoc\Param("mold", type: "int", require: true, desc: "钱包类型 不传查所有  ", mock: 1)]
        #[Apidoc\Returned(name: "order_no", type: "string", require: true, desc: '订单号', default: '23213213213217182937291')]
        #[Apidoc\Returned(name: "money", type: "string", require: true, desc: '提现金额', default: '100.00')]
        #[Apidoc\Returned(name: "status", type: "string", require: true, desc: '提现状态 1 待审核 2打款中 3审核通过 4驳回', default: '1')]
        #[Apidoc\Returned(name: "affiliated_bank", type: "string", require: true, desc: '归属银行', default: '中国银行')]
        #[Apidoc\Returned(name: "account_holder", type: "string", require: true, desc: '开户人', default: '张三')]
        #[Apidoc\Returned(name: "card_number", type: "string", require: true, desc: '卡号', default: '12321321376763274682374681')]
        #[Apidoc\Returned(name: "created_at", type: "date", require: true, desc: '申请时间', default: '2023-05-20 23:49:49')]
        #[Apidoc\Returned(name: "remarks", type: "date", require: true, desc: '驳回备注', default: '')]
        public function stream(Request $request)
        {
            $param              = $request->param_data;
            $param              = Arr::only($param, ['page', 'limit', 'mold']);
            $param['user_data'] = $request->user_data;
            try {
                Validator::input($param, [
                    'page'  => Validator::notEmpty()->intType()->setName('页码'),
                    'limit' => Validator::notEmpty()->intType()->setName('输出条数'),
                ]);
                $data = WithdrawBusiness::stream($param);
            } catch (\Throwable $exception) {
                return error($exception->getMessage());
            }
            return success($data['data'], '成功', 200, $data['total']);
        }


        #[Apidoc\Title("转账")]
        #[Apidoc\Url("api/withdraw/transfer.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("money", type: "int", require: true, desc: "提现金额", mock: 10)]
        #[Apidoc\Param("account_number", type: "string", require: true, desc: "收款账号：转账时候传 ", mock: 1)]
        #[Apidoc\Param(name: "pay_characteristic", type: "int", require: true, desc: '支付通道标识', default: '1')]
        public function transfer(Request $request)
        {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            Db::beginTransaction();
            try {
                Validator::input($param, [
                    'money' => Validator::notEmpty()->Number()->setName('提现金额'),
                    'account_number'  => Validator::notEmpty()->setName('转账账号'),
                ]);
                if (!empty(Redis::get('1212' . $request->user_data['id']))) {
                    throw new \Exception('请不要连续操作');
                }
                Redis::setEx('1212' . $request->user_data['id'], 10, $request->user_data['id']);

                $name = Db::table('wa_user_identity')->where('uid', $request->user_data['id'])->value('name');
                if (empty($name)) {
                    throw new \Exception('请实名后再进行转账!');
                }

                $accountUser = Db::table('wa_users')->where('mobile',$param['account_number'])->first();
                if(empty($accountUser)){
                    throw new \Exception('转账账号不存在!');
                }

                $userdata = Db::table('wa_users')->where('id',$param['user_data']['id'])->first();

                if(!empty($userdata) && $userdata->money_five>=$param['money']){
                    StreamBusiness::delStream($param['user_data']['id'], $param['money'], streamType17, moldType5, moldTypefild5, $param['user_data']['id']);
                    StreamBusiness::addStream($accountUser->id, $param['money'], streamType17, moldType5, moldTypefild5, $param['user_data']['id']);

                }else{
                    throw new \Exception('世界币不足!');
                }
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success([], '转账成功');

        }



    }
