<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

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

class UserWorkgroup extends Model
{
    use ModelLogChanging;

    protected $table="user_workgroups";
    protected $fillable=['name','warehouse_id'];
    protected $appends=[
        'token','isNeedRemark',
    ];

    public function users(){
        return $this->belongsToMany('App\User','user_workgroup_user','user_workgroup_id','user_id');
    }
    public  function wareHouse(){
        return $this->belongsTo('App\Warehouse','warehouse_id','id');
    }
    public  function signs(){
        return $this->belongsTo('App\Sign','signable_id','id');
    }
    public  function getTokenAttribute(){
        return md5($this['name'].Carbon::now()->format('Y-m-d'));
    }
    public  function getIsNeedRemarkAttribute()
    {
        if ($this['signs']['mark'] ?? false) {
            return $this['signs']['mark'] == '是' ? true : false;
        }
    }

    public function orderIssues(): BelongsToMany
    {
        return $this->belongsToMany(OrderIssue::class);
    }
}
