<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

/**
 * @method static orderBy(string $string, string $string1)
 */
use App\Traits\ModelTimeFormat;

class Owner extends Model
{
    use ModelTimeFormat;
    public $fillable = [
        'name',                 //名称
        'code',                 //代码
        'checking_count',       //审核数量
        'deleted_at',           //删除时间
        "customer_id",          //客户ID
        "tax_rate",             //税率
        "linkman",              //联系人
        "phone_number",         //联系电话
        "user_owner_group_id",  //项目组ID
        "waring_line_on",       //月单量预警
        "description"           //描述
    ];

    public static function filterAuthorities(){
        $user=Auth::user();
        $query = (new static)->newQuery();
        if(!$user){
            return $query->where('id','0');
        }
        $ownerIds=app('UserService')->getPermittingOwnerIds($user);
        if(empty($ownerIds))return $query;
        return $query->whereIn('id',$ownerIds);
    }

    /**
     * 退货管理里，客户审核的代码，是拼音+日期+计数，计数的后缀就是checking_count
     * @return int|mixed
     */
    public function getIncreasedCheckingCount(){
        $this['checking_count']=$this['checking_count']+1;
        $this->update();
        return $this['checking_count'];
    }

    public function paperBoxes()
    {
        return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
    }

    public function orderTrackingOwner(){
        return $this->belongsTo(OrderTrackingOwner::class,'id','owner_id');
    }

    public function contracts()
    {   //合同
        return $this->hasMany(OwnerContract::class,"owner_id","id");
    }

    public function order(){
        return $this->hasOne(Order::class,'owner_id','id');
    }
    public function customer()
    {   //客户
        return $this->hasOne(Customer::class,"id","customer_id");
    }
    public function userOwnerGroup()
    {   //项目组
        return $this->hasOne(UserOwnerGroup::class,"id","user_owner_group_id");
    }
    public function ownerStoragePriceModels()
    {   //仓储计费
        return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id");
    }
    public function ownerAreaReport()
    {   //面积报表
        return $this->hasOne(OwnerAreaReport::class,"owner_id","id");
    }
    public function ownerStoragePriceModelOwners()
    {   //仓储计费-货主 中间表
        return $this->hasMany(OwnerStoragePriceModelOwner::class,"owner_id","id");
    }
    public function ownerPriceOperations()
    {   //作业计费
        return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id");
    }
    public function ownerPriceExpresses()
    {   //快递计费
        return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id");
    }
    public function ownerPriceLogistics()
    {   //物流计费
        return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id");
    }
    public function ownerPriceDirectLogistics()
    {   //直发车计费
        return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id");
    }

    public function getOwnerStoragePriceModelIds($type = 'string')
    {   //获取仓储计费的关联ID字符串或数组
        $this->load('ownerStoragePriceModelOwners');
        if ($type == 'string'){
            $ids = '';
            foreach ($this->ownerStoragePriceModelOwners as $os){
                $ids .= $os->owner_storage_price_model_id.",";
            }
            return $ids;
        }
        return array_column(($this->ownerStoragePriceModelOwners)->toArray(),"owner_storage_price_model_id");
    }
}
