<?php


namespace App\Services;


use App\Http\helper\Helper;
use App\Http\logic\api\BaseApiLogic;
use Illuminate\Support\Facades\DB;

class PayPublic extends BaseApiLogic
{
    public function PayVerify($userId, $userName, $userEmail, $GiftsID,$pay_amount)
    {
        // 获取用户填写信息，填写过用最新填写的，没填写过 模拟
        $userPayInfo = Helper::userPayInfo($userId, $userName, $userEmail);
        $email = $userPayInfo['email'];
        $user_name = $userPayInfo['userName'];

        $userInfo = DB::connection('write')->table('QPAccountsDB.dbo.AccountsInfo')->where('UserID', $userId)->select('NickName')->first();

        if (!$userInfo) {
            $this->error = 'Payment error_6';
            return false;
        }

        // 购买礼包
        $giftService = new FirstRechargeGifts();
        $bool = $giftService->buyGifts($GiftsID, $userId);
        if ($bool === false) {
            $this->error = $giftService->getError();
            return false;
        }

        if (!empty($GiftsID)) {
            $pay_amount = (int)DB::connection('write')->table('QPAccountsDB.dbo.FirstRechargeGifts')->where('GiftsID', $GiftsID)->first()->Price ?? 0;
        }

        if ($pay_amount < 0) {
            $this->error = 'Payment error_4';
            return false;
        }


        return compact('user_name', 'email', 'pay_amount');
    }
}
