<?php


namespace App\Services;


use App\Facade\TableName;
use App\Http\helper\NumConfig;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Withdrawal
{
    public function OrderServe(SerPay $serPay, $PixType, $accountName, $phone, $email, $PixNum)
    {
        // 1:CPF/CNPJ 2:Email 3:PhoneNumber
        if ($PixType == 1) {
            strlen($PixNum) == 11 ? $serPay->setCardType('CPF') : $serPay->setCardType('CNPJ');
            $serPay->setPanNum($PixNum);
        }
    }

    // 免审配置验证 -- false人工审。true免审
    public function configVerify($UserID, $Withdraw, $OrderID)
    {

        $UserInfo = DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->select('Recharge', 'Withdraw')->first();

        // 读取免审配置
        $getConfig = DB::table(TableName::agent() . 'withdrawal_position_config')
            ->where('status', 1)
            ->first();

        // 配置没开启，返回
        if (!$getConfig) return false;

        // 提现值除100
        $Withdraw /= NumConfig::NUM_VALUE;

        // 设置默认值
        $UserWithdraw = $UserInfo->Withdraw ?? 0;
        $UserRecharge = $UserInfo->Recharge ?? 0;

        // 用户已经提现值除100
        $UserWithdraw /= NumConfig::NUM_VALUE;

        if($UserWithdraw==0) return 9;
        //0
        if($Withdraw<3000&&$Withdraw<=$UserRecharge-$UserWithdraw){
            Log::info("{$OrderID}：自动免审：级别5：{$UserRecharge}-{$UserWithdraw}");
            return 5;
        }else if($UserRecharge>=1000&&$Withdraw<$UserRecharge-$UserWithdraw+100){
            Log::info("{$OrderID}：自动免审：级别3：{$UserRecharge}-{$UserWithdraw}");
            return 3;
        }else if($UserRecharge>=100&&$Withdraw<$UserRecharge-$UserWithdraw+100){
            Log::info("{$OrderID}：自动免审：级别2：{$UserRecharge}-{$UserWithdraw}");
            return 2;
        }else if($UserRecharge>10&&$Withdraw<$UserRecharge-$UserWithdraw+100){
            Log::info("{$OrderID}：自动免审：级别4：{$UserRecharge}-{$UserWithdraw}");
            return 4;
        }else if($UserRecharge>0&&$Withdraw<$UserRecharge-$UserWithdraw+100){
            Log::info("{$OrderID}：自动免审：级别1：{$UserRecharge}-{$UserWithdraw}");
            return 1;
        }
        ///
        /// ================================ 配置判断 ====================================
        ///

        // 1、 充值额度配置开启
        if ($getConfig->recharge_checkbox) {
            if ($UserRecharge < $getConfig->recharge_min || $UserRecharge > $getConfig->recharge_max) {
                Log::info("{$OrderID}：自动免审：充值额度不符合，已充值金额：{$UserRecharge}");
                return false;
            }
        }
        // 2、提现大于充值某个值 -- 人工审核 【反之，提现+要提现金额小于充值+配置值免审】
        if ($getConfig->draw_gt_recharge > 0) {

            // 已经提现的金额 + 要提现金额 小于 充值 + 设置值
            if (($UserWithdraw + $Withdraw) >= $UserRecharge + $getConfig->draw_gt_recharge) {
                Log::info("{$OrderID}：自动免审：提现大于充值，已提现金额：{$UserWithdraw}");
                return false;
            }

        }

        // 3、提现总额度超过值 -- 人工审 【反之，提现小于配置设置额度免审】
        if ($getConfig->draw_total > 0) {
            if (($UserWithdraw + $Withdraw) > $getConfig->draw_total) {
                Log::info("{$OrderID}：自动免审：提现总额度超过值：{$UserWithdraw}");
                return false;
            }
        }

        //4、提现笔数超过 -- 人工审 【反之，提现笔数小于设置值免审】
        if ($getConfig->draw_bi > 0) {
            $redis = Redis::connection();
            $drawBi = $redis->get('draw_' . date('Ymd') . $UserID);
            if ($drawBi > $getConfig->draw_bi) {
                Log::info("{$OrderID}：自动免审：提现笔数超出设置值：{$drawBi}");
                return false;
            }
        }

        // 5、游戏时长小于配置值 -- 人工审 【反之，游戏时间大于配置值免审】
        if ($getConfig->game_time > 0) {

            // 查用户游戏时长 -- 秒
            $PlayTimeCount = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')->where('UserID', $UserID)->value('PlayTimeCount');
            if (($PlayTimeCount / 60) < $getConfig->game_time) {
                Log::info("{$OrderID}：自动免审：游戏时长小于配置值：{$PlayTimeCount}");
                return false;
            }
        }

        // 6、固定额度/倍数
        if ($getConfig->type > 0 && $getConfig->quota > 0) {
            if ($getConfig->type == 2) { // 倍数  -- 充值额度 * 倍数
                $getConfig->quota = (int)($UserRecharge * $getConfig->quota);
            }

            // 该用户已经消耗的额度 -- 免审提现额度 -- （申请提现额度额外加）
            $use_quota = DB::connection('write')->table('agent.dbo.withdrawal_position_log')
                ->where('user_id', $UserID)
                ->whereDate('create_at', date('Y-m-d'))
                ->selectRaw('IsNull(sum(use_quota),0) use_quota')
                ->first()->use_quota;

            // 该配置已经消耗的全部额度 -- （申请提现额度额外加）
            $all_use_quota = DB::connection('write')->table('agent.dbo.withdrawal_position_log')
                ->selectRaw('IsNull(sum(use_quota),0) use_quota')
                ->where('config_id', $getConfig->id)
                ->whereDate('create_at', date('Y-m-d'))
                ->first()->use_quota;

            // 用户消耗额度 + 要提现额度
            $use_quota_after = ($use_quota / NumConfig::NUM_VALUE) + $Withdraw;
            $all_use_quota_after = ($all_use_quota / NumConfig::NUM_VALUE) + $Withdraw;

            if ((int)$use_quota_after > $getConfig->quota && (int)$all_use_quota_after > $getConfig->total_quota) {
                Log::info("{$OrderID}：自动免审：用户提现超出额度：{$use_quota_after}，配置总额度消耗：{$all_use_quota_after}");
                return false;
            }
        }


        return true;
    }

    // 免审配置验证
    public function configVerifyCopy($UserID, $Withdraw)
    {

        $UserInfo = DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->select('Recharge', 'Withdraw')->first();

        // 读取免审配置
        $getConfig = DB::table(TableName::agent() . 'withdrawal_position_config')
            ->where('status', 1)
            ->first();

        // 配置没开启，返回
        if (!$getConfig) return false;

        // 提现值除100
        $Withdraw /= NumConfig::NUM_VALUE;

        // 设置默认值
        $UserWithdraw = $UserInfo->Withdraw ?? 0;
        $UserRecharge = $UserInfo->Recharge ?? 0;

        // 用户已经提现值除100
        $UserWithdraw /= NumConfig::NUM_VALUE;


        ///
        /// ================================ 配置判断 ====================================
        ///

        $flag = false;

        // 1、 充值额度配置开启
        if ($getConfig->recharge_checkbox) {
            if ($UserRecharge >= $getConfig->recharge_min && $UserRecharge <= $getConfig->recharge_max) {
                $flag = true;
            } else {
                $flag = false;
            }
        }

        // 2、提现大于充值某个值 -- 人工审核 【反之，提现+要提现金额小于充值+配置值免审】
        if ($getConfig->draw_gt_recharge > 0) {

            // 已经提现的金额 + 要提现金额 小于 充值 + 设置值
            if (($UserWithdraw + $Withdraw) < $UserRecharge + $getConfig->draw_gt_recharge) {
                $flag = true;
            } else {
                $flag = false;
            }

        }

        // 3、提现总额度超过值 -- 人工审 【反之，提现小于配置设置额度免审】
        if ($getConfig->draw_total > 0) {
            if ((($UserWithdraw + $Withdraw)) < $getConfig->draw_total) {
                $flag = true;
            } else {
                $flag = false;
            }
        }

        //4、提现笔数超过 -- 人工审 【反之，提现笔数小于设置值免审】
        if ($getConfig->draw_bi > 0) {
            $redis = Redis::connection();
            $drawBi = $redis->get('draw_' . date('Ymd') . $UserID);
            if ($drawBi < $getConfig->draw_bi) {
                $flag = true;
            } else {
                $flag = false;
            }
        }

        // 5、游戏时长小于配置值 -- 人工审 【反之，游戏时间大于配置值免审】
        if ($getConfig->game_time > 0) {

            // 查用户游戏时长 -- 秒
            $PlayTimeCount = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')->where('UserID', $UserID)->value('PlayTimeCount');

            if (($PlayTimeCount / 60) > $getConfig->game_time) {
                $flag = true;
            } else {
                $flag = false;
            }
        }

        // 6、固定额度/倍数
        if ($getConfig->type > 0 && $getConfig->quota > 0) {
            if ($getConfig->type == 2) { // 倍数  -- 充值额度 * 倍数
                $getConfig->quota = (int)($UserRecharge * $getConfig->quota);
            }

            // 该用户已经消耗的额度 -- 免审提现额度 -- （申请提现额度额外加）
            $use_quota = DB::connection('write')->table('agent.dbo.withdrawal_position_log')
                ->where('user_id', $UserID)
                ->whereDate('create_at', date('Y-m-d'))
                ->selectRaw('IsNull(sum(use_quota),0) use_quota')
                ->first()->use_quota;

            // 该配置已经消耗的全部额度 -- （申请提现额度额外加）
            $all_use_quota = DB::connection('write')->table('agent.dbo.withdrawal_position_log')
                ->selectRaw('IsNull(sum(use_quota),0) use_quota')
                ->where('config_id', $getConfig->id)
                ->whereDate('create_at', date('Y-m-d'))
                ->first()->use_quota;

            // 用户消耗额度 + 要提现额度
            $use_quota_after = ($use_quota / NumConfig::NUM_VALUE) + $Withdraw;
            $all_use_quota_after = ($all_use_quota / NumConfig::NUM_VALUE) + $Withdraw;

            if ((int)$use_quota_after < $getConfig->quota && (int)$all_use_quota_after < $getConfig->total_quota) {
                $flag = true;
            } else {
                $flag = false;
            }
        }

        return $flag;
    }
}
