<?php


namespace App\dao\Pay;


use App\Facade\TableName;
use App\Http\logic\api\BaseApiLogic;
use App\Services\FirstRechargeGifts;
use App\Services\OrderServices;
use App\Services\StoredProcedure;
use Illuminate\Support\Facades\DB;

class PayController extends BaseApiLogic
{
    public function verify($userId, $GiftsID, $pay_amount)
    {
        if (!empty($GiftsID) && false) {
            if ($GiftsID < 100) {      // 礼包--首冲
                $pay_amount = (int)DB::connection('write')->table('QPAccountsDB.dbo.FirstRechargeGifts')->where('GiftsID', $GiftsID)->first()->Price ?? 0;
                // 购买礼包
                $giftService = new FirstRechargeGifts();
                $bool = $giftService->buyGifts($GiftsID, $userId);
                if ($bool === false) {
                    $this->error = $giftService->getError();
                    return false;
                }
            } elseif ($GiftsID > 100 && $GiftsID < 200) { // 周卡
                $CardID = $GiftsID - 100;
                $WeeklyCard = DB::connection('write')->table('QPPlatformDB.dbo.MonthCard')->where([
                    'CardID' => $CardID,
                    'CardState' => 1,
                ])->first();
                $Price = $WeeklyCard->Price ?? '';
                if (!$WeeklyCard || $Price != $pay_amount) {
                    $this->error = 'A função está em manutenção, temporariamente indisponível para compra';
                    return false;
                }
            } elseif ($GiftsID == 200 || $GiftsID == 201||$GiftsID == 211 || $GiftsID == 212) { // 首充30元【修改逻辑，充值过首充礼包才不可以首充，不是只要充值过就不能首充】
//                $result = StoredProcedure::FirstCharge($userId)[0]->Ret ?? 0;
//                if ($result != 0) {
//                    $this->error = 'First charge condition not met';
//                    return false;
//                }
                $firstPayExists = DB::table(TableName::QPAccountsDB().'UserRechargeActive')->where('UserID',$userId)->first();
                if ($firstPayExists) {
                    $this->error = ['firstPay.already_got','First charge condition not met'];
                    return false;
                }
//                $pay_amount = (new OrderServices())->FirstCharge;
                $result = DB::table(TableName::agent() . 'recharge_gear')
                    ->where('gift_id', $GiftsID)
                    ->first();
                $pay_amount = $result->money ?? 0;
//            } elseif ($GiftsID == 211 || $GiftsID == 212) { // 引导付费 [只要充值过，就不能买引导付费]
//                // 判断礼包有没有充值过
//                // $guide_payment_exists = DB::table(TableName::agent() . 'guide_payment')->where('UserID',$userId)->first();
//                // 查看用户有没有充值过
//                $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
//                    ->where('UserID', $userId)
//                    ->value('Recharge') ?: 0;
//
//                if ($user_recharge > 0) {    // 引导付费礼包重复充值
//                    $this->error = 'Desculpe, você já recarregou e não pode comprar este pacote de desconto'; // 对不起，你已经充值过了，无法购买该优惠礼包
//                    return false;
//                }
//                $result = DB::table(TableName::agent() . 'recharge_gear')
//                    ->where('gift_id', $GiftsID)
//                    ->first();
//                $pay_amount = $result->money ?? 0;
            }
        }

        return $pay_amount;

    }
}
