2022-08-26 16:30:58 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models\WorkOrder;
|
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
|
use App\Exceptions\CommonException;
|
2023-01-13 14:13:46 +00:00
|
|
|
|
use App\Jobs\WorkOrder\WorkOrder as WorkOrderJob;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
use App\Models\Host;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
use App\Models\Module;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
use App\Models\User;
|
2023-01-13 14:13:46 +00:00
|
|
|
|
use App\Notifications\WorkOrder\WorkOrder as WorkOrderNotification;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-12-10 12:10:49 +00:00
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2023-01-10 12:46:53 +00:00
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2023-01-01 13:00:21 +00:00
|
|
|
|
use Illuminate\Support\Str;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
|
|
|
|
|
class WorkOrder extends Model
|
|
|
|
|
{
|
2023-01-10 12:46:53 +00:00
|
|
|
|
use Cachable, Notifiable;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
|
|
|
|
|
protected $table = 'work_orders';
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'title',
|
|
|
|
|
'content',
|
|
|
|
|
'host_id',
|
|
|
|
|
'user_id',
|
|
|
|
|
'module_id',
|
|
|
|
|
'status',
|
2023-01-02 12:25:38 +00:00
|
|
|
|
'notify'
|
2022-08-26 16:30:58 +00:00
|
|
|
|
];
|
|
|
|
|
|
2023-01-02 12:25:38 +00:00
|
|
|
|
protected $casts = [
|
|
|
|
|
'notify' => 'boolean'
|
|
|
|
|
];
|
2022-11-16 05:16:56 +00:00
|
|
|
|
|
2023-01-05 14:12:47 +00:00
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// updated
|
2023-01-10 12:46:53 +00:00
|
|
|
|
static::updated(function (self $model) {
|
2023-01-05 14:12:47 +00:00
|
|
|
|
dispatch(new WorkOrderJob($model, 'put'));
|
2023-01-10 12:46:53 +00:00
|
|
|
|
|
|
|
|
|
$model->notify(new WorkOrderNotification($model));
|
2023-01-05 14:12:47 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
|
public function scopeThisModule($query)
|
2022-11-19 04:38:26 +00:00
|
|
|
|
{
|
2023-01-01 13:00:21 +00:00
|
|
|
|
return $query->where('module_id', auth('module')->id());
|
2022-11-19 04:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
|
public function scopeThisUser($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('user_id', auth()->id());
|
|
|
|
|
}
|
2022-11-16 05:16:56 +00:00
|
|
|
|
|
2022-12-10 12:10:49 +00:00
|
|
|
|
public function user(): BelongsTo
|
2022-11-16 05:16:56 +00:00
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 12:10:49 +00:00
|
|
|
|
public function replies(): HasMany
|
2022-11-16 05:16:56 +00:00
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Reply::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 12:10:49 +00:00
|
|
|
|
public function host(): BelongsTo
|
2022-11-16 05:16:56 +00:00
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Host::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 12:10:49 +00:00
|
|
|
|
public function module(): BelongsTo
|
2022-11-16 05:16:56 +00:00
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Module::class);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
|
public function isFailure(): bool
|
2022-11-16 05:16:56 +00:00
|
|
|
|
{
|
2023-01-01 13:00:21 +00:00
|
|
|
|
return $this->status === 'pending' || $this->status === 'error';
|
2022-11-16 05:16:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 10:52:38 +00:00
|
|
|
|
public function isOpen(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->status !== 'closed' && $this->status !== 'error' && $this->status !== 'pending';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isClosed(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->status === 'closed';
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
|
/**
|
|
|
|
|
* @throws CommonException
|
|
|
|
|
*/
|
|
|
|
|
public function safeDelete(): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->status == 'pending') {
|
|
|
|
|
throw new CommonException('工单状态是 pending,无法删除');
|
|
|
|
|
}
|
2022-11-16 05:16:56 +00:00
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
|
dispatch(new WorkOrderJob($this, 'delete'));
|
2022-11-16 05:16:56 +00:00
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
|
return true;
|
2022-11-16 05:16:56 +00:00
|
|
|
|
}
|
2023-01-10 14:47:53 +00:00
|
|
|
|
|
|
|
|
|
public function routeNotificationForMail(WorkOrderNotification $work_order): array
|
|
|
|
|
{
|
|
|
|
|
$user = $this->user;
|
|
|
|
|
|
|
|
|
|
return [$user->email => $user->name];
|
|
|
|
|
}
|
2022-08-26 16:30:58 +00:00
|
|
|
|
}
|