validate([ 'subject_type' => 'required', ]); $subjectType = $request->input('subject_type'); $model = Relation::getMorphedModel($subjectType); $resource = $this->mapResource($model); $user = $request->user(); $include = ['workflow']; if ($request->input('include')) { $explodes = explode(',', $request->input('include')); $include = array_merge($include, $explodes); } $query = $model::query()->with($include) // ->whereHas('workflow', fn($q) => $q->where('check_status', CheckStatus::Processing)) ->whereHas('workflow.logs', fn($q) => $q->own($user)->where('check_status', '>', CheckStatus::None->value)) ->orderBy('created_at', 'desc'); $list = $query->paginate($request->input('per_page')); switch ($model) { case TaskHygiene::class: $list->loadMissing(['task', 'store']); $list->through(function (TaskHygiene $item) { return tap($item->task)->setRelation('taskable', $item->unsetRelation('task')); }); break; } return $resource::collection($list); } public function show($id, Request $request) { $request->validate([ 'subject_type' => 'required', ]); $user = $request->user(); $subjectType = $request->input('subject_type'); $model = Relation::getMorphedModel($subjectType); $resource = $this->mapResource($model); // 当前用户是否可以审核 $checkable = false; $include = ['workflow']; if ($request->input('include')) { $explodes = explode(',', $request->input('include')); $include = array_merge($include, $explodes); } switch ($model) { case TaskHygiene::class: $info = Task::with([ 'taskable' => function (MorphTo $morphTo) { $morphTo->morphWith([ TaskHygiene::class => ['workflow', 'store', 'storeMaster'], TaskLedger::class => ['store', 'storeMaster'], ]); }, ])->findOrFail($id); $info->taskable->setRelation('task', $info->withoutRelations()); $checkable = $info->taskable->workflow->logs()->own($user)->where('check_status', CheckStatus::Processing)->exists(); break; default: $info = $model::query()->with($include)->findOrFail($id); $checkable = $info->workflow->logs()->own($user)->where('check_status', CheckStatus::Processing)->exists(); break; } return ['data' => $resource::make($info), 'checkable' => $checkable]; } public function logs($id, Request $request) { $request->validate([ 'subject_type' => 'required', ]); $subjectType = $request->input('subject_type'); if ($subjectType == (new TaskHygiene)->getMorphClass()) { $task = Task::findOrFail($id); $taskable = $task->taskable; if (!$taskable) { throw new RuntimeException('任务不存在'); } $check = $taskable->workflow; if (!$check) { throw new RuntimeException('审核记录不存在'); } } else { $check = WorkflowCheck::where('subject_type', $subjectType)->where('subject_id', $id)->firstOrFail(); } $logs = $check->logs()->with(['checkUser'])->sort()->get(); return WorkflowLogResource::collection($logs); } public function cancel($id, Request $request, WorkFlowService $workFlowService) { $request->validate([ 'subject_type' => 'required', ]); $subjectType = $request->input('subject_type'); if ($subjectType == (new TaskHygiene)->getMorphClass()) { $task = Task::findOrFail($id); $taskable = $task->taskable; if (!$taskable) { throw new RuntimeException('任务不存在'); } $check = $taskable->workflow; if (!$check) { throw new RuntimeException('审核记录不存在'); } } else { $check = WorkflowCheck::where('subject_type', $subjectType)->where('subject_id', $id)->firstOrFail(); } try { DB::beginTransaction(); if (!$workFlowService->cancel($check)) { throw new RuntimeException($workFlowService->getError()); } DB::commit(); return response('', Response::HTTP_OK); } catch (\Exception $e) { DB::rollBack(); throw new RuntimeException($e->getMessage()); } } public function check($id, Request $request, WorkFlowService $workFlowService) { $request->validate([ 'subject_type' => 'required', 'status' => ['required'], 'remarks' => [Rule::requiredIf(fn() => !$request->input('status'))] ], [ 'remarks.required_if' => '未通过原因必填', ]); $subjectType = $request->input('subject_type'); if ($subjectType == (new TaskHygiene)->getMorphClass()) { $task = Task::findOrFail($id); $taskable = $task->taskable; if (!$taskable) { throw new RuntimeException('任务不存在'); } $check = $taskable->workflow; if (!$check) { throw new RuntimeException('审核记录不存在'); } } else { $check = WorkflowCheck::where('subject_type', $subjectType)->where('subject_id', $id)->firstOrFail(); } $user = $this->guard()->user(); try { DB::beginTransaction(); $log = $check->logs()->where('check_status', CheckStatus::Processing)->first(); if (!$log) { throw new RuntimeException('审核已经完成'); } if (!$workFlowService->check($user, $log, !!$request->input('status'), ['remarks' => $request->input('remarks')])) { throw new RuntimeException($workFlowService->getError()); } DB::commit(); return response('', Response::HTTP_OK); } catch (\Exception $e) { DB::rollBack(); throw new RuntimeException($e->getMessage()); } } protected function mapResource(string $model) { $class = match ($model) { TaskHygiene::class => TaskResource::class, default => 'App\\Http\\Resources\\'.class_basename($model).'Resource', }; if (! class_exists($class)) { throw new RuntimeException('未知的 subject_type'); } return $class; } }