<?php
namespace App\Listeners;

use App\Events\CommissionGotEvent;
use App\Events\OrderServeStartedEvent;
use App\Jobs\UnfreezeCommission;
use App\Models\PartnerCommission;
use App\Models\PartnerWallet;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

/*发佣金给合伙人*/
/*已废弃*/
class SendCommissionToPartnerListener implements ShouldQueue
{
    use InteractsWithQueue;
//
//    /**
//     * Handle the event.
//     *
//     * @param  OrderServeStartedEvent  $event
//     * @return void
//     */
//    public function handle(OrderServeStartedEvent $event)
//    {
//        // 获取订单和合伙人信息
//        $offlineOrder = $event->offlineOrder;
//        $partner_id = $offlineOrder->partner_id;
//        $order_id = $offlineOrder->order_id;
//
//        //查询此订单是否已经申城佣金记录
//        $commission = PartnerCommission::query()
//            ->where('order_id',$order_id)
//            ->where('partner_id',$partner_id)
//            ->first();
//
//        if (!$commission){
//            try {
//                DB::beginTransaction();
//                $delay = config('app.env') == 'production'?now()->addDays(7):now();
//                //生成佣金记录
//                $commission = PartnerCommission::create([
//                    'order_id' => $order_id,
//                    'partner_id' => $partner_id,
//                    'amount' => $offlineOrder->commission_amount,
//                    'status' => 0,
//                    'created_at' => now(),
//                    'unfreeze_time' => $delay,
//                ]);
//
//                //合伙人钱包增加冻结金额
//                $wallet = PartnerWallet::where('partner_id', $partner_id)->first();
//                $wallet->frozen_amount = bcadd($wallet->frozen_amount,$offlineOrder->commission_amount,2);
//                $wallet->save();
//
//                DB::commit();
//            }catch (\Exception $e){
//                DB::rollBack();
//                \Log::error('佣金记录'.$commission->id.'生成失败'.$e->getMessage());
//                return;
//            }
//        }
//        \Log::info('佣金记录'.$commission->id.'已存在1');
//        if ($commission->status == 0){
//            \Log::info($commission->id."将于".$commission->unfreeze_time.'解锁1');
//            //触发佣金入账事件
//            CommissionGotEvent::dispatch($commission->order_commission);
//        }
//
//    }
}
