diff --git a/app/Http/Controllers/Admin/ReplyController.php b/app/Http/Controllers/Admin/ReplyController.php new file mode 100644 index 0000000..af94a03 --- /dev/null +++ b/app/Http/Controllers/Admin/ReplyController.php @@ -0,0 +1,89 @@ +validate([ + 'content' => 'required|string', + ]); + + if (!$work_order->isOpen()) { + return back()->with('error', '工单还未就绪。'); + } + + Reply::create([ + 'content' => $request->input('content'), + 'work_order_id' => $work_order->id + ]); + + return back()->with('success', '回复成功,请等待同步。'); + } + + /** + * Show the form for editing the specified resource. + * + * @param WorkOrder $work_order + * @param Reply $reply + * + * @return View + */ + public function edit(WorkOrder $work_order, Reply $reply) + { + return view('admin.work-orders.reply_edit', compact('reply', 'work_order')); + } + + /** + * Update the specified resource in storage. + * + * @param Request $request + * @param WorkOrder $work_order + * @param Reply $reply + * + * @return RedirectResponse + */ + public function update(Request $request, WorkOrder $work_order, Reply $reply) + { + $request->validate([ + 'content' => 'required|string', + ]); + + $reply->update([ + 'content' => $request->input('content') + ]); + + return back()->with('success', '回复修改成功,请等待同步。'); + } + + /** + * Remove the specified resource from storage. + * + * @param WorkOrder $work_order + * @param Reply $reply + * + * @return RedirectResponse + */ + public function destroy(WorkOrder $work_order, Reply $reply) + { + $reply->safeDelete(); + + return back()->with('success', '回复删除成功,请等待同步。'); + } +} diff --git a/app/Http/Controllers/Admin/WorkOrderController.php b/app/Http/Controllers/Admin/WorkOrderController.php index 7258f35..f3a0df4 100644 --- a/app/Http/Controllers/Admin/WorkOrderController.php +++ b/app/Http/Controllers/Admin/WorkOrderController.php @@ -35,8 +35,6 @@ public function index(WorkOrder $workOrder): View */ public function show(WorkOrder $workOrder): View { - // - $workOrder->load(['user', 'module']); $replies = Reply::where('work_order_id', $workOrder->id)->latest()->paginate(100); @@ -97,18 +95,4 @@ public function destroy(WorkOrder $workOrder): RedirectResponse return redirect()->route('admin.work-orders.index')->with('success', '正在排队删除工单。'); } - - public function reply(Request $request, WorkOrder $workOrder): RedirectResponse - { - $request->validate([ - 'content' => 'required|string', - ]); - - Reply::create([ - 'content' => $request->input('content'), - 'work_order_id' => $workOrder->id - ]); - - return back()->with('success', '回复成功,请等待同步。'); - } } diff --git a/app/Jobs/Module/WorkOrder/Reply.php b/app/Jobs/Module/WorkOrder/Reply.php index 45c24ef..dc83161 100644 --- a/app/Jobs/Module/WorkOrder/Reply.php +++ b/app/Jobs/Module/WorkOrder/Reply.php @@ -2,30 +2,28 @@ namespace App\Jobs\Module\WorkOrder; -use App\Events\UserEvent; use App\Models\WorkOrder\Reply as WorkOrderReply; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -// use Illuminate\Contracts\Queue\ShouldBeUnique; - class Reply implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; protected WorkOrderReply $reply; + protected string $type; /** * Create a new job instance. * * @return void */ - public function __construct(WorkOrderReply $reply) + public function __construct(WorkOrderReply $reply, $type = 'post') { - // $this->reply = $reply; + $this->type = $type; } /** @@ -35,25 +33,33 @@ public function __construct(WorkOrderReply $reply) */ public function handle(): void { - // $this->reply->load(['workOrder', 'user']); $this->reply->workOrder->load(['module']); $reply = $this->reply->toArray(); - $response = $this->reply->workOrder->module->http()->post('work-orders/' . $this->reply->workOrder->id . '/replies', $reply); + if ($this->type == 'post') { + $response = $this->reply->workOrder->module->http()->post('work-orders/' . $this->reply->workOrder->id . '/replies', $reply); - if ($response->successful()) { - $this->reply->update([ - 'is_pending' => false - ]); + if ($response->successful()) { + $this->reply->update([ + 'is_pending' => false + ]); + } else { + $this->reply->update([ + 'is_pending' => true + ]); + } - broadcast(new UserEvent($this->reply->workOrder->user_id, 'work-order.replied', $this->reply)); + } else if ($this->type == 'patch') { + $this->reply->workOrder->module->http()->patch('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id, $reply); + } else if ($this->type == 'delete') { + $response = $this->reply->workOrder->module->http()->delete('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id); - } else { - $this->reply->update([ - 'is_pending' => true - ]); + if ($response->successful()) { + $this->reply->delete(); + } } } } + diff --git a/app/Jobs/Module/WorkOrder/WorkOrder.php b/app/Jobs/Module/WorkOrder/WorkOrder.php index 6bc3cc5..8104976 100644 --- a/app/Jobs/Module/WorkOrder/WorkOrder.php +++ b/app/Jobs/Module/WorkOrder/WorkOrder.php @@ -3,7 +3,7 @@ namespace App\Jobs\Module\WorkOrder; use App\Events\UserEvent; -use App\Models\WorkOrder\WorkOrder as WorkOrderWorkOrder; +use App\Models\WorkOrder\WorkOrder as WorkOrderModel; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; @@ -15,16 +15,16 @@ class WorkOrder implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; - protected $workOrder, $type; + protected WorkOrderModel $workOrder; + protected string $type; /** * Create a new job instance. * * @return void */ - public function __construct(WorkOrderWorkOrder $workOrder, $type = 'post') + public function __construct(WorkOrderModel $workOrder, $type = 'post') { - // $this->workOrder = $workOrder; $this->type = $type; } @@ -38,22 +38,23 @@ public function handle(): void { $this->workOrder->load(['module']); - if ($this->type == 'put') { + if ($this->type == 'post') { + $response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray()); + } else if ($this->type == 'put') { $response = $this->workOrder->module->http()->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray()); - } else if ($this->type == 'delete') { + } else { $response = $this->workOrder->module->http()->delete('work-orders/' . $this->workOrder->id); if ($response->successful()) { $this->workOrder->delete(); } - } else { - $response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray()); } if (!$response->successful()) { $this->workOrder->update([ 'status' => 'error' ]); + } else { if ($this->type == 'delete') { broadcast(new UserEvent($this->workOrder->user_id, 'work-order.deleted', $this->workOrder)); diff --git a/app/Models/WorkOrder/Reply.php b/app/Models/WorkOrder/Reply.php index 6612d18..887cc09 100644 --- a/app/Models/WorkOrder/Reply.php +++ b/app/Models/WorkOrder/Reply.php @@ -73,6 +73,38 @@ class Reply extends Model 'role' ]; + public function scopeWorkOrderId($query, $work_order_id) + { + return $query->where('work_order_id', $work_order_id); + } + + public function scopeWithUser($query) + { + return $query->with(['user' => function ($query) { + $query->select('id', 'name', 'email_md5'); + }]); + } + + public function workOrder(): BelongsTo + { + return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id'); + } + + public function module(): BelongsTo + { + return $this->belongsTo(Module::class); + } + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public function safeDelete(): void + { + dispatch(new \App\Jobs\Module\WorkOrder\Reply($this, 'delete')); + } + protected static function boot() { parent::boot(); @@ -119,37 +151,12 @@ protected static function boot() $model->workOrder->save(); } // dispatch - dispatch(new \App\Jobs\Module\WorkOrder\Reply($model)); + dispatch(new \App\Jobs\Module\WorkOrder\Reply($model, 'post')); dispatch(new \App\Jobs\Module\WorkOrder\WorkOrder($model->workOrder, 'put')); }); - } - public function workOrder(): BelongsTo - { - return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id'); - } - - public function module(): BelongsTo - { - return $this->belongsTo(Module::class); - } - - 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); - } - - public function scopeWithUser($query) - { - return $query->with(['user' => function ($query) { - $query->select('id', 'name', 'email_md5'); - }]); + static::updating(function (self $model) { + dispatch(new \App\Jobs\Module\WorkOrder\Reply($model, 'patch')); + }); } } diff --git a/app/Models/WorkOrder/WorkOrder.php b/app/Models/WorkOrder/WorkOrder.php index bcd1f68..646762a 100644 --- a/app/Models/WorkOrder/WorkOrder.php +++ b/app/Models/WorkOrder/WorkOrder.php @@ -153,7 +153,6 @@ protected static function boot() parent::boot(); static::creating(function (self $model) { - // 生成 uuid $model->uuid = Str::uuid()->toString(); if ($model->host_id) { diff --git a/resources/views/admin/work-orders/edit.blade.php b/resources/views/admin/work-orders/edit.blade.php index 7d5bcaf..ba15d74 100644 --- a/resources/views/admin/work-orders/edit.blade.php +++ b/resources/views/admin/work-orders/edit.blade.php @@ -53,7 +53,7 @@ @else -

工单状态为 推送中,无法修改

+

工单状态为 推送中,无法修改。

@endif @endsection diff --git a/resources/views/admin/work-orders/reply_edit.blade.php b/resources/views/admin/work-orders/reply_edit.blade.php new file mode 100644 index 0000000..7162389 --- /dev/null +++ b/resources/views/admin/work-orders/reply_edit.blade.php @@ -0,0 +1,31 @@ +@extends('layouts.admin') + +@section('title', '回复: ' . $reply->workOrder->title) + +@section('content') + @if (!$reply->is_pending) +
+ @csrf + @method('PATCH') + + {{-- 编辑 --}} +
+ + +
+ + + +
+ +
+
+ @csrf + @method('DELETE') + +
+ @else +

回复状态为 推送中,无法修改。

+ @endif + +@endsection diff --git a/resources/views/admin/work-orders/show.blade.php b/resources/views/admin/work-orders/show.blade.php index b3fc4fa..d7f2843 100644 --- a/resources/views/admin/work-orders/show.blade.php +++ b/resources/views/admin/work-orders/show.blade.php @@ -33,7 +33,10 @@ {{ $reply->name }} @endif - {{ $reply->created_at }} + + 编辑 + {{ $reply->created_at }} +
diff --git a/routes/admin.php b/routes/admin.php index 25d2a26..0db83ce 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -8,6 +8,7 @@ use App\Http\Controllers\Admin\HostController; use App\Http\Controllers\Admin\ModuleController; use App\Http\Controllers\Admin\NotificationController; +use App\Http\Controllers\Admin\ReplyController; use App\Http\Controllers\Admin\UserController; use App\Http\Controllers\Admin\UserGroupController; use App\Http\Controllers\Admin\WorkOrderController; @@ -40,7 +41,7 @@ Route::resource('hosts', HostController::class)->only(['index', 'edit', 'update', 'destroy']); Route::resource('work-orders', WorkOrderController::class)->only(['index', 'show', 'edit', 'update', 'destroy']); - Route::post('work-orders/{work_order}/replies', [WorkOrderController::class, 'reply'])->name('work-orders.replies.store'); + Route::resource('work-orders.replies', ReplyController::class)->only(['store', 'edit', 'update', 'destroy']); Route::resource('user-groups', UserGroupController::class);