Lae/app/Models/Task.php

147 lines
4.7 KiB
PHP
Raw Normal View History

2022-08-13 08:37:17 +00:00
<?php
2022-11-06 11:28:22 +00:00
namespace App\Models;
2022-08-13 08:37:17 +00:00
2022-09-22 06:00:03 +00:00
use App\Events\UserEvent;
2022-08-26 14:37:20 +00:00
use App\Exceptions\CommonException;
2022-12-27 16:25:22 +00:00
use Eloquent;
use GeneaLabs\LaravelModelCaching\CachedBuilder;
2022-11-06 11:28:22 +00:00
use Illuminate\Database\Eloquent\Model;
2022-11-20 14:35:53 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2022-12-27 16:25:22 +00:00
use Illuminate\Support\Carbon;
2022-11-06 11:28:22 +00:00
use Illuminate\Support\Facades\Cache;
use Ramsey\Uuid\Uuid;
use function auth;
use function broadcast;
2022-08-13 08:37:17 +00:00
2022-11-20 03:40:20 +00:00
/**
* App\Models\Task
*
2022-12-28 13:19:40 +00:00
* @property string $id
* @property string $title
* @property int $progress
* @property string $status
* @property int $user_id
* @property int $host_id
2022-12-27 16:25:22 +00:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
2022-12-28 13:19:40 +00:00
* @property-read Host $host
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|Task all($columns = [])
* @method static CachedBuilder|Task avg($column)
* @method static CachedBuilder|Task cache(array $tags = [])
* @method static CachedBuilder|Task cachedValue(array $arguments, string $cacheKey)
* @method static CachedBuilder|Task count($columns = '*')
* @method static CachedBuilder|Task disableCache()
* @method static CachedBuilder|Task disableModelCaching()
* @method static CachedBuilder|Task exists()
* @method static CachedBuilder|Task flushCache(array $tags = [])
2022-11-20 12:32:49 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task
* getModelCacheCooldown(\Illuminate\Database\Eloquent\Model $instance)
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|Task inRandomOrder($seed = '')
* @method static CachedBuilder|Task insert(array $values)
* @method static CachedBuilder|Task isCachable()
* @method static CachedBuilder|Task max($column)
* @method static CachedBuilder|Task min($column)
* @method static CachedBuilder|Task newModelQuery()
* @method static CachedBuilder|Task newQuery()
* @method static CachedBuilder|Task query()
* @method static CachedBuilder|Task sum($column)
* @method static CachedBuilder|Task truncate()
* @method static CachedBuilder|Task user()
* @method static CachedBuilder|Task whereCreatedAt($value)
* @method static CachedBuilder|Task whereHostId($value)
* @method static CachedBuilder|Task whereId($value)
* @method static CachedBuilder|Task whereProgress($value)
* @method static CachedBuilder|Task whereStatus($value)
* @method static CachedBuilder|Task whereTitle($value)
* @method static CachedBuilder|Task whereUpdatedAt($value)
* @method static CachedBuilder|Task whereUserId($value)
* @method static CachedBuilder|Task withCacheCooldownSeconds(?int $seconds = null)
* @mixin Eloquent
2022-11-20 03:40:20 +00:00
*/
2022-08-13 08:37:17 +00:00
class Task extends Model
{
public $incrementing = false;
2022-08-26 14:37:20 +00:00
protected $fillable = [
'host_id',
'title',
'progress',
'status',
];
protected $casts = [
'id' => 'string',
'progress' => 'integer',
];
2022-09-08 18:35:00 +00:00
// key type string
protected $keyType = 'string';
2022-08-26 14:37:20 +00:00
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
// id 为 uuid
$model->id = Uuid::uuid4()->toString();
2022-11-20 14:35:53 +00:00
// 如果是模块创建的任务
if (auth('module')->check()) {
$model->module_id = auth('module')->id();
}
2022-09-08 18:35:00 +00:00
2022-08-26 14:37:20 +00:00
// host_id 和 user_id 至少存在一个
if (!$model->host_id && !$model->user_id) {
throw new CommonException('host_id 和 user_id 至少存在一个');
}
// if host_id
if ($model->host_id) {
$model->load('host');
2022-09-08 18:35:00 +00:00
if ($model->host === null) {
throw new CommonException('host_id 不存在');
}
2022-08-26 14:37:20 +00:00
$model->user_id = $model->host->user_id;
2022-09-10 04:03:20 +00:00
2022-09-11 12:44:43 +00:00
Cache::forget('user_tasks_' . $model->user_id);
2022-08-26 14:37:20 +00:00
}
});
2022-09-03 05:22:40 +00:00
2022-09-22 06:00:03 +00:00
// created
static::created(function ($model) {
$model->load('host');
broadcast(new UserEvent($model->user_id, 'tasks.created', $model));
});
2022-09-03 05:22:40 +00:00
// updateing
static::updating(function ($model) {
if ($model->progress == 100) {
$model->status = 'done';
}
});
2022-09-10 04:03:20 +00:00
// updated and delete
2022-09-11 12:44:43 +00:00
static::updated(function ($model) {
2022-11-06 11:28:22 +00:00
// Cache::forget('user_tasks_' . $model->user_id);
2022-09-22 06:00:03 +00:00
$model->load('host');
broadcast(new UserEvent($model->user_id, 'tasks.updated', $model));
2022-09-10 04:03:20 +00:00
});
2022-09-11 12:44:43 +00:00
static::deleted(function ($model) {
2022-09-22 06:00:03 +00:00
broadcast(new UserEvent($model->user_id, 'tasks.deleted', $model));
2022-09-10 04:03:20 +00:00
});
2022-08-26 14:37:20 +00:00
}
2022-11-20 14:35:53 +00:00
// public function scopeUser($query)
// {
// return $query->where('user_id', auth()->id());
// }
2022-11-20 14:35:53 +00:00
public function host(): BelongsTo
{
return $this->belongsTo(Host::class);
}
2022-08-13 08:37:17 +00:00
}