diff --git a/app/Admin/Controllers/AgreementController.php b/app/Admin/Controllers/AgreementController.php index c1a8238..74ba590 100644 --- a/app/Admin/Controllers/AgreementController.php +++ b/app/Admin/Controllers/AgreementController.php @@ -44,6 +44,7 @@ class AgreementController extends AdminController ->placeholder(__('employee.name').'/'.__('employee.phone')) ->columnRatio(3) ->clearable(), + amis()->SelectControl()->name('check_status')->label(__('workflow_log.check_status'))->options(CheckStatus::options())->columnRatio(3)->clearable(), amisMake()->DateRangeControl() ->name('date_range') ->label(__('agreement.created_at')) diff --git a/app/Admin/Controllers/System/WorkflowController.php b/app/Admin/Controllers/System/WorkflowController.php index d9a516d..ff194bf 100644 --- a/app/Admin/Controllers/System/WorkflowController.php +++ b/app/Admin/Controllers/System/WorkflowController.php @@ -100,17 +100,17 @@ class WorkflowController extends AdminController amisMake()->TextControl()->name('name')->label(__('workflow.name'))->required(), amisMake()->TableControl()->name('config')->label(__('workflow.config'))->showIndex()->addable()->removable()->needConfirm(false)->draggable()->columns([ amisMake()->SelectControl()->options(CheckType::options())->name('type')->label(__('workflow.type')), - amisMake()->PickerControl() - ->source(admin_url('api/workflow/value-options?type=${type}')) - ->multiple(false) - ->pickerSchema(amisMake()->ListRenderer()->set('mode', 'list')->listItem(amisMake()->ListItem()->title('${label}')->body('${phone}'))) - // ->pickerSchema(amisMake()->CRUD2Table()->mode('list')->listItem(amisMake()->ListItem()->title('${label}')->body('${phone}'))) - ->name('value') - ->label(__('workflow.value')), - // amisMake()->SelectControl() + // amisMake()->PickerControl() // ->source(admin_url('api/workflow/value-options?type=${type}')) + // ->multiple(false) + // ->pickerSchema(amisMake()->ListRenderer()->set('mode', 'list')->listItem(amisMake()->ListItem()->title('${label}')->body('${phone}'))) + // ->pickerSchema(amisMake()->CRUD2Table()->mode('list')->listItem(amisMake()->ListItem()->title('${label}')->body('${phone}'))) // ->name('value') // ->label(__('workflow.value')), + amisMake()->SelectControl() + ->source(admin_url('api/workflow/value-options?type=${type}')) + ->name('value') + ->label(__('workflow.value')), ]), ]); } diff --git a/app/Admin/Filters/AgreementFilter.php b/app/Admin/Filters/AgreementFilter.php index fc97903..cca774e 100644 --- a/app/Admin/Filters/AgreementFilter.php +++ b/app/Admin/Filters/AgreementFilter.php @@ -18,6 +18,9 @@ class AgreementFilter extends ModelFilter 'invitor_name' => 'name', 'invitor_search' => 'search', ], + 'workflow' => [ + 'check_status' => 'check_status' + ] ]; public function employeeId($key) diff --git a/app/Admin/Filters/EmployeePromotionFilter.php b/app/Admin/Filters/EmployeePromotionFilter.php index 3fb97a1..4cfef03 100644 --- a/app/Admin/Filters/EmployeePromotionFilter.php +++ b/app/Admin/Filters/EmployeePromotionFilter.php @@ -21,6 +21,9 @@ class EmployeePromotionFilter extends ModelFilter 'invitor_name' => 'name', 'invitor_search' => 'search', ], + 'workflow' => [ + 'check_status' => 'check_status' + ] ]; public function employeeId($key) diff --git a/app/Admin/Filters/OvertimeApplyFilter.php b/app/Admin/Filters/OvertimeApplyFilter.php index a99821d..c82a777 100644 --- a/app/Admin/Filters/OvertimeApplyFilter.php +++ b/app/Admin/Filters/OvertimeApplyFilter.php @@ -17,6 +17,9 @@ class OvertimeApplyFilter extends ModelFilter 'employee_name' => 'name', 'employee_search' => 'search', ], + 'workflow' => [ + 'check_status' => 'check_status' + ] ]; public function employeeId($key) @@ -24,11 +27,6 @@ class OvertimeApplyFilter extends ModelFilter $this->where('employee_id', $key); } - public function checkStatus($key) - { - $this->where('check_status', $key); - } - public function dateRange($key) { $dates = explode(',', $key); diff --git a/app/Admin/Services/Plan/TaskService.php b/app/Admin/Services/Plan/TaskService.php index 2bc5ea5..df19c4a 100644 --- a/app/Admin/Services/Plan/TaskService.php +++ b/app/Admin/Services/Plan/TaskService.php @@ -208,7 +208,7 @@ class TaskService extends BaseService /** @var \Illuminate\Database\Eloquent\Collection */ $tasks = Task::findMany($ids); - $tasks->each(fn (Task $task) => $task->taskable()->delete()); + $tasks->each(fn (Task $task) => $task->taskable->delete()); } public function addRelations($query, string $scene = 'list') diff --git a/app/Admin/Services/WorkFlowService.php b/app/Admin/Services/WorkFlowService.php index 338b3bf..766f774 100644 --- a/app/Admin/Services/WorkFlowService.php +++ b/app/Admin/Services/WorkFlowService.php @@ -112,6 +112,9 @@ class WorkFlowService extends BaseService 'checked_at' => data_get($options, 'checked_at', now()), ]); + // 删除未审核的流程 + $check->logs()->where('check_status', CheckStatus::None)->delete(); + $check->subject->checkFail(); return true; @@ -185,9 +188,9 @@ class WorkFlowService extends BaseService ]); $log->update(['check_status' => CheckStatus::Processing]); // 自动审核通过 - if ($this->authCheck($check->employee, $log)) { - return $this->check($check->employee, $log, true, ['remarks' => '自动审核通过']); - } + // if ($this->authCheck($check->employee, $log)) { + // return $this->check($check->employee, $log, true, ['remarks' => '自动审核通过']); + // } } else { // 没有审核流程了, 审核完成 return $this->success($check); diff --git a/app/Http/Controllers/Api/TaskController.php b/app/Http/Controllers/Api/TaskController.php index 56072b0..728552e 100644 --- a/app/Http/Controllers/Api/TaskController.php +++ b/app/Http/Controllers/Api/TaskController.php @@ -76,8 +76,8 @@ MySQL; $task = Task::with([ 'taskable' => function (MorphTo $morphTo) { $morphTo->morphWith([ - TaskHygiene::class => ['workflow', 'store'], - TaskLedger::class => ['store'], + TaskHygiene::class => ['workflow', 'store', 'storeMaster'], + TaskLedger::class => ['store', 'storeMaster'], ]); }, ])->whereHasMorph( diff --git a/app/Http/Controllers/Api/WorkflowController copy.php b/app/Http/Controllers/Api/WorkflowController copy.php new file mode 100644 index 0000000..3fe7765 --- /dev/null +++ b/app/Http/Controllers/Api/WorkflowController copy.php @@ -0,0 +1,216 @@ +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; + } +} diff --git a/app/Http/Controllers/Api/WorkflowController.php b/app/Http/Controllers/Api/WorkflowController.php index deffc80..778bf94 100644 --- a/app/Http/Controllers/Api/WorkflowController.php +++ b/app/Http/Controllers/Api/WorkflowController.php @@ -10,7 +10,7 @@ use App\Http\Resources\WorkflowLogResource; use App\Models\Task; use App\Models\TaskHygiene; use App\Models\TaskLedger; -use App\Models\WorkflowCheck; +use App\Models\{WorkflowCheck, WorkflowLog}; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\Relation; @@ -26,100 +26,45 @@ class WorkflowController extends Controller 'subject_type' => 'required', ]); $subjectType = $request->input('subject_type'); - $model = Relation::getMorphedModel($subjectType); - $resource = $this->mapResource($model); - - $user = $request->user(); - - $include = ['workflow']; + $user = $this->guard()->user(); + $include = ['check']; if ($request->input('include')) { $explodes = explode(',', $request->input('include')); $include = array_merge($include, $explodes); } + $list = WorkflowLog::with($include) + ->whereHas('check', fn($q) => $q->where('subject_type', $subjectType)) + ->own($user) + ->where('check_status', '>', CheckStatus::None->value) + ->orderBy('check_status') + ->orderBy('id', 'desc') + ->paginate($request->input('per_page')); - $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); + return WorkflowLogResource::collection($list); } - public function show($id, Request $request) + public function show($id, Request $request, WorkFlowService $service) { - $request->validate([ - 'subject_type' => 'required', - ]); - - $user = $request->user(); - - $subjectType = $request->input('subject_type'); - $model = Relation::getMorphedModel($subjectType); - - $resource = $this->mapResource($model); + $include = ['check']; + if ($request->input('include')) { + $explodes = explode(',', $request->input('include')); + $include = array_merge($include, $explodes); + } + $info = WorkflowLog::with($include)->findOrFail($id); // 当前用户是否可以审核 $checkable = false; - - $include = ['workflow']; - if ($request->input('include')) { - $explodes = explode(',', $request->input('include')); - $include = array_merge($include, $explodes); + $user = $this->guard()->user(); + if ($user && $info->check_status == CheckStatus::Processing && $service->authCheck($user, $info)) { + $checkable = true; } - switch ($model) { - case TaskHygiene::class: - $info = Task::with([ - 'taskable' => function (MorphTo $morphTo) { - $morphTo->morphWith([ - TaskHygiene::class => ['workflow', 'store'], - TaskLedger::class => ['store'], - ]); - }, - ])->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]; + return ['data' => WorkflowLogResource::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(); - } + $check = WorkflowCheck::findOrFail($id); $logs = $check->logs()->with(['checkUser'])->sort()->get(); return WorkflowLogResource::collection($logs); @@ -127,23 +72,8 @@ class WorkflowController extends Controller 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(); - } + $user = $this->guard()->user(); + $check = WorkflowCheck::where('employee_id', $user->id)->findOrFail($id); try { DB::beginTransaction(); if (!$workFlowService->cancel($check)) { @@ -161,33 +91,15 @@ class WorkflowController extends Controller 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(); - } + $log = WorkflowLog::findOrFail($id); $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()); } diff --git a/app/Http/Resources/EmployeeSignRepairResource.php b/app/Http/Resources/EmployeeSignRepairResource.php index c51ca62..d68cf70 100644 --- a/app/Http/Resources/EmployeeSignRepairResource.php +++ b/app/Http/Resources/EmployeeSignRepairResource.php @@ -17,6 +17,7 @@ class EmployeeSignRepairResource extends JsonResource return [ 'id' => $this->id, 'date' => $this->date->timestamp, + 'date_format' => $this->date->format('Y-m-d H:i'), 'employee_id' => $this->employee_id, 'employee' => EmployeeResource::make($this->whenLoaded('employee')), @@ -28,6 +29,7 @@ class EmployeeSignRepairResource extends JsonResource 'sign_type' => $this->sign_type, 'outside_remarks' => $this->outside_remarks, 'created_at' => $this->created_at->timestamp, + 'created_format' => $this->created_at->format('Y-m-d H:i:s'), 'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')), ]; diff --git a/app/Http/Resources/HolidayApplyResource.php b/app/Http/Resources/HolidayApplyResource.php index f19e670..a6a0fc4 100644 --- a/app/Http/Resources/HolidayApplyResource.php +++ b/app/Http/Resources/HolidayApplyResource.php @@ -28,10 +28,13 @@ class HolidayApplyResource extends JsonResource 'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')), 'start_at' => $this->start_at->timestamp, + 'start_format' => $this->start_at->format('Y-m-d H:i'), 'end_at' => $this->end_at->timestamp, + 'end_format' => $this->end_at->format('Y-m-d H:i'), 'reason' => $this->reason, 'created_at' => $this->created_at->timestamp, + 'created_format' => $this->created_at->format('Y-m-d H:i:s') ]; } } diff --git a/app/Http/Resources/OfficalBusinessResource.php b/app/Http/Resources/OfficalBusinessResource.php index 62655da..4eb9629 100644 --- a/app/Http/Resources/OfficalBusinessResource.php +++ b/app/Http/Resources/OfficalBusinessResource.php @@ -25,11 +25,15 @@ class OfficalBusinessResource extends JsonResource 'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')), 'start_at' => $this->start_at->timestamp, + 'start_format' => $this->start_at->format('Y-m-d H:i'), 'end_at' => $this->end_at->timestamp, + 'end_format' => $this->end_at->format('Y-m-d H:i'), + 'address' => $this->address, 'reason' => $this->reason, 'created_at' => $this->created_at->timestamp, + 'created_format' => $this->created_at->format('Y-m-d H:i:s') ]; } } diff --git a/app/Http/Resources/OvertimeApplyResource.php b/app/Http/Resources/OvertimeApplyResource.php index 0be1f68..2bd8c46 100644 --- a/app/Http/Resources/OvertimeApplyResource.php +++ b/app/Http/Resources/OvertimeApplyResource.php @@ -25,8 +25,11 @@ class OvertimeApplyResource extends JsonResource 'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')), 'date' => $this->date->timestamp, + 'date_format' => $this->date->format('Y-m-d'), 'start_at' => $this->start_at->timestamp, + 'start_format' => $this->start_at->format('H:i'), 'end_at' => $this->end_at->timestamp, + 'end_format' => $this->end_at->format('H:i'), 'hours' => $this->hours, 'reason' => $this->reason, diff --git a/app/Http/Resources/ReimbursementResource.php b/app/Http/Resources/ReimbursementResource.php index 4ea74bc..3e7476d 100644 --- a/app/Http/Resources/ReimbursementResource.php +++ b/app/Http/Resources/ReimbursementResource.php @@ -27,6 +27,7 @@ class ReimbursementResource extends JsonResource 'reason' => $this->reason, 'photos' => $this->photos, 'created_at' => $this->created_at?->getTimestamp(), + 'created_at_format' => $this->created_at->format('Y-m-d H:i:s'), 'updated_at' => $this->updated_at?->getTimestamp(), ]; } diff --git a/app/Http/Resources/TaskHygieneResource.php b/app/Http/Resources/TaskHygieneResource.php index 1d7c8a0..0df2c68 100644 --- a/app/Http/Resources/TaskHygieneResource.php +++ b/app/Http/Resources/TaskHygieneResource.php @@ -4,6 +4,7 @@ namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; +use App\Enums\TaskHygieneStatus; class TaskHygieneResource extends JsonResource { @@ -15,13 +16,19 @@ class TaskHygieneResource extends JsonResource public function toArray(Request $request): array { return [ - 'id' => $this->resource->id, - 'month' => $this->resource->month, + 'id' => $this->id, + 'month' => $this->month, + 'store_id' => $this->store_id, 'store' => StoreResource::make($this->whenLoaded('store')), - 'description' => $this->resource->description, - 'photos' => $this->resource->photos, - 'status' => $this->resource->task_status, - 'created_at' => $this->resource->created_at->timestamp, + 'description' => $this->description, + 'photos' => $this->photos, + 'status' => $this->task_status, + 'status_text' => $this->task_status->text(), + 'created_at' => $this->created_at->timestamp, + 'store_master_id' => $this->store_master_id, + 'store_master' => EmployeeResource::make($this->whenLoaded('storeMaster')), + 'task_id' => $this->task_id, + 'task' => TaskResource::make($this->whenLoaded('task')), ]; } } diff --git a/app/Http/Resources/TaskLedgerResource.php b/app/Http/Resources/TaskLedgerResource.php index 2feb9dc..2615568 100644 --- a/app/Http/Resources/TaskLedgerResource.php +++ b/app/Http/Resources/TaskLedgerResource.php @@ -20,6 +20,8 @@ class TaskLedgerResource extends JsonResource 'store' => StoreResource::make($this->whenLoaded('store')), 'status' => $this->resource->task_status, 'created_at' => $this->resource->created_at->timestamp, + 'store_master_id' => $this->store_master_id, + 'store_master' => EmployeeResource::make($this->whenLoaded('storeMaster')), ]; } } diff --git a/app/Http/Resources/WorkflowCheckResource.php b/app/Http/Resources/WorkflowCheckResource.php index f65f7ca..ddc4307 100644 --- a/app/Http/Resources/WorkflowCheckResource.php +++ b/app/Http/Resources/WorkflowCheckResource.php @@ -4,6 +4,7 @@ namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Database\Eloquent\Relations\Relation; class WorkflowCheckResource extends JsonResource { @@ -14,12 +15,30 @@ class WorkflowCheckResource extends JsonResource */ public function toArray(Request $request): array { + $resource = $this->mapResource($this->subject_type); return [ 'check_status' => $this->check_status, 'check_status_text' => $this->check_status?->text(), 'checked_at' => $this->checked_at?->getTimestamp(), 'check_remarks' => (string) $this->check_remarks, 'logs' => WorkflowLogResource::collection($this->whenLoaded('logs')), + 'subject_id' => $this->subject_id, + 'subject_type' => $this->subject_type, + 'subject' => $resource::make($this->whenLoaded('subject')), ]; } + + protected function mapResource(string $type) + { + $model = Relation::getMorphedModel($type); + $class = match ($model) { + default => 'App\\Http\\Resources\\'.class_basename($model).'Resource', + }; + + if (! class_exists($class)) { + throw new RuntimeException('未知的 subject_type'); + } + + return $class; + } }