<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use think\Db;

/**
 * 提现
 */
class Takecash extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //提现配置
    public function take_cash_config(){

        $min_withdrawal_money = config('site.min_withdrawal_money') ? config('site.min_withdrawal_money') : 1;
        $max_withdrawal_money = config('site.max_withdrawal_money') ? config('site.max_withdrawal_money') : 50000;
        $data = [
            'money' => model('walletcompany')->getwallet($this->auth->company_id,'money'),
            'bank_name' => $this->auth->bank_name,
            'bank_branchname' => $this->auth->bank_branchname,
            'bank_account' => $this->auth->bank_account,
            'bank_card' => $this->auth->bank_card,
            'min' => $min_withdrawal_money,
            'max' => $max_withdrawal_money,
        ];

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

    //提现
    public function take_cash(){

        $money = floatval(input('money', 0, 'trim'));

        //格式
        if(empty($money) || $money <= 0){
            $this->error('请输入正确提现金额');
        }

        //最低提现金额
        $min_withdrawal_money = config('site.min_withdrawal_money') ? config('site.min_withdrawal_money') : 1;
        $max_withdrawal_money = config('site.max_withdrawal_money') ? config('site.max_withdrawal_money') : 50000;
        if ($money < $min_withdrawal_money) {
            $this->error('最低提现金额' . $min_withdrawal_money . '元');
        }
        if ($money > $max_withdrawal_money) {
            $this->error('最高提现金额' . $max_withdrawal_money . '元');
        }

        //查重
        $check = Db::name('company_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
        if($check){
            $this->error('您已经申请了提现,请等待审核');
        }

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

        //查询提现手续费百分比
        $withdrawal_server_fee = (int)config('site.withdrawal_server_fee') ? : 0;
        if ($withdrawal_server_fee < 0 || $withdrawal_server_fee >= 100) {
            $this->error('提现手续费异常');
        }

        //
        $real_money = bcdiv(bcmul($money,bcsub(100,$withdrawal_server_fee,2),2),100,2);
        $data = [
            'order_no' => createUniqueNo('T',$this->auth->id),
            'user_id' => $this->auth->id,
            'money' => $money,
            'real_money' => $real_money,
            'bank_name' => $this->auth->bank_name,
            'bank_branchname' => $this->auth->bank_branchname,
            'bank_account' => $this->auth->bank_account,
            'bank_card' => $this->auth->bank_card,
            'status' => 0,
            'createtime' => time(),
            'updatetime' => time(),
        ];

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

        $rs_wallet = model('walletcompany')->lockChangeAccountRemain($this->auth->id,'money',-$money,101,'提现','company_take_cash',$logid);
        if($rs_wallet['status'] === false){
            Db::rollback();
            $this->error($rs_wallet['msg']);
        }

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

    //提现记录
    public function take_cash_log(){
        $list = Db::name('company_take_cash')->where(['user_id'=>$this->auth->id])->autopage()->select();

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

}