'array' ]; // user public function user() { return $this->belongsTo(User::class); } // module public function module() { return $this->belongsTo(Module::class); } // workOrders public function workOrders() { return $this->hasMany(WorkOrder::class); } // module 远程一对一 // public function module() { // return $this->hasOneThrough(Module::class, ProviderModule::class); // } // scope public function scopeActive($query) { return $query->where('status', 'running')->where('price', '!=', 0); } public function scopeThisUser($query, $module = null) { if ($module) { return $query->where('user_id', auth()->id())->where('module_id', $module); } else { return $query->where('user_id', auth()->id()); } } // cost public function cost($price = null) { $cache_key = 'user_' . $this->user_id; // if cache has user if (Cache::has($cache_key)) { // if user is not instances of Model $user = Cache::get($cache_key); if (!($user instanceof User)) { $user = Cache::put($cache_key, $this->user, now()->addDay()); } } else { $user = Cache::put($cache_key, $this->user, now()->addDay()); } // Log::debug($user); if ($price !== null) { $this->managed_price = $price; } if ($this->managed_price) { $this->price = $this->managed_price; } $user->drops -= $this->price; // update cache Cache::put($cache_key, $user, now()->addDay()); return true; } /** * 创建主机 * * 在此之后,所有的主机都将由 module 创建,并且主机的数据仅被用作计费。 * * 废弃 * @deprecated */ // on create // protected static function boot() // { // parent::boot(); // // static::creating(function ($model) { // // // if sanctum // // // if (auth('sanctum')->check()) { // // // $model->user_id = auth('sanctum')->id(); // // // } else { // // // // if user_id is null // // // // check user_id is exists // // // throw_if(!User::find($model->user_id), CommonException::class, 'user is not exists'); // // // } // // // // set price to 0 // // // $model->price = 0; // // // $model->load('module'); // // // $model->module->load(['provider', 'module']); // // // add to queue // // }); // // when Updated // static::updated(function ($model) { // dispatch(new \App\Jobs\Remote\Host($model, 'put')); // }); // // when delete // static::deleting(function ($model) { // // return false; // dispatch(new \App\Jobs\Remote\Host($model, 'delete')); // }); // } }