<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

use App\Traits\ModelLogChanging;

class OwnerPriceExpress extends Model
{
    use ModelLogChanging;

    protected $fillable = [
        "name",             //名称
        "initial_weight",   //首重
        "additional_weight",//续重
        "operation",        //操作
        "target_id",        //目标ID
        "amount_interval",  //数量区间
        "weight_interval",  //重量区间
        "tax_rate_id",      //税率
    ];
    protected $casts = [
        "amount_interval" => "array",
        "weight_interval" => "array"
    ];

    public function owners()
    {   //货主
        return $this->belongsToMany(Owner::class,"owner_price_express_owner","owner_price_express_id","owner_id");
    }
    public function logistics()
    {   //物流
        return $this->belongsToMany(Logistic::class,"owner_price_express_logistic","owner_price_express_id","logistic_id");
    }
    public function details()
    {   //计费详情
        return $this->hasMany(OwnerPriceExpressProvince::class,"owner_price_express_id","id");
    }
    public function taxRate()
    {   //税率
        return $this->belongsTo(TaxRate::class);
    }

    public function getOwnerIdAttribute()
    {   //获取货主ID数组
        return array_column(DB::select(DB::raw("SELECT * FROM owner_price_express_owner WHERE owner_price_express_id = ?"),[$this->id]),"owner_id");
    }
    public function getLogisticIdAttribute()
    {   //获取快递ID数组
        return array_column(DB::select(DB::raw("SELECT * FROM owner_price_express_logistic WHERE owner_price_express_id = ?"),[$this->id]),"logistic_id");
    }
}
