<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Trace extends Model
{
    public $fillable = ['key', 'gid'];

    public $appends = ['slug_value'];

    public function good()
    {
        return $this->belongsTo(Good::class, 'gid');
    }

    public function getKeyAttribute()
    {
        if ($this->attributes['id'] <= 50000) {
            return number_format(10000000 + $this->attributes['id'], 0, '', '');
        } else {
            return bcadd('1000000000000000', "{$this->attributes['id']}");
        }
    }

    public static function get_key($id)
    {
        if ($id <= 10050000) {
            $id = bcsub("{$id}", '10000000');
        } else {
            $id = bcsub("{$id}", '1000000000000000');
        }
        return $id;
    }

    public function dealer()
    {
        return $this->belongsTo(Dealer::class);
    }

    public function getSlugAttribute($value)
    {
        return config('app.url') . '/key/' . $value;
    }

    public function getSlugValueAttribute()
    {
        return $this->attributes['slug'];
    }

    public function setSlugValueAttribute($val)
    {
        $this->attributes['slug'] = $val;
    }

}
