guard()->user(); $role = $user->userRole(); $filter = $request->all(); $query = Employee::with(['jobs', 'store', 'adminUser'])->filter($filter)->enable(); if (! in_array(UserRole::Admin, $role)) { $query->whereIn('store_id', [$user->store_id]); } $list = $query->orderBy('id', 'desc')->paginate($request->input('per_page')); return EmployeeResource::collection($list); } public function show($id) { $info = Employee::with(['jobs', 'store'])->findOrFail($id); return EmployeeResource::make($info); } public function store(Request $request) { try { DB::beginTransaction(); $service = EmployeeService::make(); $data = $request->all(); if (! $service->store($data)) { throw new RuntimeException($service->getError()); } DB::commit(); } catch (\Exception $e) { DB::rollback(); throw new RuntimeException($e->getMessage()); } return response('', Response::HTTP_CREATED); } public function update($id, Request $request) { try { DB::beginTransaction(); $service = EmployeeService::make(); $data = $request->only(['name', 'avatar', 'password', 'confirm_password', 'phone', 'store_id']); if (! $service->update($id, $data)) { throw new RuntimeException($service->getError()); } DB::commit(); } catch (\Exception $e) { DB::rollback(); throw new RuntimeException($e->getMessage()); } return response('', Response::HTTP_OK); } public function destroy($id) { try { DB::beginTransaction(); $service = EmployeeService::make(); if (! $service->delete($id)) { throw new RuntimeException($service->getError()); } DB::commit(); } catch (\Exception $e) { DB::rollback(); throw new RuntimeException($e->getMessage()); } return response('', Response::HTTP_OK); } public function leave($id) { $info = Employee::findOrFail($id); try { DB::beginTransaction(); if ($info->employee_status != EmployeeStatus::Online) { throw new RuntimeException('未入职'); } $service = EmployeeService::make(); if (! $service->leave($info)) { throw new RuntimeException($service->getError()); } DB::commit(); } catch (\Exception $e) { DB::rollback(); throw new RuntimeException($e->getMessage()); } return response('', Response::HTTP_OK); } }