uuid = Str::uuid()->toString(); if ($model->host_id) { $model->load(['host']); $model->module_id = $model->host->module_id; } if (auth('sanctum')->check()) { $model->user_id = auth()->id(); if ($model->host_id) { if (!$model->user_id == $model->host->user_id) { throw new CommonException('user_id not match host user_id'); } } } else { if (!$model->user_id) { throw new CommonException('user_id is required'); } } if ($model->host_id) { $model->host->load('module'); $module = $model->host->module; if ($module === null) { $model->status = 'open'; } else { $model->status = 'pending'; } } }); // updated static::updated(function ($model) { dispatch(new WorkOrderJob($model, 'put')); }); } public function scopeThisModule($query) { return $query->where('module_id', auth('module')->id()); } public function scopeThisUser($query) { return $query->where('user_id', auth()->id()); } public function user(): BelongsTo { return $this->belongsTo(User::class); } public function replies(): HasMany { return $this->hasMany(Reply::class); } public function host(): BelongsTo { return $this->belongsTo(Host::class); } public function module(): BelongsTo { return $this->belongsTo(Module::class); } public function isFailure(): bool { return $this->status === 'pending' || $this->status === 'error'; } /** * @throws CommonException */ public function safeDelete(): bool { if ($this->status == 'pending') { throw new CommonException('工单状态是 pending,无法删除'); } dispatch(new WorkOrderJob($this, 'delete')); return true; } }