<?php
/**
 * Created by PhpStorm.
 * User: scstf
 * Date: 2019/4/18
 * Time: 14:09
 */

namespace App\Api\Model;

use \System\Model;
use Biz\Account\AccountManager;

class Withdraw extends Model
{
    /**
     * 奖记录中
     *
     * @access public
     * @return String
     */
    public function withdrawalRequest()
    {
        // 获取用户信息
        $accountManagerClass = new AccountManager;
        $userInfo = $accountManagerClass -> getCurrentUser();
        if (empty($userInfo['identity'])) {
            Render([], '2001', lang('Common','Api') -> get('user does login'));
        }
        // 验证提款金额小于一百
        if (!$_POST['money'] || $_POST['money'] < 100) {
            Render([], '2112', lang('Common','Api') -> get('cash withdrawal less than 100'));
        }
        $accountDetailedWhere = array();
        // 用户ID
        $accountDetailedWhere['account_identity'] = $userInfo['identity'];
        $accountDetailedSelect = ['available_cash', 'cash'];
        $accountDetailed = lm('account_detailed', "commons");
        // 获取用户可提现金额
        $accountInfo = $accountDetailed -> accountDetailed($accountDetailedSelect, $accountDetailedWhere);
        // 验证提款额度大于可提现余额
        if($accountInfo['available_cash'] < $_POST['money']) {
            Render([], '2112', lang('Common','Api') -> get('insufficient amount of account'));
        }
        //验证交易密码
        $accountManagerClass -> verifyPayPwd($userInfo['identity'], $_POST['payPassword']);
        // 用户银行卡
        $bankCardWhere['account_identity'] = $userInfo['identity'];
        $bankCardWhere['bank_number'] = $_POST['bank_number'];
        $bankCardSelect = ['bank_number', 'account_name', 'bank_address', 'bank_name'];
        $getBankCardInfo = lm('account_bank', "commons");
        // 用户是否绑定该银行卡
        $bankCardInfo = $getBankCardInfo -> bankCard($bankCardSelect, $bankCardWhere);
        if(!$bankCardInfo['bank_number']) {
            Render([], '2114', lang('Common','Api') -> get('the bank card is not bound'));
        }
        //生成订单id
        $order_id = OrderID();
        //提现后总金额
        $cashMoney = floatval($accountInfo['cash']) - floatval($_POST['money']);
        //提现后可提现金额
        $availableCashMoney = floatval($accountInfo['available_cash']) - floatval($_POST['money']);
        $moneyTakeData = array(
            'info_identity' => UUID(),
            'order_id' => $order_id,
            'account_identity' => $userInfo['identity'],
            'account_name' => $userInfo['name'],
            'money' => $_POST['money'],
            'apply_time' => date('Y-m-d H:i:s', time()),
            'apply_date' => date('Y-m-d', time()),
            'bank_info' => $bankCardInfo['bank_name'],
            'bank_no' => $_POST['bank_number'],
            'bank_address' => $bankCardInfo['bank_address'],
            'bank_user' => $bankCardInfo['account_name'],
            'money_cash' => $cashMoney,
        );
        //验证三分钟内不能重复提交提现申请
        $moneyTake = lm('money_take', 'commons');
        $takeRecordWhere['account_identity'] = $userInfo['identity'];
        $takeRecordSelect = ['apply_time'];
        $moneyTakeInfo = $moneyTake -> takeRecord($takeRecordSelect, $takeRecordWhere);
        $applyTime = date('Y-m-d H:i:s', time() - 300);
        if ($moneyTakeInfo) {
            if (!empty($moneyTakeInfo['apply_time']) && $moneyTakeInfo['apply_time'] >= $applyTime) {
                Render([], '2051', lang('Common','Api') -> get('you have submitted your application for withdrawal'));
            }
        }

        // 添加提现记录
        _beginTransaction();
        try {
            $insertTake = $moneyTake -> insertTake($moneyTakeData);
            if ($insertTake) {
                $accountUpdateData = [
                    'available_cash' => $availableCashMoney,
                    'cash' => $cashMoney,
                ];
                $updateAccountWhere['account_identity'] = $userInfo['identity'];
                // 修改用户信息表剩余金额
                $updateResult = $accountDetailed -> updateDetailed($updateAccountWhere, $accountUpdateData);
                if ($updateResult > 0) {
                    _commit();
                    Render([], '40453', lang('Common','Api') -> get('application has been submitted and customer service is under review'));
                }
            }
        } catch (PDOException $e) {
            _rollBack();
            Render([], '141313', lang('Common','Api') -> get('the application failed. Please try again'));
        }
    }

}