<?php

namespace app\api\controller;

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

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

    //提现账号添加
    public function bankadd() {
        $realname = input('realname', '', 'trim'); //账户真实姓名
        $banknumber = input('banknumber', '', 'trim'); //卡号或账号
        $bankname = input('bankname', '', 'trim'); //银行名称


        if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
            $this->error('账号姓名1-30位');
        }
        if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
            $this->error('账号1-50位');
        }
        if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
            $this->error('银行名称1-50位');
        }

        //添加
        $count = Db::name('user_bank')->where(['user_id' => $this->auth->id])->count('id');
        if ($count) {
            $this->error('您已经拥有该类型账号,不能再添加');
        }

        $data['user_id'] = $this->auth->id;
        $data['realname'] = $realname;
        $data['banknumber'] = $banknumber;
        $data['bankname'] = $bankname;
        $data['createtime'] = time();

        $rs = Db::name('user_bank')->insertGetId($data);
        if (!$rs) {
            $this->error('添加失败');
        }

        $this->success('添加成功');
    }

    //提现账号编辑
    public function bankedit(){
        $id = input('id', 0, 'intval'); //账号id
        $realname = input('realname', '', 'trim'); //账户真实姓名
        $banknumber = input('banknumber', '', 'trim'); //卡号或账号
        $bankname = input('bankname', '', 'trim'); //银行名称


        if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
            $this->error('账号姓名1-30位');
        }
        if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
            $this->error('账号1-50位');
        }
        if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
            $this->error('银行名称1-50位');
        }

        //查询账号是否存在
        $info = Db::name('user_bank')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
        if (!$info) {
            $this->error('账号不存在');
        }

        $data['realname'] = $realname;
        $data['banknumber'] = $banknumber;
        $data['bankname'] = $bankname;
        $data['updatetime'] = time();

        $rs = Db::name('user_bank')->where(['id' => $id])->update($data);
        if ($rs === false) {
            $this->error('修改失败');
        }

        $this->success('修改成功');
    }

    //提现账号信息
    public function bankinfo() {

        $info = Db::name('user_bank')->where(['user_id' => $this->auth->id])->find();
        if (!$info) {
            $info = (object)[];
        }

        $this->success('账户信息', $info);
    }

    //提现
    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);
    }
}