<?php


namespace App\Services;


use App\Facade\TableName;
use App\Http\helper\NumConfig;
use App\Http\logic\api\BaseApiLogic;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class ExemptDraw extends BaseApiLogic
{

    /**
     * 验证是否符合免审条件
     * @param $UserID // 用户ID
     * @param $WithDraw // 要提现的金额
     * @return array|bool
     */
    public function verify($UserID, $WithDraw)
    {

        // 查询用户信息
        $GameTime = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
            ->where('UserID', $UserID)
            ->value('PlayTimeCount');

        // 充值金额，提现金额
        $UserRecord = DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')
            ->where('UserID', $UserID)
            ->select('Withdraw', 'Recharge')
            ->first();

        $HistoryWithdraw = (int)$UserRecord->Withdraw ?? 0;
        $HistoryRecharge = (int)$UserRecord->Recharge ?? 0;

        // 用户提现笔数
        $redis = Redis::connection();
        $TodayDrawBi = $redis->get('draw_' . date('Ymd') . $UserID);


        if (empty($HistoryRecharge)) {
            $config = DB::table(TableName::agent() . 'withdrawal_position_config')
                ->where('status', 1)
                ->where('recharge_checkbox', 1)
                ->where('recharge_min', 0)
                ->where('recharge_max', 0)
                ->first();
        } else {
            $config = DB::table(TableName::agent() . 'withdrawal_position_config')
                ->where('status', 1)
                ->where('recharge_min', '<=', $HistoryRecharge)
                ->where('recharge_max', '>=', $HistoryRecharge)
                ->first();
        }


        if (!$config) {
            Log::info('免审配置没获取到' . $UserID);
            $this->error = '免审配置没获取到';
            return false;
        }

        // 验证免审提现类型
        $verifyType = $this->checkType($config->type, $UserID, $config->quota, $WithDraw, $HistoryWithdraw);
        if (!$verifyType) {
            $this->error = '固定额度已超出，或倍数已超出';
            return false;
        }

        // 提现大于充值验证
        if ($config->draw_gt_recharge > 0) {
            if (($HistoryWithdraw / NumConfig::NUM_VALUE) > $HistoryRecharge) {
                $this->error = '提现大于充值';
                return false;
            }
        }

        // 提现总额度验证 [历史提现 + 要提现的这笔 大于设置值。不允许免审]
        if ($config->draw_total > 0) {
            if (($HistoryWithdraw / NumConfig::NUM_VALUE) + $WithDraw > $config->draw_total) {
                $this->error = '提现总额度验证 [历史提现 + 要提现的这笔 大于设置值。不允许免审]';
                return false;
            }
        }

        // 提现笔数 [单日提现笔数大于设置值。不允许免审]
        if ($config->draw_bi > 0) {
            if ($TodayDrawBi > $config->draw_bi) {
                $this->error = '提现笔数 [单日提现笔数大于设置值。不允许免审]';
                return false;
            }
        }


        // 游戏时长验证[总游戏时长大于设置值。不允许免审]
        if ($config->game_time > 0) {
            if ($GameTime > $config->game_time) {
                $this->error = '游戏时长验证[总游戏时长大于设置值。不允许免审]';
                return false;
            }
        }

        // 总额度验证
        $checkTotalScore = $this->checkTotalScore($config->id, $config->total_quota);
        if (!$checkTotalScore) {
            $this->error = '总额度已超出';
            return false;
        }

        // 返回信息
        return [true, $config->id];
    }

    // 检查免审提现类型

    /**
     * @param $Type // 1 固定额度 2 倍数
     * @param $UserID // 用户ID
     * @param $quota // 平台设置的提现额度【根据type变化】
     * @param $WithDraw // 要提现的值
     * @param $HistoryRecharge // 历史充值
     * @return bool
     */
    public function checkType($Type, $UserID, $quota, $WithDraw, $HistoryRecharge)
    {

        if ($Type == 0) return true;


        // 查询用户今日免审消耗的额度
        $UseAmount = DB::table(TableName::agent() . 'withdrawal_position_log')
            ->where('take_effect', 2)
            ->where('user_id', $UserID)
            ->sum('use_quota');

        if ($Type == 1) {
            if ($quota < $UseAmount + $WithDraw) { // 提现固定额度 小于 要提现的额度 + 今天已经提现的额度，不允许免审
                $this->error = '提现固定额度 小于 要提现的额度 + 今天已经提现的额度，不允许免审';
                return false;
            }
        } elseif ($Type == 2) {
            if ($UseAmount + $WithDraw > $HistoryRecharge * $quota) {    // 要提现的金额 + 今天已经提现的额度  大于  历史充值的n倍。不允许免审
                $this->error = '要提现的金额 + 今天已经提现的额度  大于  历史充值的n倍。不允许免审';
                return false;
            }
        }

        return true;
    }

    // 总额度验证
    public function checkTotalScore($ConfigId, $TotalScore)
    {
        // 查该配置的所有已使用额度
        $UseQuoTa = DB::table(TableName::agent() . 'withdrawal_position_log')
            ->where('config_id', $ConfigId)
            ->where('take_effect', 2)
            ->sum('use_quota');

        // 使用额度，大于设置值，不允许免审
        if ($UseQuoTa > ($TotalScore * NumConfig::NUM_VALUE)) {
            $this->error = '使用额度，大于设置值，不允许免审';
            return false;
        }

        return true;
    }
}
