2022-08-26 16:30:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\WorkOrder;
|
|
|
|
|
2022-09-22 06:00:03 +00:00
|
|
|
use App\Exceptions\CommonException;
|
2023-01-01 13:00:21 +00:00
|
|
|
use App\Models\Module;
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Models\User;
|
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-12-09 08:14:37 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
|
|
|
class Reply extends Model
|
|
|
|
{
|
2022-12-09 08:14:37 +00:00
|
|
|
use Cachable;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
|
|
|
protected $table = 'work_order_replies';
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'content',
|
|
|
|
'work_order_id',
|
|
|
|
'user_id',
|
2023-01-01 13:00:21 +00:00
|
|
|
'name',
|
|
|
|
'module_id',
|
2022-08-26 16:30:58 +00:00
|
|
|
'is_pending',
|
2023-01-30 16:14:07 +00:00
|
|
|
'role',
|
2022-08-26 16:30:58 +00:00
|
|
|
];
|
|
|
|
|
2023-01-14 12:15:04 +00:00
|
|
|
protected $hidden = [
|
|
|
|
'ip',
|
|
|
|
];
|
|
|
|
|
2022-08-26 16:30:58 +00:00
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
2023-01-01 13:00:21 +00:00
|
|
|
static::creating(function (self $model) {
|
2023-01-19 09:25:14 +00:00
|
|
|
$model->is_pending = false;
|
2022-08-26 16:30:58 +00:00
|
|
|
|
|
|
|
// load work order
|
|
|
|
$model->load(['workOrder']);
|
|
|
|
|
2022-12-11 01:53:37 +00:00
|
|
|
// throw if work order is null
|
|
|
|
if (is_null($model->workOrder)) {
|
|
|
|
throw new CommonException('Work order not found');
|
|
|
|
}
|
2023-01-01 13:00:21 +00:00
|
|
|
|
|
|
|
throw_if($model->workOrder->isFailure(), CommonException::class, '工单还没有就绪。');
|
2022-08-26 16:30:58 +00:00
|
|
|
|
|
|
|
// change work order status
|
2023-01-01 13:44:47 +00:00
|
|
|
if (auth('admin')->check()) {
|
|
|
|
$model->role = 'admin';
|
2023-01-10 14:27:19 +00:00
|
|
|
$model->workOrder->status = 'replied';
|
2023-02-07 09:04:11 +00:00
|
|
|
} elseif (auth('sanctum')->check()) {
|
2023-01-01 13:36:06 +00:00
|
|
|
$model->user_id = auth('sanctum')->id();
|
|
|
|
$model->role = 'user';
|
2022-08-26 16:30:58 +00:00
|
|
|
$model->workOrder->status = 'user_replied';
|
2023-02-07 09:04:11 +00:00
|
|
|
} elseif (auth('module')->check()) {
|
2022-08-26 16:30:58 +00:00
|
|
|
$model->user_id = null;
|
2023-01-01 13:36:06 +00:00
|
|
|
$model->role = 'module';
|
2022-08-26 16:30:58 +00:00
|
|
|
$model->workOrder->status = 'replied';
|
2022-09-22 06:00:03 +00:00
|
|
|
|
2023-02-07 09:04:11 +00:00
|
|
|
// broadcast(new Users($model->user_id, 'work-order.replied', $model->workOrder));
|
2023-01-01 13:36:06 +00:00
|
|
|
} else {
|
|
|
|
$model->role = 'guest';
|
2022-08-26 16:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$model->workOrder->save();
|
2023-01-14 12:15:04 +00:00
|
|
|
|
2023-01-19 09:25:14 +00:00
|
|
|
if ($model->workOrder->isPlatform()) {
|
|
|
|
$model->is_pending = false;
|
|
|
|
}
|
|
|
|
|
2023-01-14 12:15:04 +00:00
|
|
|
$model->ip = request()->ip();
|
2022-08-26 16:30:58 +00:00
|
|
|
});
|
|
|
|
|
2023-01-10 12:47:26 +00:00
|
|
|
static::created(function (self $model) {
|
2022-11-06 14:57:01 +00:00
|
|
|
if (auth('module')->check()) {
|
2022-08-26 16:30:58 +00:00
|
|
|
$model->workOrder->status = 'replied';
|
|
|
|
$model->workOrder->save();
|
|
|
|
}
|
2023-01-10 12:47:26 +00:00
|
|
|
|
2022-08-26 16:30:58 +00:00
|
|
|
// dispatch
|
2023-01-13 14:13:46 +00:00
|
|
|
dispatch(new \App\Jobs\WorkOrder\Reply($model, 'post'));
|
|
|
|
dispatch(new \App\Jobs\WorkOrder\WorkOrder($model->workOrder, 'put'));
|
2022-08-26 16:30:58 +00:00
|
|
|
});
|
2022-11-16 05:16:56 +00:00
|
|
|
|
2023-01-02 15:43:56 +00:00
|
|
|
static::updating(function (self $model) {
|
2023-01-13 14:13:46 +00:00
|
|
|
dispatch(new \App\Jobs\WorkOrder\Reply($model, 'patch'));
|
2023-01-02 15:43:56 +00:00
|
|
|
});
|
2023-01-02 12:12:32 +00:00
|
|
|
}
|
2023-01-05 14:12:47 +00:00
|
|
|
|
|
|
|
public function scopeWorkOrderId($query, $work_order_id)
|
|
|
|
{
|
|
|
|
return $query->where('work_order_id', $work_order_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeWithUser($query)
|
|
|
|
{
|
|
|
|
return $query->with(['user' => function ($query) {
|
|
|
|
$query->select('id', 'name', 'email_md5');
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function workOrder(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function module(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Module::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function safeDelete(): void
|
|
|
|
{
|
2023-01-13 14:13:46 +00:00
|
|
|
dispatch(new \App\Jobs\WorkOrder\Reply($this, 'delete'));
|
2023-01-05 14:12:47 +00:00
|
|
|
}
|
2022-08-26 16:30:58 +00:00
|
|
|
}
|