<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 提现
 */
class Takecash extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //提现配置
    public function take_cash_config(){
        $config = Db::name('take_cash_config')->order('weigh asc,id asc')->select();

        $data = [
            'config' => $config,
            'jewel' => model('wallet')->getwallet($this->auth->id,'jewel'),
            'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
            'user_alipay' => Db::name('user_alipay')->where('user_id',$this->auth->id)->find(),
            'remark' => config('site.takecash_rule'),
        ];

        $this->success('success',$data);
    }

    //提现
    public function take_cash(){
        $rc_id     = input('rc_id',0);
        $type = input('type',1);

        if(!$rc_id){
            $this->error('请选择金额');
        }

        if (!in_array($type,[1,2])) {
            $this->error('未知的提现类型');
        }

        if(!$this->user_auth_limit()){
            $this->error('请先完成实名认证');
        }

        //赋值money
        $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
        $money = $recharge_config['money'] ?: 0;
        $jewel = $recharge_config['jewel'] ?: 0;


        //
        if($money<=0 || $jewel<=0)
        {
            $this->error('金额必须大于0');
        }


        /*$check = Db::name('take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
        if($check){
            $this->error('您已经申请了提现,请等待审核');
        }*/
        $check = Db::name('take_cash')->where('user_id',$this->auth->id)->whereTime('createtime','today')->find();
        if($check){
            $this->error('您今天已经申请了提现,明天再来吧');
        }

        $user_money = model('wallet')->getwallet($this->auth->id,'jewel');
        if($jewel > $user_money){
            $this->error('提现金额不能大于可提现余额');
        }

        if($type == 1){
            $table_name = 'user_alipay';
            $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }else{
            $table_name = 'user_bank';
            $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }

        //平台手续费
        $plat_bilv = $recharge_config['plat_bilv'];
        $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);

        //减去手续费,得实得金额
        $get_money = bcsub($money,$plat_money,2);

        $data = [
            'user_id' => $this->auth->id,
            'jewel' => $jewel,
            'money' => $money,
            'plat_bilv'  => $plat_bilv,
            'plat_money' => $plat_money,
            'get_money'  => $get_money,
            'type' => $type,
            'acount_json' => json_encode($account_json),
            'createtime' => time(),
            'updatetime' => time(),
            'status' => 0,
        ];
        Db::startTrans();

        $log_id = Db::name('take_cash')->insertGetId($data);
        if(!$log_id){
            Db::rollback();
            $this->error('提现失败');
        }

        //扣除money
        $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'jewel',-$jewel,30,'提现(审核中)','take_cash',$log_id);
        if($rs_wallet['status']===false)
        {
            Db::rollback();
            $this->error($rs_wallet['msg']);
        }

        Db::commit();
        $this->success('申请成功请等待审核');
    }

    //提现记录
    public function take_cash_log(){
        $list = Db::name('take_cash')->field('acount_json',true)->where(['user_id'=>$this->auth->id])->order('id desc')->autopage()->select();
        foreach($list as $key => &$val){
            $val['remark'] = '';

            if($val['type'] == 1){
                $val['remark'] = '支付宝提现';
            }else{
                $val['remark'] = '银行卡提现';
            }
        }

        $this->success('success',$list);
    }

}