<?php

namespace app\api\controller;

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

/**
 * 会员中心
 */
class Usercenter extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';



    //提现
    public function moneywithdraw() {
        $id = input('id', 0, 'intval'); //提现账号id
        $money = input('money', '', 'trim');
        if (!$id) {
            $this->error('请选择提现账号');
        }
        $bankinfo = Db::name('user_bank')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
        if (!$bankinfo) {
            $this->error('账号不存在');
        }
        if (!preg_match('/^[0-9]+(.[0-9]{1,8})?$/', $money) || $money <= 0) {
            $this->error('请输入正确提现金额');
        }

        //余额查询
        $user_money = model('wallet')->getWallet($this->auth->id,'money');
        if ($user_money < $money) {
            $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 . '元');
        }

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

        $real_money = bcdiv(bcmul($money,bcsub(100,$withdrawal_service_fee)),100);

        $data['order_no'] = createUniqueNo('T',$this->auth->id);
        $data['user_id'] = $this->auth->id;
        $data['amount'] = $money;
        $data['money'] = $real_money;
        $data['user_bank_id'] = $id;
        $data['realname'] = $bankinfo['realname'];
        $data['banknumber'] = $bankinfo['banknumber'];
        $data['bankname'] = $bankinfo['bankname'];
        $data['status'] = 0;
        $data['createtime'] = time();

        //开启事务
        Db::startTrans();
        //添加提现记录
        $log_id = Db::name('user_withdraw')->insertGetId($data);
        if (!$log_id) {
            Db::rollback();
            $this->error('申请提现失败');
        }

        $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'money',$money,4,'提现','user_withdraw',$log_id);
        if ($rs_wallet['status'] == false) {
            $this->error($rs_wallet['msg']);
            Db::rollback();
        }

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

    //用户钱包流水
    public function moneylog(){
        $list = Db::name('user_money_log')->field('id,change_value,log_type,createtime')->where('user_id',$this->auth->id)->page($this->page,$this->listrow)->select();
        foreach($list as $key => &$val){
            $val['log_type_text'] = model('wallet')->getlogtype($val['log_type']);
        }

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