<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Events\BusinessOrdersUpdate;

class StockWarning implements ShouldQueue
{
    use InteractsWithQueue;

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

    public function handle($event)
    {
        if (isset($event->goodsSku->stock) && 5 >= $event->goodsSku->stock) {
            // 修改状态为预警,发送通知给管理员
            $event->goodsSku->status = 2;
            $event->goodsSku->save();
        }
        if (isset($event->goodsSkus)) {
            $data = [];
            foreach ($event->goodsSkus as $goodsSku) {
                $data[] = [
                    'id' => $goodsSku->id,
                    'status' => 2
                ];
            }
            if ($data) {
                $event->goodsSkus->update($data);
            }
        }
    }
}
