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-11-06 11:28:22 +00:00
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
2022-08-26 14:37:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
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-11-20 12:32:49 +00:00
|
|
|
* @property string $id
|
|
|
|
* @property string $title
|
|
|
|
* @property int $progress
|
|
|
|
* @property string $status
|
|
|
|
* @property int $user_id
|
|
|
|
* @property int $host_id
|
2022-11-20 03:40:20 +00:00
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
2022-11-20 12:32:49 +00:00
|
|
|
* @property-read \App\Models\Host $host
|
2022-11-20 03:40:20 +00:00
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task all($columns = [])
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task avg($column)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task cache(array $tags = [])
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task cachedValue(array $arguments, string $cacheKey)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task count($columns = '*')
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task disableCache()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task disableModelCaching()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task exists()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\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-11-20 03:40:20 +00:00
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task inRandomOrder($seed = '')
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task insert(array $values)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task isCachable()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task max($column)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task min($column)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task newModelQuery()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task newQuery()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task query()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task sum($column)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task truncate()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task user()
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereCreatedAt($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereHostId($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereId($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereProgress($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereStatus($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereTitle($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereUpdatedAt($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task whereUserId($value)
|
|
|
|
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Task withCacheCooldownSeconds(?int $seconds = null)
|
|
|
|
* @mixin \Eloquent
|
|
|
|
*/
|
2022-08-13 08:37:17 +00:00
|
|
|
class Task extends Model
|
|
|
|
{
|
2022-11-06 11:28:22 +00:00
|
|
|
use HasFactory, Cachable;
|
2022-08-26 14:37:20 +00:00
|
|
|
|
2022-11-16 05:16:56 +00:00
|
|
|
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-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 不存在');
|
|
|
|
}
|
|
|
|
|
|
|
|
// dd($model);
|
2022-08-26 14:37:20 +00:00
|
|
|
|
|
|
|
// dd($model->host_id);
|
|
|
|
// $host = Host::where('id', $model->host_id)->first();
|
|
|
|
|
|
|
|
// dd($host);
|
|
|
|
|
2022-09-08 18:35:00 +00:00
|
|
|
|
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-11-06 11:28:22 +00:00
|
|
|
// Cache::forget('user_tasks_' . $model->user_id);
|
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-16 05:16:56 +00:00
|
|
|
|
|
|
|
public function scopeUser($query)
|
|
|
|
{
|
|
|
|
return $query->where('user_id', auth()->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCurrentUserTasks()
|
|
|
|
{
|
|
|
|
return Cache::remember('user_tasks_' . auth()->id(), 3600, function () {
|
|
|
|
return $this->user()->with('host')->latest()->get();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// before create
|
|
|
|
|
|
|
|
public function host()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Host::class);
|
|
|
|
}
|
2022-08-13 08:37:17 +00:00
|
|
|
}
|