<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;
use app\data\service\UserRebateService;
use app\data\service\UserTransferService;
use think\admin\extend\CodeExtend;

/**
 * 用户提现接口
 * Class Transfer
 * @package app\data\controller\api\auth
 */
class Transfer extends Auth
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'DataUserTransfer';

    /**
     * 提交提现处理
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $data = $this->_vali([
            'type.require'   => '提现方式不能为空!',
            'amount.require' => '提现金额不能为空!',
            'remark.default' => '用户提交提现申请!',
        ]);
        $state = UserTransferService::instance()->config('status');
        if (empty($state)) $this->error('提现还没有开启!');
        $transfers = UserTransferService::instance()->config('transfer');
        if (empty($transfers[$data['type']]['state'])) $this->error('提现方式已停用!');
        // 提现数据补充
        $data['uid'] = $this->uuid;
        $data['date'] = date('Y-m-d');
        $data['code'] = CodeExtend::uniqidDate(20, 'T');
        $data['openid1'] = $this->user['openid1'];
        $data['openid2'] = $this->user['openid2'];
        // 提现状态处理
        if (empty($transfers[$data['type']]['state']['audit'])) {
            $data['status'] = 1;
            $data['audit_status'] = 0;
        } else {
            $data['status'] = 3;
            $data['audit_status'] = 1;
            $data['audit_remark'] = '提现免审核';
            $data['audit_datetime'] = date('Y-m-d H:i:s');
        }
        // 扣除手续费
        $chargeRate = floatval(UserTransferService::instance()->config('transfer_charge'));
        $data['charge'] = $chargeRate * $data['amount'] / 100;
        // 提现方式处理
        if (in_array($data['type'], ['alipay_account'])) {
            $data = array_merge($data, $this->_vali([
                'alipay_user.require' => '开户姓名不能为空!',
                'alipay_code.require' => '支付账号不能为空!',
            ]));
        } elseif (in_array($data['type'], ['wechat_qrcode', 'alipay_qrcode'])) {
            $data = array_merge($data, $this->_vali([
                'qrcode.require' => '收款码不能为空!',
            ]));
        } elseif (in_array($data['type'], ['wechat_banks', 'transfer_banks'])) {
            $data = array_merge($data, $this->_vali([
                'bank_name.require' => '银行名称不能为空!',
                'bank_user.require' => '开户账号不能为空!',
                'bank_bran.require' => '银行分行不能为空!',
                'bank_code.require' => '银行卡号不能为空!',
            ]));
        } elseif (!in_array($data['type'], ['wechat_wallet'])) {
            $this->error('转账方式不存在!');
        }
        // 检查可提现余额
        [$total, $count] = UserRebateService::instance()->amount($this->uuid);
        if ($total - $count < $data['amount']) $this->error('可提现余额不足!');
        // 当日提现次数限制
        $map = ['uid' => $this->uuid, 'type' => $data['type'], 'date' => $data['date']];
        $count = $this->app->db->name($this->table)->where($map)->count();
        if ($count >= $transfers[$data['type']]['dayNumber']) $this->error("当日提现次数受限");
        // 提现金额范围控制
        if ($transfers[$data['type']]['minAmount'] > $data['amount']) {
            $this->error("不能少于{$transfers[$data['type']]['minAmount']}元");
        }
        if ($transfers[$data['type']]['maxAmount'] < $data['amount']) {
            $this->error("不能大于{$transfers[$data['type']]['minAmount']}元");
        }
        // 写入用户提现数据
        if ($this->app->db->name($this->table)->insert($data) !== false) {
            UserRebateService::instance()->amount($this->uuid);
            $this->success('提现申请成功');
        } else {
            $this->error('提现申请失败');
        }
    }

    /**
     * 用户提现记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $query = $this->_query($this->table)->where(['uid' => $this->uuid]);
        $result = $query->like('date,code')->in('status')->order('id desc')->page(true, false, false, 10);
        // 统计历史数据
        $map = [['uid', '=', $this->uuid], ['status', '>', 0]];
        [$total, $count, $locks] = UserRebateService::instance()->amount($this->uuid);
        $this->success('获取提现成功', array_merge($result, [
            'total' => [
                '锁定' => $locks,
                '可提' => $total - $count,
                '上月' => $this->app->db->name($this->table)->where($map)->whereLike('date', date("Y-m-%", strtotime('-1 month')))->sum('amount'),
                '本月' => $this->app->db->name($this->table)->where($map)->whereLike('date', date("Y-m-%"))->sum('amount'),
                '全年' => $this->app->db->name($this->table)->where($map)->whereLike('date', date("Y-%"))->sum('amount'),
            ],
        ]));
    }

    /**
     * 用户取消提现
     * @throws \think\db\exception\DbException
     */
    public function cancel()
    {
        $data = $this->_vali(['uid.value' => $this->uuid, 'code.require' => '单号不能为空!']);
        $this->app->db->name($this->table)->where($data)->whereIn('status', [1, 2, 3])->update([
            'status' => 0, 'change_time' => date("Y-m-d H:i:s"), 'change_desc' => '用户主动取消提现',
        ]);
        UserRebateService::instance()->amount($this->uuid);
        $this->success('取消提现成功');
    }

    /**
     * 用户确认提现
     * @throws \think\db\exception\DbException
     */
    public function confirm()
    {
        $data = $this->_vali(['uid.value' => $this->uuid, 'code.require' => '单号不能为空!']);
        $this->app->db->name($this->table)->where($data)->whereIn('status', [4])->update([
            'status' => 5, 'change_time' => date("Y-m-d H:i:s"), 'change_desc' => '用户主动确认收款',
        ]);
        UserRebateService::instance()->amount($this->uuid);
        $this->success('取消提现成功');
    }

    /**
     * 获取用户提现配置
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function config()
    {
        $data = UserTransferService::instance()->config();
        $this->success('获取用户提现配置', $data);
    }
}