<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use App\Traits\ModelLogChanging;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Batch extends Model
{
    use ModelLogChanging;

    protected $fillable = [
        'id','code','type', 'wms_type', 'status', 'wms_status', 'wms_created_at',"remark","owner_id",
    ];
    public function orders(){
        return $this->hasMany('App\Order','batch_id','id');
    }
    public function setProcessed(){
        $this['status'] = '已处理';
        $this->orders()->each(function (Order $order){
            $order->setProcessed();
        });
        $this->update();
    }
    public function assignBins(){
        $this->orders()->each(function (Order $order,$i){
            $bin=new OrderBin(['order_id'=>$order['id'],'number'=>($i+1)]);
            $bin->save();
        });
        return $this->orders()->count();
    }
    public function delete()
    {
        $this->orders()->each(function(Order $order){
            $order->delete();
        });
        return parent::delete();
    }
    public function owner()
    {
        return $this->hasOne(Owner::class,"id","owner_id");
    }
    public function stationTaskBatch(): HasOne
    {
        return $this->hasOne(StationTaskBatch::class);
    }
    public function stationTask()
    {

        return $this->stationTaskBatch?
            $this->stationTaskBatch->belongsTo(StationTask::class):
            null;
    }


}
