<?php

namespace App\Modules\Trade\Pay;

use App\Jobs\UserBalanceJob;
use App\Modules\Product\Models\Product;
use App\Modules\Subscribe\Models\Order;
use App\Modules\User\Models\User;
use DB;

/**
 * 
 * @property \App\Modules\Product\Models\Product $product
 */
class RechargeOrderPaySuccess extends PaySuccessAbstract
{

    private $product;

    public function __construct(Order $order)
    {
        parent::__construct($order);
        $this->product = Product::find($order->product_id);
    }

    protected function handlePayProcess()
    {
        $charge = $this->product->price * 100;
        $given = $this->product->given;
        $uid = $this->order->uid;
        User::where('id', $uid)->update(
            [
                'balance' => DB::raw('balance+' . ($charge + $given)),
                'charge_balance' => DB::raw('charge_balance+' . $charge),
                'reward_balance' => DB::raw('reward_balance+' . $given),
            ]
        );
        try{
            $job = (new UserBalanceJob($uid,1,$charge,1,'充值'))->onConnection('rabbitmq')->delay(0)->onQueue('user_balance_job');
            dispatch($job);
            if($given){
                $job = (new UserBalanceJob($uid,2,$given,2,'充送'))->onConnection('rabbitmq')->delay(0)->onQueue('user_balance_job');
                dispatch($job);
            }
        }catch (\Exception $e){
            myLog('UserBalanceJob')->info($e);
        }
    }
}
