<?php

namespace App\Models;

use App\Models\Agent\AgentDwbsUser;
use App\Models\DwbsUser;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use App\Models\DwbsWarea;
use App\Models\DBUser;

class Store extends Model
{
    use SoftDeletes;

    protected $table='store';
    protected $guarded=[];

    public function user(){
        return $this->hasOne(DwbsUser::class,'id','user_id')->withTrashed();
    }

    public function warea(){
        return $this->hasOne(DwbsWarea::class,'id','warea_id');
    }
    public function dwuser(){
        return $this->belongsTo(DwbsUser::class,'user_id','id')->withTrashed();
    }

    public function users(){
        return $this->hasMany(User::class,'store_id','id');
    }

    public function order(){
        return $this->hasMany(Order::class,'store_id','id');
    }

    public function order_detail(){
        return $this->hasMany(OrderDetail::class,'store_id','id');
    }

    public function order_details(){
        return $this->hasMany(OrderDetail::class,'store_id','id')->whereHas('order',function($query){
            $query->where('is_pay',1);
        });
    }

    public function orderDetail(){
        return $this->belongsToMany(OrderDetail::class,'store_id','id');
    }

    public function comment(){
        return $this->hasMany(Comment::class,'store_id','id')->where('is_reply',1);
    }

    public static function getTest(){
        return [];
    }

    public function posts()
    {
        return $this->hasManyThrough(
            'App\Models\OrderDetail',
            'App\Models\Order',
            'store_id', // 国家表外键...
            'order_id', // 用户表外键...
            'id', // 国家表本地键...
            'id' // 用户表本地键...
        );
    }

}
