<?php

namespace App\Listeners;

use App\Events\AddOrUpdateOrderIssues;
use App\Events\UpdateOrderPackageExceptionListenerEvent;
use App\OrderIssue;
use App\OrderPackage;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    /**
     * Handle the event.
     *
     * @param AddOrUpdateOrderIssues $event
     * @return void
     */
    public function handle(AddOrUpdateOrderIssues $event)
    {
        $order_ids = OrderIssue::query()->select('order_id')->whereIn('order_id', $event->order_ids)->whereIn('order_issue_type_id', function ($query) {
            $query->from('order_issue_types')->select('id')->whereNotIn('name', ['拦截', '信息更改', '其他', '错漏发', '仓库问题', '二次加工', '全检问题', '系统问题', '快递方式更改', '无记录']);
        })->pluck('order_id');
        OrderPackage::query()->whereIn('order_id',
            $order_ids)
            ->update([
                'exception_type' => '其他',
                'exception' => '是',
            ]);
        //更新统计数据
        $orderPackageIds = OrderPackage::query()->whereIn('order_id', $order_ids)->pluck('id');
        event(new UpdateOrderPackageExceptionListenerEvent($orderPackageIds));
    }
}
