更改 模型监听
This commit is contained in:
parent
a9d1adc71e
commit
8266639fc8
@ -7,13 +7,11 @@
|
||||
use App\Models\Host;
|
||||
use App\Models\Module;
|
||||
use App\Models\User;
|
||||
use App\Notifications\WorkOrder\WorkOrder as WorkOrderNotification;
|
||||
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class WorkOrder extends Model
|
||||
{
|
||||
@ -39,58 +37,6 @@ class WorkOrder extends Model
|
||||
'notify' => 'boolean',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function (self $model) {
|
||||
$model->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';
|
||||
}
|
||||
}
|
||||
|
||||
$model->notify = true;
|
||||
|
||||
$model->ip = request()->ip();
|
||||
});
|
||||
|
||||
// updated
|
||||
static::updated(function (self $model) {
|
||||
dispatch(new WorkOrderJob($model, 'put'));
|
||||
|
||||
if ($model->notify && $model->isDirty('status')) {
|
||||
$model->notify(new WorkOrderNotification($model));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeThisModule($query)
|
||||
{
|
||||
return $query->where('module_id', auth('module')->id());
|
||||
|
63
app/Observers/WorkOrderObserver.php
Normal file
63
app/Observers/WorkOrderObserver.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Exceptions\CommonException;
|
||||
use App\Jobs\WorkOrder\WorkOrder as WorkOrderJob;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use App\Notifications\WorkOrder\WorkOrder as WorkOrderNotification;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class WorkOrderObserver
|
||||
{
|
||||
/**
|
||||
* @throws CommonException
|
||||
*/
|
||||
public function creating(WorkOrder $workOrder): void
|
||||
{
|
||||
$workOrder->uuid = Str::uuid()->toString();
|
||||
|
||||
if ($workOrder->host_id) {
|
||||
$workOrder->load(['host']);
|
||||
$workOrder->module_id = $workOrder->host->module_id;
|
||||
}
|
||||
|
||||
if (auth('sanctum')->check()) {
|
||||
$workOrder->user_id = auth()->id();
|
||||
|
||||
if ($workOrder->host_id) {
|
||||
if (! $workOrder->user_id == $workOrder->host->user_id) {
|
||||
throw new CommonException('user_id not match host user_id');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (! $workOrder->user_id) {
|
||||
throw new CommonException('user_id is required');
|
||||
}
|
||||
}
|
||||
|
||||
if ($workOrder->host_id) {
|
||||
$workOrder->host->load('module');
|
||||
$module = $workOrder->host->module;
|
||||
|
||||
if ($module === null) {
|
||||
$workOrder->status = 'open';
|
||||
} else {
|
||||
$workOrder->status = 'pending';
|
||||
}
|
||||
}
|
||||
|
||||
$workOrder->notify = true;
|
||||
|
||||
$workOrder->ip = request()->ip();
|
||||
}
|
||||
|
||||
public function updated(WorkOrder $workOrder): void
|
||||
{
|
||||
dispatch(new WorkOrderJob($workOrder, 'put'));
|
||||
|
||||
if ($workOrder->notify && $workOrder->isDirty('status')) {
|
||||
$workOrder->notify(new WorkOrderNotification($workOrder));
|
||||
}
|
||||
}
|
||||
}
|
@ -8,11 +8,13 @@
|
||||
use App\Models\PersonalAccessToken;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use App\Observers\BalanceObserver;
|
||||
use App\Observers\HostObserver;
|
||||
use App\Observers\ModuleObserver;
|
||||
use App\Observers\TaskObserver;
|
||||
use App\Observers\UserObserver;
|
||||
use App\Observers\WorkOrderObserver;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@ -60,5 +62,6 @@ private function registerObservers(): void
|
||||
Task::observe(TaskObserver::class);
|
||||
Module::observe(ModuleObserver::class);
|
||||
Balance::observe(BalanceObserver::class);
|
||||
WorkOrder::observe(WorkOrderObserver::class);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user