<?php

namespace Modules\Mall\Listeners;

use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Modules\Mall\Events\RefundAgreed;

class RefundAgreedListener implements ShouldQueue
{

    use Dispatchable, InteractsWithQueue;

    public string $queue = 'LISTENER';

    /**
     * Handle the event.
     *
     * @param  RefundAgreed  $event
     * @return void
     */
    public function handle(RefundAgreed $event)
    {
        try {
            $refund = $event->refund->refresh();

            foreach ($refund->order->items as $item) {
                $item->item->addStock($item->qty);        //加库存
                $item->item->deductSold($item->qty);      //减销量
            }
        } catch (Exception $exception) {
            Log::error($exception->getMessage());
        }
    }

}
