<?php


namespace App\Http\Controllers\Api;


use App\Models\PaymentLogModel;
use App\Models\User;
use App\Models\UserInviteLog;
use App\Models\UserVipLogModel;
use App\Services\PayService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use PHPUnit\Util\Exception;
use Symfony\Component\HttpFoundation\Request;
use Yansongda\Pay\Pay;

class PayNotifyController extends Controller
{
    /**
     * 微信支付回调
     */
    public function wx_notify(){
        Log::info(Request::createFromGlobals()->getContent());

        $wxpay = Pay::wechat(PayService::wx_config());
        DB::beginTransaction();
        try{
            $data = $wxpay->verify();
            Log::info($data);
            $order_no = $data->out_trade_no;
            $this->order_do_sth($order_no);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            Log::info($e->getMessage());
        }
        return $wxpay->success();
    }

    /**
     * 支付宝支付回调
     */
    public function ali_notify(){
        $alipay = Pay::alipay(PayService::ali_config());
        DB::beginTransaction();
        try{
            $data = $alipay->verify();
            Log::info($data);
            $order_no = $data->out_trade_no;
            $this->order_do_sth($order_no);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            Log::info($e->getMessage());
        }
        return $alipay->success();
    }

    /**
     * 支付回调业务处理
     */
    public function order_do_sth($order_no){
        $order = PaymentLogModel::query()->where(['order_no'=>$order_no])->first();
        if(!$order){
            throw new Exception("订单不存在");
        }
        if($order['status']==1){
            return true;
        }
        //修改订单状态
        $order->status = 1;
        $order->save();

        $vip_info = json_decode($order->content,true);

        //修改用户vip状态
        $user = User::query()->where(['id'=>$order->user_id])->first();
        if($user->is_vip==0){
            $user->is_vip = 1;
            $user->save();
        }
        //变更vip记录
        $user_vip_log = UserVipLogModel::query()->where(['user_id'=>$order->user_id])->first();
        if(!$user_vip_log){
            UserVipLogModel::query()->create([
               'user_id'=>$order->user_id,
               'status'=>1,
               'day'=>$vip_info['day'],
               'end_day'=> date("Y-m-d H:i:s",strtotime("+".$vip_info['day']." day")),
            ]);
        }elseif($user_vip_log->status==1){
            $user_vip_log->end_day = date("Y-m-d H:i:s",strtotime($user_vip_log->end_day."+".$vip_info['day']." day"));
            $user_vip_log->save();
        }elseif ($user_vip_log->status==0){
            $user_vip_log->end_day = date("Y-m-d H:i:s",strtotime("+".$vip_info['day']." day"));
            $user_vip_log->status = 1;
            $user_vip_log->save();
        }
        //赠送上级会员天数
        if($user->pid>0){
            //赠送会员天数
            UserInviteLog::query()->create([
                'user_id'=>$user->pid,
                'invite_id'=>$user->id,
                'day'=>2,
                'status'=>0,
            ]);
        }
        return true;
    }
}
