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;
|
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-27 16:25:22 +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
|
|
|
|
* @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',
|
|
|
|
'is_pending',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
|
|
$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');
|
|
|
|
}
|
2022-08-26 16:30:58 +00:00
|
|
|
throw_if($model->workOrder->status == 'pending' || $model->workOrder->status == 'error', CommonException::class, '工单状态不正确');
|
|
|
|
|
|
|
|
// change work order status
|
2022-11-06 11:28:22 +00:00
|
|
|
if (auth()->check()) {
|
2022-08-26 16:30:58 +00:00
|
|
|
$model->user_id = auth()->id();
|
|
|
|
$model->workOrder->status = 'user_replied';
|
|
|
|
}
|
|
|
|
|
2022-12-09 08:14:37 +00:00
|
|
|
if (auth('module')->check() || auth('admin')->check()) {
|
2022-08-26 16:30:58 +00:00
|
|
|
$model->user_id = null;
|
|
|
|
$model->workOrder->status = 'replied';
|
2022-09-22 06:00:03 +00:00
|
|
|
|
|
|
|
broadcast(new UserEvent($model->user_id, 'work-order.replied', $model->workOrder));
|
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-11-16 05:16:56 +00:00
|
|
|
|
2022-12-09 08:14:37 +00:00
|
|
|
public function workOrder(): BelongsTo
|
2022-11-16 05:16:56 +00:00
|
|
|
{
|
|
|
|
return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id');
|
|
|
|
}
|
|
|
|
|
2022-12-09 08:14:37 +00:00
|
|
|
public function user(): BelongsTo
|
2022-11-16 05:16:56 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|