Lae/app/Models/Workorder/Workorder.php

79 lines
1.7 KiB
PHP
Raw Normal View History

2022-08-13 06:04:47 +00:00
<?php
2022-08-13 06:12:37 +00:00
namespace App\Models\WorkOrder;
2022-08-13 06:04:47 +00:00
2022-08-13 08:37:17 +00:00
use App\Models\User\Host;
use App\Exceptions\CommonException;
2022-08-13 06:04:47 +00:00
use Illuminate\Database\Eloquent\Model;
2022-08-13 08:37:17 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Arr;
2022-08-13 06:04:47 +00:00
2022-08-13 06:12:37 +00:00
class WorkOrder extends Model
2022-08-13 06:04:47 +00:00
{
use HasFactory;
2022-08-13 06:12:37 +00:00
protected $table = 'workorders';
protected $fillable = [
'title',
'content',
2022-08-13 08:37:17 +00:00
'host_id',
2022-08-13 06:12:37 +00:00
'user_id',
'provider_module_id',
'status',
];
// replies
public function replies()
{
return $this->hasMany(Reply::class);
}
2022-08-13 08:37:17 +00:00
// provider module
public function host()
{
return $this->belongsTo(Host::class);
}
// on create
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->load('host');
if (!$model->user_id === $model->host->user_id) {
throw new CommonException('user_id not match host user_id');
}
$model->host->load('provider_module');
$provider_module = $model->host->provider_module;
if ($provider_module === null) {
$model->status = 'open';
} else {
$model->status = 'pending';
}
});
// 更新时获取差异部分
static::updating(function ($model) {
$original = $model->getOriginal();
// dd($original);
$diff = array_diff_assoc($model->attributes, $original);
// 如果更新了host_id则抛出异常
if (isset($diff['host_id'])) {
throw new CommonException('host_id cannot be updated');
}
// queue patch diff
});
}
2022-08-13 06:04:47 +00:00
}