改进 工单系统

支持 工单投递给平台
This commit is contained in:
iVampireSP.com 2023-01-19 17:25:14 +08:00
parent fecf0efd74
commit 7e36657e36
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
10 changed files with 144 additions and 72 deletions

View File

@ -29,16 +29,6 @@ public function store(Request $request): JsonResponse
'host_id' => 'nullable|sometimes|exists:hosts,id', 'host_id' => 'nullable|sometimes|exists:hosts,id',
]); ]);
// module_id 和 host_id 必须有个要填写
if ($request->input('module_id') === null && $request->input('host_id') === null) {
$message = 'module_id 和 host_id 必须有个要填写';
throw ValidationException::withMessages([
'module_id' => $message,
'host_id' => $message,
]);
}
$workOrder = (new WorkOrder)->create([ $workOrder = (new WorkOrder)->create([
'title' => $request->input('title'), 'title' => $request->input('title'),
'content' => $request->input('content'), 'content' => $request->input('content'),
@ -68,7 +58,7 @@ public function update(Request $request, WorkOrder $workOrder): JsonResponse
// 访客不能关闭 // 访客不能关闭
if ($request->input('status') === 'closed' && !auth('sanctum')->check()) { if ($request->input('status') === 'closed' && !auth('sanctum')->check()) {
return $this->error('访客不能修改工单状态。'); return $this->forbidden('访客不能修改工单状态。');
} }
$workOrder->update($request->only('status')); $workOrder->update($request->only('status'));

View File

@ -10,8 +10,6 @@
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
// use Illuminate\Contracts\Queue\ShouldBeUnique;
class PushWorkOrderJob implements ShouldQueue class PushWorkOrderJob implements ShouldQueue
{ {
use InteractsWithQueue, Queueable, SerializesModels; use InteractsWithQueue, Queueable, SerializesModels;
@ -33,9 +31,15 @@ public function __construct()
*/ */
public function handle(): void public function handle(): void
{ {
//
(new WorkOrder)->whereIn('status', ['pending'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) { (new WorkOrder)->whereIn('status', ['pending'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) {
foreach ($workOrders as $workOrder) { foreach ($workOrders as $workOrder) {
if ($workOrder->isPlatform()) {
if ($workOrder->status == 'pending') {
$workOrder->update(['status' => 'open']);
}
continue;
}
if ($workOrder->host) { if ($workOrder->host) {
if ($workOrder->host->status === 'pending') { if ($workOrder->host->status === 'pending') {
@ -43,7 +47,6 @@ public function handle(): void
} }
} }
if ($workOrder->status === 'error') { if ($workOrder->status === 'error') {
continue; continue;
} }

View File

@ -22,7 +22,7 @@ class Reply implements ShouldQueue
*/ */
public function __construct(WorkOrderReply $reply, $type = 'post') public function __construct(WorkOrderReply $reply, $type = 'post')
{ {
$this->reply = $reply; $this->reply = $reply->load(['workOrder', 'user']);
$this->type = $type; $this->type = $type;
} }
@ -33,7 +33,14 @@ public function __construct(WorkOrderReply $reply, $type = 'post')
*/ */
public function handle(): void public function handle(): void
{ {
$this->reply->load(['workOrder', 'user']); if ($this->reply->workOrder->isPlatform()) {
if ($this->reply->is_pending) {
$this->reply->update(['is_pending' => false]);
}
return;
}
$this->reply->workOrder->load(['module']); $this->reply->workOrder->load(['module']);
$reply = $this->reply->toArray(); $reply = $this->reply->toArray();
@ -52,6 +59,16 @@ public function handle(): void
} }
} else if ($this->type == 'patch') { } else if ($this->type == 'patch') {
// 不更新 is_pending
if ($this->reply->workOrder->isPlatform()) {
$this->reply->update([
'is_pending' => false
]);
return;
}
$this->reply->workOrder->module->http()->patch('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id, $reply); $this->reply->workOrder->module->http()->patch('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id, $reply);
} else if ($this->type == 'delete') { } else if ($this->type == 'delete') {
$response = $this->reply->workOrder->module->http()->delete('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id); $response = $this->reply->workOrder->module->http()->delete('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id);

View File

@ -24,7 +24,7 @@ class WorkOrder implements ShouldQueue
*/ */
public function __construct(WorkOrderModel $workOrder, $type = 'post') public function __construct(WorkOrderModel $workOrder, $type = 'post')
{ {
$this->workOrder = $workOrder; $this->workOrder = $workOrder->load(['module']);
$this->type = $type; $this->type = $type;
} }
@ -35,7 +35,10 @@ public function __construct(WorkOrderModel $workOrder, $type = 'post')
*/ */
public function handle(): void public function handle(): void
{ {
$this->workOrder->load(['module']); if ($this->workOrder->isPlatform()) {
return;
}
if ($this->type == 'post') { if ($this->type == 'post') {
$response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray()); $response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray());

View File

@ -34,7 +34,7 @@ protected static function boot()
{ {
parent::boot(); parent::boot();
static::creating(function (self $model) { static::creating(function (self $model) {
$model->is_pending = 1; $model->is_pending = false;
// load work order // load work order
$model->load(['workOrder']); $model->load(['workOrder']);
@ -70,6 +70,10 @@ protected static function boot()
$model->workOrder->save(); $model->workOrder->save();
if ($model->workOrder->isPlatform()) {
$model->is_pending = false;
}
$model->ip = request()->ip(); $model->ip = request()->ip();
}); });

View File

@ -85,7 +85,7 @@ protected static function boot()
static::updated(function (self $model) { static::updated(function (self $model) {
dispatch(new WorkOrderJob($model, 'put')); dispatch(new WorkOrderJob($model, 'put'));
if ($model->notify) { if ($model->notify && $model->isDirty('status')) {
$model->notify(new WorkOrderNotification($model)); $model->notify(new WorkOrderNotification($model));
} }
}); });
@ -136,6 +136,11 @@ public function isClosed(): bool
return $this->status === 'closed'; return $this->status === 'closed';
} }
public function isPlatform(): bool
{
return $this->module_id === null && $this->host_id === null;
}
/** /**
* @throws CommonException * @throws CommonException
*/ */

View File

@ -73,11 +73,7 @@ public function toArray(WorkOrderModel $workOrder): array
{ {
$array = $workOrder->toArray(); $array = $workOrder->toArray();
$array['event'] = 'WorkOrder.updated'; $array['event'] = 'work-order.' . $workOrder->status;
if ($workOrder->status === 'replied') {
$array['event'] = 'WorkOrder.replied';
}
return $array; return $array;
} }
@ -86,12 +82,16 @@ public function toWeCom(WorkOrderModel $workOrder): false|array
{ {
$workOrder->load(['module', 'user']); $workOrder->load(['module', 'user']);
$module = $workOrder->module; $module = null;
if ($workOrder->module) {
$module = $workOrder->module;
// 取消隐藏字段 // 取消隐藏字段
$module->makeVisible(['wecom_key']); $module->makeVisible(['wecom_key']);
if ($module->wecom_key == null) { }
if ($module?->wecom_key == null) {
$wecom_key = config('settings.wecom.robot_hook.default'); $wecom_key = config('settings.wecom.robot_hook.default');
} else { } else {
$wecom_key = $module->wecom_key; $wecom_key = $module->wecom_key;

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->string('module_id')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->string('module_id')->nullable(false)->change();
});
}
};

View File

@ -30,11 +30,15 @@
</a> </a>
</td> </td>
<td> <td>
<a @if ($workOrder->module_id)
href="{{ route('admin.modules.show', $workOrder->module_id) }}" <a
class="module_name" href="{{ route('admin.modules.show', $workOrder->module_id) }}"
module="{{ $workOrder->module_id }}">{{ $workOrder->module_id }} class="module_name"
</a> module="{{ $workOrder->module_id }}">{{ $workOrder->module_id }}
</a>
@else
{{ config('app.display_name') }}
@endif
</td> </td>
<td> <td>
@if ($workOrder->host_id) @if ($workOrder->host_id)

View File

@ -4,63 +4,77 @@
@switch ($data->status) @switch ($data->status)
@case('pending') @case('pending')
@php @php
$title = '工单挂起'; $title = '工单挂起';
@endphp @endphp
@break @break
@case('open') @case('open')
@php @php
$title = '工单投递成功,并且已开启'; $title = '工单投递成功,并且已开启';
@endphp @endphp
@break @break
@case('user_replied') @case('user_replied')
@php @php
$title = '用户已回复'; $title = '用户已回复';
@endphp @endphp
@break @break
@case('closed') @case('closed')
@php @php
$title = '已结单'; $title = '已结单';
@endphp @endphp
@break @break
@case('replied') @case('replied')
@php @php
$title = '工作人员已回复'; $title = '工作人员已回复';
@endphp @endphp
@break @break
@case('on_hold') @case('on_hold')
@php @php
$title = '挂起'; $title = '挂起';
@endphp @endphp
@break @break
@case('in_progress') @case('in_progress')
@php @php
$title = '正在处理中'; $title = '正在处理中';
@endphp @endphp
@break @break
@case('error') @case('error')
@php @php
$title = '投递失败'; $title = '投递失败';
@endphp @endphp
@break @break
@case('read')
@php
$title = '工作人员已读';
@endphp
@break
@case('user_read')
@php
$title = '用户已读';
@endphp
@break
@default @default
@php @php
$title = '状态更新'; $title = '状态更新';
@endphp @endphp
@break @break
@endswitch @endswitch
# 标题: {{ $title }} # 标题: {{ $title }}
# 模块: {{ $data->module->name }} @if ($data->module)
# 模块: {{ $data->module->name }}
@endif
## 客户 {{ $data->user->name }} ## 客户 {{ $data->user->name }}
##### 邮箱 {{ $data->user->email }} ##### 邮箱 {{ $data->user->email }}