<?php

namespace App\Events;

use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
use App\Utils\DateTimeUtils;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class StockUpdateEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $goodsSku;
    public $combinationGoodsUpdate = true;

    /**
     * Create a new event instance.
     *
     * @param $goodsSku
     */
    public function __construct($goodsSku)
    {
        $this->goodsSku = $goodsSku;
        $this->checkStatusAndStock($goodsSku);
    }

    private function checkStatusAndStock($goodsSku)
    {
        //新版上下架和真实库存无关
        if (0 >= $goodsSku->sale_stock) {
            $status = GoodsSku::$STATUS_DOWN;
        } else {
            $status = GoodsSku::$STATUS_ON_SALE;
        }

        $goodsSku->status = $status;
        $goodsSku->save();
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}
