改进 工单 增加 编辑和删除回复
This commit is contained in:
parent
bc00ea1cbc
commit
fcbdc501b6
89
app/Http/Controllers/Admin/ReplyController.php
Normal file
89
app/Http/Controllers/Admin/ReplyController.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\WorkOrder\Reply;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ReplyController extends Controller
|
||||
{
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param WorkOrder $work_order
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function store(Request $request, WorkOrder $work_order)
|
||||
{
|
||||
$request->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', '回复删除成功,请等待同步。');
|
||||
}
|
||||
}
|
@ -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', '回复成功,请等待同步。');
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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'));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -53,7 +53,7 @@
|
||||
<button type="submit" class="btn btn-danger mt-3">删除</button>
|
||||
</form>
|
||||
@else
|
||||
<p>工单状态为 推送中,无法修改</p>
|
||||
<p>工单状态为 推送中,无法修改。</p>
|
||||
@endif
|
||||
|
||||
@endsection
|
||||
|
31
resources/views/admin/work-orders/reply_edit.blade.php
Normal file
31
resources/views/admin/work-orders/reply_edit.blade.php
Normal file
@ -0,0 +1,31 @@
|
||||
@extends('layouts.admin')
|
||||
|
||||
@section('title', '回复: ' . $reply->workOrder->title)
|
||||
|
||||
@section('content')
|
||||
@if (!$reply->is_pending)
|
||||
<form method="post" action="{{ route('admin.work-orders.replies.update', [$work_order, $reply]) }}">
|
||||
@csrf
|
||||
@method('PATCH')
|
||||
|
||||
{{-- 编辑 --}}
|
||||
<div class="form-group">
|
||||
<label for="content">内容</label>
|
||||
<textarea name="content" id="content" class="form-control" rows="10">{{ $reply->content }}</textarea>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary mt-3">修改</button>
|
||||
|
||||
</form>
|
||||
|
||||
<hr/>
|
||||
<form method="post" action="{{ route('admin.work-orders.replies.destroy', [$work_order, $reply]) }}">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn btn-danger mt-3">删除</button>
|
||||
</form>
|
||||
@else
|
||||
<p>回复状态为 推送中,无法修改。</p>
|
||||
@endif
|
||||
|
||||
@endsection
|
@ -33,7 +33,10 @@
|
||||
{{ $reply->name }}
|
||||
@endif
|
||||
|
||||
<span class="text-end">{{ $reply->created_at }}</span>
|
||||
<span class="text-end">
|
||||
<a href="{{ route('admin.work-orders.replies.edit', [$workOrder, $reply]) }}">编辑</a>
|
||||
{{ $reply->created_at }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user