<?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'),
            'min' => $min_withdrawal_money,
            'max' => $max_withdrawal_money,
            'bank_info' => Db::name('company_bank')->where('company_id',$this->auth->company_id)->find(),
        ];

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

    //提现
    public function take_cash(){
        if($this->auth->type != 1){
            $this->error('只有门店老板才能操作');
        }

        $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->company_id,'status'=>0])->find();
        if($check){
            $this->error('您已经申请了提现,请等待审核');
        }

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

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

        $bank_info = Db::name('company_bank')->where('company_id',$this->auth->company_id)->find();
        if(empty($bank_info)){
            $this->error('请先设置银行卡');
        }

        //
        $real_money = bcdiv(bcmul($money,bcsub(100,$withdrawal_service_fee,2),2),100,2);
        $data = [
            'order_no' => createUniqueNo('T',$this->auth->id),
            'user_id' => $this->auth->company_id,
            'money' => $money,
            'real_money' => $real_money,
            'bank_name' => $bank_info['bank_name'],
            'bank_branchname' => $bank_info['bank_branchname'],
            'bank_account' => $bank_info['bank_account'],
            'bank_card' => $bank_info['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->company_id,'money',-$money,205,'提现','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->company_id)->autopage()->select();

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

}
