<?php

namespace App\Listeners;

use App\Events\CreateLogisticEvent;
use App\Models\Shop;
use App\Models\Waybill;
use App\Services\Business\BusinessFactory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class CancelLogisticListener implements ShouldQueue
{
    public $connection = 'redis';

    public $queue = 'listeners';

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param CreateLogisticEvent $event
     * @return void
     */
    public function handle(CreateLogisticEvent $event)
    {
        $waybillNo = Waybill::query()
            ->where('shop_id', $event->shopId)
            ->where('order_sn', $event->orderSn)
            ->value('waybill_code');
        if (empty($waybillNo)) {
            return;
        }
        $shop = Shop::query()->findOrFail($event->shopId);
        $client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop);
        $client->cancelLogistic($event->orderSn, $event->waybillNo);
    }
}
