Lae/app/Models/WorkOrder/WorkOrder.php

183 lines
5.9 KiB
PHP
Raw 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;
2022-11-19 04:38:26 +00:00
use App\Jobs\Module\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-12-27 16:25:22 +00:00
use Eloquent;
use GeneaLabs\LaravelModelCaching\CachedBuilder;
2022-11-06 11:28:22 +00:00
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2022-12-27 16:25:22 +00:00
use Illuminate\Database\Eloquent\Collection;
2022-11-06 11:28:22 +00:00
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;
2022-12-27 16:25:22 +00:00
use Illuminate\Support\Carbon;
2022-08-26 16:30:58 +00:00
2022-11-20 03:40:20 +00:00
/**
* App\Models\WorkOrder\WorkOrder
*
2022-11-20 12:32:49 +00:00
* @property int $id
2022-12-27 16:25:22 +00:00
* @property string $title
* @property string $content
* @property int $user_id
* @property string $module_id
* @property int|null $host_id
* @property string $status
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Host|null $host
* @property-read Module $module
* @property-read Collection|Reply[] $replies
* @property-read int|null $replies_count
* @property-read User $user
* @method static CachedBuilder|WorkOrder all($columns = [])
* @method static CachedBuilder|WorkOrder avg($column)
* @method static CachedBuilder|WorkOrder cache(array $tags = [])
* @method static CachedBuilder|WorkOrder cachedValue(array $arguments, string $cacheKey)
* @method static CachedBuilder|WorkOrder count($columns = '*')
* @method static CachedBuilder|WorkOrder disableCache()
* @method static CachedBuilder|WorkOrder disableModelCaching()
* @method static CachedBuilder|WorkOrder exists()
* @method static CachedBuilder|WorkOrder flushCache(array $tags = [])
2022-11-20 12:32:49 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|WorkOrder
* getModelCacheCooldown(\Illuminate\Database\Eloquent\Model $instance)
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|WorkOrder inRandomOrder($seed = '')
* @method static CachedBuilder|WorkOrder insert(array $values)
* @method static CachedBuilder|WorkOrder isCachable()
* @method static CachedBuilder|WorkOrder max($column)
* @method static CachedBuilder|WorkOrder min($column)
* @method static CachedBuilder|WorkOrder newModelQuery()
* @method static CachedBuilder|WorkOrder newQuery()
* @method static CachedBuilder|WorkOrder query()
* @method static CachedBuilder|WorkOrder sum($column)
* @method static CachedBuilder|WorkOrder thisModule()
* @method static CachedBuilder|WorkOrder thisUser()
* @method static CachedBuilder|WorkOrder truncate()
* @method static CachedBuilder|WorkOrder whereContent($value)
* @method static CachedBuilder|WorkOrder whereCreatedAt($value)
* @method static CachedBuilder|WorkOrder whereHostId($value)
* @method static CachedBuilder|WorkOrder whereId($value)
* @method static CachedBuilder|WorkOrder whereModuleId($value)
* @method static CachedBuilder|WorkOrder whereStatus($value)
* @method static CachedBuilder|WorkOrder whereTitle($value)
* @method static CachedBuilder|WorkOrder whereUpdatedAt($value)
* @method static CachedBuilder|WorkOrder whereUserId($value)
* @method static CachedBuilder|WorkOrder withCacheCooldownSeconds(?int $seconds = null)
* @mixin Eloquent
2022-11-20 03:40:20 +00:00
*/
2022-08-26 16:30:58 +00:00
class WorkOrder extends Model
{
2022-12-10 12:10:49 +00:00
use Cachable;
2022-08-26 16:30:58 +00:00
protected $table = 'work_orders';
protected $fillable = [
'title',
'content',
'host_id',
'user_id',
'module_id',
'status',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if ($model->host_id) {
$model->load(['host']);
$model->module_id = $model->host->module_id;
}
// if logged
2022-11-06 11:28:22 +00:00
if (auth()->check()) {
$model->user_id = auth()->id();
2022-08-26 16:30:58 +00:00
if ($model->host_id) {
if (!$model->user_id === $model->host->user_id) {
throw new CommonException('user_id not match host user_id');
}
}
} else {
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';
}
}
});
// updated
static::updated(function ($model) {
2022-12-27 16:25:22 +00:00
dispatch(new WorkOrderJob($model, 'put'));
2022-08-26 16:30:58 +00:00
});
}
2022-12-10 12:10:49 +00:00
/**
* @throws CommonException
*/
2022-11-19 04:38:26 +00:00
public function safeDelete(): bool
{
if ($this->status == 'pending') {
throw new CommonException('工单状态是 pending无法删除');
}
dispatch(new WorkOrderJob($this, 'delete'));
return true;
}
// replies
2022-12-10 12:10:49 +00:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
// host
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);
}
// scope
2022-12-10 12:10:49 +00:00
public function module(): BelongsTo
{
return $this->belongsTo(Module::class);
}
public function scopeThisModule($query)
{
return $query->where('module_id', auth('module')->id());
}
// on create
public function scopeThisUser($query)
{
return $query->where('user_id', auth()->id());
}
2022-08-26 16:30:58 +00:00
}