Lae/app/Models/WorkOrder/WorkOrder.php

154 lines
3.3 KiB
PHP
Raw Permalink Normal View History

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;
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;
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-30 16:14:07 +00:00
'notify',
2022-08-26 16:30:58 +00:00
];
2023-01-14 12:15:04 +00:00
protected $hidden = [
'ip',
];
2023-01-02 12:25:38 +00:00
protected $casts = [
2023-01-30 16:14:07 +00:00
'notify' => 'boolean',
2023-01-02 12:25:38 +00:00
];
2023-02-22 12:15:16 +00:00
protected array $orderBy = [
'closed' => 'desc',
'created_at' => 'desc',
];
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-12-10 12:10:49 +00:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
2022-12-10 12:10:49 +00:00
public function replies(): HasMany
{
return $this->hasMany(Reply::class);
}
2022-12-10 12:10:49 +00:00
public function host(): BelongsTo
{
return $this->belongsTo(Host::class);
}
2022-12-10 12:10:49 +00:00
public function module(): BelongsTo
{
return $this->belongsTo(Module::class);
}
2023-01-01 13:00:21 +00:00
public function isFailure(): bool
{
2023-01-01 13:00:21 +00:00
return $this->status === 'pending' || $this->status === 'error';
}
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-02-02 12:31:34 +00:00
public function markAsRead(): bool
{
2023-02-07 09:04:11 +00:00
if (! $this->isWaitingForResponse()) {
2023-02-02 12:31:34 +00:00
return false;
}
2023-02-22 09:08:54 +00:00
if (auth('admin')->check() && $this->status !== 'replied') {
2023-02-02 12:31:34 +00:00
$this->status = 'read';
2023-02-22 09:08:54 +00:00
} elseif (
2023-02-22 11:10:51 +00:00
auth('sanctum')->check()
2023-02-22 09:08:54 +00:00
&&
$this->status !== 'user_replied'
) {
2023-02-02 12:31:34 +00:00
$this->status = 'user_read';
}
2023-02-22 15:58:25 +00:00
// if status is dirty, save it
if ($this->isDirty('status')) {
$this->save();
}
2023-02-02 12:31:34 +00:00
return true;
}
2023-02-07 09:03:47 +00:00
public function isWaitingForResponse(): bool
{
return $this->status === 'replied' || $this->status === 'user_replied';
}
2023-01-01 13:00:21 +00:00
/**
* @throws CommonException
*/
public function safeDelete(): bool
{
if ($this->status == 'pending') {
throw new CommonException('工单状态是 pending无法删除');
}
2023-02-01 06:22:30 +00:00
if ($this->isPlatform()) {
$this->delete();
} else {
dispatch(new WorkOrderJob($this, 'delete'));
}
2023-02-01 16:52:33 +00:00
2023-01-01 13:00:21 +00:00
return true;
}
2023-01-10 14:47:53 +00:00
2023-02-07 09:03:47 +00:00
public function isPlatform(): bool
{
return $this->module_id === null && $this->host_id === null;
}
2023-01-13 14:16:48 +00:00
public function routeNotificationForMail(): array
2023-01-10 14:47:53 +00:00
{
$user = $this->user;
return [$user->email => $user->name];
}
public function getWecomKeyAttribute(): string
{
return $this->module?->wecom_key ?? config('settings.wecom.robot_hook.default');
}
2022-08-26 16:30:58 +00:00
}