<?php

namespace App\Listeners;

use App\Events\OrderIssueProcessLogCreateEvent;
use App\Events\UpdateOrderPackageExceptionListenerEvent;
use App\OrderPackage;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

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

    /**
     * Handle the event.
     *
     * @param OrderIssueProcessLogCreateEvent $event
     * @return void
     */
    public function handle(OrderIssueProcessLogCreateEvent $event)
    {
        switch ($event->status) {
            case 1:
                $status = '返回派件';
                break;
            case 2:
                $status = '已收件';
                break;
            default:
                $status = '无';
        }
        $orderPackageIds = $event->orderPackageIds;
        OrderPackage::query()->whereIn('id', $orderPackageIds)->update([
            'exception_type' => '无',
            'exception' => '否',
            'status' => $status,
        ]);
        //更新统计数据
        event(new UpdateOrderPackageExceptionListenerEvent($orderPackageIds));
    }
}
