<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2023/7/26
 * Time: 15:17
 */
namespace app\api\controller;
use app\admin\model\user\Bankcard;
use app\api\model\UsersModel;
use app\common\controller\Api;
use app\admin\model\user\Withdraw as UserWithdraw;
use fast\Random;
use think\Config;
use think\Db;
use think\Exception;
use think\Validate;
class Withdraw extends Api{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 提现
     */
    public function withdraw()
    {
        $user_id = $this->auth->id;
        $money = abs((int)($this->request->post("money", 0)) * 100); //提现金额（分）
        $withdraw_way = (int)$this->request->post("withdraw_way", 1); //提现方式:1=提现至零钱,2=提现至银行卡
        $bankcard_id = $this->request->post('bankcard_id', 0); //银行卡id
        $mobile = $this->request->post('mobile', 0); //手机号
        $realname = $this->request->post('realname', ''); //真实姓名
        $code = $this->request->param('code'); //手机验证码
       /* $switchCash = config('switchCash');
        if ($switchCash != 'on') {
            $this->error('提现入口暂时关闭');
        }*/

        if (!$realname) {
            $this->error('请填写真实姓名');
        }
        if ($money <= 0 || !is_integer($money)) {
            $this->error("输入金额有误");
        } else if ($money < 100) {
            $this->error("最低提现1元！");
        } else if ($money > 50000) {
            $this->error("单笔最高提现500元！");
        }
        if ($money > $this->auth->commission * 100) {
            $this->error("输入金额不可大于佣金金额");
        }
        $withdrawcount = UserWithdraw::where(['user_id' => $user_id, 'status' => ['neq', -1]])->whereTime('start_time', 'today')->count();
        if ($withdrawcount >= 20) {
            $this->error("每天提现不能超过20次");
        }
        $data = [
            'cash_sn' => getOrdersn('withdraw'),
            'user_id' => $user_id,
            'name' => $realname,
            'tel' => $mobile,
            'money' => $money,
            'start_time' => time(),
            'withdraw_way' => $withdraw_way,
        ];
        if ($withdraw_way == 2) {
            $bankcard = Bankcard::where(['id' => $bankcard_id])->find();
            if (!$bankcard) {
                $this->error('银行卡不存在');
            }
            if ($money < 50 * 100) {
                $this->error('银行卡提现最低50元！');
            }
            $data['bank_name'] = $bankcard['bank_name'];
            $data['bank_num'] = $bankcard['bank_num'];
        }
        try {
            Db::startTrans();
            $res = UserWithdraw::create($data);
            $cut_money = sprintf("%.2f", $money / 100);
            UsersModel::where(['id' => $user_id])->setDec('commission', $cut_money);
            if ($res->toArray()) {
                Db::commit();
                $this->success('提现申请成功,待审核', ['data' => $res->toArray()]);
            }
        } catch (Exception $e) {
            Db::rollback();
            $this->error('提现申请成失败' . $e->getMessage());
        }
    }

    /**
     * 添加银行卡
     * @param $code
     */
    public function addBankCard()
    {
        $name = $this->request->param('name', '');
        $bank_name = $this->request->param('bank_name', '');
        $bank_code = $this->request->param('bank_code', ''); //银行代码
        $wx_bank_code = $this->request->param('wx_bank_code', ''); //微信银行编码
        $bank_num = $this->request->param('bank_num', 0);
        $open_address = $this->request->param('open_address', '');
        if (!$name || !$bank_name || !$bank_num || !$open_address) {
            $this->error('缺少参数没填');
        }

        /*$usersinfo = UsersModel::where(['id' => $this->auth->id])->find();
        if ($usersinfo['realname'] != $name) {
            $this->error('真实名字不符');
        }*/
        $bankcard = Bankcard::where(['bank_num' => $bank_num])->find();
        if ($bankcard) {
            $this->error('此银行卡已存在');
        }
        $data = [
            'name' => $name,
            'bank_name' => $bank_name,
            'bank_code' => $bank_code,
            'wx_bank_code' => $wx_bank_code,
            'bank_num' => $bank_num,
            'open_address' => $open_address,
            'user_id' => $this->auth->id,
        ];
        $res = Bankcard::create($data);
        if (!$res->toArray()) {
            $this->error('银行卡添加失败');
        }
        $this->success('银行卡添加成功');
    }

    /**
     * 我的银行卡
     */
    public function myBank()
    {
        $Bankcards = Bankcard::where(['user_id' => $this->auth->id])->select();
        if (!$Bankcards) {
            $this->error('暂无银行卡');
        }
        $this->success('银行卡列表', ['Bankcards' => $Bankcards]);
    }

    /**
     * 删除银行卡
     */
    public function delBank()
    {
        $bankcard_id = $this->request->post('bankcard_id', 0);
        $res = Bankcard::where(['id' => $bankcard_id])->delete();
        if (!$res) {
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }


    /**
     * 选择银行卡
     */
    public function selectBank()
    {
        $banklist = Db::name('bank')->select();
        $this->success('银行卡列表', ['banklist' => $banklist]);
    }
}