<?php

namespace App;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use mysql_xdevapi\Exception;
use Zttp\Zttp;

use App\Traits\ModelLogChanging;

class SortingStation extends Model
{
    use ModelLogChanging;

    protected $fillable = [
        'name','type', 'bin_amount','is_occupied','is_online','processing_batch_id','login_at'
    ];
    static public function findOrCreate($name){
        $station=SortingStation::where('name',$name)->first();
        if(!$station){
            $station=new SortingStation(['name'=>$name]);
            $station->save();
        }
        return $station;
    }
    public function login(){
        $this['login_at'] = Carbon::now();
        $this['is_online'] = '是';
        $this->update();
    }
    public function setProcessingBatch(Batch $batch){
        $this['processing_batch_id'] = $batch['id'];
        $this['is_occupied'] = '是';
        $this->login();
        $this->update();
    }
    public function clearProcessingBatch(){
        $this['processing_batch_id'] = null;
        $this['is_occupied'] = '否';
        $this->login();
        $this->update();
    }
}
