Lae/app/Models/WorkOrder/Reply.php

149 lines
4.9 KiB
PHP
Raw Normal View History

2022-08-26 16:30:58 +00:00
<?php
namespace App\Models\WorkOrder;
2022-09-22 06:00:03 +00:00
use App\Events\UserEvent;
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;
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;
use Illuminate\Database\Eloquent\Model;
2022-12-09 08:14:37 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
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\Reply
*
2022-12-28 13:19:40 +00:00
* @property int $id
* @property string $content
* @property int $work_order_id
* @property int|null $user_id
* @property int $is_pending
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read User|null $user
* @property-read WorkOrder $workOrder
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|Reply all($columns = [])
* @method static CachedBuilder|Reply avg($column)
* @method static CachedBuilder|Reply cache(array $tags = [])
* @method static CachedBuilder|Reply cachedValue(array $arguments, string $cacheKey)
* @method static CachedBuilder|Reply count($columns = '*')
* @method static CachedBuilder|Reply disableCache()
* @method static CachedBuilder|Reply disableModelCaching()
* @method static CachedBuilder|Reply exists()
* @method static CachedBuilder|Reply flushCache(array $tags = [])
2022-11-20 12:32:49 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Reply
* getModelCacheCooldown(\Illuminate\Database\Eloquent\Model $instance)
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|Reply inRandomOrder($seed = '')
* @method static CachedBuilder|Reply insert(array $values)
* @method static CachedBuilder|Reply isCachable()
* @method static CachedBuilder|Reply max($column)
* @method static CachedBuilder|Reply min($column)
* @method static CachedBuilder|Reply newModelQuery()
* @method static CachedBuilder|Reply newQuery()
* @method static CachedBuilder|Reply query()
* @method static CachedBuilder|Reply sum($column)
* @method static CachedBuilder|Reply truncate()
* @method static CachedBuilder|Reply whereContent($value)
* @method static CachedBuilder|Reply whereCreatedAt($value)
* @method static CachedBuilder|Reply whereId($value)
* @method static CachedBuilder|Reply whereIsPending($value)
* @method static CachedBuilder|Reply whereUpdatedAt($value)
* @method static CachedBuilder|Reply whereUserId($value)
* @method static CachedBuilder|Reply whereWorkOrderId($value)
* @method static CachedBuilder|Reply withCacheCooldownSeconds(?int $seconds = null)
* @method static CachedBuilder|Reply workOrderId($work_order_id)
* @mixin Eloquent
2022-11-20 03:40:20 +00:00
*/
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-01 13:36:06 +00:00
'role'
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) {
2022-08-26 16:30:58 +00:00
$model->is_pending = 1;
// 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
2023-01-01 13:36:06 +00:00
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';
} else if (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-01-01 13:36:06 +00:00
} else if (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
broadcast(new UserEvent($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
}
2023-01-01 13:36:06 +00:00
2022-08-26 16:30:58 +00:00
$model->workOrder->save();
});
static::created(function ($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();
}
// dispatch
2022-11-16 02:29:50 +00:00
dispatch(new \App\Jobs\Module\WorkOrder\Reply($model));
dispatch(new \App\Jobs\Module\WorkOrder\WorkOrder($model->workOrder, 'put'));
2022-08-26 16:30:58 +00:00
});
}
2022-12-09 08:14:37 +00:00
public function workOrder(): BelongsTo
{
return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id');
}
2023-01-01 13:00:21 +00:00
public function module(): BelongsTo
{
return $this->belongsTo(Module::class);
}
2022-12-09 08:14:37 +00:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
// before create
public function scopeWorkOrderId($query, $work_order_id)
{
return $query->where('work_order_id', $work_order_id);
}
2022-08-26 16:30:58 +00:00
}